-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (41 loc) · 981 Bytes
/
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
const http = require('http');
const socketio = require('socket.io');
const fs = require('fs');
const handler = (req, res) => {
fs.readFile(`${__dirname}/index.html`, (err, data) => {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
};
const app = http.createServer(handler);
const io = socketio(app);
let playerOneScore = 0;
let playerTwoScore = 0;
io.on('connection', (socket) => {
const broadcastScore = () => {
io.emit('score', {
playerOne: playerOneScore,
playerTwo: playerTwoScore,
});
};
broadcastScore();
socket.on('tug', (data) => {
const team = data.team;
if (team === '1') {
playerOneScore += 1;
} else if (team === '2') {
playerTwoScore += 1;
};
broadcastScore();
});
socket.on('reset', () => {
playerOneScore = 0;
playerTwoScore = 0;
broadcastScore();
});
});
app.listen(8181);