Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

finally an attack algorithm that has desirable properties

  • Loading branch information...
commit 9bd477c896a078607afe48ad1b6aeffdf4e652d5 1 parent d09e777
@substack authored
Showing with 33 additions and 36 deletions.
  1. +2 −2 example/balance.js
  2. +4 −4 example/relay.js
  3. +27 −30 index.js
View
4 example/balance.js
@@ -1,10 +1,10 @@
var relay = require('../');
var counts = { success : 0, failure : 0 };
-var pending = 1000;
+var pending = 10000;
for (var i = 0; i < 10000; i++) {
- var monster = relay({ attack : 11, defend : 3 }, { delay : 1 });
+ var monster = relay({ attack : 100, defend : 3 }, { delay : 1 });
var tree = relay({ attack : 0, defend : 10 }, { delay : 1 });
var attack = monster.attack(tree);
View
8 example/relay.js
@@ -1,12 +1,12 @@
var relay = require('../');
-var monster = relay({ attack : 10, defend : 3 }, { delay : 1 });
-var tree = relay({ attack : 0, defend : 10 }, { delay : 1 });
+var monster = relay({ attack : 100, defend : 3 });
+var tree = relay({ attack : 0, defend : 10 });
var attack = monster.attack(tree);
-attack.on('damage', function (damage) {
+attack.on('delta', function (delta) {
console.log([
- 'tree took ' + damage + ' damage',
+ 'tree ' + delta.defend + ', monster ' + delta.attack,
' tree: ' + JSON.stringify(tree.energy),
' monster: ' + JSON.stringify(monster.energy),
].join('\n'));
View
57 index.js
@@ -16,51 +16,48 @@ function Relay (energy, opts) {
inherits(Relay, EventEmitter);
-Relay.prototype.attack = function (n, target) {
- var self = this;
-
- if (target === undefined) {
- target = n;
- n = self.energy.attack;
- }
-
+Relay.prototype.attack = function (defender) {
var attack = new EventEmitter;
+ var attacker = this;
+
var iv = setInterval(function () {
- n = Math.min(n, self.energy.attack);
+ var sum = attacker.energy.attack + defender.energy.defend;
- var power = {
- defend : Math.floor((target.energy.defend + 1) * Math.random()),
- attack : Math.floor((n + 1) * Math.random())
- };
+ var da = Math.ceil(
+ Math.log(Math.ceil(Math.pow(2, Math.random() * 4) * sum))
+ / Math.log(2)
+ );
- var damage = Math.max(0, Math.abs(power.attack - power.defend));
- var delta = {
- defend : power.attack > power.defend
- ? - damage : 0,
- attack : power.attack > power.defend
- ? 0 : - damage
- ,
+ var dd = Math.ceil(
+ Math.log(Math.ceil(Math.pow(2, Math.random() * 4) * sum))
+ / Math.log(1.9)
+ );
+
+ var deltas = {
+ attack : da > dd ? 0 : -da,
+ defend : dd > da ? 0 : -dd,
};
+ attacker.energy.attack += deltas.attack;
+ defender.energy.defend += deltas.defend;
- n = Math.max(0, 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);
+ if (attacker.energy.attack < 0) attacker.energy.attack = 0;
+ if (defender.energy.defend < 0) defender.energy.defend = 0;
- attack.emit('damage', - delta.defend);
+ attack.emit('delta', deltas);
- if (target.energy.defend === 0) {
- attack.emit('success');
- target.emit('defeat');
+ if (defender.energy.defend === 0) {
attack.cancel();
+ attack.emit('success');
+ defender.emit('defeat');
}
- else if (n === 0) {
+ else if (attacker.energy.attack === 0) {
attack.cancel();
attack.emit('failure');
}
- }, self.delay);
+ }, this.delay);
attack.cancel = function () { clearInterval(iv) };
- self.on('defeat', function () { attack.cancel() });
+ this.on('defeat', function () { attack.cancel() });
return attack;
};
Please sign in to comment.
Something went wrong with that request. Please try again.