This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
139 lines (99 loc) · 3.43 KB
/
server.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const config = require('./config');
const Koa = require('koa');
const serve = require('koa-static');
const debug = require('debug')('snake');
const assert = require('assert');
const crypto = require('crypto');
const axios = require('axios');
const app = new Koa();
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);
const redis = require('./lib/redis');
const Grid = require('./lib/Grid');
const Snake = require('./lib/Snake');
const Food = require('./lib/Food');
const detectCollisions = require('./lib/detectCollisions');
const coinhive = require('./lib/coinhive');
app.use(serve(`${__dirname}/public`));
const grid = new Grid(50);
const blockHashes = 250;
io.on('connection', socket => {
socket.on('init', async privateKey => {
const publicKey = (() => {
const hash = crypto.createHash('sha256');
hash.update(privateKey, 'hex');
return hash.digest('hex');
})();
console.log({ privateKey, publicKey });
const snake = new Snake(grid.getRandomEmptyBlock());
snake.publicKey = publicKey;
grid.addEntity(snake);
socket.emit('snakeId', snake.id);
socket.emit('entities', [ ...grid.entities ]);
socket.on('direction', direction => {
snake.lastInput = Date.now();
snake.direction = direction;
});
socket.on('disconnect', async () => {
await snake.die();
grid.removeEntity(snake);
});
socket.emit('mining-id', process.env.COINHIVE_SITE_KEY);
socket.on('add-block', async () => {
snake.appendBlockFromBalance();
});
socket.on('remove-block', async () => {
snake.popBlock();
});
snake.on('tick', async () => {
socket.emit('balance', Math.floor(await redis.get(`balance:${publicKey}`) / blockHashes || 0))
});
socket.on('update-balance', async () => {
const { success, balance } = await coinhive.getBalance(publicKey);
if(success !== true)
return;
await coinhive.withdraw(publicKey, balance);
await redis.incrby(`balance:${publicKey}`, balance);
});
socket.on('withdraw-zcash', async address => {
const hashes = await redis.getset(`balance:${publicKey}`, 0);
debug('address', address);
debug(`Withdrawing ${hashes} hashes`);
try {
const ticker = (await axios.get('https://poloniex.com/public?command=returnTicker')).data;
const xmrPrice = ticker['USDT_XMR'].last;
const zecPrice = ticker['USDT_ZEC'].last;
debug('XMR', xmrPrice);
debug('ZEC', zecPrice);
const { difficulty, last_reward } = (await axios.get('https://moneroblocks.info/api/get_stats')).data;
debug('difficulty', difficulty);
debug('last_reward', last_reward);
const xmr = last_reward / difficulty * hashes / Math.pow(10, 12);
const usd = xmr * xmrPrice;
const zec = usd / zecPrice;
debug('$ (XMR)', xmr);
debug('$ (USD)', usd);
debug('$ (ZEC)', zec);
await axios.get(`${process.env.ZFAUCET_URL}/api/external/withdraw`, {
params: {
key: process.env.ZFAUCET_PRIVATE_KEY,
address,
amount: zec
}
});
} catch(err) {
debug(err);
debug('Adding hashes back to balance');
await redis.incrby(`balance:${publicKey}`, hashes);
}
});
});
});
const fps = 5;
setInterval(async () => {
await grid.tick();
io.emit('entities', [ ...grid.entities ]);
io.emit('server-balance', (await redis.get('server-balance')) / blockHashes);
io.emit('server-food', (await redis.get('server-food')) / blockHashes);
}, 1000 / fps);
server.listen(process.env.SERVER_PORT || 3000);