feat: implemented NPC serialization properly
This commit is contained in:
parent
fe7687bc21
commit
4802b0113d
@ -2,6 +2,6 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="" vcs="Git" />
|
<mapping directory="" vcs="Git" />
|
||||||
<mapping directory="$PROJECT_DIR$/Gamelib" vcs="Git" />
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -43,8 +43,9 @@ public class EntitySerializer extends StdSerializer<Entity> {
|
|||||||
private void serializeNPC(NPC value, JsonGenerator gen, SerializerProvider provider)
|
private void serializeNPC(NPC value, JsonGenerator gen, SerializerProvider provider)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
gen.writeNumberField("ID", value.id.id);
|
gen.writeNumberField("ID", value.id.id);
|
||||||
// gen.writeNumberField("MP",value.MP); // TODO: Doesn't exist yet?
|
gen.writeNumberField("MP", value.mp.getValue());
|
||||||
// gen.writeArray(value.); // TODO: Put values here
|
gen.writeFieldName("stones");
|
||||||
|
gen.writeArray(inventoryToIntArray(value.inventory), 0, value.inventory.getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void serializeCharacter(Character value, JsonGenerator gen, SerializerProvider provider)
|
private void serializeCharacter(Character value, JsonGenerator gen, SerializerProvider provider)
|
||||||
@ -57,10 +58,7 @@ public class EntitySerializer extends StdSerializer<Entity> {
|
|||||||
|
|
||||||
gen.writeFieldName("stones");
|
gen.writeFieldName("stones");
|
||||||
gen.writeArray(
|
gen.writeArray(
|
||||||
Arrays.stream(value.inventory.getStonesAsArray())
|
inventoryToIntArray(value.inventory),
|
||||||
.mapToInt(StoneType::getID)
|
|
||||||
.sorted()
|
|
||||||
.toArray(),
|
|
||||||
0, value.inventory.getSize());
|
0, value.inventory.getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,4 +72,12 @@ public class EntitySerializer extends StdSerializer<Entity> {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
gen.writeNumberField("ID", value.id.id);
|
gen.writeNumberField("ID", value.id.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns an int[] with the sorted integer representations of the stones in the given Inventory */
|
||||||
|
private int[] inventoryToIntArray(Inventory inventory) {
|
||||||
|
return Arrays.stream(inventory.getStonesAsArray())
|
||||||
|
.mapToInt(StoneType::getID)
|
||||||
|
.sorted()
|
||||||
|
.toArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,12 +12,13 @@ import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeseria
|
|||||||
|
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
class EntitySerializerTest {
|
class EntitySerializerTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void notATestJustSomethingToRun() throws JsonProcessingException {
|
void serializeCharacter() throws JsonProcessingException {
|
||||||
var mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
|
|
||||||
var chara = new Character(
|
var chara = new Character(
|
||||||
@ -35,9 +36,21 @@ class EntitySerializerTest {
|
|||||||
chara.inventory.addStone(StoneType.SpaceStone);
|
chara.inventory.addStone(StoneType.SpaceStone);
|
||||||
chara.inventory.addStone(StoneType.MindStone);
|
chara.inventory.addStone(StoneType.MindStone);
|
||||||
|
|
||||||
var result = mapper.writeValueAsString(chara);
|
var jsonRepresentingCharacter = """
|
||||||
|
{
|
||||||
|
"entityType":"Character",
|
||||||
|
"name":"CoolHeroThing",
|
||||||
|
"PID":1,
|
||||||
|
"ID":1,
|
||||||
|
"HP":200,
|
||||||
|
"MP":300,
|
||||||
|
"AP":8,
|
||||||
|
"stones":[1,2,5],
|
||||||
|
"position":[12,24]
|
||||||
|
}""".replace("\n", "");
|
||||||
|
|
||||||
System.out.println(result);
|
assertThat(mapper.writeValueAsString(chara))
|
||||||
|
.isEqualTo(jsonRepresentingCharacter);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user