From a9acbda34320e1490b204b71b57e4e69f76a7edf Mon Sep 17 00:00:00 2001 From: punchready Date: Tue, 10 Aug 2021 12:59:47 +0200 Subject: [PATCH] fix: add more checks, fix power stone check and usestone not having a target entity field --- .../gamelibrary/gamelogic/GameLogic.java | 58 +++++++++++++++++-- 1 file changed, 54 insertions(+), 4 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 3f63f26..d168e4b 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogic.java @@ -136,9 +136,13 @@ public class GameLogic { } } case MindStone -> { + verifyCoordinates(state, data.targetField); + requireLineOfSight(state, data.originField, data.targetField); } case RealityStone -> { + verifyCoordinates(state, data.targetField); + boolean rock = false; boolean empty = true; for(Entity entity: state.entities.findByPosition(data.targetField)) { @@ -154,16 +158,28 @@ public class GameLogic { } } case PowerStone -> { - Character target = getCharacter(state, data.targetField, data.targetEntity); + verifyCoordinates(state, data.targetField); - requireAlive(target); - requireOppositeTeam(origin, target); + Entity target = getAttackableWithoutID(state, data.targetField); + + if(target instanceof Character) { + Character targetCharacter = (Character)target; + + requireOppositeTeam(origin, targetCharacter); + requireAlive(targetCharacter); + } + + if(data.originField.distanceChebyshev(data.targetField) != 1) { + throw new InvalidRequestException("Invalid melee target distance"); + } } case TimeStone -> { // "👍 i approve" - the server } case SoulStone -> { - Character target = getCharacter(state, data.targetField, data.targetEntity); + verifyCoordinates(state, data.targetField); + + Character target = getCharacterWithoutID(state, data.targetField); if(data.originEntity.equals(data.targetEntity)) { throw new InvalidRequestException("Invalid soul stone target (same as origin)"); @@ -211,6 +227,21 @@ public class GameLogic { return entity; } + /** + * Retrieves an attack-able Entity ({@link Character} or {@link Rock}) for a {@link Request}. + * @param state The game state to use + * @param position The requested position + * @return The found entity + * @throws InvalidRequestException if the entity is invalid or not found + */ + private static Entity getAttackableWithoutID(GameState state, IntVector2 position) throws InvalidRequestException { + ArrayList entities = state.entities.findByPosition(position); + if(entities.isEmpty() || entities.get(0).id.type == EntityType.NPC) { + throw new InvalidRequestException("Invalid target character or rock"); + } + return entities.get(0); + } + /** * Retrieves a {@link Character} for a {@link Request}. * @param state The game state to use @@ -231,6 +262,25 @@ public class GameLogic { } } + /** + * Retrieves a {@link Character} for a {@link Request}. + * @param state The game state to use + * @param position The requested position + * @return The found character + * @throws InvalidRequestException if the character is invalid or not found + */ + private static Character getCharacterWithoutID(GameState state, IntVector2 position) throws InvalidRequestException { + ArrayList entities = state.entities.findByPosition(position); + if(entities.isEmpty() || !(entities.get(0) instanceof Character) || entities.get(0).id.type == EntityType.NPC) { + throw new InvalidRequestException("Invalid origin or target character"); + } + try { + return (Character)entities.get(0); + }catch(Exception ignored) { + throw new InvalidRequestException("Invalid origin or target character (cast failed)"); + } + } + /** * Verifies that a {@link Character} has a turn. */