refactor: clean up code and improve event builder
This commit is contained in:
parent
45ac7d6a62
commit
d06ac91ef8
@ -5,8 +5,10 @@ import uulm.teamname.marvelous.gamelibrary.entities.Entity;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* A class made for building all sorts of {@link Event}s as nicely as possible.
|
||||
@ -47,28 +49,18 @@ public class EventBuilder {
|
||||
private String teamIdentifier;
|
||||
private HashMap<String, Object> customContent;
|
||||
|
||||
// /**
|
||||
// * Creates a new {@link EventBuilder} used for building {@link Event}s
|
||||
// */
|
||||
// public EventBuilder() {
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link EventBuilder} used for building {@link Event}s.
|
||||
*
|
||||
* @param eventType is the type of Event that the final event will have.
|
||||
* @param eventType is the type of Event that the final event will have
|
||||
*/
|
||||
public EventBuilder(EventType eventType) {
|
||||
this.type = eventType;
|
||||
}
|
||||
|
||||
// with<something> for builder pattern style things
|
||||
|
||||
/**
|
||||
* Deprecated. Use constructor with EventType instead.
|
||||
* @deprecated Use {@link #EventBuilder(EventType)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public EventBuilder withType(EventType type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
@ -89,7 +81,6 @@ public class EventBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public EventBuilder withEntity(Entity entity) {
|
||||
this.entity = entity;
|
||||
return this;
|
||||
@ -110,12 +101,12 @@ public class EventBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventBuilder withRoundCount(int roundCount) {
|
||||
public EventBuilder withRoundCount(Integer roundCount) {
|
||||
this.roundCount = roundCount;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventBuilder withTurnCount(int turnCount) {
|
||||
public EventBuilder withTurnCount(Integer turnCount) {
|
||||
this.turnCount = turnCount;
|
||||
return this;
|
||||
}
|
||||
@ -130,7 +121,7 @@ public class EventBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventBuilder withPlayerWon(int playerWon) {
|
||||
public EventBuilder withPlayerWon(Integer playerWon) {
|
||||
this.playerWon = playerWon;
|
||||
return this;
|
||||
}
|
||||
@ -140,7 +131,7 @@ public class EventBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventBuilder withTimeLeft(int timeLeft) {
|
||||
public EventBuilder withTimeLeft(Integer timeLeft) {
|
||||
this.timeLeft = timeLeft;
|
||||
return this;
|
||||
}
|
||||
@ -160,7 +151,7 @@ public class EventBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventBuilder withWinCondition(boolean winCondition) {
|
||||
public EventBuilder withWinCondition(Boolean winCondition) {
|
||||
this.winCondition = winCondition;
|
||||
return this;
|
||||
}
|
||||
@ -177,9 +168,10 @@ public class EventBuilder {
|
||||
|
||||
/**
|
||||
* A method to check whether the current event is actually built correctly. If that is not the case, it
|
||||
* throws an {@link IllegalStateException}. This occurs if for example a property is null even though it shouldn't
|
||||
* be. The check is based on the EventType. <b>!!! Using this method is strongly recommended when working with
|
||||
* entities, as it prevents unnoticed bugs before they might happen !!!</b>
|
||||
* throws an {@link IllegalStateException}.
|
||||
* This occurs if for example a property is null even though it shouldn't be.
|
||||
* The check is based on the EventType. <b>Using this method is strongly recommended when working with
|
||||
* entities, as it prevents unnoticed bugs before they might happen!</b>
|
||||
*
|
||||
* @throws IllegalStateException if the current event is non-valid
|
||||
*/
|
||||
@ -314,19 +306,15 @@ public class EventBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility function for throwing a specific exception
|
||||
*
|
||||
* @throws IllegalStateException meaning that the event is non-valid for the current builder state
|
||||
* Utility function for throwing a specific exception.
|
||||
* @throws IllegalStateException if the builder hasn't received enough properties to construct the event
|
||||
*/
|
||||
private void throwException() throws IllegalStateException {
|
||||
throw new IllegalStateException("Properties malformed for " + this.type + ".\n" +
|
||||
"Builder properties: " + this.notNullToString());
|
||||
throw new IllegalStateException("Properties malformed for " + this.type + ".\n" + "Builder properties: " + this.notNullToString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a generic {@link GameEvent} from the values given to the builder.
|
||||
* This method checks for the correctness of the event before building it. If this is not desired,
|
||||
* use the {@link #buildGameEventUnchecked()} method instead
|
||||
* Builds a {@link GameEvent} from the values given to the builder.
|
||||
* <ul>
|
||||
* <li>type is the {@link EventType} of the {@link GameEvent}.</li>
|
||||
* <li>roundCount describes the count of rounds in the RoundSetupEvent</li>
|
||||
@ -338,31 +326,21 @@ public class EventBuilder {
|
||||
* <li>timeLeft describes the time left for a client to act before getting kicked in the TimeoutWarning event</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link GameEvent} based on the builder. Caution: non-given fields are null!
|
||||
* @param checked Determines if properties should be checked
|
||||
* @return a {@link GameEvent} based on the builder
|
||||
*/
|
||||
public GameEvent buildGameEvent() throws IllegalStateException {
|
||||
this.check();
|
||||
return buildGameEventUnchecked();
|
||||
public GameEvent buildGameEvent(boolean checked) throws IllegalStateException {
|
||||
if(checked) {
|
||||
this.check();
|
||||
}
|
||||
return buildGameEvent();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Builds a generic {@link GameEvent} from the values given to the builder <b>without checking whether this
|
||||
* * event is valid. Please use checked method {@link #buildGameEvent()} instead</b>
|
||||
* <ul>
|
||||
* <li>type is the {@link EventType} of the {@link GameEvent}.</li>
|
||||
* <li>roundCount describes the count of rounds in the RoundSetupEvent</li>
|
||||
* <li>turnCount describes the amount of turns taken in Events</li>
|
||||
* <li>characterOrder describes the order in which characters have their turn</li>
|
||||
* <li>nextCharacter describes the next character in the turn order in the TurnEvent</li>
|
||||
* <li>playerWon describes the player that has won the game in the WinEvent</li>
|
||||
* <li>message describes a generic message delivered as a String in several GameEvents. It is optional.</li>
|
||||
* <li>timeLeft describes the time left for a client to act before getting kicked in the TimeoutWarning event</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link GameEvent} based on the builder. Caution: non-given fields are null!
|
||||
* Builds a {@link GameEvent} from the values given to the builder without checking.
|
||||
* @return a {@link GameEvent} based on the builder
|
||||
*/
|
||||
public GameEvent buildGameEventUnchecked() throws IllegalStateException {
|
||||
private GameEvent buildGameEvent() {
|
||||
var gameEvent = new GameEvent();
|
||||
gameEvent.type = this.type;
|
||||
gameEvent.roundCount = this.roundCount;
|
||||
@ -379,38 +357,28 @@ public class EventBuilder {
|
||||
|
||||
/**
|
||||
* Builds an {@link EntityEvent} from the values given to the builder.
|
||||
* This method checks for the correctness of the event before building it. If this is not desired,
|
||||
* use the {@link #buildEntityEventUnchecked()} method instead
|
||||
*
|
||||
* <ul>
|
||||
* <li>type is the {@link EventType} of the EntityEvent</li>
|
||||
* <li>targetEntity is the target {@link Entity}</li>
|
||||
* <li>targetField is the target field in the form of an {@link IntVector2}</li>
|
||||
* <li>amount is the amount of something, like damage. It's an int.</li>
|
||||
* <li>amount is a generic amount, for example damage taken</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link EntityEvent} based on the builder. Caution: non-given fields are null!
|
||||
* @param checked Determines if properties should be checked
|
||||
* @return an {@link EntityEvent} based on the builder
|
||||
*/
|
||||
public EntityEvent buildEntityEvent() throws IllegalStateException {
|
||||
this.check();
|
||||
return this.buildEntityEventUnchecked();
|
||||
public EntityEvent buildEntityEvent(boolean checked) throws IllegalStateException {
|
||||
if(checked) {
|
||||
this.check();
|
||||
}
|
||||
return this.buildEntityEvent();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Builds an {@link EntityEvent} from the values given to the builder <b>without checking whether this
|
||||
* event is valid. Please use checked method {@link #buildEntityEvent()} instead</b>
|
||||
*
|
||||
* <ul>
|
||||
* <li>type is the {@link EventType} of the EntityEvent</li>
|
||||
* <li>targetEntity is the target {@link Entity}</li>
|
||||
* <li>targetField is the target field in the form of an {@link IntVector2}</li>
|
||||
* <li>amount is the amount of something, like damage. It's an int.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link EntityEvent} based on the builder. Caution: non-given fields are null!
|
||||
* Builds an {@link EntityEvent} from the values given to the builder without checking.
|
||||
* @return an {@link EntityEvent} based on the builder
|
||||
*/
|
||||
public EntityEvent buildEntityEventUnchecked() {
|
||||
private EntityEvent buildEntityEvent() {
|
||||
var entityEvent = new EntityEvent();
|
||||
entityEvent.type = this.type;
|
||||
entityEvent.targetEntity = this.targetEntity;
|
||||
@ -421,10 +389,7 @@ public class EventBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a generic {@link CharacterEvent} from the values given to the builder.
|
||||
* This method checks for the correctness of the event before building it. If this is not desired,
|
||||
* use the {@link #buildCharacterEventUnchecked()} method instead
|
||||
*
|
||||
* Builds a {@link CharacterEvent} from the values given to the builder.
|
||||
* <ul>
|
||||
* <li>type is the {@link EventType} of the EntityEvent</li>
|
||||
* <li>originEntity is the origin {@link Entity}</li>
|
||||
@ -433,31 +398,21 @@ public class EventBuilder {
|
||||
* <li>targetField is the target field in the form of an {@link IntVector2}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link CharacterEvent} based on the builder. Caution: non-given fields are null!
|
||||
* @throws IllegalStateException if the event being built is non-valid.
|
||||
* @param checked Determines if properties should be checked
|
||||
* @return a {@link CharacterEvent} based on the builder
|
||||
*/
|
||||
public CharacterEvent buildCharacterEvent() throws IllegalStateException {
|
||||
this.check();
|
||||
return buildCharacterEventUnchecked();
|
||||
public CharacterEvent buildCharacterEvent(boolean checked) throws IllegalStateException {
|
||||
if(checked) {
|
||||
this.check();
|
||||
}
|
||||
return buildCharacterEvent();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Builds a generic {@link CharacterEvent} from the values given to the builder <b>without checking whether this
|
||||
* event is valid. Please use checked method {@link #buildCharacterEvent()} instead</b>
|
||||
*
|
||||
* <ul>
|
||||
* <li>type is the {@link EventType} of the EntityEvent</li>
|
||||
* <li>originEntity is the origin {@link Entity}</li>
|
||||
* <li>targetEntity is the target {@link Entity}</li>
|
||||
* <li>originField is the field that an action originates in, delivered as an {@link IntVector2}</li>
|
||||
* <li>targetField is the target field in the form of an {@link IntVector2}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link CharacterEvent} based on the builder. Caution: non-given fields are null!
|
||||
* @throws IllegalStateException if the event being built is non-valid.
|
||||
* Builds a {@link CharacterEvent} from the values given to the builder without checking.
|
||||
* @return a {@link CharacterEvent} based on the builder
|
||||
*/
|
||||
public CharacterEvent buildCharacterEventUnchecked() {
|
||||
private CharacterEvent buildCharacterEvent() {
|
||||
var characterEvent = new CharacterEvent();
|
||||
characterEvent.type = this.type;
|
||||
characterEvent.originEntity = this.originEntity;
|
||||
@ -469,11 +424,8 @@ public class EventBuilder {
|
||||
return characterEvent;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Builds a generic {@link GameStateEvent} from the values given to the builder.
|
||||
* This method checks for the correctness of the event before building it. If this is not desired,
|
||||
* use the {@link #buildGameStateEventUnchecked()} method instead
|
||||
* <ul>
|
||||
* <li>type is the {@link EventType} of the {@link GameStateEvent}</li>
|
||||
* <li>entities is an array of {@link Entity Entities}</li>
|
||||
@ -482,29 +434,21 @@ public class EventBuilder {
|
||||
* <li>describes whether the win condition is in effect</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link GameStateEvent} based on the builder. Caution: non-given fields are null!
|
||||
* @throws IllegalStateException if the event being built is non-valid.
|
||||
* @param checked Determines if properties should be checked
|
||||
* @return a {@link GameStateEvent} based on the builder
|
||||
*/
|
||||
public GameStateEvent buildGameStateEvent() throws IllegalStateException {
|
||||
this.check();
|
||||
return this.buildGameStateEventUnchecked();
|
||||
public GameStateEvent buildGameStateEvent(boolean checked) throws IllegalStateException {
|
||||
if(checked) {
|
||||
this.check();
|
||||
}
|
||||
return this.buildGameStateEvent();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Builds a generic {@link GameStateEvent} from the values given to the builder <b>without checking whether this
|
||||
* event is valid. Please use checked method {@link #buildGameStateEvent()} instead</b>
|
||||
* <ul>
|
||||
* <li>type is the {@link EventType} of the {@link GameStateEvent}</li>
|
||||
* <li>entities is an array of {@link Entity Entities}</li>
|
||||
* <li>turnOrder describes the order in which characters take turns</li>
|
||||
* <li>activeCharacter describes the currently active character</li>
|
||||
* <li>describes whether the win condition is in effect</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link GameStateEvent} based on the builder. Caution: non-given fields are null!
|
||||
* Builds a {@link GameStateEvent} from the values given to the builder without checking.
|
||||
* @return a {@link GameStateEvent} based on the builder
|
||||
*/
|
||||
public GameStateEvent buildGameStateEventUnchecked() {
|
||||
private GameStateEvent buildGameStateEvent() {
|
||||
var gameStateEvent = new GameStateEvent();
|
||||
gameStateEvent.type = this.type;
|
||||
gameStateEvent.entities = this.entities;
|
||||
@ -515,33 +459,27 @@ public class EventBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a {@link CustomEvent} from the values given to the builder. This method checks for the correctness of the
|
||||
* event before building it. If this is not desired, use the {@link #buildCustomEventUnchecked()} method instead
|
||||
* Builds a {@link CustomEvent} from the values given to the builder.
|
||||
* <ul>
|
||||
* <li>teamIdentifier is a String for identifying a specific custom content</li>
|
||||
* <li>customContent is a {@link HashMap}<{@link String}, {@link Object}> resembling the JSON keys in the event</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link CustomEvent} based on the builder. Caution: non-given fields are null!
|
||||
* @throws IllegalStateException if the event being built is non-valid.
|
||||
* @param checked Determines if properties should be checked
|
||||
* @return a {@link CustomEvent} based on the builder
|
||||
*/
|
||||
public CustomEvent buildCustomEvent() throws IllegalStateException {
|
||||
this.check();
|
||||
var customEvent = new CustomEvent();
|
||||
return this.buildCustomEventUnchecked();
|
||||
public CustomEvent buildCustomEvent(boolean checked) throws IllegalStateException {
|
||||
if(checked) {
|
||||
this.check();
|
||||
}
|
||||
return this.buildCustomEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a {@link CustomEvent} from the values given to the builder <b>without checking whether this event
|
||||
* is valid. Please use checked method {@link #buildCustomEvent()} instead</b>
|
||||
* <ul>
|
||||
* <li>teamIdentifier is a String for identifying a specific custom content</li>
|
||||
* <li>customContent is a {@link HashMap}<{@link String}, {@link Object}> resembling the JSON keys in the event</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return a {@link CustomEvent} based on the builder. Caution: non-given fields are null!
|
||||
* Builds a {@link CustomEvent} from the values given to the builder without checking.
|
||||
* @return a {@link CustomEvent} based on the builder
|
||||
*/
|
||||
public CustomEvent buildCustomEventUnchecked() {
|
||||
private CustomEvent buildCustomEvent() {
|
||||
var customEvent = new CustomEvent();
|
||||
customEvent.type = this.type;
|
||||
customEvent.teamIdentifier = this.teamIdentifier;
|
||||
@ -551,55 +489,29 @@ public class EventBuilder {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "EventBuilder{" +
|
||||
"\ntype=" + type +
|
||||
",\n targetEntity=" + targetEntity +
|
||||
",\n targetField=" + targetField +
|
||||
",\n amount=" + amount +
|
||||
",\n entity=" + entity +
|
||||
",\n originEntity=" + originEntity +
|
||||
",\n originField=" + originField +
|
||||
",\n stoneType=" + stoneType +
|
||||
",\n roundCount=" + roundCount +
|
||||
",\n turnCount=" + turnCount +
|
||||
",\n characterOrder=" + Arrays.toString(characterOrder) +
|
||||
",\n nextCharacter=" + nextCharacter +
|
||||
",\n playerWon=" + playerWon +
|
||||
",\n message='" + message + '\'' +
|
||||
",\n timeLeft=" + timeLeft +
|
||||
",\n entities=" + Arrays.toString(entities) +
|
||||
",\n turnOrder=" + Arrays.toString(turnOrder) +
|
||||
",\n activeCharacter=" + activeCharacter +
|
||||
",\n winCondition=" + winCondition +
|
||||
",\n teamIdentifier='" + teamIdentifier + '\'' +
|
||||
",\n customContent=" + customContent +
|
||||
"\n}";
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
joiner.add("EventBuilder(all) {");
|
||||
for(Field field: this.getClass().getDeclaredFields()) {
|
||||
try {
|
||||
joiner.add(field.getName() + " = " + field.get(this));
|
||||
}catch(IllegalAccessException ignore) { }
|
||||
}
|
||||
joiner.add("}");
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
|
||||
public String notNullToString() {
|
||||
return "EventBuilder (only not null) {" +
|
||||
(type != null ? "\n type=" + type : "" )+
|
||||
(targetEntity != null ? "\n targetEntity=" + targetEntity : "" )+
|
||||
(targetField != null ? "\n targetField=" + targetField : "" )+
|
||||
(amount != null ? "\n amount=" + amount : "" )+
|
||||
(entity != null ? "\n entity=" + entity : "" )+
|
||||
(originEntity != null ? "\n originEntity=" + originEntity : "" )+
|
||||
(originField != null ? "\n originField=" + originField : "" )+
|
||||
(stoneType != null ? "\n stoneType=" + stoneType : "" )+
|
||||
(roundCount != null ? "\n roundCount=" + roundCount : "" )+
|
||||
(turnCount != null ? "\n turnCount=" + turnCount : "" )+
|
||||
(characterOrder != null ? "\n characterOrder=" + Arrays.toString(characterOrder) : "" )+
|
||||
(nextCharacter != null ? "\n nextCharacter=" + nextCharacter : "" )+
|
||||
(playerWon != null ? "\n playerWon=" + playerWon : "" )+
|
||||
(message != null ? "\n message='" + message : "" )+ '\'' +
|
||||
(timeLeft != null ? "\n timeLeft=" + timeLeft : "" )+
|
||||
(entities != null ? "\n entities=" + Arrays.toString(entities) : "" )+
|
||||
(turnOrder != null ? "\n turnOrder=" + Arrays.toString(turnOrder) : "" )+
|
||||
(activeCharacter != null ? "\n activeCharacter=" + activeCharacter : "" )+
|
||||
(winCondition != null ? "\n winCondition=" + winCondition : "" )+
|
||||
(teamIdentifier != null ? "\n teamIdentifier='" + teamIdentifier : "" )+ '\'' +
|
||||
(customContent != null ? "\n customContent=" + customContent : "" )+
|
||||
"\n}";
|
||||
StringJoiner joiner = new StringJoiner("\n");
|
||||
joiner.add("EventBuilder(non-null) {");
|
||||
for(Field field: this.getClass().getDeclaredFields()) {
|
||||
try {
|
||||
Object value = field.get(this);
|
||||
if(value != null) {
|
||||
joiner.add(field.getName() + " = " + value);
|
||||
}
|
||||
}catch(IllegalAccessException ignore) { }
|
||||
}
|
||||
joiner.add("}");
|
||||
return joiner.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package uulm.teamname.marvelous.gamelibrary.events;
|
||||
|
||||
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
@ -25,7 +23,7 @@ public class GameEvent extends Event {
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
GameEvent gameEvent = (GameEvent) o;
|
||||
return Objects.equals(roundCount, gameEvent.roundCount) && Objects.equals(turnCount, gameEvent.turnCount) && Objects.equals(playerWon, gameEvent.playerWon) && Objects.equals(timeLeft, gameEvent.timeLeft) && Arrays.equals(characterOrder, gameEvent.characterOrder) && Objects.equals(nextCharacter, gameEvent.nextCharacter) && Objects.equals(message, gameEvent.message);
|
||||
return Objects.equals(roundCount, gameEvent.roundCount) && Objects.equals(turnCount, gameEvent.turnCount) && Objects.equals(playerWon, gameEvent.playerWon) && Objects.equals(timeLeft, gameEvent.timeLeft) && Arrays.equals(characterOrder, gameEvent.characterOrder) && Objects.equals(nextCharacter, gameEvent.nextCharacter) && Objects.equals(message, gameEvent.message);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -3,7 +3,6 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
||||
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.Entity;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -5,7 +5,6 @@ import uulm.teamname.marvelous.gamelibrary.entities.Entity;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
||||
/** Represents a game state view containing getters for all the properties of a {@link GameState}. */
|
||||
|
@ -1,10 +1,10 @@
|
||||
package uulm.teamname.marvelous.gamelibrary.events;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
||||
import uulm.teamname.marvelous.gamelibrary.entities.*;
|
||||
@ -13,9 +13,6 @@ import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EventBuilderTest {
|
||||
EntityID[] turns;
|
||||
EntityID turn;
|
||||
@ -116,13 +113,20 @@ class EventBuilderTest {
|
||||
.withCustomContent(new HashMap<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
void builderToStringTest() {
|
||||
assertEquals("EventBuilder(non-null) {\ntype = TurnEvent\nroundCount = 5\n}", new EventBuilder(EventType.TurnEvent)
|
||||
.withRoundCount(5)
|
||||
.notNullToString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildGameEventUncheckedActualEvents() {
|
||||
// System.out.println("Checks for mistakes in GameEvent creation");
|
||||
var roundSetupEvent = new EventBuilder(EventType.RoundSetupEvent)
|
||||
.withRoundCount(4)
|
||||
.withCharacterOrder(turns)
|
||||
.buildGameEvent();
|
||||
.buildGameEvent(true);
|
||||
|
||||
var roundSetupEventBaseline = new GameEvent();
|
||||
roundSetupEventBaseline.type = EventType.RoundSetupEvent;
|
||||
@ -136,7 +140,7 @@ class EventBuilderTest {
|
||||
var turnEvent = new EventBuilder(EventType.TurnEvent)
|
||||
.withNextCharacter(turn)
|
||||
.withRoundCount(5)
|
||||
.buildGameEventUnchecked();
|
||||
.buildGameEvent(false);
|
||||
|
||||
var turnEventBaseline = new GameEvent();
|
||||
turnEventBaseline.type = EventType.TurnEvent;
|
||||
@ -159,7 +163,7 @@ class EventBuilderTest {
|
||||
.withPlayerWon(5912)
|
||||
.withMessage("This message is very much not useful at all")
|
||||
.withTimeLeft(-144)
|
||||
.buildGameEventUnchecked();
|
||||
.buildGameEvent(false);
|
||||
|
||||
var baseline = new GameEvent();
|
||||
baseline.type = EventType.DisconnectEvent;
|
||||
@ -184,7 +188,7 @@ class EventBuilderTest {
|
||||
.withTurnOrder(turns)
|
||||
.withActiveCharacter(turn)
|
||||
.withWinCondition(true)
|
||||
.buildGameStateEventUnchecked();
|
||||
.buildGameStateEvent(false);
|
||||
|
||||
var baseline = new GameStateEvent();
|
||||
baseline.type = EventType.ConsumedAPEvent;
|
||||
@ -217,28 +221,28 @@ class EventBuilderTest {
|
||||
.withAmount(15) // also properties of different EventTypes, they just get ignored
|
||||
.withEntities(entities) // properties belonging to the same eventType get incorporated into
|
||||
.withWinCondition(false) // the final event, so they have to be ignored
|
||||
.buildGameStateEvent()); // by the programmer later on
|
||||
.buildGameStateEvent(true)); // by the programmer later on
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildGameStateEvent() {
|
||||
assertThatNoException()
|
||||
.isThrownBy(() -> new EventBuilder(EventType.Ack) // needs no properties
|
||||
.buildGameStateEvent());
|
||||
.buildGameStateEvent(true));
|
||||
|
||||
|
||||
|
||||
assertThatNoException()
|
||||
.isThrownBy(() -> new EventBuilder(EventType.Nack).buildGameStateEvent());
|
||||
.isThrownBy(() -> new EventBuilder(EventType.Nack).buildGameStateEvent(true));
|
||||
|
||||
assertThatNoException()
|
||||
.isThrownBy(() -> new EventBuilder(EventType.Req).buildGameStateEvent());
|
||||
.isThrownBy(() -> new EventBuilder(EventType.Req).buildGameStateEvent(true));
|
||||
|
||||
assertThatExceptionOfType(IllegalStateException.class)
|
||||
.isThrownBy(() -> new EventBuilder(EventType.GameStateEvent) // if properties missing throw exception
|
||||
.withTurnOrder(turns)
|
||||
.withActiveCharacter(turn)
|
||||
.buildGameStateEvent());
|
||||
.buildGameStateEvent(true));
|
||||
|
||||
assertThatNoException()
|
||||
.isThrownBy(() -> new EventBuilder(EventType.GameStateEvent) // no exception if all properties present
|
||||
@ -246,7 +250,7 @@ class EventBuilderTest {
|
||||
.withTurnOrder(turns)
|
||||
.withActiveCharacter(turn)
|
||||
.withWinCondition(false)
|
||||
.buildGameStateEvent());
|
||||
.buildGameStateEvent(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -254,4 +258,4 @@ class EventBuilderTest {
|
||||
// TODO: check CustomEvent validation for correctness
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user