feat: completed event deserializer, completing the deserialization pipeline

This commit is contained in:
Yannik Bretschneider 2021-05-17 20:10:13 +02:00
parent c5fe7ec7db
commit f3744ef225

View File

@ -7,13 +7,18 @@ import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; 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.Entity;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID; 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.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType; import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent; import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
public class EventDeserializer extends JsonDeserializer<Event> { public class EventDeserializer extends JsonDeserializer<Event> {
@ -26,43 +31,115 @@ public class EventDeserializer extends JsonDeserializer<Event> {
JsonNode currentNode = null; JsonNode currentNode = null;
Event result = null; EventBuilder builder;
EventType eventType = null; EventType eventType = null;
if (!node.has("eventType")) { if (!node.has("eventType")) {
throw new IOException("Event had wrong format: no EventType found"); throw new IOException("Event had wrong format: no EventType found");
} else { } else {
eventType = EventType.valueOf(node.get("eventType").asText()); eventType = EventType.valueOf(node.get("eventType").asText());
builder = new EventBuilder(eventType);
}
// 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<String, Object>) getIfNodeValid(node,"customContent", HashMap.class));
builder.withTargetEntity(getIfNodeValid(node, "targetEntity", EntityID.class));
builder.withTargetField(getIfNodeValid(node, "targetField", IntVector2.class));
switch (eventType) { switch (eventType) {
case Ack: case Ack, Nack, GamestateEvent -> { return builder.buildGameStateEvent(); }
case Nack: case CustomEvent -> { return builder.buildCustomEvent(); }
case GamestateEvent: case DestroyedEntityEvent,
result = new GamestateEvent(); TakenDamageEvent,
result.type = eventType; ConsumedAPEvent,
ConsumedMPEvent,
if ((currentNode = node.get("activeCharacter")) != null) { SpawnEntityEvent,
((GamestateEvent) result).activeCharacter = mapper HealedEvent -> { return builder.buildEntityEvent(); }
.readValue(currentNode.toString(), EntityID.class); case MeleeAttackEvent,
RangedAttackEvent,
MoveEvent,
UseInfinityStoneEvent,
ExchangeInfinityStoneEvent -> { return builder.buildCharacterEvent(); }
case RoundSetupEvent,
TurnEvent,
WinEvent,
TurnTimeoutEvent,
TimeoutWarningEvent,
TimeoutEvent,
PauseStartEvent,
PauseStopEvent,
DisconnectEvent -> { return builder.buildGameEvent(); }
}
return null;
} }
if ((currentNode = node.get("turnOrder")) != null) { private <T> T getIfNodeValid (JsonNode rootNode, String key, Class<T> type) throws JsonProcessingException {
((GamestateEvent) result).turnOrder = mapper JsonNode subNode;
.readValue(currentNode.toString(), EntityID[].class); if ((subNode = rootNode.get(key)) != null) {
return mapper.readValue(subNode.toString(), type);
} }
return null;
// 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);
}
}
return result;
} }
} }