feat: implemented JSON stuff related to PortalEvents

This commit is contained in:
Yannik Bretschneider 2021-06-24 15:51:18 +02:00
parent 371424e6c3
commit 7334b234d4
5 changed files with 69 additions and 4 deletions

View File

@ -80,6 +80,10 @@ public class EventDeserializer extends JsonDeserializer<Event> {
.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<Event> {
MoveEvent,
UseInfinityStoneEvent,
ExchangeInfinityStoneEvent -> { return builder.buildCharacterEvent(); }
case TeleportedEvent -> { return builder.buildTeleportedEvent(); }
case RoundSetupEvent,
TurnEvent,
WinEvent,

View File

@ -32,6 +32,8 @@ public class EventSerializer extends StdSerializer<Event> {
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<Event> {
}
}
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,

View File

@ -36,7 +36,6 @@ class JSONTest {
CharacterConfig characterConfig;
HashMap<String, CharacterProperties> propertiesMap;
// TODO: add proper CharacterConfig, otherwise Character events won't be deserializable
@BeforeEach
void setUp() {
characterConfig = mock(CharacterConfig.class);

View File

@ -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);
}
}

View File

@ -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);
}
}