From ae9f5902e1f7d047f83004b5ff9c0de48e2cfc80 Mon Sep 17 00:00:00 2001 From: punchready Date: Tue, 1 Jun 2021 00:50:29 +0200 Subject: [PATCH] refactor: create proper game logic test --- .../gamelibrary/gamelogic/GameLogicTest.java | 165 +++++++++++++++--- .../gamelibrary/gamelogic/GameStateTest.java | 42 ----- 2 files changed, 142 insertions(+), 65 deletions(-) delete mode 100644 src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateTest.java diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java index 46e3473..215675a 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameLogicTest.java @@ -1,46 +1,165 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic; import net.jqwik.api.*; +import org.junit.jupiter.api.BeforeAll; 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.events.Event; -import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig; -import uulm.teamname.marvelous.gamelibrary.json.config.PartyConfig; -import uulm.teamname.marvelous.gamelibrary.json.config.ScenarioConfig; +import uulm.teamname.marvelous.gamelibrary.json.config.*; import uulm.teamname.marvelous.gamelibrary.requests.*; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +import static org.junit.jupiter.api.Assertions.*; class GameLogicTest { + private static final Iterator randomIntegers = ThreadLocalRandom.current().ints().iterator(); + + private static PartyConfig partyConfig; + private static CharacterConfig characterConfig; + private static ScenarioConfig scenarioConfig; + + @BeforeAll + static void setUp() { + partyConfig = new PartyConfig(); + partyConfig.maxRounds = 100; + partyConfig.mindStoneCD = 2; + partyConfig.powerStoneCD = 3; + partyConfig.realityStoneCD = 4; + partyConfig.soulStoneCD = 5; + partyConfig.spaceStoneCD = 6; + partyConfig.timeStoneCD = 7; + partyConfig.mindStoneDMG = 3; + + characterConfig = new CharacterConfig(); + characterConfig.characters = new CharacterProperties[] { + generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), + generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter() + }; + + scenarioConfig = new ScenarioConfig(); + scenarioConfig.name = generateName(20); + scenarioConfig.author = generateName(20); + scenarioConfig.scenario = new FieldType[30][30]; + for(int x = 0; x < scenarioConfig.scenario[0].length; x++) { + for(int y = 0; y < scenarioConfig.scenario.length; y++) { + if(Math.abs(randomIntegers.next() % 100) < 10) { + scenarioConfig.scenario[y][x] = FieldType.ROCK; + }else { + scenarioConfig.scenario[y][x] = FieldType.GRASS; + } + } + } + } + + private static String generateName(int length) { + StringBuilder name = new StringBuilder(); + for (int j = 0; j < length; j++) { + name.append((char) ( + 65 + Math.abs(randomIntegers.next() % 26) + 32 * Math.abs(randomIntegers.next() % 2) + )); + } + return name.toString(); + } + + private static CharacterProperties generateCharacter() { + CharacterProperties props = new CharacterProperties(); + + props.name = generateName(10); + + props.HP = Math.abs(randomIntegers.next() % 20); + props.MP = Math.abs(randomIntegers.next() % 7); + props.AP = Math.abs(randomIntegers.next() % 7); + props.meleeDamage = Math.abs(randomIntegers.next() % 7); + props.rangedDamage = Math.abs(randomIntegers.next() % 7); + props.attackRange = Math.abs(randomIntegers.next() % 7); + + return props; + } + @Test - void executeRequest() { - GameState state = new GameState(new PartyConfig(), new CharacterConfig(), new ScenarioConfig()); - //build a nice state (add entities and stuff) - ArrayList result = GameLogic.executeRequest(state, new RequestBuilder(RequestType.MeleeAttackRequest) - //build a nice request to check - //.withOriginEntity() - .buildCharacterRequest()); - //check if result contains the right events + void testGeneration() { + GameInstance game = new GameInstance(partyConfig, characterConfig, scenarioConfig); + + ArrayList selected1 = new ArrayList<>(); + for(int i = 0; i < 6; i++) { + selected1.add(i); + } + ArrayList selected2 = new ArrayList<>(); + for(int i = 6; i < 12; i++) { + selected2.add(i); + } + + game.startGame(selected1, selected2); + + int n = 0; + + for(int x = 0; x < scenarioConfig.scenario[0].length; x++) { + for(int y = 0; y < scenarioConfig.scenario.length; y++) { + if(scenarioConfig.scenario[y][x] == FieldType.ROCK) { + ArrayList found = game.state.getEntities().findByPosition(new IntVector2(x, y)); + assertEquals(1, found.size(), "Rock Entity "+n+" should exist"); + assertEquals(EntityType.Rocks, found.get(0).id.type, "Rock Entity "+n+" should be of type Rock"); + assertEquals(100, ((Rock)found.get(0)).getHp(), "Rock Entity "+n+" should have 100 HP"); + n++; + } + } + } + + n = 0; + + for(Integer i: selected1) { + Entity found = game.state.getEntities().findEntity(new EntityID(EntityType.P1, n)); + assertNotEquals(null, found, "Character Entity "+n+" for Player 1 should exist"); + Character c = (Character)found; + assertEquals(characterConfig.characters[i].HP, c.hp.getMax(), "Character "+n+" should have the right amount of HP"); + assertEquals(characterConfig.characters[i].MP, c.mp.getMax(), "Character "+n+" should have the right amount of MP"); + assertEquals(characterConfig.characters[i].AP, c.ap.getMax(), "Character "+n+" should have the right amount of AP"); + assertEquals(characterConfig.characters[i].meleeDamage, c.meleeDamage, "Character "+n+" should have the right amount of meleeDamage"); + assertEquals(characterConfig.characters[i].rangedDamage, c.rangedDamage, "Character "+n+" should have the right amount of rangedDamage"); + assertEquals(characterConfig.characters[i].attackRange, c.attackRange, "Character "+n+" should have the right amount of attackRange"); + n++; + } + + n = 0; + + for(Integer i: selected2) { + Entity found = game.state.getEntities().findEntity(new EntityID(EntityType.P2, n)); + assertNotEquals(null, found, "Character Entity "+n+" for Player 2 should exist"); + Character c = (Character)found; + assertEquals(characterConfig.characters[i].HP, c.hp.getMax(), "Character "+n+" should have the right amount of HP"); + assertEquals(characterConfig.characters[i].MP, c.mp.getMax(), "Character "+n+" should have the right amount of MP"); + assertEquals(characterConfig.characters[i].AP, c.ap.getMax(), "Character "+n+" should have the right amount of AP"); + assertEquals(characterConfig.characters[i].meleeDamage, c.meleeDamage, "Character "+n+" should have the right amount of meleeDamage"); + assertEquals(characterConfig.characters[i].rangedDamage, c.rangedDamage, "Character "+n+" should have the right amount of rangedDamage"); + assertEquals(characterConfig.characters[i].attackRange, c.attackRange, "Character "+n+" should have the right amount of attackRange"); + n++; + } } @Test - void buildGameStateEvent() { + void testSnapshot() { + GameState state = new GameState(partyConfig, characterConfig, scenarioConfig); + + state.entities.addEntity(new Rock(new EntityID(EntityType.Rocks, 0), new IntVector2(0, 0), 100)); + + GameState snapshot = state.snapshot(); + + snapshot.turnNumber = 10; + assertEquals(0, state.turnNumber, "Original's turn number should remain unchanged"); + + assertTrue(snapshot.entities.getEntities().hasNext(), "Snapshot should contain cloned entities"); + + ((Rock)snapshot.entities.getEntities().next()).decreaseHp(5); + assertEquals(100, ((Rock)state.entities.getEntities().next()).getHp(), "Original's rock entity hp should remain unchanged"); } - @Test - void checkRequest() { - } - - @Test - void applyEvent() { - } - - @Test - void checkWinConditions() { - } // @Provide("gamestate") // Arbitrary gamestate() { diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateTest.java deleted file mode 100644 index d860938..0000000 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/gamelogic/GameStateTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package uulm.teamname.marvelous.gamelibrary.gamelogic; - -import org.junit.jupiter.api.Test; -import uulm.teamname.marvelous.gamelibrary.IntVector2; -import uulm.teamname.marvelous.gamelibrary.entities.EntityID; -import uulm.teamname.marvelous.gamelibrary.entities.EntityType; -import uulm.teamname.marvelous.gamelibrary.entities.Rock; -import uulm.teamname.marvelous.gamelibrary.events.Event; -import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig; -import uulm.teamname.marvelous.gamelibrary.json.config.PartyConfig; -import uulm.teamname.marvelous.gamelibrary.json.config.ScenarioConfig; -import uulm.teamname.marvelous.gamelibrary.requests.CharacterRequest; - -import java.util.ArrayList; - -import static org.junit.jupiter.api.Assertions.*; - -class GameStateTest { - @Test - void testSnapshot() { - //TODO: test all properties in GameStateTest.testSnapshot - - //base state - GameState state = new GameState(new PartyConfig(), new CharacterConfig(), new ScenarioConfig()); - - //set up game - state.entities.addEntity(new Rock(new EntityID(EntityType.Rocks, 0), new IntVector2(0, 0), 100)); - - //snapshot state - GameState snapshot = state.snapshot(); - - //modify snapshot and test for disconnection - - snapshot.turnNumber = 10; - assertEquals(0, state.turnNumber, "Original's turn number should remain unchanged"); - - assertTrue(snapshot.entities.getEntities().hasNext(), "Snapshot should contain cloned entities"); - - ((Rock)snapshot.entities.getEntities().next()).decreaseHp(5); - assertEquals(100, ((Rock)state.entities.getEntities().next()).getHp(), "Original's rock entity hp should remain unchanged"); - } -}