-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (54 loc) · 1.84 KB
/
index.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
'use strict';
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const Cucumber = require('./cucumber');
const fs = require('fs');
let thrownCucumbers;
io.on('connection', function (client) {
client.on('join', function (data) {
for (let i = 0; i < Cucumber._getRandomInt(5, 15); i++) {
const x = Cucumber._getRandomInt(50, data.windowWidth - 50) / data.windowWidth;
client.emit('throwCucumber', Cucumber.create(x, Cucumber._getRandomInt(20, 50)));
}
sendInfo(client);
});
client.on('throwCucumber', function (data) {
thrownCucumbers++;
io.emit('throwCucumber', Cucumber.create(data.x, data.y));
sendInfo(io);
});
client.on('disconnect', function(data) {
sendInfo(io);
});
client.on('timeout', function(){
console.log("client.timeout")
});
});
io.on('timeout', function(){
console.log("io.timeout")
});
setInterval(() => {
for(let i = 0; i < Cucumber._getRandomInt(1, 5); i++) {
io.emit('throwCucumber', Cucumber.create(Math.random(), Cucumber._getRandomFloat(20, 50)))
}
}, 20000);
app.use(express.static('public'));
server.listen(3000, function () {
console.log('Example app listening on port 3000!');
fs.readFile('./db/cucumber.json', 'UTF8', (err, data) => {
if(err) throw err;
thrownCucumbers = JSON.parse(data).thrownCucumbers;
})
});
function sendInfo(client) {
client.emit('info', {
thrownCucumbers: thrownCucumbers,
connectedUsers: Object.keys(io.sockets.sockets).length
});
writeThrownCucumbers(thrownCucumbers);
}
function writeThrownCucumbers(thrownCucumbers){
fs.writeFile('./db/cucumber.json', JSON.stringify({thrownCucumbers: thrownCucumbers}), (err) => {});
}