fix: remove major flaw in state manipulation

This commit is contained in:
punchready 2021-06-04 06:41:59 +02:00
parent 4eb71fa972
commit b20b428705
2 changed files with 66 additions and 23 deletions

View File

@ -187,6 +187,7 @@ public class GameLogic {
}
}
}catch(InvalidRequestException exception) {
System.out.println("Request denied: " + exception.getMessage());
return false;
}catch(Exception ignored) {
return false;
@ -505,6 +506,11 @@ public class GameLogic {
if(!target.isAlive()) {
state.winConditions.increaseValue(opposing, WinCondition.TotalKnockouts, 1);
}
if(state.activeCharacter != null && state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) {
NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter);
target.inventory.transfer(thanos.inventory);
}
}
case ConsumedAPEvent -> {
EntityEvent data = (EntityEvent)event;
@ -520,6 +526,21 @@ public class GameLogic {
((NPC)state.entities.findEntity(data.targetEntity)).mp.decreaseValue(data.amount);
}
}
case RoundSetupEvent -> {
GameEvent data = (GameEvent)event;
state.roundNumber++;
state.turnNumber = 0;
state.turnOrder = new ArrayList<>();
for(EntityID turn: data.characterOrder) {
if(turn.type != EntityType.NPC) {
state.turnOrder.add(turn);
}
}
state.stoneCooldown.update();
}
case TurnEvent -> {
GameEvent data = (GameEvent)event;
@ -529,11 +550,23 @@ public class GameLogic {
target.mp.setValue(target.mp.getMax());
}else if(data.nextCharacter.id == NPCType.Thanos.getID()) {
NPC target = (NPC)state.entities.findEntity(data.nextCharacter);
if(state.roundNumber > state.partyConfig.maxRounds + 1) {
target.mp.setMax(target.mp.getMax() + 1);
}
target.mp.setValue(target.mp.getMax());
}
state.turnNumber++;
state.activeCharacter = data.nextCharacter.clone();
}
case SpawnEntityEvent -> {
state.entities.addEntity(((EntityEvent)event).entity);
EntityEvent data = (EntityEvent)event;
state.entities.addEntity(data.entity);
if(state.activeCharacter != null && state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Goose.getID()) {
state.unvomitedStones.remove(((InfinityStone)data.entity).type);
}
}
case HealedEvent -> {
CharacterEvent data = (CharacterEvent)event;
@ -577,6 +610,9 @@ public class GameLogic {
state.winConditions.updateValue(target.id.type, WinCondition.MaxStones, target.inventory.getSize());
}
case WinEvent -> {
state.won = true;
}
case GamestateEvent -> {
GamestateEvent data = (GamestateEvent)event;
@ -584,7 +620,7 @@ public class GameLogic {
state.entities.addEntities(data.entities);
state.mapSize.set(data.mapSize);
state.turnOrder = ArrayTools.toArrayList(data.turnOrder);
state.activeCharacter = data.activeCharacter;
state.activeCharacter = data.activeCharacter == null ? null : data.activeCharacter.clone();
state.stoneCooldown.import_(data.stoneCooldowns);
state.won = data.winCondition;
}
@ -680,6 +716,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events}
*/
protected static ArrayList<Event> startGame(GameState state, ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
System.out.println("Starting game");
ArrayList<Event> result = new ArrayList<>();
ArrayList<IntVector2> free = new ArrayList<>();
@ -771,6 +809,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events}
*/
private static ArrayList<Event> handleTurnEnd(GameState state) {
System.out.println("Turn ended");
ArrayList<Event> result = new ArrayList<>();
ArrayList<EntityID> alive = new ArrayList<>();
@ -824,10 +864,11 @@ public class GameLogic {
* @return The list of resulting {@link Event Events}
*/
private static ArrayList<Event> handleRoundStart(GameState state) {
System.out.println("Starting round " + (state.roundNumber + 1));
ArrayList<Event> result = new ArrayList<>();
state.roundNumber++;
state.turnNumber = 0;
result.add(new EventBuilder(EventType.RoundSetupEvent)
.withRoundCount(state.roundNumber)
@ -865,13 +906,13 @@ public class GameLogic {
}
}
state.stoneCooldown.update();
turns.addAll(state.turnOrder);
// RoundSetupEvent has to be sent first, but the contents of it are determined later...
((GameEvent)result.get(0)).characterOrder = turns.toArray(new EntityID[0]);
System.out.println(turns);
return result;
}
@ -881,13 +922,14 @@ public class GameLogic {
* @return The list of resulting {@link Event Events}
*/
private static ArrayList<Event> handleGoose(GameState state) {
System.out.println("Handling goose");
ArrayList<Event> result = new ArrayList<>();
ArrayList<StoneType> inventory = new ArrayList<>(state.unvomitedStones);
int picked = rand.nextInt(inventory.size());
StoneType stone = inventory.get(picked);
inventory.remove(picked);
state.unvomitedStones.remove(stone);
ArrayList<IntVector2> free = new ArrayList<>();
for(int x = 0; x < state.mapSize.getX(); x++) {
@ -925,6 +967,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events}
*/
private static ArrayList<Event> handleStan(GameState state, HashSet<EntityID> revived) {
System.out.println("Handling stan");
ArrayList<Event> result = new ArrayList<>();
ArrayList<Character> characters = new ArrayList<>();
@ -989,6 +1033,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events}
*/
private static ArrayList<Event> spawnThanos(GameState state) {
System.out.println("Spawning thanos");
ArrayList<Event> result = new ArrayList<>();
ArrayList<IntVector2> free = getFreeFields(state);
@ -1019,6 +1065,8 @@ public class GameLogic {
* @return The list of resulting {@link Event Events}
*/
private static ArrayList<Event> handleThanos(GameState state, NPC thanos) {
System.out.println("Handling thanos");
ArrayList<Event> result = new ArrayList<>();
result.add(new EventBuilder(EventType.TurnEvent)
@ -1099,7 +1147,6 @@ public class GameLogic {
.withTargetField(current)
.withAmount(((Character)entity).hp.getValue())
.buildEntityEvent());
((Character)entity).inventory.transfer(thanos.inventory);
break;
}
if(entity instanceof InfinityStone) {
@ -1139,24 +1186,16 @@ public class GameLogic {
* @return The list of resulting {@link Event Events}
*/
private static ArrayList<Event> handleTurnStart(GameState state) {
ArrayList<Event> result = new ArrayList<>();
System.out.println("Turn of " + state.activeCharacter + " started");
state.turnNumber++;
ArrayList<Event> result = new ArrayList<>();
if(state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == NPCType.Thanos.getID()) {
NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter);
if(state.roundNumber > state.partyConfig.maxRounds + 1) {
thanos.mp.setMax(thanos.mp.getMax() + 1);//TODO: use event for this...
}
result.addAll(handleThanos(state, thanos));
return result;
}
Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter);
result.add(new EventBuilder(EventType.TurnEvent)
.withTurnCount(state.turnOrder.size())
.withNextCharacter(state.activeCharacter)
@ -1174,8 +1213,6 @@ public class GameLogic {
private static ArrayList<Event> handlePlayerWin(GameState state, EntityType winner) {
ArrayList<Event> result = new ArrayList<>();
state.won = true;
result.add(new EventBuilder(EventType.WinEvent)
.withPlayerWon(winner == EntityType.P1 ? 1 : 2)
.buildGameEvent());

View File

@ -99,7 +99,8 @@ class GameStateManager {
* @return The optionally resulting {@link Event Events}
*/
public ArrayList<Event> checkPostPhase() {
ArrayList<Event> result = GameLogic.checkTurnEnd(state);
GameState snapshot = state.snapshot();
ArrayList<Event> result = GameLogic.checkTurnEnd(snapshot);
applyEvents(result);
return result;
}
@ -111,10 +112,14 @@ class GameStateManager {
* @return The resulting {@link Event Events}
*/
public ArrayList<Event> startGame(ArrayList<Integer> selectedCharacters1, ArrayList<Integer> selectedCharacters2) {
ArrayList<Event> result = GameLogic.startGame(state, selectedCharacters1, selectedCharacters2);
GameState snapshot = state.snapshot();
ArrayList<Event> result = GameLogic.startGame(snapshot, selectedCharacters1, selectedCharacters2);
applyEvents(result);
ArrayList<Event> result2 = GameLogic.startRound(state);
snapshot = state.snapshot();
ArrayList<Event> result2 = GameLogic.startRound(snapshot);
applyEvents(result2);
result.addAll(result2);
@ -127,7 +132,8 @@ class GameStateManager {
* @return The resulting {@link Event Events}
*/
public ArrayList<Event> endTurn() {
ArrayList<Event> result = GameLogic.endTurn(state);
GameState snapshot = state.snapshot();
ArrayList<Event> result = GameLogic.endTurn(snapshot);
applyEvents(result);
return result;
}