fix: add more checks, fix power stone check and usestone not having a target entity field

This commit is contained in:
punchready 2021-08-10 12:59:47 +02:00
parent 6e249280c1
commit a9acbda343

View File

@ -136,9 +136,13 @@ public class GameLogic {
} }
} }
case MindStone -> { case MindStone -> {
verifyCoordinates(state, data.targetField);
requireLineOfSight(state, data.originField, data.targetField); requireLineOfSight(state, data.originField, data.targetField);
} }
case RealityStone -> { case RealityStone -> {
verifyCoordinates(state, data.targetField);
boolean rock = false; boolean rock = false;
boolean empty = true; boolean empty = true;
for(Entity entity: state.entities.findByPosition(data.targetField)) { for(Entity entity: state.entities.findByPosition(data.targetField)) {
@ -154,16 +158,28 @@ public class GameLogic {
} }
} }
case PowerStone -> { case PowerStone -> {
Character target = getCharacter(state, data.targetField, data.targetEntity); verifyCoordinates(state, data.targetField);
requireAlive(target); Entity target = getAttackableWithoutID(state, data.targetField);
requireOppositeTeam(origin, target);
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 -> { case TimeStone -> {
// "👍 i approve" - the server // "👍 i approve" - the server
} }
case SoulStone -> { 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)) { if(data.originEntity.equals(data.targetEntity)) {
throw new InvalidRequestException("Invalid soul stone target (same as origin)"); throw new InvalidRequestException("Invalid soul stone target (same as origin)");
@ -211,6 +227,21 @@ public class GameLogic {
return entity; 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<Entity> 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}. * Retrieves a {@link Character} for a {@link Request}.
* @param state The game state to use * @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<Entity> 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. * Verifies that a {@link Character} has a turn.
*/ */