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);
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);
}
} }
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<String, Object>) 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> T getIfNodeValid (JsonNode rootNode, String key, Class<T> type) throws JsonProcessingException {
JsonNode subNode;
if ((subNode = rootNode.get(key)) != null) {
return mapper.readValue(subNode.toString(), type);
}
return null;
} }
} }