feat: implemented config-based character deserialization
This commit is contained in:
parent
fa6207e47d
commit
c8d34fcc97
@ -3,18 +3,19 @@ package uulm.teamname.marvelous.gamelibrary.json.ingame;
|
|||||||
import com.fasterxml.jackson.core.JsonParser;
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.ObjectCodec;
|
import com.fasterxml.jackson.core.ObjectCodec;
|
||||||
import com.fasterxml.jackson.databind.DeserializationContext;
|
import com.fasterxml.jackson.databind.*;
|
||||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
||||||
import uulm.teamname.marvelous.gamelibrary.entities.*;
|
import uulm.teamname.marvelous.gamelibrary.entities.*;
|
||||||
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.json.config.CharacterProperties;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class EntityDeserializer extends JsonDeserializer<Entity> {
|
public class EntityDeserializer extends JsonDeserializer<Entity> {
|
||||||
|
|
||||||
|
|
||||||
private enum DeserializedEntityType {
|
private enum DeserializedEntityType {
|
||||||
Character,
|
Character,
|
||||||
InfinityStone,
|
InfinityStone,
|
||||||
@ -23,6 +24,9 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
var config = (CharacterConfig) ctxt
|
||||||
|
.findInjectableValue("CharacterConfig", null, null);
|
||||||
|
|
||||||
ObjectCodec codec = p.getCodec();
|
ObjectCodec codec = p.getCodec();
|
||||||
JsonNode node = codec.readTree(p);
|
JsonNode node = codec.readTree(p);
|
||||||
|
|
||||||
@ -35,16 +39,21 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
|
|||||||
EntityID id = new EntityID(
|
EntityID id = new EntityID(
|
||||||
EntityType.valueOf("P" + node.get("PID").intValue()),
|
EntityType.valueOf("P" + node.get("PID").intValue()),
|
||||||
node.get("ID").intValue());
|
node.get("ID").intValue());
|
||||||
|
|
||||||
|
String characterName = node.get("name").asText();
|
||||||
|
|
||||||
|
CharacterProperties properties = config.getMap().get(characterName);
|
||||||
|
|
||||||
result = new Character(
|
result = new Character(
|
||||||
id,
|
id,
|
||||||
codec.treeToValue(node.get("position"), IntVector2.class),
|
codec.treeToValue(node.get("position"), IntVector2.class),
|
||||||
node.get("name").asText(),
|
characterName,
|
||||||
node.get("HP").asInt(),
|
node.get("HP").asInt(),
|
||||||
node.get("MP").asInt(),
|
node.get("MP").asInt(),
|
||||||
node.get("AP").asInt(),
|
node.get("AP").asInt(),
|
||||||
-1,
|
properties.attackRange,
|
||||||
-1,
|
properties.rangedDamage,
|
||||||
-1
|
properties.meleeDamage
|
||||||
);
|
);
|
||||||
|
|
||||||
for (var i: codec.treeToValue(node.get("stones"), Integer[].class)) {
|
for (var i: codec.treeToValue(node.get("stones"), Integer[].class)) {
|
||||||
|
@ -1,16 +1,22 @@
|
|||||||
package uulm.teamname.marvelous.gamelibrary.json.ingame;
|
package uulm.teamname.marvelous.gamelibrary.json.ingame;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.InjectableValues;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.*;
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
||||||
import uulm.teamname.marvelous.gamelibrary.entities.*;
|
import uulm.teamname.marvelous.gamelibrary.entities.*;
|
||||||
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.json.config.CharacterProperties;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
@ -18,14 +24,32 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||||||
class EntityDeserializerTest {
|
class EntityDeserializerTest {
|
||||||
|
|
||||||
ObjectMapper mapper;
|
ObjectMapper mapper;
|
||||||
|
HashMap<String, CharacterProperties> propertiesMap;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
mapper = new ObjectMapper();
|
mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
propertiesMap = new HashMap<>();
|
||||||
|
|
||||||
|
var config = mock(CharacterConfig.class);
|
||||||
|
when(config.getMap()).thenReturn(propertiesMap);
|
||||||
|
|
||||||
|
mapper.setInjectableValues(new InjectableValues
|
||||||
|
.Std()
|
||||||
|
.addValue("CharacterConfig", config));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void simpleCharacterTest() throws JsonProcessingException {
|
void simpleCharacterTest() throws JsonProcessingException {
|
||||||
|
|
||||||
|
var characterProperties = new CharacterProperties();
|
||||||
|
characterProperties.attackRange = 12;
|
||||||
|
characterProperties.rangedDamage = 13;
|
||||||
|
characterProperties.meleeDamage = 14;
|
||||||
|
|
||||||
|
propertiesMap.put("God", characterProperties);
|
||||||
|
|
||||||
var character = new Character(
|
var character = new Character(
|
||||||
new EntityID(EntityType.P1, 3),
|
new EntityID(EntityType.P1, 3),
|
||||||
new IntVector2(4, 2),
|
new IntVector2(4, 2),
|
||||||
@ -33,9 +57,9 @@ class EntityDeserializerTest {
|
|||||||
255,
|
255,
|
||||||
65535,
|
65535,
|
||||||
4,
|
4,
|
||||||
-1,
|
12,
|
||||||
-1,
|
13,
|
||||||
-1
|
14
|
||||||
);
|
);
|
||||||
|
|
||||||
character.inventory.addStone(StoneType.MindStone);
|
character.inventory.addStone(StoneType.MindStone);
|
||||||
@ -125,6 +149,19 @@ class EntityDeserializerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate a random character
|
// Generate a random character
|
||||||
|
|
||||||
|
int attackRange = Math.abs(randomIntegers.next());
|
||||||
|
int rangedDamage = Math.abs(randomIntegers.next());
|
||||||
|
int meleeDamage = Math.abs(randomIntegers.next());
|
||||||
|
|
||||||
|
var characterProperties = new CharacterProperties();
|
||||||
|
characterProperties.name = characterName.toString();
|
||||||
|
characterProperties.attackRange = attackRange;
|
||||||
|
characterProperties.rangedDamage = rangedDamage;
|
||||||
|
characterProperties.meleeDamage = meleeDamage;
|
||||||
|
|
||||||
|
propertiesMap.put(characterName.toString(), characterProperties);
|
||||||
|
|
||||||
Character character = new Character(
|
Character character = new Character(
|
||||||
new EntityID(
|
new EntityID(
|
||||||
randomIntegers.next() % 2 == 0 ? EntityType.P1 : EntityType.P2,
|
randomIntegers.next() % 2 == 0 ? EntityType.P1 : EntityType.P2,
|
||||||
@ -134,9 +171,9 @@ class EntityDeserializerTest {
|
|||||||
Math.abs(randomIntegers.next()),
|
Math.abs(randomIntegers.next()),
|
||||||
Math.abs(randomIntegers.next()),
|
Math.abs(randomIntegers.next()),
|
||||||
Math.abs(randomIntegers.next()),
|
Math.abs(randomIntegers.next()),
|
||||||
-1,
|
attackRange,
|
||||||
-1,
|
rangedDamage,
|
||||||
-1
|
meleeDamage
|
||||||
);
|
);
|
||||||
|
|
||||||
// give the character 0 to 5 infinity stones randomly
|
// give the character 0 to 5 infinity stones randomly
|
||||||
|
Loading…
Reference in New Issue
Block a user