2021-04-29 17:15:29 +00:00
|
|
|
package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
|
|
|
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.events.Event;
|
2021-06-03 00:19:10 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.events.EventType;
|
2021-04-29 17:15:29 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.requests.Request;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2021-06-06 15:55:57 +00:00
|
|
|
import java.util.List;
|
2021-04-29 17:15:29 +00:00
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/** Represents manager for a game state. */
|
2021-04-29 17:15:29 +00:00
|
|
|
class GameStateManager {
|
2021-04-30 18:54:34 +00:00
|
|
|
/** The managed {@link GameState} */
|
2021-04-29 17:15:29 +00:00
|
|
|
private final GameState state;
|
|
|
|
|
2021-06-03 01:54:18 +00:00
|
|
|
/** The queue of {@link Event Events} to be applied during {@link Request} processing */
|
2021-06-03 00:19:10 +00:00
|
|
|
private final ArrayList<Event> queue = new ArrayList<>();
|
2021-04-29 17:15:29 +00:00
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new {@link GameStateManager}.
|
|
|
|
* @param state A reference to the state to be managed
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
|
|
|
public GameStateManager(GameState state) {
|
|
|
|
this.state = state;
|
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
2021-06-03 01:54:18 +00:00
|
|
|
* Checks a list of {@link Request Requests} for validity and optionally produces resulting {@link Event Events}.
|
2021-04-30 18:54:34 +00:00
|
|
|
* @param requests The requests to check
|
|
|
|
* @param apply True if resulting events should be stored for later application
|
2021-06-03 00:19:10 +00:00
|
|
|
* @return Whether the requests are valid
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
2021-06-03 00:19:10 +00:00
|
|
|
public boolean processRequests(ArrayList<Request> requests, boolean apply) {
|
2021-04-29 17:15:29 +00:00
|
|
|
GameState snapshot = state.snapshot();
|
|
|
|
|
|
|
|
for(Request request: requests) {
|
|
|
|
if (GameLogic.checkRequest(snapshot, request)) {
|
2021-05-02 12:38:03 +00:00
|
|
|
ArrayList<Event> result = GameLogic.executeRequest(snapshot, request);
|
2021-06-03 00:19:10 +00:00
|
|
|
for(Event event: result) {
|
|
|
|
GameLogic.applyEvent(snapshot, event);
|
|
|
|
}
|
2021-04-29 17:15:29 +00:00
|
|
|
if(apply) {
|
|
|
|
queue.addAll(result);
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
queue.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-03 01:33:20 +00:00
|
|
|
/**
|
2021-06-03 01:54:18 +00:00
|
|
|
* Applies an array of {@link Event Events} to the game state.
|
2021-06-03 01:33:20 +00:00
|
|
|
* @param events The events to apply
|
|
|
|
*/
|
|
|
|
public void applyEvents(Event[] events) {
|
|
|
|
for(Event event: events) {
|
|
|
|
GameLogic.applyEvent(state, event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 17:59:41 +00:00
|
|
|
/**
|
2021-06-03 01:54:18 +00:00
|
|
|
* Applies an array of {@link Event Events} to the game state.
|
2021-06-03 00:19:10 +00:00
|
|
|
* @param events The events to apply
|
2021-05-19 17:59:41 +00:00
|
|
|
*/
|
2021-06-06 15:55:57 +00:00
|
|
|
public void applyEvents(List<Event> events) {
|
2021-06-03 00:19:10 +00:00
|
|
|
for(Event event: events) {
|
|
|
|
GameLogic.applyEvent(state, event);
|
|
|
|
}
|
2021-05-19 17:59:41 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 19:37:18 +00:00
|
|
|
/**
|
2021-06-03 00:19:10 +00:00
|
|
|
* Applies an {@link Event} to the game state.
|
|
|
|
* @param event The event to apply
|
2021-05-31 19:37:18 +00:00
|
|
|
*/
|
2021-06-03 00:19:10 +00:00
|
|
|
public void applyEvent(Event event) {
|
|
|
|
GameLogic.applyEvent(state, event);
|
2021-05-31 19:37:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 18:04:10 +00:00
|
|
|
/**
|
2021-06-03 00:19:10 +00:00
|
|
|
* Applies the result of the last processRequests call.
|
|
|
|
* @return A list of applied events
|
2021-05-19 18:04:10 +00:00
|
|
|
*/
|
2021-06-03 00:19:10 +00:00
|
|
|
public ArrayList<Event> apply() {
|
|
|
|
for(Event event: queue) {
|
|
|
|
GameLogic.applyEvent(state, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
// i feel so smart for remembering this trick
|
|
|
|
try {
|
|
|
|
return new ArrayList<>(queue);
|
|
|
|
}finally {
|
|
|
|
queue.clear();
|
|
|
|
}
|
2021-05-19 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
2021-06-03 00:19:10 +00:00
|
|
|
* Handles events that happen after a turn phase.
|
2021-06-03 01:54:18 +00:00
|
|
|
* @return The optionally resulting {@link Event Events}
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
2021-06-03 00:19:10 +00:00
|
|
|
public ArrayList<Event> checkPostPhase() {
|
2021-06-04 04:41:59 +00:00
|
|
|
GameState snapshot = state.snapshot();
|
|
|
|
ArrayList<Event> result = GameLogic.checkTurnEnd(snapshot);
|
2021-06-03 00:19:10 +00:00
|
|
|
applyEvents(result);
|
|
|
|
return result;
|
2021-04-29 17:15:29 +00:00
|
|
|
}
|
|
|
|
|
2021-04-30 18:54:34 +00:00
|
|
|
/**
|
2021-06-03 00:19:10 +00:00
|
|
|
* Initializes and starts the game.
|
|
|
|
* @param selectedCharacters1 The characters selected by player 1
|
|
|
|
* @param selectedCharacters2 The characters selected by player 2
|
2021-06-03 01:54:18 +00:00
|
|
|
* @return The resulting {@link Event Events}
|
2021-04-29 17:15:29 +00:00
|
|
|
*/
|
2021-06-06 15:55:57 +00:00
|
|
|
public List<Event> startGame(List<Integer> selectedCharacters1, List<Integer> selectedCharacters2) {
|
2021-06-04 04:41:59 +00:00
|
|
|
GameState snapshot = state.snapshot();
|
|
|
|
|
|
|
|
ArrayList<Event> result = GameLogic.startGame(snapshot, selectedCharacters1, selectedCharacters2);
|
2021-06-03 00:19:10 +00:00
|
|
|
applyEvents(result);
|
|
|
|
|
2021-07-28 22:22:11 +00:00
|
|
|
result.add(GameLogic.buildGameStateEvent(state));
|
|
|
|
|
2021-06-04 04:41:59 +00:00
|
|
|
snapshot = state.snapshot();
|
|
|
|
|
|
|
|
ArrayList<Event> result2 = GameLogic.startRound(snapshot);
|
2021-06-03 00:19:10 +00:00
|
|
|
applyEvents(result2);
|
|
|
|
|
|
|
|
result.addAll(result2);
|
|
|
|
|
2021-07-28 22:22:11 +00:00
|
|
|
result.add(GameLogic.buildGameStateEvent(state));
|
2021-07-28 22:17:41 +00:00
|
|
|
|
2021-06-03 00:19:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-03 00:21:12 +00:00
|
|
|
/**
|
|
|
|
* Forcefully ends the current turn.
|
2021-06-03 01:54:18 +00:00
|
|
|
* @return The resulting {@link Event Events}
|
2021-06-03 00:21:12 +00:00
|
|
|
*/
|
|
|
|
public ArrayList<Event> endTurn() {
|
2021-06-04 04:41:59 +00:00
|
|
|
GameState snapshot = state.snapshot();
|
|
|
|
ArrayList<Event> result = GameLogic.endTurn(snapshot);
|
2021-06-03 00:21:12 +00:00
|
|
|
applyEvents(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-03 00:19:10 +00:00
|
|
|
/**
|
|
|
|
* Produces a {@link EventType#GamestateEvent} for the current {@link GameState}.
|
|
|
|
* @return The resulting event
|
|
|
|
*/
|
|
|
|
public Event getGameStateEvent() {
|
|
|
|
return GameLogic.buildGameStateEvent(state);
|
2021-04-29 17:15:29 +00:00
|
|
|
}
|
|
|
|
}
|