feat: completed EventSerializer
This commit is contained in:
parent
f184c6b3b9
commit
ea6a178c82
@ -0,0 +1,156 @@
|
|||||||
|
package uulm.teamname.marvelous.gamelibrary.json.ingame.serialize;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
|
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||||
|
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.entities.*;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.events.*;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class EventSerializer extends StdSerializer<Event> {
|
||||||
|
|
||||||
|
public EventSerializer() {
|
||||||
|
this(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected EventSerializer(Class<Event> t) {
|
||||||
|
super(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void serialize(Event value, JsonGenerator gen, SerializerProvider provider) throws IOException {
|
||||||
|
gen.writeStartObject();
|
||||||
|
gen.writeObjectField("eventType", value.type);
|
||||||
|
|
||||||
|
if (value instanceof GamestateEvent) {
|
||||||
|
serializeGamestateEvent((GamestateEvent) value, gen, provider);
|
||||||
|
} else if (value instanceof EntityEvent) {
|
||||||
|
serializeEntityEvent((EntityEvent) value, gen, provider);
|
||||||
|
} else if (value instanceof CharacterEvent) {
|
||||||
|
serializeCharacterEvent((CharacterEvent) value, gen, provider);
|
||||||
|
} else if (value instanceof GameEvent) {
|
||||||
|
serializeGameEvent((GameEvent) value, gen, provider);
|
||||||
|
} else if (value instanceof CustomEvent) {
|
||||||
|
serializeCustomEvent((CustomEvent) value, gen, provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
gen.writeEndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void serializeGamestateEvent(
|
||||||
|
GamestateEvent value,
|
||||||
|
JsonGenerator gen,
|
||||||
|
SerializerProvider provider)
|
||||||
|
throws IOException {
|
||||||
|
|
||||||
|
// Ack and Nack don't need anything more, and are technically gamestateEvents, so this if is here
|
||||||
|
if (value.type.equals(EventType.GamestateEvent)) {
|
||||||
|
gen.writeArrayFieldStart("entities");
|
||||||
|
for (Entity entity : value.entities) {
|
||||||
|
gen.writeObject(entity);
|
||||||
|
}
|
||||||
|
gen.writeEndArray();
|
||||||
|
|
||||||
|
gen.writeObjectField("mapSize", value.mapSize);
|
||||||
|
|
||||||
|
gen.writeArrayFieldStart("turnOrder");
|
||||||
|
for (EntityID id : value.turnOrder) {
|
||||||
|
gen.writeObject(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
gen.writeObjectField("activeCharacter", value.activeCharacter);
|
||||||
|
|
||||||
|
gen.writeFieldName("stoneCooldowns");
|
||||||
|
gen.writeArray(
|
||||||
|
Arrays.stream(value.stoneCooldowns).mapToInt(x -> x).toArray(),
|
||||||
|
0,
|
||||||
|
value.stoneCooldowns.length);
|
||||||
|
|
||||||
|
gen.writeBooleanField("winCondition", value.winCondition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void serializeEntityEvent(
|
||||||
|
EntityEvent value,
|
||||||
|
JsonGenerator gen,
|
||||||
|
SerializerProvider provider)
|
||||||
|
throws IOException {
|
||||||
|
if (value.type == EventType.SpawnEntityEvent) {
|
||||||
|
gen.writeObjectField("entity", value.entity);
|
||||||
|
} else {
|
||||||
|
gen.writeObjectField("targetEntity", value.targetEntity);
|
||||||
|
gen.writeObjectField("targetField", value.targetField);
|
||||||
|
|
||||||
|
if (value.type != EventType.DestroyedEntityEvent) {
|
||||||
|
// a EntityDestroyedEvent doesn't have an Amount, but everything else does
|
||||||
|
gen.writeNumberField("amount", value.amount);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void serializeCharacterEvent(
|
||||||
|
CharacterEvent value,
|
||||||
|
JsonGenerator gen,
|
||||||
|
SerializerProvider provider)
|
||||||
|
throws IOException {
|
||||||
|
gen.writeObjectField("originEntity", value.originEntity);
|
||||||
|
gen.writeObjectField("targetEntity", value.targetEntity);
|
||||||
|
gen.writeObjectField("originField", value.originField);
|
||||||
|
gen.writeObjectField("targetField", value.targetField);
|
||||||
|
|
||||||
|
if (value.type == EventType.UseInfinityStoneEvent || value.type == EventType.ExchangeInfinityStoneEvent) {
|
||||||
|
gen.writeObject(new EntityID(EntityType.InfinityStones, value.stoneType.getID()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void serializeGameEvent(
|
||||||
|
GameEvent value,
|
||||||
|
JsonGenerator gen,
|
||||||
|
SerializerProvider provider)
|
||||||
|
throws IOException {
|
||||||
|
switch (value.type) {
|
||||||
|
case RoundSetupEvent -> {
|
||||||
|
gen.writeNumberField("roundCount", value.roundCount);
|
||||||
|
gen.writeFieldName("characterOrder");
|
||||||
|
for (EntityID id : value.characterOrder) {
|
||||||
|
gen.writeObject(id);
|
||||||
|
}
|
||||||
|
gen.writeEndArray();
|
||||||
|
}
|
||||||
|
case TurnEvent -> {
|
||||||
|
gen.writeNumberField("turnCount", value.turnCount);
|
||||||
|
gen.writeObjectField("nextCharacter", value.nextCharacter);
|
||||||
|
}
|
||||||
|
case WinEvent -> {
|
||||||
|
gen.writeNumberField("playerWon", value.playerWon);
|
||||||
|
}
|
||||||
|
case TimeoutWarningEvent -> {
|
||||||
|
gen.writeStringField("message", value.message);
|
||||||
|
gen.writeNumberField("timeLeft", value.timeLeft);
|
||||||
|
}
|
||||||
|
case TimeoutEvent -> {
|
||||||
|
gen.writeStringField("message", value.message);
|
||||||
|
}
|
||||||
|
default -> { /* do nothing, as all other events don't need any additional keys/values */}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void serializeCustomEvent(
|
||||||
|
CustomEvent value,
|
||||||
|
JsonGenerator gen,
|
||||||
|
SerializerProvider provider)
|
||||||
|
throws IOException {
|
||||||
|
gen.writeStringField("teamIdentifier", value.teamIdentifier);
|
||||||
|
gen.writeObjectField("customContent", value.customContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private int[] inventoryToIntArray(Inventory inventory) {
|
||||||
|
return Arrays.stream(inventory.getStonesAsArray())
|
||||||
|
.mapToInt(StoneType::getID)
|
||||||
|
.sorted()
|
||||||
|
.toArray();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user