refactor: restructure code and fix some events not being applied

This commit is contained in:
2021-06-03 02:19:10 +02:00
parent e0af4cf01c
commit 3bb6d81b27
4 changed files with 371 additions and 364 deletions

View File

@ -1,9 +1,9 @@
package uulm.teamname.marvelous.gamelibrary.gamelogic;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import java.util.ArrayDeque;
import java.util.ArrayList;
/** Represents manager for a game state. */
@ -12,7 +12,7 @@ class GameStateManager {
private final GameState state;
/** The queue of {@link Event}s to be applied during {@link Request} processing */
private final ArrayDeque<Event> queue = new ArrayDeque<>();
private final ArrayList<Event> queue = new ArrayList<>();
/**
* Constructs a new {@link GameStateManager}.
@ -26,13 +26,17 @@ class GameStateManager {
* Checks a list of {@link Request}s for validity and optionally produces resulting {@link Event}s.
* @param requests The requests to check
* @param apply True if resulting events should be stored for later application
* @return Whether the requests are valid
*/
public boolean processRequests(Request[] requests, boolean apply) {
public boolean processRequests(ArrayList<Request> requests, boolean apply) {
GameState snapshot = state.snapshot();
for(Request request: requests) {
if (GameLogic.checkRequest(snapshot, request)) {
ArrayList<Event> result = GameLogic.executeRequest(snapshot, request);
for(Event event: result) {
GameLogic.applyEvent(snapshot, event);
}
if(apply) {
queue.addAll(result);
}
@ -45,32 +49,6 @@ class GameStateManager {
return true;
}
/**
* Handles events that happen after a turn phase.
* @return The optionally resulting {@link Event}s
*/
public ArrayList<Event> checkPostPhase() {
return GameLogic.checkTurnEnd(state);
}
/**
* Initializes the game.
* @param selectedCharacters1 The characters selected by player 1
* @param selectedCharacters2 The characters selected by player 2
* @return The resulting {@link Event}s
*/
public ArrayList<Event> initGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
return GameLogic.startGame(state, selectedCharacters1, selectedCharacters2);
}
/**
* Starts the game.
* @return The resulting {@link Event}s
*/
public ArrayList<Event> startGame() {
return GameLogic.handleRoundStart(state);
}
/**
* Applies an array of {@link Event}s to the game state.
* @param events The events to apply
@ -81,17 +59,64 @@ class GameStateManager {
}
}
/**
* Applies an {@link Event} to the game state.
* @param event The event to apply
*/
public void applyEvent(Event event) {
GameLogic.applyEvent(state, event);
}
/**
* Applies the result of the last processRequests call.
* @return A list of applied events
*/
public ArrayList<Event> apply() {
ArrayList<Event> toReturn = new ArrayList<>(queue.size());
Event current;
while ((current = queue.poll()) != null) {
GameLogic.applyEvent(state, current);
toReturn.add(current);
for(Event event: queue) {
GameLogic.applyEvent(state, event);
}
return toReturn;
// i feel so smart for remembering this trick
try {
return new ArrayList<>(queue);
}finally {
queue.clear();
}
}
/**
* Handles events that happen after a turn phase.
* @return The optionally resulting {@link Event}s
*/
public ArrayList<Event> checkPostPhase() {
ArrayList<Event> result = GameLogic.checkTurnEnd(state);
applyEvents(result);
return result;
}
/**
* Initializes and starts the game.
* @param selectedCharacters1 The characters selected by player 1
* @param selectedCharacters2 The characters selected by player 2
* @return The resulting {@link Event}s
*/
public ArrayList<Event> startGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
ArrayList<Event> result = GameLogic.startGame(state, selectedCharacters1, selectedCharacters2);
applyEvents(result);
ArrayList<Event> result2 = GameLogic.startRound(state);
applyEvents(result2);
result.addAll(result2);
return result;
}
/**
* Produces a {@link EventType#GamestateEvent} for the current {@link GameState}.
* @return The resulting event
*/
public Event getGameStateEvent() {
return GameLogic.buildGameStateEvent(state);
}
}