feat: implemented NPC serialization properly
This commit is contained in:
parent
fe7687bc21
commit
4802b0113d
@ -2,6 +2,6 @@
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$/Gamelib" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -43,8 +43,9 @@ public class EntitySerializer extends StdSerializer<Entity> {
|
||||
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
|
||||
gen.writeNumberField("MP", value.mp.getValue());
|
||||
gen.writeFieldName("stones");
|
||||
gen.writeArray(inventoryToIntArray(value.inventory), 0, value.inventory.getSize());
|
||||
}
|
||||
|
||||
private void serializeCharacter(Character value, JsonGenerator gen, SerializerProvider provider)
|
||||
@ -57,10 +58,7 @@ public class EntitySerializer extends StdSerializer<Entity> {
|
||||
|
||||
gen.writeFieldName("stones");
|
||||
gen.writeArray(
|
||||
Arrays.stream(value.inventory.getStonesAsArray())
|
||||
.mapToInt(StoneType::getID)
|
||||
.sorted()
|
||||
.toArray(),
|
||||
inventoryToIntArray(value.inventory),
|
||||
0, value.inventory.getSize());
|
||||
}
|
||||
|
||||
@ -74,4 +72,12 @@ public class EntitySerializer extends StdSerializer<Entity> {
|
||||
throws IOException {
|
||||
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.junit.jupiter.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
class EntitySerializerTest {
|
||||
|
||||
|
||||
@Test
|
||||
void notATestJustSomethingToRun() throws JsonProcessingException {
|
||||
void serializeCharacter() throws JsonProcessingException {
|
||||
var mapper = new ObjectMapper();
|
||||
|
||||
var chara = new Character(
|
||||
@ -35,9 +36,21 @@ class EntitySerializerTest {
|
||||
chara.inventory.addStone(StoneType.SpaceStone);
|
||||
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