Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

balance and relay examples work, balance is lop-sided for off-power o…

…utcomes
  • Loading branch information...
commit 15a4f1a56b577aa91fe5a688e22ed33a9cf5ae33 1 parent 1a99556
@substack authored
Showing with 98 additions and 3 deletions.
  1. +25 −0 example/balance.js
  2. +7 −3 example/relay.js
  3. +66 −0 index.js
View
25 example/balance.js
@@ -0,0 +1,25 @@
+var relay = require('../');
+
+var counts = { success : 0, failure : 0 };
+var pending = 1000;
+
+for (var i = 0; i < 10000; i++) {
+ var monster = relay({ attack : 20, defend : 3 }, { delay : 1 });
+ var tree = relay({ attack : 0, defend : 10 }, { delay : 1 });
+
+ var attack = monster.attack(tree);
+
+ attack.on('failure', function () {
+ counts.failure ++;
+ if (--pending === 0) done();
+ });
+
+ attack.on('success', function () {
+ counts.success ++;
+ if (--pending === 0) done();
+ });
+}
+
+function done () {
+ console.dir(counts);
+}
View
10 example/relay.js
@@ -1,10 +1,10 @@
var relay = require('../');
-var monster = relay({ attack : 10, defend : 3 });
-var tree = relay({ attack : 0, defend : 50 });
+var monster = relay({ attack : 10, defend : 3 }, { delay : 1 });
+var tree = relay({ attack : 0, defend : 10 }, { delay : 1 });
var attack = monster.attack(tree);
-attack.on('hit', function (damage) {
+attack.on('damage', function (damage) {
console.log([
'tree took ' + damage + ' damage',
' tree: ' + JSON.stringify(tree.energy),
@@ -12,6 +12,10 @@ attack.on('hit', function (damage) {
].join('\n'));
});
+attack.on('failure', function () {
+ console.log('tree withstood attack, out of attack energy');
+});
+
attack.on('success', function () {
console.log('tree was defeated');
});
View
66 index.js
@@ -0,0 +1,66 @@
+var clone = require('clone');
+var EventEmitter = require('events').EventEmitter;
+var inherits = require('inherits');
+
+module.exports = function (energy, opts) {
+ return new Relay(clone(energy), opts || {});
+};
+
+function Relay (energy, opts) {
+ [ 'defend', 'attack', 'throughput', 'radius', 'storage', 'generate' ]
+ .forEach(function (key) { if (!energy[key]) energy[key] = 0 })
+ ;
+ this.energy = energy;
+ this.delay = opts.delay || 1000;
+}
+
+inherits(Relay, EventEmitter);
+
+Relay.prototype.attack = function (n, target) {
+ var self = this;
+
+ if (target === undefined) {
+ target = n;
+ n = self.energy.attack;
+ }
+
+ var attack = new EventEmitter;
+ var iv = setInterval(function () {
+ n = Math.min(n, self.energy.attack);
+
+ var power = {
+ defend : Math.floor((target.energy.defend + 1) * Math.random()),
+ attack : Math.floor((n + 1) * Math.random())
+ };
+
+ var delta = {
+ defend : power.attack > power.defend ? - power.attack : 0,
+ attack : power.attack > power.defend ? 0 : - power.defend
+ };
+
+ n += delta.attack;
+ self.energy.attack = Math.max(
+ 0, self.energy.attack + delta.attack
+ );
+ target.energy.defend = Math.max(
+ 0, target.energy.defend + delta.defend
+ );
+
+ attack.emit('damage', -delta.defend);
+
+ if (target.energy.defend === 0) {
+ attack.emit('success');
+ target.emit('defeat');
+ attack.cancel();
+ }
+ else if (n <= 0) {
+ attack.cancel();
+ attack.emit('failure');
+ }
+ }, self.delay);
+
+ attack.cancel = function () { clearInterval(iv) };
+ self.on('defeat', function () { attack.cancel() });
+
+ return attack;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.