-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (58 loc) · 1.94 KB
/
app.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
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require("socket.io")(http);
var redis = require("redis");
var client = redis.createClient(process.env.REDIS_URL || null);
client.on('connect',()=>{
client.set('game:scoreToWin',10);
client.set('game:team-1-score',5);
client.set('game:team-2-score',5);
})
app.use('/public', express.static(__dirname + '/public'));
app.get("/", (req,res) => {
res.sendFile(__dirname + '/public/index.html');
})
app.get("/team1", (req,res) => {
res.sendFile(__dirname + '/public/team1.html');
})
app.get("/team2", (req,res) => {
res.sendFile(__dirname + '/public/team2.html');
})
io.on("connection",(socket) => {
getCurrentScore( (currentScore)=>io.sockets.emit('update score', currentScore) )
socket.on("keypress",() => {
getCurrentScore( (gameData) => {
if(parseInt(gameData.scoreA) >= parseInt(gameData.scoreToWin)){
io.sockets.emit('game end',"team a")
}
else if (parseInt(gameData.scoreB) >= parseInt(gameData.scoreToWin)) {
io.sockets.emit('game end',"team b")
}
});
});
socket.on("team 1 press",()=>{
client.incr('game:team-1-score');
client.decr('game:team-2-score')
getCurrentScore( (currentScore)=>io.sockets.emit('update score', currentScore) )
})
socket.on("team 2 press",()=>{
client.incr('game:team-2-score');
client.decr('game:team-1-score')
getCurrentScore( (currentScore)=>io.sockets.emit('update score', currentScore) )
})
})
function getCurrentScore(callback){
let gameData = {};
client.get("game:scoreToWin", (err,score) => {
gameData.scoreToWin = score;
client.get('game:team-1-score', (err,scoreA) => {
gameData.scoreA = scoreA;
client.get('game:team-2-score', (err,scoreB) => {
gameData.scoreB = scoreB;
callback(gameData);
});
});
});
}
http.listen(process.env.PORT|| 8000,()=>console.log('working on 8000'));