From 8e6f61a665e2d70edf4e903abbf5451c66f05cf2 Mon Sep 17 00:00:00 2001 From: punchready Date: Tue, 1 Jun 2021 17:59:45 +0200 Subject: [PATCH] fix: add missing checks to game logic and fix events not being applied --- .../gamelibrary/gamelogic/GameInstance.java | 55 ++++++++++++++++++- .../gamelibrary/gamelogic/GameLogic.java | 42 ++++++++++++++ .../gamelibrary/gamelogic/GameLogicTest.java | 4 +- 3 files changed, 98 insertions(+), 3 deletions(-) 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 9909a5d..72925da 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,7 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; +import uulm.teamname.marvelous.gamelibrary.IntVector2; +import uulm.teamname.marvelous.gamelibrary.entities.Entity; import uulm.teamname.marvelous.gamelibrary.events.Event; import uulm.teamname.marvelous.gamelibrary.events.EventType; import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig; @@ -37,7 +39,12 @@ public class GameInstance { public Optional> checkRequestsAndApply(Request... requests) { if(manager.processRequests(requests, true)) { ArrayList result = manager.apply(); - result.addAll(manager.checkPostPhase()); + + ArrayList result2 = manager.checkPostPhase(); + manager.applyEvents(result2); + + result.addAll(result2); + return Optional.of(result); } @@ -62,7 +69,11 @@ public class GameInstance { public ArrayList startGame(ArrayList selectedCharacters1, ArrayList selectedCharacters2) { ArrayList result = manager.initGame(selectedCharacters1, selectedCharacters2); manager.applyEvents(result); - result.addAll(manager.startGame()); + + ArrayList result2 = manager.startGame(); + manager.applyEvents(result2); + + result.addAll(result2); //do not merge this, result needs to be applied before startGame is called return result; } @@ -90,4 +101,44 @@ public class GameInstance { public void applyEvents(ArrayList events) { manager.applyEvents(events); } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for(int y = 0; y < _state.mapSize.getY(); y++) { + for(int x = 0; x < _state.mapSize.getX(); x++) { + ArrayList entities = _state.entities.findByPosition(new IntVector2(x, y)); + if(entities.isEmpty()) { + sb.append(". "); + }else { + switch(entities.get(0).id.type) { + case NPC -> { + switch(entities.get(0).id.id) { + case 0 -> { + sb.append("G "); + } + case 1 -> { + sb.append("S "); + } + case 2 -> { + sb.append("T "); + } + } + } + case P1, P2 -> { + sb.append("X "); + } + case Rocks -> { + sb.append("O "); + } + case InfinityStones -> { + sb.append("# "); + } + } + } + } + sb.append("\n"); + } + return sb.toString(); + } } 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 0baa4b3..6c8db10 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -214,6 +214,9 @@ class GameLogic { Character origin = getCharacter(state, data.originField, data.originEntity); Character target = getCharacter(state, data.targetField, data.targetEntity); + requireTurn(state, origin); + requireOppositeTeam(origin, target); + requireAlive(origin); requireAlive(target); requireAP(origin, 1); @@ -245,6 +248,8 @@ class GameLogic { Character origin = getCharacter(state, data.originField, data.originEntity); + requireTurn(state, origin); + requireAlive(origin); requireMP(origin, 1); verifyCoordinates(state, data.targetField); @@ -265,6 +270,9 @@ class GameLogic { Character origin = getCharacter(state, data.originField, data.originEntity); Character target = getCharacter(state, data.targetField, data.targetEntity); + requireTurn(state, origin); + requireSameTeam(origin, target); + requireAlive(origin); requireAlive(target); requireAP(origin, 1); @@ -281,6 +289,8 @@ class GameLogic { Character origin = getCharacter(state, data.originField, data.originEntity); + requireTurn(state, origin); + requireAlive(origin); requireAP(origin, 1); requireInfinityStone(origin, data.stoneType); @@ -327,6 +337,7 @@ class GameLogic { Character target = getCharacter(state, data.targetField, data.targetEntity); requireAlive(target); + requireOppositeTeam(origin, target); if(origin.rangedDamage * 2 != data.value) { throw new InvalidRequestException(); @@ -338,6 +349,10 @@ class GameLogic { case SoulStone -> { Character target = getCharacter(state, data.targetField, data.targetEntity); + if(data.originEntity == data.targetEntity) { + throw new InvalidRequestException(); + } + if(target.hp.getValue() != 0) { throw new InvalidRequestException(); } @@ -377,6 +392,33 @@ class GameLogic { } } + /** + * Verifies that a {@link Character} has a turn. + */ + private static void requireTurn(GameState state, Character entity) throws InvalidRequestException { + if(entity.id != state.activeCharacter) { + throw new InvalidRequestException(); + } + } + + /** + * Verifies that a {@link Character} is of the opposite team of another Character. + */ + private static void requireOppositeTeam(Character a, Character b) throws InvalidRequestException { + if(a.id.type == b.id.type) { + throw new InvalidRequestException(); + } + } + + /** + * Verifies that a {@link Character} is of the same team of another Character, but not the same. + */ + private static void requireSameTeam(Character a, Character b) throws InvalidRequestException { + if(a.id.type != b.id.type || a.id.id == b.id.id) { + throw new InvalidRequestException(); + } + } + /** * Verifies that a {@link Character} is alive. */ 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 2f5dbb1..e0b225b 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java @@ -44,7 +44,7 @@ class GameLogicTest { scenarioConfig.name = generateName(20); scenarioConfig.author = generateName(20); - scenarioConfig.scenario = new FieldType[30][30]; + scenarioConfig.scenario = new FieldType[20][20]; for(int x = 0; x < scenarioConfig.scenario[0].length; x++) { for(int y = 0; y < scenarioConfig.scenario.length; y++) { if(Math.abs(randomIntegers.next() % 100) < 10) { @@ -170,6 +170,8 @@ class GameLogicTest { Event actual = result.get(0); assertEquals(EventType.GamestateEvent, actual.type, "First event should be a GameStateEvent"); + + System.out.println(game.toString()); }