diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java index 1f1ccd5..3bb42b7 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializer.java @@ -10,7 +10,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; 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.JSON; import java.io.IOException; @@ -18,7 +17,7 @@ public class EntityDeserializer extends JsonDeserializer { private enum DeserializedEntityType { Character, - Stone, + InfinityStone, Rock } @@ -49,8 +48,12 @@ public class EntityDeserializer extends JsonDeserializer { -1, -1 ); + + for (var i: mapper.readValue(node.get("stones").toString(), Integer[].class)) { + ((Character) result).inventory.addStone(StoneType.valueOf(i)); + } } - case Stone -> { + case InfinityStone -> { result = new InfinityStone( new EntityID(EntityType.InfinityStones, node.get("ID").asInt()), mapper.readValue(node.get("position").toString(), IntVector2.class), diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializerTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializerTest.java index b40169f..df604c8 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializerTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/json/ingame/EntityDeserializerTest.java @@ -3,17 +3,17 @@ package uulm.teamname.marvelous.gamelibrary.json.ingame; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.BeforeEach; + import static org.assertj.core.api.Assertions.*; 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.entities.Entity; -import uulm.teamname.marvelous.gamelibrary.entities.EntityID; -import uulm.teamname.marvelous.gamelibrary.entities.EntityType; -import static org.mockito.Mockito.*; -import static org.junit.jupiter.api.Assertions.*; +import java.util.HashSet; +import java.util.Iterator; +import java.util.concurrent.ThreadLocalRandom; class EntityDeserializerTest { @@ -38,19 +38,224 @@ class EntityDeserializerTest { -1 ); + character.inventory.addStone(StoneType.MindStone); + character.inventory.addStone(StoneType.SoulStone); + var jsonRepresentingCharacter = """ -{ - "entityType": "Character", - "name": "God", - "PID": 1, - "ID": 3, - "HP": 255, - "MP": 65535, - "AP": 4, - "position": [4, 2] -} -"""; - assertThat(mapper.readValue(jsonRepresentingCharacter, Entity.class)) + { + "entityType": "Character", + "name": "God", + "PID": 1, + "ID": 3, + "HP": 255, + "MP": 65535, + "AP": 4, + "stones": [1, 5], + "position": [4, 2] + } + """; + assertThat((Character) mapper.readValue(jsonRepresentingCharacter, Entity.class)) .isEqualTo(character); } + + + @Test + void simpleInfinityStoneTest() throws JsonProcessingException { + var infinityStone = new InfinityStone( + new EntityID(EntityType.InfinityStones, 2), + new IntVector2(98765, 43210), + StoneType.RealityStone + ); + + var jsonRepresentingInfinityStone = """ + { + "entityType": "InfinityStone", + "ID": 2, + "position": [98765, 43210] + } + """; + + assertThat((InfinityStone) mapper.readValue(jsonRepresentingInfinityStone, Entity.class)) + .isEqualTo(infinityStone); + } + + + @Test + void simpleRockTest() throws JsonProcessingException { + var rock = new Rock( + new EntityID(EntityType.Rocks, 324985), + new IntVector2(12357, 1371113), + 86420 + ); + + var jsonRepresentingRock = """ + { + "entityType": "Rock", + "ID": 324985, + "HP": 86420, + "position": [12357, 1371113] + } + """; + assertThat((Rock) mapper.readValue(jsonRepresentingRock, Rock.class)) + .isEqualTo(rock); + } + + + /** + * Random tests to see if the deserialization actually works + */ + @Test + void randomTests() throws JsonProcessingException { + // Get the randoms + Iterator randomIntegers = ThreadLocalRandom.current().ints().iterator(); + int repetitions = 1000; + + // Initialize as StringBuilder for performance + StringBuilder characterName = new StringBuilder(); + + for (int i = 0; i < repetitions; i++) { + // Get a random character name based on weird strings + int nameLength = Math.abs(randomIntegers.next() % 12); + for (int j = 0; j < (nameLength) + 3; j++) { + // append char between A and Z, and make lowercase 50% of the time + characterName.append((char) ( + (65 + Math.abs(randomIntegers.next() % 26)) + + 32 * Math.abs(randomIntegers.next() % 2) + )); + } + + // Generate a random character + Character character = new Character( + new EntityID( + randomIntegers.next() % 2 == 0 ? EntityType.P1 : EntityType.P2, + Math.abs(randomIntegers.next() % 6)), + new IntVector2(Math.abs(randomIntegers.next()), Math.abs(randomIntegers.next())), + characterName.toString(), + Math.abs(randomIntegers.next()), + Math.abs(randomIntegers.next()), + Math.abs(randomIntegers.next()), + -1, + -1, + -1 + ); + + // give the character 0 to 5 infinity stones randomly + + int characterInfinityStoneCount = Math.abs(randomIntegers.next()) % 6; + HashSet usedStones = new HashSet<>(14); + + for (int j = 0; j < characterInfinityStoneCount; j++) { + int currentStone; + do { + currentStone = Math.abs(randomIntegers.next() % 6); + } while (!usedStones.add(currentStone)); + character.inventory.addStone(StoneType.valueOf(currentStone)); + } + + // create a string representing the character's current inventory state + String characterInventoryStringRepresentation = ""; + for (Iterator it = character.inventory.iterator(); it.hasNext(); ) { + characterInventoryStringRepresentation += it.next().getID() + (it.hasNext() ? ", " : ""); + } + + // create the actual JSON representation of the character + String jsonRepresentingCharacter = """ + { + "entityType": "Character", + "name": "%s", + "PID": %s, + "ID": %d, + "HP": %d, + "MP": %d, + "AP": %d, + "stones": [%s], + "position": [%d, %d] + } + """.formatted( // formatting the string with all required values + character.name, + character.id.type.toString().charAt(1), // Take the second char from P1 and P2 + character.id.id, + character.hp.getValue(), + character.mp.getValue(), + character.ap.getValue(), + characterInventoryStringRepresentation, + character.getPosition().getX(), + character.getPosition().getY() + ); + + // === RANDOM INFINITY STONE === + + int infinityStoneID = Math.abs(randomIntegers.next() % 6); // get the InfinityStoneID here for Enum conversion + + InfinityStone stone = new InfinityStone( + new EntityID( + EntityType.InfinityStones, + infinityStoneID), + new IntVector2(Math.abs(randomIntegers.next()), Math.abs(randomIntegers.next())), + StoneType.valueOf(infinityStoneID) + ); + + // create the JSON representation of the InfinityStone + + String jsonRepresentingStone = """ + { + "entityType": "InfinityStone", + "ID": %d, + "position": [%d, %d] + } + """.formatted( // formatting the string with the required values. Less this time, luckily + stone.id.id, + stone.getPosition().getX(), + stone.getPosition().getY() + ); + + // === RANDOM ROCK === + + Rock rock = new Rock( + new EntityID( + EntityType.Rocks, + Math.abs(randomIntegers.next())), + new IntVector2(Math.abs(randomIntegers.next()), Math.abs(randomIntegers.next())), + Math.abs(randomIntegers.next()) + ); + + // create the JSON representing Rock + + String jsonRepresentingRock = """ + { + "entityType": "Rock", + "ID": %d, + "HP": %d, + "position": [%d, %d] + } + """.formatted(// formatting the string with the required arguments + rock.id.id, + rock.getHp(), + rock.getPosition().getX(), + rock.getPosition().getY() + ); + +// System.out.println(character); +// System.out.println(jsonRepresentingCharacter); +// System.out.println(stone); +// System.out.println(jsonRepresentingStone); +// System.out.println(rock); +// System.out.println(jsonRepresentingRock); + +// // Assertions + assertThat((Character) (mapper.readValue(jsonRepresentingCharacter, Entity.class))) + .isEqualTo(character); + + assertThat((InfinityStone) (mapper.readValue(jsonRepresentingStone, Entity.class))) + .isEqualTo(stone); + + assertThat((Rock) (mapper.readValue(jsonRepresentingRock, Entity.class))) + .isEqualTo(rock); + + + + + characterName.setLength(0); // empty the StringBuilder + } + } } \ No newline at end of file