From f3744ef225afc71a128fd37453d4d9909eb17e4d Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Mon, 17 May 2021 20:10:13 +0200 Subject: [PATCH] feat: completed event deserializer, completing the deserialization pipeline --- .../json/ingame/EventDeserializer.java | 139 ++++++++++++++---- 1 file changed, 108 insertions(+), 31 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializer.java index 5fc3f2f..33ed465 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializer.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EventDeserializer.java @@ -7,13 +7,18 @@ import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.entities.Entity; import uulm.teamname.marvelous.gamelibrary.entities.EntityID; +import uulm.teamname.marvelous.gamelibrary.entities.StoneType; import uulm.teamname.marvelous.gamelibrary.events.Event; +import uulm.teamname.marvelous.gamelibrary.events.EventBuilder; import uulm.teamname.marvelous.gamelibrary.events.EventType; import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent; import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; public class EventDeserializer extends JsonDeserializer { @@ -26,43 +31,115 @@ public class EventDeserializer extends JsonDeserializer { JsonNode currentNode = null; - Event result = null; + EventBuilder builder; EventType eventType = null; if (!node.has("eventType")) { throw new IOException("Event had wrong format: no EventType found"); } else { eventType = EventType.valueOf(node.get("eventType").asText()); - - switch (eventType) { - case Ack: - case Nack: - case GamestateEvent: - result = new GamestateEvent(); - result.type = eventType; - - if ((currentNode = node.get("activeCharacter")) != null) { - ((GamestateEvent) result).activeCharacter = mapper - .readValue(currentNode.toString(), EntityID.class); - } - - if ((currentNode = node.get("turnOrder")) != null) { - ((GamestateEvent) result).turnOrder = mapper - .readValue(currentNode.toString(), EntityID[].class); - } - - -// currentNode = node.get("entities"); -// ((GameStateEvent) result).entities = currentNode == null -// ? null -// : mapper.readValue(currentNode.asText(), Entity[].class); -// -// currentNode = node.get("turnOrder"); -// ((GameStateEvent) result).turnOrder = currentNode == null -// ? null -// : mapper.readValue(currentNode.asText(), EntityID[].class); - } + builder = new EventBuilder(eventType); } - return result; + +// eventType = EventType.valueOf(node.get("eventType").asText()); +// +// switch (eventType) { +// case Ack: +// case Nack: +// case GamestateEvent: +// result = new GamestateEvent(); +// result.type = eventType; +// +// if ((currentNode = node.get("activeCharacter")) != null) { +// ((GamestateEvent) result).activeCharacter = mapper +// .readValue(currentNode.toString(), EntityID.class); +// } +// +// if ((currentNode = node.get("turnOrder")) != null) { +// ((GamestateEvent) result).turnOrder = mapper +// .readValue(currentNode.toString(), EntityID[].class); +// } +// +// +//// currentNode = node.get("entities"); +//// ((GameStateEvent) result).entities = currentNode == null +//// ? null +//// : mapper.readValue(currentNode.asText(), Entity[].class); +//// +//// currentNode = node.get("turnOrder"); +//// ((GameStateEvent) result).turnOrder = currentNode == null +//// ? null +//// : mapper.readValue(currentNode.asText(), EntityID[].class); +// } + + // necessary because of exact type mismatch + var stoneTypeID = getIfNodeValid(node, "stoneType", EntityID.class); + + builder.withTargetEntity(getIfNodeValid(node, "targetEntity", EntityID.class)) + .withTargetField(getIfNodeValid(node, "targetField", IntVector2.class)) + .withAmount(getIfNodeValid(node, "targetField", Integer.class)) + .withEntity(getIfNodeValid(node, "entity", Entity.class)) + + .withOriginEntity(getIfNodeValid(node, "originEntity", EntityID.class)) + .withOriginField(getIfNodeValid(node, "originField", IntVector2.class)) + .withStoneType(stoneTypeID != null ? StoneType.valueOf(stoneTypeID.id) : null) + + .withRoundCount(getIfNodeValid(node, "roundCount", Integer.class)) + .withTurnCount(getIfNodeValid(node, "turnCount", Integer.class)) + .withCharacterOrder(getIfNodeValid(node, "characterOrder", EntityID[].class)) + .withNextCharacter(getIfNodeValid(node, "nextCharacter", EntityID.class)) + + .withPlayerWon(getIfNodeValid(node, "playerWon", Integer.class)) + + .withMessage(getIfNodeValid(node, "message", String.class)) + .withTimeLeft(getIfNodeValid(node, "timeLeft", Integer.class)) + + .withEntities(getIfNodeValid(node, "entities", Entity[].class)) + .withTurnOrder(getIfNodeValid(node, "turnOrder", EntityID[].class)) + .withMapSize(getIfNodeValid(node, "mapSize", IntVector2.class)) + .withActiveCharacter(getIfNodeValid(node, "activeCharacter", EntityID.class)) + .withStoneCooldowns(getIfNodeValid(node, "stoneCooldowns",Integer[].class)) + .withWinCondition(getIfNodeValid(node, "winCondition",Boolean.class)) + + .withTeamIdentifier(getIfNodeValid(node, "teamIdentifier", String.class)) + .withCustomContent((HashMap) getIfNodeValid(node,"customContent", HashMap.class)); + + + builder.withTargetEntity(getIfNodeValid(node, "targetEntity", EntityID.class)); + builder.withTargetField(getIfNodeValid(node, "targetField", IntVector2.class)); + + switch (eventType) { + case Ack, Nack, GamestateEvent -> { return builder.buildGameStateEvent(); } + case CustomEvent -> { return builder.buildCustomEvent(); } + case DestroyedEntityEvent, + TakenDamageEvent, + ConsumedAPEvent, + ConsumedMPEvent, + SpawnEntityEvent, + HealedEvent -> { return builder.buildEntityEvent(); } + case MeleeAttackEvent, + RangedAttackEvent, + MoveEvent, + UseInfinityStoneEvent, + ExchangeInfinityStoneEvent -> { return builder.buildCharacterEvent(); } + case RoundSetupEvent, + TurnEvent, + WinEvent, + TurnTimeoutEvent, + TimeoutWarningEvent, + TimeoutEvent, + PauseStartEvent, + PauseStopEvent, + DisconnectEvent -> { return builder.buildGameEvent(); } + } + return null; + } + + private T getIfNodeValid (JsonNode rootNode, String key, Class type) throws JsonProcessingException { + JsonNode subNode; + if ((subNode = rootNode.get(key)) != null) { + return mapper.readValue(subNode.toString(), type); + } + return null; } }