-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplayBroadcaster.js
48 lines (43 loc) · 1.36 KB
/
ReplayBroadcaster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var Primus = require('primus');
var WebSocket = require('ws');
var maxmind = require('maxmind');
var fs = require('fs');
var ReplayBroadcaster = module.exports = function (server) {
this.primus = new Primus(server, {
transformer: 'engine.io',
parser: 'JSON'
});
this.txs = JSON.parse(fs.readFileSync('tx.json'));
/*console.log(this.txs.reduce(function (memo, tx) {
return memo + tx.out.reduce(function (memo, out, n) {
if (n == tx.out.length - 2) {
return memo;calc
}
return memo + parseInt(out.value, 10);
}, 0);
}, 0));*/
maxmind.init('./geolite/GeoIPCity.dat');
}
ReplayBroadcaster.prototype.start = function () {
setTimeout(function () {
var interval = setInterval(function () {
if (this.txs.length === 0) {
return clearInterval(interval);
}
var tx = this.txs.pop();
var location = maxmind.getLocation(tx.relayed_by);
if (!location) return;
var payload = {
latitude: location.latitude,
longitude: location.longitude,
city: location.city,
country: location.countryCode,
amount: tx.out.reduce(function (memo, out) {
return memo + parseInt(out.value, 10);
}, 0) / 100000000
};
console.log('SENDING', payload);
this.primus.write(payload);
}.bind(this), process.argv[3] || 5);
}.bind(this), 5000);
};