fix: add missing checks to game logic and fix events not being applied

This commit is contained in:
punchready 2021-06-01 17:59:45 +02:00
parent 9c2c814e98
commit 8e6f61a665
3 changed files with 98 additions and 3 deletions

View File

@ -1,5 +1,7 @@
package uulm.teamname.marvelous.gamelibrary.gamelogic; 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.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventType; import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig; import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig;
@ -37,7 +39,12 @@ public class GameInstance {
public Optional<List<Event>> checkRequestsAndApply(Request... requests) { public Optional<List<Event>> checkRequestsAndApply(Request... requests) {
if(manager.processRequests(requests, true)) { if(manager.processRequests(requests, true)) {
ArrayList<Event> result = manager.apply(); ArrayList<Event> result = manager.apply();
result.addAll(manager.checkPostPhase());
ArrayList<Event> result2 = manager.checkPostPhase();
manager.applyEvents(result2);
result.addAll(result2);
return Optional.of(result); return Optional.of(result);
} }
@ -62,7 +69,11 @@ public class GameInstance {
public ArrayList<Event> startGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) { public ArrayList<Event> startGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
ArrayList<Event> result = manager.initGame(selectedCharacters1, selectedCharacters2); ArrayList<Event> result = manager.initGame(selectedCharacters1, selectedCharacters2);
manager.applyEvents(result); manager.applyEvents(result);
result.addAll(manager.startGame());
ArrayList<Event> result2 = manager.startGame();
manager.applyEvents(result2);
result.addAll(result2); //do not merge this, result needs to be applied before startGame is called
return result; return result;
} }
@ -90,4 +101,44 @@ public class GameInstance {
public void applyEvents(ArrayList<Event> events) { public void applyEvents(ArrayList<Event> events) {
manager.applyEvents(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<Entity> 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();
}
} }

View File

@ -214,6 +214,9 @@ class GameLogic {
Character origin = getCharacter(state, data.originField, data.originEntity); Character origin = getCharacter(state, data.originField, data.originEntity);
Character target = getCharacter(state, data.targetField, data.targetEntity); Character target = getCharacter(state, data.targetField, data.targetEntity);
requireTurn(state, origin);
requireOppositeTeam(origin, target);
requireAlive(origin); requireAlive(origin);
requireAlive(target); requireAlive(target);
requireAP(origin, 1); requireAP(origin, 1);
@ -245,6 +248,8 @@ class GameLogic {
Character origin = getCharacter(state, data.originField, data.originEntity); Character origin = getCharacter(state, data.originField, data.originEntity);
requireTurn(state, origin);
requireAlive(origin); requireAlive(origin);
requireMP(origin, 1); requireMP(origin, 1);
verifyCoordinates(state, data.targetField); verifyCoordinates(state, data.targetField);
@ -265,6 +270,9 @@ class GameLogic {
Character origin = getCharacter(state, data.originField, data.originEntity); Character origin = getCharacter(state, data.originField, data.originEntity);
Character target = getCharacter(state, data.targetField, data.targetEntity); Character target = getCharacter(state, data.targetField, data.targetEntity);
requireTurn(state, origin);
requireSameTeam(origin, target);
requireAlive(origin); requireAlive(origin);
requireAlive(target); requireAlive(target);
requireAP(origin, 1); requireAP(origin, 1);
@ -281,6 +289,8 @@ class GameLogic {
Character origin = getCharacter(state, data.originField, data.originEntity); Character origin = getCharacter(state, data.originField, data.originEntity);
requireTurn(state, origin);
requireAlive(origin); requireAlive(origin);
requireAP(origin, 1); requireAP(origin, 1);
requireInfinityStone(origin, data.stoneType); requireInfinityStone(origin, data.stoneType);
@ -327,6 +337,7 @@ class GameLogic {
Character target = getCharacter(state, data.targetField, data.targetEntity); Character target = getCharacter(state, data.targetField, data.targetEntity);
requireAlive(target); requireAlive(target);
requireOppositeTeam(origin, target);
if(origin.rangedDamage * 2 != data.value) { if(origin.rangedDamage * 2 != data.value) {
throw new InvalidRequestException(); throw new InvalidRequestException();
@ -338,6 +349,10 @@ class GameLogic {
case SoulStone -> { case SoulStone -> {
Character target = getCharacter(state, data.targetField, data.targetEntity); Character target = getCharacter(state, data.targetField, data.targetEntity);
if(data.originEntity == data.targetEntity) {
throw new InvalidRequestException();
}
if(target.hp.getValue() != 0) { if(target.hp.getValue() != 0) {
throw new InvalidRequestException(); 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. * Verifies that a {@link Character} is alive.
*/ */

View File

@ -44,7 +44,7 @@ class GameLogicTest {
scenarioConfig.name = generateName(20); scenarioConfig.name = generateName(20);
scenarioConfig.author = 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 x = 0; x < scenarioConfig.scenario[0].length; x++) {
for(int y = 0; y < scenarioConfig.scenario.length; y++) { for(int y = 0; y < scenarioConfig.scenario.length; y++) {
if(Math.abs(randomIntegers.next() % 100) < 10) { if(Math.abs(randomIntegers.next() % 100) < 10) {
@ -170,6 +170,8 @@ class GameLogicTest {
Event actual = result.get(0); Event actual = result.get(0);
assertEquals(EventType.GamestateEvent, actual.type, "First event should be a GameStateEvent"); assertEquals(EventType.GamestateEvent, actual.type, "First event should be a GameStateEvent");
System.out.println(game.toString());
} }