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