From 09d798f37f9c609777608fd65351ca20c7af2cc9 Mon Sep 17 00:00:00 2001 From: punchready Date: Thu, 3 Jun 2021 03:33:20 +0200 Subject: [PATCH] refactor: introduce utils method for converting arrays into arraylists and make methods accessible with either --- .../marvelous/gamelibrary/ArrayTools.java | 21 ++++++++++++++ .../gamelibrary/gamelogic/GameInstance.java | 28 +++++++++++++++++++ .../gamelibrary/gamelogic/GameLogic.java | 3 +- .../gamelogic/GameStateManager.java | 10 +++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/main/java/uulm/teamname/marvelous/gamelibrary/ArrayTools.java diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/ArrayTools.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/ArrayTools.java new file mode 100644 index 0000000..024a8df --- /dev/null +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/ArrayTools.java @@ -0,0 +1,21 @@ +package uulm.teamname.marvelous.gamelibrary; + +import java.util.ArrayList; + +/** Provides various tools for Arrays. */ +public class ArrayTools { + /** + * Returns a variable-size array list backed by the specified array without allocating more memory than necessary. + * @param the class of the objects in the array + * @param a the array by which the list will be backed + * @return a list view of the specified array + * @throws NullPointerException if the specified array is {@code null} + */ + public static ArrayList toArrayList(E[] a) { + ArrayList l = new ArrayList<>(a.length); + for(E e: a) { + l.add(e); + } + return l; + } +} 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 876d654..72fd41d 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameInstance.java @@ -1,5 +1,6 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; +import uulm.teamname.marvelous.gamelibrary.ArrayTools; import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.entities.Entity; import uulm.teamname.marvelous.gamelibrary.events.Event; @@ -10,6 +11,7 @@ import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig; import uulm.teamname.marvelous.gamelibrary.requests.Request; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -31,6 +33,15 @@ public class GameInstance { manager = new GameStateManager(_state); } + /** + * 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 {@link Optional#empty()} if the check failed + */ + public Optional> checkRequestsAndApply(Request... requests) { + return checkRequestsAndApply(ArrayTools.toArrayList(requests)); + } + /** * Checks an array of {@link Request}s for validity and automatically applies them if valid. * @param requests The requests to check @@ -48,6 +59,15 @@ public class GameInstance { return Optional.empty(); } + /** + * Checks an array of {@link Request}s for validity without applying it. + * @param requests The requests to check + * @return Whether or not the given set of requests is valid + */ + public boolean checkRequests(Request... requests) { + return checkRequests(ArrayTools.toArrayList(requests)); + } + /** * Checks an array of {@link Request}s for validity without applying it. * @param requests The requests to check @@ -57,6 +77,14 @@ public class GameInstance { return manager.processRequests(requests, false); } + /** + * Applies an array of {@link Event}s to the game state. + * @param events The events to apply. + */ + public void applyEvents(Event... events) { + manager.applyEvents(events); + } + /** * Applies an array of {@link Event}s to the game state. * @param events The events to apply. 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 e2f7a62..7e8ff05 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -1,5 +1,6 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; +import uulm.teamname.marvelous.gamelibrary.ArrayTools; import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.entities.Character; import uulm.teamname.marvelous.gamelibrary.entities.*; @@ -543,7 +544,7 @@ public class GameLogic { state.mapSize.set(data.mapSize); - state.turnOrder = new ArrayList<>(Arrays.asList(data.turnOrder)); + state.turnOrder = ArrayTools.toArrayList(data.turnOrder); state.activeCharacter = data.activeCharacter; diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateManager.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateManager.java index 167060b..d851678 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateManager.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateManager.java @@ -49,6 +49,16 @@ class GameStateManager { return true; } + /** + * Applies an array of {@link Event}s to the game state. + * @param events The events to apply + */ + public void applyEvents(Event[] events) { + for(Event event: events) { + GameLogic.applyEvent(state, event); + } + } + /** * Applies an array of {@link Event}s to the game state. * @param events The events to apply