Comparing version 0.0.10 to 0.0.11
{ | ||
"name": "drugwars", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"description": "A lightweight JavaScript library for DrugWars", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,1 @@ | ||
const debug = require('debug')('army'); | ||
const { orderBy } = require('lodash'); | ||
@@ -6,9 +5,10 @@ const Troop = require('./troop'); | ||
class Army { | ||
constructor(units, name) { | ||
constructor(units, name, log) { | ||
this.troops = []; | ||
this.alive = true; | ||
this.name = name; | ||
this.log = log; | ||
units.forEach((unit) => { | ||
this.troops.push(new Troop(unit.key, unit.amount, name)); | ||
this.troops.push(new Troop(unit.key, unit.amount, name, log)); | ||
}); | ||
@@ -24,3 +24,3 @@ } | ||
if (attack > 0) { | ||
debug(`${this.name} ${troop.key} x ${troop.undead} attack ${attack}`); | ||
this.log.add(`${this.name} ${troop.key} x ${troop.undead} attack ${attack}`); | ||
attacks.push(attack); | ||
@@ -27,0 +27,0 @@ } |
@@ -1,3 +0,3 @@ | ||
const debug = require('debug')('fight'); | ||
const Army = require('./army'); | ||
const Log = require('./log'); | ||
@@ -8,8 +8,9 @@ class Fight { | ||
this.targetUnits = json.target.units || []; | ||
this.log = new Log(); | ||
} | ||
fight() { | ||
debug('Fight start'); | ||
const attackerArmy = new Army(this.attackerUnits, 'A'); | ||
const targetArmy = new Army(this.targetUnits, 'T'); | ||
this.log.add('Fight start'); | ||
const attackerArmy = new Army(this.attackerUnits, 'attacker', this.log); | ||
const targetArmy = new Army(this.targetUnits, 'target', this.log); | ||
let round = 0; | ||
@@ -19,3 +20,3 @@ | ||
round += 1; | ||
debug(`Round ${round} start`); | ||
this.log.add(`Round ${round} start`); | ||
@@ -28,3 +29,3 @@ const attackerAttacks = attackerArmy.getAttacks(); | ||
debug(`Fight ended in round ${round}`); | ||
this.log.add(`Fight ended in round ${round}`); | ||
@@ -48,4 +49,8 @@ let result = 2; | ||
} | ||
getLog() { | ||
return this.log.log; | ||
} | ||
} | ||
module.exports = Fight; |
@@ -1,6 +0,6 @@ | ||
const debug = require('debug')('troop'); | ||
const unitsJson = require('../units.json'); | ||
class Troop { | ||
constructor(key, amount, name) { | ||
constructor(key, amount, name, log) { | ||
this.log = log; | ||
this.key = key; | ||
@@ -13,3 +13,2 @@ this.amount = amount; | ||
this.priority = unitsJson[key].priority; | ||
this.dead = 0; | ||
@@ -27,6 +26,6 @@ this.undead = amount; | ||
debug(`${this.name} ${this.key} ${this.undead} -${damages}/${health}`); | ||
this.log.add(`${this.name} ${this.key} ${this.undead} -${damages}/${health}`); | ||
if (healthAfterDamage <= 0) { | ||
debug(`${this.name} ${this.key} ${this.undead} dead`); | ||
this.log.add(`${this.name} ${this.key} ${this.undead} dead`); | ||
@@ -36,5 +35,5 @@ this.dead = this.amount; | ||
} else { | ||
const undead = parseFloat(healthAfterDamage / this.defense).toFixed(0); | ||
const undead = Math.ceil(healthAfterDamage / this.defense); | ||
if (undead !== this.undead) { | ||
debug(`${this.name} ${this.key} ${this.undead - undead} dead`); | ||
this.log.add(`${this.name} ${this.key} ${this.undead - undead} dead`); | ||
this.dead += this.undead - undead; | ||
@@ -41,0 +40,0 @@ this.undead = undead; |
@@ -212,6 +212,59 @@ const { Fight } = require('..'); | ||
it('the priority is respected', () => { | ||
it('should respect priority', () => { | ||
expect(new Fight(fight).fight()).toEqual(expected); | ||
}); | ||
fight = { | ||
attacker: { | ||
units: [ | ||
{ | ||
key: 'ninja', | ||
amount: 5, | ||
}, | ||
], | ||
}, | ||
target: { | ||
units: [ | ||
{ | ||
key: 'rowdy', | ||
amount: 1, | ||
}, | ||
{ | ||
key: 'knifer', | ||
amount: 1, | ||
}, | ||
], | ||
}, | ||
}; | ||
expected = { | ||
result: 1, | ||
attacker: { | ||
units: [ | ||
{ | ||
key: 'ninja', | ||
amount: 5, | ||
}, | ||
], | ||
}, | ||
target: { | ||
units: [ | ||
{ | ||
key: 'rowdy', | ||
amount: 1, | ||
dead: 1, | ||
}, | ||
{ | ||
key: 'knifer', | ||
amount: 1, | ||
dead: 1, | ||
}, | ||
], | ||
}, | ||
}; | ||
it('should not kill ninja', () => { | ||
expect(new Fight(fight).fight()).toEqual(expected); | ||
}); | ||
}); | ||
}); |
32911
18
1212