feat: implemented EntitySerializer and partial test

This commit is contained in:
Yannik Bretschneider 2021-06-01 00:33:36 +02:00
parent ad9d314b6b
commit bfe7b10169
3 changed files with 123 additions and 0 deletions

View File

@ -1,13 +1,16 @@
package uulm.teamname.marvelous.gamelibrary.entities;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeserializer;
import uulm.teamname.marvelous.gamelibrary.json.ingame.serialize.EntitySerializer;
import java.util.Objects;
/** Represents an abstract entity. */
@JsonDeserialize(using = EntityDeserializer.class)
@JsonSerialize(using = EntitySerializer.class)
public abstract class Entity {
/** Whether or not the entity is currently active in the game */
protected boolean active = true;

View File

@ -0,0 +1,77 @@
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.entities.Character;
import uulm.teamname.marvelous.gamelibrary.json.ingame.SerializedEntityType;
import java.io.IOException;
import java.util.Arrays;
public class EntitySerializer extends StdSerializer<Entity> {
public EntitySerializer() {
this(null);
}
protected EntitySerializer(Class<Entity> t) {
super(t);
}
@Override
public void serialize(Entity value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeObjectField("entityType", SerializedEntityType.valueOf(value.id.type));
if (value instanceof NPC) {
System.out.println("instanceof NPC");
serializeNPC((NPC) value, gen, provider);
} else if (value instanceof Character) {
System.out.println("instanceof Chara");
serializeCharacter((Character) value, gen, provider);
} else if (value instanceof Rock) {
serializeRock((Rock) value, gen, provider);
} else if (value instanceof InfinityStone) {
serializeInfinityStone((InfinityStone) value, gen, provider);
}
gen.writeObjectField("position", value.getPosition());
gen.writeEndObject();
}
private void serializeNPC(NPC value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeNumberField("ID", value.id.id);
// gen.writeNumberField("MP",value.MP); // TODO: Doesn't exist yet?
// gen.writeArray(value.); // TODO: Put values here
}
private void serializeCharacter(Character value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeNumberField("PID", value.id.type == EntityType.P1 ? 1 : 2);
gen.writeNumberField("ID", value.id.id);
gen.writeNumberField("HP", value.hp.getValue());
gen.writeNumberField("MP", value.mp.getValue());
gen.writeNumberField("AP", value.ap.getValue());
gen.writeFieldName("stones");
gen.writeArray(
Arrays.stream(value.inventory.getStonesAsArray())
.mapToInt(StoneType::getID)
.sorted()
.toArray(),
0, value.inventory.getSize());
}
private void serializeRock(Rock value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeNumberField("ID", value.id.id);
gen.writeNumberField("HP", value.getHp());
}
private void serializeInfinityStone(InfinityStone value, JsonGenerator gen, SerializerProvider provider)
throws IOException {
gen.writeNumberField("ID", value.id.id);
}
}

View File

@ -0,0 +1,43 @@
package uulm.teamname.marvelous.gamelibrary.json.ingame.serialize;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import uulm.teamname.marvelous.gamelibrary.entities.EntityID;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import uulm.teamname.marvelous.gamelibrary.entities.StoneType;
import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeserializer;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
class EntitySerializerTest {
@Test
void notATestJustSomethingToRun() throws JsonProcessingException {
var mapper = new ObjectMapper();
var chara = new Character(
new EntityID(EntityType.P2, 3),
new IntVector2(12, 24),
"CoolHeroThing",
200,
300,
400,
500,
600,
700);
chara.inventory.addStone(StoneType.SoulStone);
chara.inventory.addStone(StoneType.SpaceStone);
chara.inventory.addStone(StoneType.MindStone);
var result = mapper.writeValueAsString(chara);
System.out.println(result);
}
}