From e90b8f4562fb2e99933f3282d2896330d92ba5c2 Mon Sep 17 00:00:00 2001 From: punchready Date: Fri, 4 Jun 2021 05:14:00 +0200 Subject: [PATCH] fix: resolve various casting and equality check mistakes --- .../gamelibrary/gamelogic/GameLogic.java | 46 +++++++++++++------ .../marvelous/gamelibrary/json/JSON.java | 1 - 2 files changed, 32 insertions(+), 15 deletions(-) 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 2eb9ac9..705063e 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -137,7 +137,7 @@ public class GameLogic { } } case RealityStone -> { - if(data.originEntity == data.targetEntity) { // => place stone + if(data.originEntity.equals(data.targetEntity)) { // => place stone if(state.entities.findByPosition(data.targetField).size() != 0) { throw new InvalidRequestException(); } @@ -170,7 +170,7 @@ public class GameLogic { case SoulStone -> { Character target = getCharacter(state, data.targetField, data.targetEntity); - if(data.originEntity == data.targetEntity) { + if(data.originEntity.equals(data.targetEntity)) { throw new InvalidRequestException(); } @@ -203,7 +203,7 @@ public class GameLogic { */ private static Character getCharacter(GameState state, IntVector2 position, EntityID entityID) throws InvalidRequestException { Entity entity = state.entities.findEntity(entityID); - if(entity == null || entity.getPosition() != position || !(entity instanceof Character) || entity.id.type == EntityType.NPC) { + if(entity == null || !entity.getPosition().equals(position) || !(entity instanceof Character) || entity.id.type == EntityType.NPC) { throw new InvalidRequestException(); } try { @@ -217,7 +217,7 @@ public 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) { + if(!entity.id.equals(state.activeCharacter)) { throw new InvalidRequestException(); } } @@ -505,14 +505,18 @@ public class GameLogic { } } case ConsumedAPEvent -> { - CharacterEvent data = (CharacterEvent)event; + EntityEvent data = (EntityEvent)event; ((Character)state.entities.findEntity(data.targetEntity)).ap.decreaseValue(data.amount); } case ConsumedMPEvent -> { - CharacterEvent data = (CharacterEvent)event; + EntityEvent data = (EntityEvent)event; - ((NPC)state.entities.findEntity(data.targetEntity)).mp.decreaseValue(data.amount); + if(data.targetEntity.type != EntityType.NPC) { + ((Character)state.entities.findEntity(data.targetEntity)).mp.decreaseValue(data.amount); + }else { + ((NPC)state.entities.findEntity(data.targetEntity)).mp.decreaseValue(data.amount); + } } case TurnEvent -> { GameEvent data = (GameEvent)event; @@ -537,15 +541,27 @@ public class GameLogic { case MoveEvent -> { CharacterEvent data = (CharacterEvent)event; - NPC target = (NPC)state.entities.findEntity(data.originEntity); - for(Entity entity: state.entities.findByPosition(data.targetField)) { - if(entity instanceof InfinityStone) { - target.inventory.addStone(((InfinityStone)entity).type); + if(data.originEntity.type != EntityType.NPC) { + Character target = (Character)state.entities.findEntity(data.originEntity); + for(Entity entity: state.entities.findByPosition(data.targetField)) { + if(entity instanceof InfinityStone) { + target.inventory.addStone(((InfinityStone)entity).type); - state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize()); + state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize()); + } } + target.setPosition(data.targetField); + }else { + NPC target = (NPC)state.entities.findEntity(data.originEntity); + for(Entity entity: state.entities.findByPosition(data.targetField)) { + if(entity instanceof InfinityStone) { + target.inventory.addStone(((InfinityStone)entity).type); + + state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize()); + } + } + target.setPosition(data.targetField); } - target.setPosition(data.targetField); } case UseInfinityStoneEvent -> { state.stoneCooldown.setCooldown(((CharacterEvent)event).stoneType); @@ -716,7 +732,9 @@ public class GameLogic { * @return The list of resulting {@link Event Events} */ protected static ArrayList startRound(GameState state) { - return handleRoundStart(state); + ArrayList result = handleRoundStart(state); + result.addAll(handleTurnStart(state)); + return result; } /** diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java index eaf5cf7..1fc0889 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/JSON.java @@ -29,7 +29,6 @@ public class JSON { * or an empty {@link Optional} if the deserialization failed */ public Optional parse(String input) { - BasicMessage result = null; try { return Optional.of(mapper.readValue(input, BasicMessage.class)); } catch (JsonProcessingException e) {