1
0
Fork 0
spacetac/src/core/BattleOutcome.ts

35 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac {
/**
* Result of an ended battle
*
* This stores the winner, and the retrievable loot
*/
export class BattleOutcome {
// Indicates if the battle is a draw (no winner)
draw: boolean
// Victorious fleet
winner: RObjectId | null
2017-03-09 17:11:00 +00:00
constructor(winner: Fleet | null) {
this.winner = winner ? winner.id : null;
this.draw = winner ? false : true;
}
2017-05-09 17:19:26 +00:00
/**
* Grant experience to participating fleets
*/
grantExperience(fleets: Fleet[]) {
fleets.forEach(fleet => {
let winfactor = (fleet.is(this.winner)) ? 0.03 : (this.draw ? 0.01 : 0.005);
2017-05-09 17:19:26 +00:00
let enemies = flatten(fleets.filter(f => f !== fleet).map(f => f.ships));
let difficulty = sum(enemies.map(enemy => 100 + enemy.level.getExperience()));
fleet.ships.forEach(ship => {
ship.level.addExperience(Math.floor(difficulty * winfactor));
ship.level.checkLevelUp();
});
});
}
}
}