1
0
Fork 0

Added battle stat "Equipment wear"

This commit is contained in:
Michaël Lemaire 2017-09-15 00:24:53 +02:00
parent a8032eea8b
commit 34d87516cf
3 changed files with 61 additions and 11 deletions

View file

@ -194,6 +194,7 @@ module TS.SpaceTac {
// Apply to all ships
iforeach(this.iships(), ship => ship.endBattle(this.turn));
this.stats.onBattleEnd(this.fleets[0], this.fleets[1]);
}
// Checks end battle conditions, returns true if the battle ended
@ -287,6 +288,7 @@ module TS.SpaceTac {
this.ended = false;
this.turn = 0;
this.placeShips();
this.stats.onBattleStart(this.fleets[0], this.fleets[1]);
this.throwInitiative();
iforeach(this.iships(), ship => ship.startBattle());
this.advanceToNextShip();

View file

@ -26,15 +26,15 @@ module TS.SpaceTac.Specs {
expect(stats.stats).toEqual({});
battle.log.add(new DamageEvent(attacker, 10, 12));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Damage dealt": [0, 22] });
battle.log.add(new DamageEvent(defender, 40, 0));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Damage dealt": [40, 22] });
battle.log.add(new DamageEvent(attacker, 5, 4));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Damage dealt": [40, 31] });
})
@ -47,11 +47,11 @@ module TS.SpaceTac.Specs {
expect(stats.stats).toEqual({});
battle.log.add(new MoveEvent(attacker, new ArenaLocationAngle(0, 0), new ArenaLocationAngle(10, 0)));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Move distance (km)": [10, 0] });
battle.log.add(new MoveEvent(defender, new ArenaLocationAngle(10, 5), new ArenaLocationAngle(10, 63)));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Move distance (km)": [10, 58] });
})
@ -64,11 +64,11 @@ module TS.SpaceTac.Specs {
expect(stats.stats).toEqual({});
battle.log.add(new DroneDeployedEvent(new Drone(attacker)));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Drones deployed": [1, 0] });
battle.log.add(new DroneDeployedEvent(new Drone(defender)));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Drones deployed": [1, 1] });
})
@ -81,12 +81,33 @@ module TS.SpaceTac.Specs {
expect(stats.stats).toEqual({});
battle.log.add(new ActionAppliedEvent(attacker, new BaseAction("nop", "nop", false), null, 4));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Power used": [4, 0] });
battle.log.add(new ActionAppliedEvent(defender, new BaseAction("nop", "nop", false), null, 2));
stats.processLog(battle.log, battle.fleets[0]);
stats.processLog(battle.log, battle.fleets[0], true);
expect(stats.stats).toEqual({ "Power used": [4, 2] });
})
it("evaluates equipment depreciation", function () {
let stats = new BattleStats();
let battle = new Battle();
let attacker = battle.fleets[0].addShip();
let defender = battle.fleets[1].addShip();
let equ1 = TestTools.addEngine(attacker, 50);
equ1.price = 1000;
let equ2 = TestTools.addEngine(defender, 50);
equ2.price = 1100;
stats.onBattleStart(attacker.fleet, defender.fleet);
expect(stats.stats).toEqual({ "Equipment wear (zotys)": [1000, 1100] });
equ1.price = 500;
equ2.price = 800;
stats.onBattleEnd(attacker.fleet, defender.fleet);
expect(stats.stats).toEqual({ "Equipment wear (zotys)": [500, 300] });
})
})
}

View file

@ -37,8 +37,10 @@ module TS.SpaceTac {
/**
* Process a battle log
*/
processLog(log: BattleLog, attacker: Fleet) {
this.stats = {};
processLog(log: BattleLog, attacker: Fleet, clear = false) {
if (clear) {
this.stats = {};
}
log.events.forEach(event => {
if (event instanceof ActionAppliedEvent) {
@ -52,5 +54,30 @@ module TS.SpaceTac {
}
});
}
/**
* Get the raw value of a fleet
*/
private getFleetValue(fleet: Fleet): number {
return sum(fleet.ships.map(ship => {
return sum(ship.listEquipment().map(equipment => equipment.getPrice()));
}));
}
/**
* Prepare some stats at the start of battle
*/
onBattleStart(attacker: Fleet, defender: Fleet): void {
this.addStat("Equipment wear (zotys)", this.getFleetValue(attacker), true);
this.addStat("Equipment wear (zotys)", this.getFleetValue(defender), false);
}
/**
* Finalize some stats at the start of battle
*/
onBattleEnd(attacker: Fleet, defender: Fleet): void {
this.addStat("Equipment wear (zotys)", -this.getFleetValue(attacker), true);
this.addStat("Equipment wear (zotys)", -this.getFleetValue(defender), false);
}
}
}