diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java index 61d4bf8..876d654 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java @@ -34,7 +34,7 @@ public class GameInstance { /** * Checks an array of {@link Request}s for validity and automatically applies them if valid. * @param requests The requests to check - * @return The list of resulting {@link Event}s or `null` if the check failed + * @return The list of resulting {@link Event}s or {@link Optional#empty()} if the check failed */ public Optional> checkRequestsAndApply(ArrayList requests) { if(manager.processRequests(requests, true)) { diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java index 623e788..8bb7aed 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -13,10 +13,7 @@ import uulm.teamname.marvelous.gamelibrary.requests.RequestType; import java.awt.*; import java.awt.geom.Line2D; import java.awt.geom.Point2D; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.Random; +import java.util.*; /** Contains game logic handling. */ public class GameLogic { @@ -517,6 +514,22 @@ public class GameLogic { state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize()); } + case GamestateEvent -> { + GamestateEvent data = (GamestateEvent)event; + + state.entities.clear(); + state.entities.addEntities(data.entities); + + state.mapSize.set(data.mapSize); + + state.turnOrder = new ArrayList<>(Arrays.asList(data.turnOrder)); + + state.activeCharacter = data.activeCharacter; + + state.stoneCooldown.import_(data.stoneCooldowns); + + state.won = data.winCondition; + } } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/StoneCooldownManager.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/StoneCooldownManager.java index 9607c40..9e1fbb9 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/StoneCooldownManager.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/StoneCooldownManager.java @@ -82,4 +82,14 @@ public class StoneCooldownManager { } return data; } + + /** + * Imports the cooldowns from an array ordered according to the enum. + * @param data An array containing every cooldown + */ + public void import_(Integer[] data) { + for(int i = 0; i < data.length; i++) { + cooldowns.put(StoneType.valueOf(i), data[i]); + } + } } diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java index f097ff3..97af980 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java @@ -7,6 +7,7 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.config.*; import uulm.teamname.marvelous.gamelibrary.entities.*; import uulm.teamname.marvelous.gamelibrary.entities.Character; +import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.requests.*; import java.util.*; @@ -167,6 +168,16 @@ class GameLogicTest { } + @Test + void testGamestateEvent() { + GameInstance a = new GameInstance(partyConfig, characterConfig, scenarioConfig); + GameInstance b = new GameInstance(partyConfig, characterConfig, scenarioConfig); + + b.applyEvents(a.startGame(player1Selection, player2Selection)); + + assertEquals(a.toString(), b.toString(), "Second GameInstance copies the state successfully"); + } + @Test void testRasterize() { ArrayList result = GameLogic.rasterize(new IntVector2(0, 0), new IntVector2(1, 1), false, false);