feat: completed EventBuilder with complete exception-based event checking for all events

This commit is contained in:
Yannik Bretschneider 2021-05-02 04:14:00 +02:00
parent 1bd3cdf63f
commit c190111de1

View File

@ -5,6 +5,7 @@ import uulm.teamname.marvelous.gamelibrary.entities.Entity;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
import java.util.Arrays;
import java.util.HashMap;
/**
@ -46,12 +47,12 @@ 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
// */
// public EventBuilder() {
//
// }
/**
@ -318,11 +319,14 @@ public class EventBuilder {
* @throws IllegalStateException meaning that the event is non-valid for the current builder state
*/
private void throwException() throws IllegalStateException {
throw new IllegalStateException("Properties malformed for " + this.type);
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
* 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
* <ul>
* <li>type is the {@link EventType} of the {@link GameEvent}.</li>
* <li>roundCount describes the count of rounds in the RoundSetupEvent</li>
@ -338,6 +342,27 @@ public class EventBuilder {
*/
public GameEvent buildGameEvent() throws IllegalStateException {
this.check();
return buildGameEventUnchecked();
}
/**
* 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!
*/
public GameEvent buildGameEventUnchecked() throws IllegalStateException {
var gameEvent = new GameEvent();
gameEvent.type = this.type;
gameEvent.roundCount = this.roundCount;
@ -355,7 +380,7 @@ 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 #buildCharacterEventUnchecked()} method instead
* use the {@link #buildEntityEventUnchecked()} method instead
*
* <ul>
* <li>type is the {@link EventType} of the EntityEvent</li>
@ -374,7 +399,7 @@ public class EventBuilder {
/**
* Builds an {@link EntityEvent} from the values given to the builder <b>without checking whether this
* event is valid. Please use checked method {@link #buildCharacterEvent()} instead</b>
* event is valid. Please use checked method {@link #buildEntityEvent()} instead</b>
*
* <ul>
* <li>type is the {@link EventType} of the EntityEvent</li>
@ -524,4 +549,57 @@ public class EventBuilder {
return customEvent;
}
@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}";
}
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}";
}
}