2021-05-01 22:05:55 +00:00
|
|
|
package uulm.teamname.marvelous.gamelibrary.events;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
|
import org.junit.jupiter.api.Test;
|
2021-05-02 02:14:28 +00:00
|
|
|
|
2021-05-01 22:05:55 +00:00
|
|
|
import static org.assertj.core.api.Assertions.*;
|
2021-05-02 12:28:51 +00:00
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
2021-05-02 02:14:28 +00:00
|
|
|
|
2021-05-01 22:05:55 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.IntVector2;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.*;
|
2021-05-02 02:14:28 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.Character;
|
2021-05-01 22:05:55 +00:00
|
|
|
|
2021-05-02 02:14:28 +00:00
|
|
|
import java.util.ArrayList;
|
2021-05-01 22:05:55 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
class EventBuilderTest {
|
2021-05-02 02:14:28 +00:00
|
|
|
EntityID[] turns;
|
|
|
|
EntityID turn;
|
|
|
|
Entity[] entities;
|
|
|
|
Entity entity;
|
|
|
|
EventBuilder filled;
|
2021-05-01 22:05:55 +00:00
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
void setUp() {
|
2021-05-02 02:14:28 +00:00
|
|
|
turns = new EntityID[]{
|
|
|
|
new EntityID(EntityType.P1, 0),
|
|
|
|
new EntityID(EntityType.P1, 1),
|
|
|
|
new EntityID(EntityType.P1, 2),
|
|
|
|
new EntityID(EntityType.P1, 3),
|
|
|
|
new EntityID(EntityType.P1, 4),
|
|
|
|
new EntityID(EntityType.P1, 5),
|
|
|
|
new EntityID(EntityType.P2, 5),
|
|
|
|
new EntityID(EntityType.P2, 4),
|
|
|
|
new EntityID(EntityType.P2, 3),
|
|
|
|
new EntityID(EntityType.P2, 2),
|
|
|
|
new EntityID(EntityType.P2, 1),
|
|
|
|
new EntityID(EntityType.P2, 0),
|
|
|
|
};
|
|
|
|
|
2021-05-03 17:25:36 +00:00
|
|
|
turn = turns[0];
|
2021-05-02 02:14:28 +00:00
|
|
|
|
|
|
|
entities = new Entity[]{
|
|
|
|
new Character(
|
|
|
|
new EntityID(EntityType.P1, 4),
|
|
|
|
new IntVector2(4, 16),
|
|
|
|
"This is a name",
|
|
|
|
15,
|
|
|
|
14,
|
|
|
|
13,
|
|
|
|
12,
|
|
|
|
11,
|
|
|
|
10),
|
|
|
|
new Character(
|
|
|
|
new EntityID(EntityType.P2, 2),
|
|
|
|
new IntVector2(4, 16),
|
|
|
|
"This is a name",
|
|
|
|
16,
|
|
|
|
17,
|
|
|
|
18,
|
|
|
|
19,
|
|
|
|
21,
|
|
|
|
25),
|
|
|
|
new NPC(
|
|
|
|
new EntityID(EntityType.NPC, 1),
|
2021-05-31 20:54:13 +00:00
|
|
|
new IntVector2(11, 14)
|
2021-05-02 02:14:28 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
entity = new Character(
|
|
|
|
new EntityID(EntityType.P2, 2),
|
|
|
|
new IntVector2(4, 16),
|
|
|
|
"This is a name",
|
|
|
|
16,
|
|
|
|
17,
|
|
|
|
18,
|
|
|
|
19,
|
|
|
|
21,
|
|
|
|
25);
|
|
|
|
|
|
|
|
filled = new EventBuilder(EventType.CustomEvent)
|
2021-05-01 22:05:55 +00:00
|
|
|
.withTargetEntity(new EntityID(EntityType.P1, 1))
|
|
|
|
.withTargetField(new IntVector2(11, 13))
|
|
|
|
.withAmount(15)
|
|
|
|
.withOriginEntity(new EntityID(EntityType.P2, 4))
|
|
|
|
.withOriginField(new IntVector2(15, 3))
|
|
|
|
.withStoneType(StoneType.MindStone)
|
|
|
|
.withRoundCount(3)
|
|
|
|
.withTurnCount(4)
|
2021-05-02 02:14:28 +00:00
|
|
|
.withCharacterOrder(new EntityID[]{
|
2021-05-01 22:05:55 +00:00
|
|
|
new EntityID(EntityType.P1, 4),
|
|
|
|
new EntityID(EntityType.P1, 1),
|
|
|
|
new EntityID(EntityType.P2, 2),
|
|
|
|
new EntityID(EntityType.P2, 4),
|
|
|
|
new EntityID(EntityType.P1, 3),
|
|
|
|
new EntityID(EntityType.P2, 5),})
|
|
|
|
.withNextCharacter(new EntityID(EntityType.P2, 2))
|
|
|
|
.withPlayerWon(2)
|
|
|
|
.withMessage("Some message")
|
|
|
|
.withTimeLeft(11)
|
2021-05-02 02:14:28 +00:00
|
|
|
.withEntities(new Entity[]{})
|
|
|
|
.withTurnOrder(new EntityID[]{
|
2021-05-01 22:05:55 +00:00
|
|
|
new EntityID(EntityType.P1, 4),
|
|
|
|
new EntityID(EntityType.P1, 1),
|
|
|
|
new EntityID(EntityType.P2, 2),
|
|
|
|
new EntityID(EntityType.P2, 4),
|
|
|
|
new EntityID(EntityType.P1, 3),
|
|
|
|
new EntityID(EntityType.P2, 5),})
|
|
|
|
.withActiveCharacter(new EntityID(EntityType.P1, 1))
|
|
|
|
.withWinCondition(false)
|
|
|
|
.withTeamIdentifier("Team25")
|
|
|
|
.withCustomContent(new HashMap<>());
|
|
|
|
}
|
|
|
|
|
2021-05-02 12:28:51 +00:00
|
|
|
@Test
|
|
|
|
void builderToStringTest() {
|
|
|
|
assertEquals("EventBuilder(non-null) {\ntype = TurnEvent\nroundCount = 5\n}", new EventBuilder(EventType.TurnEvent)
|
|
|
|
.withRoundCount(5)
|
|
|
|
.notNullToString());
|
|
|
|
}
|
|
|
|
|
2021-05-01 22:05:55 +00:00
|
|
|
@Test
|
2021-05-02 02:14:28 +00:00
|
|
|
void buildGameEventUncheckedActualEvents() {
|
|
|
|
// System.out.println("Checks for mistakes in GameEvent creation");
|
|
|
|
var roundSetupEvent = new EventBuilder(EventType.RoundSetupEvent)
|
|
|
|
.withRoundCount(4)
|
|
|
|
.withCharacterOrder(turns)
|
2021-05-03 17:25:36 +00:00
|
|
|
.buildGameEvent();
|
|
|
|
|
|
|
|
assertThat(roundSetupEvent.check())
|
|
|
|
.isTrue()
|
|
|
|
.withFailMessage("RoundSetupEvent failed check");
|
2021-05-02 02:14:28 +00:00
|
|
|
|
|
|
|
var roundSetupEventBaseline = new GameEvent();
|
|
|
|
roundSetupEventBaseline.type = EventType.RoundSetupEvent;
|
|
|
|
roundSetupEventBaseline.roundCount = 4;
|
|
|
|
roundSetupEventBaseline.characterOrder = turns;
|
|
|
|
|
|
|
|
assertThat(roundSetupEvent)
|
|
|
|
.isEqualTo(roundSetupEventBaseline)
|
|
|
|
.withFailMessage("RoundSetupEvent built improperly");
|
|
|
|
|
|
|
|
var turnEvent = new EventBuilder(EventType.TurnEvent)
|
|
|
|
.withNextCharacter(turn)
|
2021-05-03 17:25:36 +00:00
|
|
|
.withTurnCount(5)
|
|
|
|
.buildGameEvent();
|
|
|
|
|
|
|
|
assertThat(turnEvent.check())
|
|
|
|
.isTrue()
|
|
|
|
.withFailMessage("TurnEvent failed check");
|
2021-05-02 02:14:28 +00:00
|
|
|
|
|
|
|
var turnEventBaseline = new GameEvent();
|
|
|
|
turnEventBaseline.type = EventType.TurnEvent;
|
|
|
|
turnEventBaseline.nextCharacter = turn;
|
2021-05-03 17:25:36 +00:00
|
|
|
turnEventBaseline.turnCount = 5;
|
2021-05-02 02:14:28 +00:00
|
|
|
|
|
|
|
assertThat(turnEvent)
|
|
|
|
.isEqualTo(turnEventBaseline)
|
|
|
|
.withFailMessage("TurnEvent was built improperly");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void buildGameEventsUncheckedTest() {
|
|
|
|
// System.out.println("Checks for unsafe (direct) event creation with non-standard GameEvents");
|
|
|
|
var gameEvent = new EventBuilder(EventType.DisconnectEvent) // this event is invalid, but can be tested against
|
|
|
|
.withRoundCount(4112)
|
|
|
|
.withTurnCount(2113)
|
|
|
|
.withCharacterOrder(turns)
|
|
|
|
.withNextCharacter(turn)
|
|
|
|
.withPlayerWon(5912)
|
|
|
|
.withMessage("This message is very much not useful at all")
|
|
|
|
.withTimeLeft(-144)
|
2021-05-03 17:25:36 +00:00
|
|
|
.buildGameEvent();
|
|
|
|
|
|
|
|
assertThat(gameEvent.check())
|
|
|
|
.isTrue()
|
|
|
|
.withFailMessage("GameEvent failed check");
|
2021-05-02 02:14:28 +00:00
|
|
|
|
|
|
|
var baseline = new GameEvent();
|
|
|
|
baseline.type = EventType.DisconnectEvent;
|
|
|
|
baseline.roundCount = 4112;
|
|
|
|
baseline.turnCount = 2113;
|
|
|
|
baseline.characterOrder = turns;
|
|
|
|
baseline.nextCharacter = turn;
|
|
|
|
baseline.playerWon = 5912;
|
|
|
|
baseline.message = "This message is very much not useful at all";
|
|
|
|
baseline.timeLeft = -144;
|
2021-05-01 22:05:55 +00:00
|
|
|
|
2021-05-02 02:14:28 +00:00
|
|
|
assertThat(gameEvent)
|
|
|
|
.isEqualTo(baseline)
|
|
|
|
.withFailMessage("GameEvent was built improperly");
|
2021-05-01 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 02:14:28 +00:00
|
|
|
@Test
|
|
|
|
void buildGameStateEventsUncheckedTest() {
|
|
|
|
// System.out.println("Checks for unsafe (direct) event creation with non-standard GameStateEvents");
|
|
|
|
var gameStateEvent = new EventBuilder(EventType.ConsumedAPEvent)
|
|
|
|
.withEntities(entities)
|
|
|
|
.withTurnOrder(turns)
|
|
|
|
.withActiveCharacter(turn)
|
|
|
|
.withWinCondition(true)
|
2021-05-03 17:25:36 +00:00
|
|
|
.buildGameStateEvent();
|
|
|
|
|
|
|
|
assertThat(gameStateEvent.check())
|
|
|
|
.isTrue()
|
|
|
|
.withFailMessage("GameStateEvent failed check");
|
2021-05-02 02:14:28 +00:00
|
|
|
|
2021-05-11 03:06:00 +00:00
|
|
|
var baseline = new GamestateEvent();
|
2021-05-02 02:14:28 +00:00
|
|
|
baseline.type = EventType.ConsumedAPEvent;
|
|
|
|
baseline.entities = entities;
|
|
|
|
baseline.turnOrder = turns;
|
|
|
|
baseline.activeCharacter = turn;
|
|
|
|
baseline.winCondition = true;
|
|
|
|
|
|
|
|
assertThat(gameStateEvent)
|
|
|
|
.isEqualTo(baseline)
|
|
|
|
.withFailMessage("GameStateEvent was built improperly");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: implement tests for unchecked CharacterEvents and EntityEvents
|
2021-05-28 11:18:20 +00:00
|
|
|
@Test
|
|
|
|
void buildWrongEntityEvent1() {
|
|
|
|
EventBuilder eb = new EventBuilder(EventType.DestroyedEntityEvent);
|
|
|
|
//the target entity is not set
|
|
|
|
EntityEvent des2 = eb
|
|
|
|
.withTargetField(new IntVector2(1,1))
|
|
|
|
.buildEntityEvent();
|
|
|
|
//the target entity is not set --> check() return false
|
|
|
|
boolean b = des2.check();
|
|
|
|
assertEquals(b, false);
|
|
|
|
|
|
|
|
}
|
2021-05-02 02:14:28 +00:00
|
|
|
|
2021-05-01 22:05:55 +00:00
|
|
|
@Test
|
|
|
|
void buildEntityEvent() {
|
2021-05-28 11:18:20 +00:00
|
|
|
//testing EntityEvent class
|
|
|
|
assertThat(new EventBuilder(EventType.DestroyedEntityEvent)
|
|
|
|
.withTargetField(new IntVector2(1,1))
|
|
|
|
.withTargetEntity(new EntityID(EntityType.P1,1))
|
|
|
|
.buildEntityEvent().check()).isTrue();
|
|
|
|
|
|
|
|
EntityEvent des = new EntityEvent();
|
|
|
|
des.type = EventType.DestroyedEntityEvent;
|
|
|
|
des.targetEntity= new EntityID(EntityType.P1,1);
|
|
|
|
des.targetField= new IntVector2(1,1);
|
|
|
|
|
|
|
|
EventBuilder eb = new EventBuilder(EventType.DestroyedEntityEvent);
|
|
|
|
EntityEvent des2 = eb
|
|
|
|
.withTargetField(new IntVector2(1,1))
|
|
|
|
.withTargetEntity(new EntityID(EntityType.P1,1))
|
|
|
|
.buildEntityEvent();
|
|
|
|
|
|
|
|
//testing EventBuilder
|
|
|
|
assertEquals(des.type, des2.type);
|
|
|
|
assertEquals(des.targetEntity, des2.targetEntity);
|
|
|
|
assertEquals(des.targetField, des2.targetField);
|
|
|
|
// EntityEvent des2 = new EntityEvent(eb.);
|
|
|
|
|
|
|
|
|
2021-05-01 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void buildCharacterEvent() {
|
2021-05-28 11:18:20 +00:00
|
|
|
//testing CharacterEvent class
|
|
|
|
assertThat(new EventBuilder(EventType.MeleeAttackEvent)
|
|
|
|
.withOriginField(new IntVector2(2,2))
|
|
|
|
.withTargetField(new IntVector2(1,1))
|
|
|
|
.withOriginEntity(new EntityID(EntityType.P2,2))
|
|
|
|
.withTargetEntity(new EntityID(EntityType.P1,1))
|
|
|
|
.buildEntityEvent().check()).isTrue();
|
|
|
|
|
|
|
|
CharacterEvent des = new CharacterEvent();
|
|
|
|
des.type = EventType.MeleeAttackEvent;
|
|
|
|
des.originField= new IntVector2(2,2);
|
|
|
|
des.targetEntity= new EntityID(EntityType.P1,1);
|
|
|
|
des.originEntity = new EntityID(EntityType.P2,2);
|
|
|
|
des.targetField= new IntVector2(1,1);
|
|
|
|
|
|
|
|
EventBuilder eb = new EventBuilder(EventType.MeleeAttackEvent);
|
|
|
|
CharacterEvent des2 = eb
|
|
|
|
.withOriginField(new IntVector2(2,2))
|
|
|
|
.withTargetField(new IntVector2(1,1))
|
|
|
|
.withOriginEntity(new EntityID(EntityType.P2,2))
|
|
|
|
.withTargetEntity(new EntityID(EntityType.P1,1))
|
|
|
|
.buildCharacterEvent();
|
|
|
|
//testing EventBuilder
|
|
|
|
assertEquals(des.type, des2.type);
|
|
|
|
assertEquals(des.targetEntity, des2.targetEntity);
|
|
|
|
assertEquals(des.targetField, des2.targetField);
|
|
|
|
assertEquals(des.originEntity, des2.originEntity);
|
|
|
|
assertEquals(des.originField, des2.originField);
|
|
|
|
|
2021-05-02 02:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void buildGameStateEventWithTooManyProperties() {
|
2021-05-03 17:25:36 +00:00
|
|
|
assertThat(new EventBuilder(EventType.Ack) // too many properties is fine
|
|
|
|
.withAmount(15) // also properties of different EventTypes, they just get ignored
|
|
|
|
.withEntities(entities) // properties belonging to the same eventType get incorporated into
|
|
|
|
.withWinCondition(false) // the final event, so they have to be ignored
|
|
|
|
.buildGameStateEvent() // by the programmer later on
|
|
|
|
.check()).isTrue();
|
2021-05-01 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2021-05-02 02:14:28 +00:00
|
|
|
void buildGameStateEvent() {
|
2021-05-03 17:25:36 +00:00
|
|
|
assertThat(new EventBuilder(EventType.Ack) // needs no properties
|
|
|
|
.buildGameStateEvent()
|
|
|
|
.check()).isTrue();
|
2021-05-02 02:14:28 +00:00
|
|
|
|
|
|
|
|
2021-05-03 17:25:36 +00:00
|
|
|
assertThat(new EventBuilder(EventType.Nack).buildGameStateEvent().check()).isTrue();
|
2021-05-02 02:14:28 +00:00
|
|
|
|
2021-05-11 03:06:00 +00:00
|
|
|
// assertThat(new EventBuilder(EventType.Req).buildGameStateEvent().check()).isTrue();
|
2021-05-02 02:14:28 +00:00
|
|
|
|
2021-05-11 03:06:00 +00:00
|
|
|
assertThat(new EventBuilder(EventType.GamestateEvent) // if properties missing throw exception
|
2021-05-03 17:25:36 +00:00
|
|
|
.withTurnOrder(turns)
|
|
|
|
.withActiveCharacter(turn)
|
|
|
|
.buildGameStateEvent()
|
|
|
|
.check()).isFalse();
|
2021-05-02 02:14:28 +00:00
|
|
|
|
2021-05-11 03:06:00 +00:00
|
|
|
assertThat(new EventBuilder(EventType.GamestateEvent) // no exception if all properties present
|
2021-05-03 17:25:36 +00:00
|
|
|
.withEntities(entities)
|
|
|
|
.withTurnOrder(turns)
|
2021-05-11 01:20:19 +00:00
|
|
|
.withMapSize(new IntVector2(42, 24))
|
2021-05-03 17:25:36 +00:00
|
|
|
.withActiveCharacter(turn)
|
2021-05-11 01:20:19 +00:00
|
|
|
.withStoneCooldowns(new Integer[] {6, 5, 4, 3, 2, 1})
|
2021-05-03 17:25:36 +00:00
|
|
|
.withWinCondition(false)
|
|
|
|
.buildGameStateEvent()
|
|
|
|
.check()).isTrue();
|
2021-05-01 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void buildCustomEvent() {
|
2021-05-28 11:18:20 +00:00
|
|
|
//testing CustomEvent class
|
|
|
|
assertThat(new EventBuilder(EventType.CustomEvent)
|
|
|
|
.withCustomContent(new HashMap<String, Object>())
|
|
|
|
.buildCustomEvent().check()).isTrue();
|
|
|
|
|
|
|
|
CustomEvent des = new CustomEvent();
|
|
|
|
des.type = EventType.CustomEvent;
|
|
|
|
des.customContent= new HashMap<String, Object>();
|
|
|
|
|
|
|
|
|
|
|
|
EventBuilder eb = new EventBuilder(EventType.CustomEvent);
|
|
|
|
CustomEvent des2 = eb
|
|
|
|
.withCustomContent(new HashMap<String, Object>())
|
|
|
|
.buildCustomEvent();
|
|
|
|
|
|
|
|
//testing EventBuilder
|
|
|
|
assertEquals(des.type, des2.type);
|
|
|
|
assertEquals(des.customContent, des2.customContent);
|
|
|
|
|
2021-05-01 22:05:55 +00:00
|
|
|
}
|
2021-05-02 12:28:51 +00:00
|
|
|
}
|