feat: implemented config-based character deserialization

This commit is contained in:
Yannik Bretschneider 2021-05-31 23:23:27 +02:00
parent fa6207e47d
commit c8d34fcc97
2 changed files with 60 additions and 14 deletions

View File

@ -3,18 +3,19 @@ package uulm.teamname.marvelous.gamelibrary.json.ingame;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.*;
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;
public class EntityDeserializer extends JsonDeserializer<Entity> {
private enum DeserializedEntityType {
Character,
InfinityStone,
@ -23,6 +24,9 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
@Override
public Entity deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
var config = (CharacterConfig) ctxt
.findInjectableValue("CharacterConfig", null, null);
ObjectCodec codec = p.getCodec();
JsonNode node = codec.readTree(p);
@ -35,16 +39,21 @@ public class EntityDeserializer extends JsonDeserializer<Entity> {
EntityID id = new EntityID(
EntityType.valueOf("P" + node.get("PID").intValue()),
node.get("ID").intValue());
String characterName = node.get("name").asText();
CharacterProperties properties = config.getMap().get(characterName);
result = new Character(
id,
codec.treeToValue(node.get("position"), IntVector2.class),
node.get("name").asText(),
characterName,
node.get("HP").asInt(),
node.get("MP").asInt(),
node.get("AP").asInt(),
-1,
-1,
-1
properties.attackRange,
properties.rangedDamage,
properties.meleeDamage
);
for (var i: codec.treeToValue(node.get("stones"), Integer[].class)) {

View File

@ -1,16 +1,22 @@
package uulm.teamname.marvelous.gamelibrary.json.ingame;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.junit.jupiter.api.BeforeEach;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.*;
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.Iterator;
import java.util.concurrent.ThreadLocalRandom;
@ -18,14 +24,32 @@ import java.util.concurrent.ThreadLocalRandom;
class EntityDeserializerTest {
ObjectMapper mapper;
HashMap<String, CharacterProperties> propertiesMap;
@BeforeEach
void setUp() {
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
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(
new EntityID(EntityType.P1, 3),
new IntVector2(4, 2),
@ -33,9 +57,9 @@ class EntityDeserializerTest {
255,
65535,
4,
-1,
-1,
-1
12,
13,
14
);
character.inventory.addStone(StoneType.MindStone);
@ -125,6 +149,19 @@ class EntityDeserializerTest {
}
// 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(
new EntityID(
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()),
-1,
-1,
-1
attackRange,
rangedDamage,
meleeDamage
);
// give the character 0 to 5 infinity stones randomly