2017-05-29 23:15:32 +00:00
|
|
|
/// <reference path="events/BaseBattleEvent.ts"/>
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2017-09-24 22:23:22 +00:00
|
|
|
module TK.SpaceTac {
|
2014-12-31 00:00:00 +00:00
|
|
|
// Check a single game log event
|
2017-05-29 23:15:32 +00:00
|
|
|
function checkEvent(got: BaseBattleEvent, ship: Ship, code: string,
|
2017-03-09 17:11:00 +00:00
|
|
|
target_ship: Ship | null = null, target_x: number | null = null, target_y: number | null = null): void {
|
2014-12-31 00:00:00 +00:00
|
|
|
if (target_ship) {
|
|
|
|
if (target_x === null) {
|
|
|
|
target_x = target_ship.arena_x;
|
|
|
|
}
|
|
|
|
if (target_y === null) {
|
|
|
|
target_y = target_ship.arena_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(got.ship).toBe(ship);
|
|
|
|
expect(got.code).toEqual(code);
|
2017-03-09 17:11:00 +00:00
|
|
|
if (got.target) {
|
|
|
|
expect(got.target.ship).toBe(target_ship);
|
|
|
|
if (target_x === null) {
|
|
|
|
expect(got.target.x).toBeNull();
|
|
|
|
} else {
|
|
|
|
expect(got.target.x).toBeCloseTo(target_x, 0.000001);
|
|
|
|
}
|
|
|
|
if (target_y === null) {
|
|
|
|
expect(got.target.y).toBeNull();
|
|
|
|
} else {
|
|
|
|
expect(got.target.y).toBeCloseTo(target_y, 0.000001);
|
|
|
|
}
|
2017-06-25 21:07:53 +00:00
|
|
|
} else if (target_ship || target_x || target_y) {
|
2017-03-09 17:11:00 +00:00
|
|
|
fail("Got no target");
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
// Fake event
|
2017-05-29 23:15:32 +00:00
|
|
|
class FakeEvent extends BaseBattleEvent {
|
2014-12-31 00:00:00 +00:00
|
|
|
constructor() {
|
2017-03-09 17:11:00 +00:00
|
|
|
super("fake", new Ship());
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
describe("BattleLog", function () {
|
2014-12-31 00:00:00 +00:00
|
|
|
it("forwards events to subscribers, until unsubscribe", function () {
|
2014-12-31 00:00:00 +00:00
|
|
|
var log = new BattleLog();
|
2017-05-29 23:15:32 +00:00
|
|
|
var received: BaseBattleEvent[] = [];
|
2014-12-31 00:00:00 +00:00
|
|
|
var fake = new FakeEvent();
|
|
|
|
|
2017-05-29 23:15:32 +00:00
|
|
|
var sub = log.subscribe(function (event: BaseBattleEvent) {
|
2014-12-31 00:00:00 +00:00
|
|
|
received.push(event);
|
|
|
|
});
|
|
|
|
|
|
|
|
log.add(fake);
|
|
|
|
expect(received).toEqual([fake]);
|
|
|
|
|
|
|
|
log.add(fake);
|
|
|
|
expect(received).toEqual([fake, fake]);
|
|
|
|
|
|
|
|
log.unsubscribe(sub);
|
|
|
|
log.add(fake);
|
|
|
|
expect(received).toEqual([fake, fake]);
|
|
|
|
});
|
|
|
|
|
2014-12-31 00:00:00 +00:00
|
|
|
it("logs ship change events", function () {
|
2014-12-31 00:00:00 +00:00
|
|
|
var battle = Battle.newQuickRandom();
|
2015-01-22 00:00:00 +00:00
|
|
|
battle.log.clear();
|
2017-02-07 19:15:21 +00:00
|
|
|
battle.log.addFilter("value");
|
2014-12-31 00:00:00 +00:00
|
|
|
expect(battle.log.events.length).toBe(0);
|
|
|
|
|
|
|
|
battle.advanceToNextShip();
|
|
|
|
expect(battle.log.events.length).toBe(1);
|
|
|
|
checkEvent(battle.log.events[0], battle.play_order[0], "ship_change", battle.play_order[1]);
|
|
|
|
});
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2015-01-06 00:00:00 +00:00
|
|
|
it("can receive simulated initial state events", function () {
|
2017-05-02 22:49:35 +00:00
|
|
|
let battle = Battle.newQuickRandom(true, 1, 4);
|
2017-03-09 17:11:00 +00:00
|
|
|
let playing = nn(battle.playing_ship);
|
2014-12-31 00:00:00 +00:00
|
|
|
|
2017-05-29 23:15:32 +00:00
|
|
|
let result = battle.getBootstrapEvents();
|
2017-06-25 21:07:53 +00:00
|
|
|
expect(result.length).toBe(17);
|
2014-12-31 00:00:00 +00:00
|
|
|
for (var i = 0; i < 8; i++) {
|
2017-05-29 23:15:32 +00:00
|
|
|
checkEvent(result[i], battle.play_order[i], "move", null,
|
2015-01-07 00:00:00 +00:00
|
|
|
battle.play_order[i].arena_x, battle.play_order[i].arena_y);
|
2014-12-31 00:00:00 +00:00
|
|
|
}
|
2017-06-25 21:07:53 +00:00
|
|
|
for (var i = 0; i < 8; i++) {
|
|
|
|
checkEvent(result[8 + i], battle.play_order[i], "activeeffects");
|
|
|
|
}
|
|
|
|
checkEvent(result[16], playing, "ship_change", playing);
|
2014-12-31 00:00:00 +00:00
|
|
|
});
|
2017-07-10 21:40:52 +00:00
|
|
|
|
|
|
|
it("stop accepting events once the battle is ended", function () {
|
|
|
|
let log = new BattleLog();
|
|
|
|
|
|
|
|
log.add(new ValueChangeEvent(new Ship(), new ShipValue("test"), 1));
|
|
|
|
log.add(new EndBattleEvent(new BattleOutcome(null)));
|
|
|
|
log.add(new ShipChangeEvent(new Ship(), new Ship()));
|
|
|
|
|
|
|
|
expect(log.events.length).toBe(2);
|
|
|
|
expect(log.events[0] instanceof ValueChangeEvent).toBe(true);
|
|
|
|
expect(log.events[1] instanceof EndBattleEvent).toBe(true);
|
|
|
|
});
|
2014-12-31 00:00:00 +00:00
|
|
|
});
|
2015-01-07 00:00:00 +00:00
|
|
|
}
|