Comparing version 0.0.8 to 0.0.9
{ | ||
"name": "drugwars", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "A lightweight JavaScript library for DrugWars", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
const debug = require('debug')('army'); | ||
const { orderBy } = require('lodash'); | ||
const Unit = require('./unit'); | ||
const Troop = require('./troop'); | ||
class Army { | ||
constructor(units, name) { | ||
this.units = []; | ||
this.troops = []; | ||
this.alive = true; | ||
@@ -12,5 +12,3 @@ this.name = name; | ||
units.forEach((unit) => { | ||
for (let i = 0; i < unit.amount; i += 1) { | ||
this.units.push(new Unit(unit.key, i + 1, name)); | ||
} | ||
this.troops.push(new Troop(unit.key, unit.amount, name)); | ||
}); | ||
@@ -22,6 +20,9 @@ } | ||
this.units.forEach((unit) => { | ||
if (!unit.dead) { | ||
debug(`${this.name} ${unit.key} ${unit.i} attack ${unit.spec.attack}`); | ||
attacks.push(unit.spec.attack); | ||
this.troops.forEach((troop) => { | ||
if (troop.undead > 0) { | ||
const attack = troop.getAttack(); | ||
if (attack > 0) { | ||
debug(`${this.name} ${troop.key} x ${troop.undead} attack ${attack}`); | ||
attacks.push(attack); | ||
} | ||
} | ||
@@ -35,6 +36,6 @@ }); | ||
const unitsSorted = orderBy(this.units, ['priority'], ['asc']); | ||
unitsSorted.forEach((unit) => { | ||
while (!unit.dead && pending.length > 0) { | ||
unit.takeDamages(pending[0]); | ||
const troopsSorted = orderBy(this.troops, ['priority'], ['asc']); | ||
troopsSorted.forEach((troop) => { | ||
if (troop.undead > 0 && pending.length > 0) { | ||
troop.takeDamages(pending[0]); | ||
pending.splice(0, 1); | ||
@@ -44,8 +45,8 @@ } | ||
const unitsAlive = this.units.filter(unit => !unit.dead).length; | ||
if (!unitsAlive) { | ||
const troopsAlive = this.troops.filter(troop => troop.undead > 0).length; | ||
if (!troopsAlive) { | ||
this.alive = false; | ||
} | ||
if (unitsAlive > 0 && pending.length > 0) { | ||
if (troopsAlive > 0 && pending.length > 0) { | ||
this.takeDamages(pending); | ||
@@ -56,18 +57,8 @@ } | ||
getResult() { | ||
const unitsObj = {}; | ||
this.units.forEach((unit) => { | ||
if (!unitsObj[unit.key]) { | ||
unitsObj[unit.key] = { | ||
amount: 0, | ||
dead: 0, | ||
}; | ||
} | ||
unitsObj[unit.key].amount += 1; | ||
unitsObj[unit.key].dead += unit.dead ? 1 : 0; | ||
}); | ||
return Object.keys(unitsObj).map((key) => { | ||
const unit = { key, amount: unitsObj[key].amount }; | ||
if (unitsObj[key].dead) unit.dead = unitsObj[key].dead; | ||
return this.troops.map((troop) => { | ||
const unit = { | ||
key: troop.key, | ||
amount: troop.amount, | ||
}; | ||
if (troop.dead > 0) unit.dead = troop.dead; | ||
return unit; | ||
@@ -74,0 +65,0 @@ }); |
@@ -183,3 +183,3 @@ const { Fight } = require('..'); | ||
expected = { | ||
result: 1, | ||
result: 3, | ||
attacker: { | ||
@@ -190,2 +190,3 @@ units: [ | ||
amount: 5, | ||
dead: 5, | ||
}, | ||
@@ -195,3 +196,3 @@ { | ||
amount: 10, | ||
dead: 8, | ||
dead: 10, | ||
}, | ||
@@ -205,3 +206,2 @@ ], | ||
amount: 1, | ||
dead: 1, | ||
}, | ||
@@ -211,3 +211,2 @@ { | ||
amount: 1, | ||
dead: 1, | ||
}, | ||
@@ -214,0 +213,0 @@ ], |
31808
17
1149