diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/events/EventBuilder.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/events/EventBuilder.java new file mode 100644 index 0000000..9212081 --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/events/EventBuilder.java @@ -0,0 +1,259 @@ +package uulm.teamname.marvelous.gamelibrary.events; + +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.StoneType; + +import java.util.HashMap; + +/** + * A class made for building all sorts of {@link Event}s as nicely as possible. It works by taking + * .with[KEY]() functions and then building a specific event at the end + */ +public class EventBuilder { + + // variable definitions (as in all possible keys) + /** The type of event that is being built */ + private EventType type; + + // Keys used primarily in EntityEvents, but also in others + private EntityID targetEntity; + private IntVector2 targetField; + private Integer amount; + + // Keys used primarily in CharacterEvents + private EntityID originEntity; + private IntVector2 originField; + private StoneType stoneType; + + // Keys used primarily in GameEvents + private int roundCount; + private int turnCount; + private EntityID[] characterOrder; + private EntityID nextCharacter; + + private int playerWon; + + private String message; + private int timeLeft; + + // Keys used primarily in GamestateEvents + private Entity[] entities; + private EntityID[] turnOrder; + private EntityID activeCharacter; + private boolean winCondition; + + // Keys used in CustomEvents + private String teamIdentifier; + private HashMap customContent; + + /** + * Creates an empty {@link EventBuilder} used for building {@link Event}s + */ + public EventBuilder () { + + } + + + // With for builder pattern style things + + public EventBuilder withType(EventType type) { + this.type = type; + return this; + } + + public EventBuilder withTargetEntity(EntityID targetEntity) { + this.targetEntity = targetEntity; + return this; + } + + public EventBuilder withTargetField(IntVector2 targetField) { + this.targetField = targetField; + return this; + } + + public EventBuilder withAmount(Integer amount) { + this.amount = amount; + return this; + } + + public EventBuilder withOriginEntity(EntityID originEntity) { + this.originEntity = originEntity; + return this; + } + + public EventBuilder withOriginField(IntVector2 originField) { + this.originField = originField; + return this; + } + + public EventBuilder withStoneType(StoneType stoneType) { + this.stoneType = stoneType; + return this; + } + + public EventBuilder withRoundCount(int roundCount) { + this.roundCount = roundCount; + return this; + } + + public EventBuilder withTurnCount(int turnCount) { + this.turnCount = turnCount; + return this; + } + + public EventBuilder withCharacterOrder(EntityID[] characterOrder) { + this.characterOrder = characterOrder; + return this; + } + + public EventBuilder withNextCharacter(EntityID nextCharacter) { + this.nextCharacter = nextCharacter; + return this; + } + + public EventBuilder withPlayerWon(int playerWon) { + this.playerWon = playerWon; + return this; + } + + public EventBuilder withMessage(String message) { + this.message = message; + return this; + } + + public EventBuilder withTimeLeft(int timeLeft) { + this.timeLeft = timeLeft; + return this; + } + + public EventBuilder withEntities(Entity[] entities) { + this.entities = entities; + return this; + } + + public EventBuilder withTurnOrder(EntityID[] turnOrder) { + this.turnOrder = turnOrder; + return this; + } + + public EventBuilder withActiveCharacter(EntityID activeCharacter) { + this.activeCharacter = activeCharacter; + return this; + } + + public EventBuilder withWinCondition(boolean winCondition) { + this.winCondition = winCondition; + return this; + } + + public EventBuilder withTeamIdentifier(String teamIdentifier) { + this.teamIdentifier = teamIdentifier; + return this; + } + + public EventBuilder withCustomContent(HashMap customContent) { + this.customContent = customContent; + return this; + } + + /** + * Builds a {@link GameEvent} from the values given to the builder + * type is the {@link EventType} of the {@link GameEvent}. + * roundCount describes the count of rounds in the RoundSetupEvent + * turnCount describes the amount of turns taken in Events + * characterOrder describes the order in which characters have their turn + * nextCharacter describes the next character in the turn order in the TurnEvent + * playerWon describes the player that has won the game in the WinEvent + * message describes a generic message delivered as a String in several GameEvents. It is optional. + * timeLeft describes the time left for a client to act before getting kicked in the TimeoutWarning event + * @return a {@link GameEvent} based on the builder. Caution: non-given fields are null! + */ + public GameEvent buildGameEvent() { + var gameEvent = new GameEvent(); + gameEvent.type = this.type; + gameEvent.roundCount = this.roundCount; + gameEvent.turnCount= this.turnCount; + gameEvent.characterOrder = this.characterOrder; + gameEvent.nextCharacter = this.nextCharacter; + + gameEvent.playerWon = this.playerWon; + + gameEvent.message = this.message; + gameEvent.timeLeft = this.timeLeft; + return gameEvent; + } + + /** + * Builds an {@link EntityEvent} from the values given to the builder. + * type is the {@link EventType} of the EntityEvent + * targetEntity is the target {@link Entity} + * targetField is the target field in the form of an {@link IntVector2} + * amount is the amount of something, like damage. It's an int. + * @return a {@link EntityEvent} based on the builder. Caution: non-given fields are null! + */ + public EntityEvent buildEntityEvent() { + var entityEvent = new EntityEvent(); + entityEvent.type = this.type; + entityEvent.targetEntity = this.targetEntity; + entityEvent.targetField = this.targetField; + entityEvent.amount = this.amount; + return entityEvent; + } + + /** + * Builds a {@link CharacterEvent} from the values given to the builder. + * type is the {@link EventType} of the EntityEvent + * originEntity is the origin {@link Entity} + * targetEntity is the target {@link Entity} + * originField is the field that an action originates in, delivered as an {@link IntVector2} + * targetField is the target field in the form of an {@link IntVector2} + * @return a {@link CharacterEvent} based on the builder. Caution: non-given fields are null! + */ + public CharacterEvent buildCharacterEvent() { + var characterEvent = new CharacterEvent(); + characterEvent.type = this.type; + characterEvent.originEntity = this.originEntity; + characterEvent.targetEntity = this.targetEntity; + characterEvent.originField = this.originField; + characterEvent.targetField = this.targetField; + characterEvent.amount = this.amount; + characterEvent.stoneType = this.stoneType; + return characterEvent; + } + + /** + * Builds a {@link GameStateEvent} from the values given to the builder. + * type is the {@link EventType} of the {@link GameStateEvent} + * entities is an array of {@link Entity Entities} + * turnOrder describes the order in which characters take turns + * activeCharacter describes the currently active character + * describes whether the win condition is in effect + * @return a {@link GameStateEvent} based on the builder. Caution: non-given fields are null! + */ + public GameStateEvent buildGamestateEvent() { + var gamestateEvent = new GameStateEvent(); + gamestateEvent.type = this.type; + gamestateEvent.entities = this.entities; + gamestateEvent.turnOrder = this.turnOrder; + gamestateEvent.activeCharacter = this.activeCharacter; + gamestateEvent.winCondition = this.winCondition; + return gamestateEvent; + } + + /** + * Builds a {@link CustomEvent} from the values given to the builder + * teamIdentifier is a String for identifying a specific custom content + * customContent is a {@link HashMap}<{@link String}, {@link Object}> resembling the JSON keys in the event + * @return a {@link CustomEvent} based on the builder. Caution: non-given fields are null! + */ + public CustomEvent buildCustomEvent() { + var customEvent = new CustomEvent(); + customEvent.type = this.type; + customEvent.teamIdentifier = this.teamIdentifier; + customEvent.customContent = this.customContent; + return customEvent; + } +} +