refactor: create proper game logic test

This commit is contained in:
punchready 2021-06-01 00:50:29 +02:00
parent 787ba7d38e
commit ae9f5902e1
2 changed files with 142 additions and 65 deletions

View File

@ -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<Integer> 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<Event> 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<Integer> selected1 = new ArrayList<>();
for(int i = 0; i < 6; i++) {
selected1.add(i);
}
ArrayList<Integer> 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<Entity> 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> gamestate() {

View File

@ -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");
}
}