feat: added EventBuilder for building Events with more convenience and nicer code
This commit is contained in:
parent
d00c18c9d3
commit
1cb86f6be8
@ -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<String, Object> customContent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an empty {@link EventBuilder} used for building {@link Event}s
|
||||||
|
*/
|
||||||
|
public EventBuilder () {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// With <something> 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<String, Object> customContent) {
|
||||||
|
this.customContent = customContent;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds a {@link GameEvent} from the values given to the builder
|
||||||
|
* <b>type</b> is the {@link EventType} of the {@link GameEvent}.
|
||||||
|
* <b>roundCount</b> describes the count of rounds in the RoundSetupEvent
|
||||||
|
* <b>turnCount</b> describes the amount of turns taken in Events
|
||||||
|
* <b>characterOrder</b> describes the order in which characters have their turn
|
||||||
|
* <b>nextCharacter</b> describes the next character in the turn order in the TurnEvent
|
||||||
|
* <b>playerWon</b> describes the player that has won the game in the WinEvent
|
||||||
|
* <b>message</b> describes a generic message delivered as a String in several GameEvents. It is optional.
|
||||||
|
* <b>timeLeft</b> 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.
|
||||||
|
* <b>type</b> is the {@link EventType} of the EntityEvent
|
||||||
|
* <b>targetEntity</b> is the target {@link Entity}
|
||||||
|
* <b>targetField</b> is the target field in the form of an {@link IntVector2}
|
||||||
|
* <b>amount</b> 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.
|
||||||
|
* <b>type</b> is the {@link EventType} of the EntityEvent
|
||||||
|
* <b>originEntity</b> is the origin {@link Entity}
|
||||||
|
* <b>targetEntity</b> is the target {@link Entity}
|
||||||
|
* <b>originField</b> is the field that an action originates in, delivered as an {@link IntVector2}
|
||||||
|
* <b>targetField</b> 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.
|
||||||
|
* <b>type</b> is the {@link EventType} of the {@link GameStateEvent}
|
||||||
|
* <b>entities</b> is an array of {@link Entity Entities}
|
||||||
|
* <b>turnOrder</b> describes the order in which characters take turns
|
||||||
|
* <b>activeCharacter</b> describes the currently active character
|
||||||
|
* <b>describes</b> 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
|
||||||
|
* <b>teamIdentifier</b> is a String for identifying a specific custom content
|
||||||
|
* <b>customContent</b> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user