From 7334b234d4c21b0537f5060eb73ff997ad4295e9 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Thu, 24 Jun 2021 15:51:18 +0200 Subject: [PATCH] feat: implemented JSON stuff related to PortalEvents --- .../ingame/deserialize/EventDeserializer.java | 5 ++++ .../ingame/serialize/EventSerializer.java | 13 +++++++++ .../marvelous/gamelibrary/json/JSONTest.java | 1 - .../deserialize/EventDeserializerTest.java | 28 +++++++++++++++++-- .../ingame/serialize/EventSerializerTest.java | 26 +++++++++++++++++ 5 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/EventDeserializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/EventDeserializer.java index d80b9ec..472e431 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/EventDeserializer.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/EventDeserializer.java @@ -80,6 +80,10 @@ public class EventDeserializer extends JsonDeserializer { .withOriginField(unwrap(node.get("originField"), IntVector2.class, codec)) .withStoneType(stoneTypeID != null ? StoneType.valueOf(stoneTypeID.id) : null) + .withTeleportedEntity(unwrap(node.get("teleportedEntity"), EntityID.class, codec)) + .withOriginPortal(unwrap(node.get("originPortal"), EntityID.class, codec)) + .withTargetPortal(unwrap(node.get("targetPortal"), EntityID.class, codec)) + .withRoundCount(unwrap(node.get("roundCount"), Integer.class, codec)) .withTurnCount(unwrap(node.get("turnCount"), Integer.class, codec)) .withCharacterOrder(unwrap(node.get("characterOrder"), EntityID[].class, codec)) @@ -118,6 +122,7 @@ public class EventDeserializer extends JsonDeserializer { MoveEvent, UseInfinityStoneEvent, ExchangeInfinityStoneEvent -> { return builder.buildCharacterEvent(); } + case TeleportedEvent -> { return builder.buildTeleportedEvent(); } case RoundSetupEvent, TurnEvent, WinEvent, diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializer.java index ae74246..cbe076b 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializer.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializer.java @@ -32,6 +32,8 @@ public class EventSerializer extends StdSerializer { serializeCharacterEvent((CharacterEvent) value, gen, provider); } else if (value instanceof GameEvent) { serializeGameEvent((GameEvent) value, gen, provider); + } else if (value instanceof TeleportedEvent) { + serializeTeleportedEvent((TeleportedEvent) value, gen, provider); } else if (value instanceof CustomEvent) { serializeCustomEvent((CustomEvent) value, gen, provider); } @@ -144,6 +146,17 @@ public class EventSerializer extends StdSerializer { } } + private void serializeTeleportedEvent( + TeleportedEvent value, + JsonGenerator gen, + SerializerProvider provider) throws IOException { + gen.writeObjectField("teleportedEntity", value.teleportedEntity); + gen.writeObjectField("originField", value.originField); + gen.writeObjectField("targetField", value.targetField); + gen.writeObjectField("originPortal", value.originPortal); + gen.writeObjectField("targetPortal", value.targetPortal); + } + private void serializeCustomEvent( CustomEvent value, JsonGenerator gen, diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java index 106ef3f..a7c0b8e 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/JSONTest.java @@ -36,7 +36,6 @@ class JSONTest { CharacterConfig characterConfig; HashMap propertiesMap; - // TODO: add proper CharacterConfig, otherwise Character events won't be deserializable @BeforeEach void setUp() { characterConfig = mock(CharacterConfig.class); diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/EventDeserializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/EventDeserializerTest.java index a3e2b97..f1e7865 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/EventDeserializerTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/deserialize/EventDeserializerTest.java @@ -343,8 +343,30 @@ class EventDeserializerTest { } } - // TODO: Write tests for the other event types - // NOTE: it is not required to check all, only the categories (like GamestateEvent or CharacterEvent) - // because of unchecked event building in the deserializer + @Test + void teleportedEventDeserializationTest() throws JsonProcessingException { + + var jsonRepresentingTeleportedEvent = """ + { + "eventType":"TeleportedEvent", + "teleportedEntity":{"entityID":"P1", "ID":3}, + "originField":[6, 2], + "targetField":[11, 14], + "originPortal":{"entityID":"Portals", "ID":0}, + "targetPortal":{"entityID":"Portals", "ID":2} + } + """; + + var teleportedEvent = new EventBuilder(EventType.TeleportedEvent) + .withTeleportedEntity(new EntityID(EntityType.P1, 3)) + .withOriginField(new IntVector2(6, 2)) + .withTargetField(new IntVector2(11, 14)) + .withOriginPortal(new EntityID(EntityType.Portals, 0)) + .withTargetPortal(new EntityID(EntityType.Portals, 2)) + .buildTeleportedEvent(); + + assertThat((TeleportedEvent) mapper.readValue(jsonRepresentingTeleportedEvent, Event.class)) + .isEqualTo(teleportedEvent); + } } diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializerTest.java index 805d081..e60bf8c 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializerTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/serialize/EventSerializerTest.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; //import net.jqwik.api.lifecycle.BeforeProperty; import net.jqwik.api.*; import net.jqwik.api.lifecycle.BeforeProperty; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.entities.*; @@ -377,6 +378,31 @@ public class EventSerializerTest { }); } + @Example + void teleportedEventSerializationTest() throws JsonProcessingException { + + var jsonRepresentingTeleportedEvent = """ + { + "eventType":"TeleportedEvent", + "teleportedEntity":{"entityID":"P1","ID":3}, + "originField":[6,2], + "targetField":[11,14], + "originPortal":{"entityID":"Portals","ID":0}, + "targetPortal":{"entityID":"Portals","ID":2} + } + """.replace("\n", ""); + + var teleportedEvent = new EventBuilder(EventType.TeleportedEvent) + .withTeleportedEntity(new EntityID(EntityType.P1, 3)) + .withOriginField(new IntVector2(6, 2)) + .withTargetField(new IntVector2(11, 14)) + .withOriginPortal(new EntityID(EntityType.Portals, 0)) + .withTargetPortal(new EntityID(EntityType.Portals, 2)) + .buildTeleportedEvent(); + + assertThat(mapper.writeValueAsString(teleportedEvent)) + .isEqualTo(jsonRepresentingTeleportedEvent); + } }