-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
83 lines (65 loc) · 2.06 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
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const bodyParser = require('body-parser');
const path = require('path');
const port = process.env.PORT || 3000;
const index = require("./routes/index");
const app = express();
// app.use(index);
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.use(express.static(path.join(__dirname, 'build')));
app.use(bodyParser.json());
const fs = require("fs");
const cmd = require('node-cmd');
const crypto = require('crypto');
const onWebhook = (req, res) => {
let hmac = crypto.createHmac('sha1', process.env.SECRET);
let sig = `sha1=${hmac.update(JSON.stringify(req.body)).digest('hex')}`;
if (req.headers['x-github-event'] === 'push' && sig === req.headers['x-hub-signature']) {
cmd.run('chmod 777 ./git.sh');
cmd.run('./git.sh', (err, data) => {
if (data) {
console.log(data);
}
if (err) {
console.log(err);
}
})
cmd.run('refresh');
}
return res.sendStatus(200);
}
app.post('/git', onWebhook);
const registerJoinMenuHandlers = require("./src/server/handlers/joinMenu");
const registerGameHandlers = require("./src/server/handlers/game");
const { context } = require("./src/server/context");
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
io.on("connection", (socket) => {
const token = socket.handshake.auth.token.toString()
console.log(`New client connected ${token}`);
registerJoinMenuHandlers(io, socket);
registerGameHandlers(io, socket);
socket.on("disconnect", () => {
console.log(`Client disconnected ${token}`);
for (var game of Object.values(context.games)) {
if (game.host.handshake.auth.token == token) {
game.delete();
delete context.games[game.gameID];
break;
}
if (game.clients[token]) {
game.getPlayer(token).disconnected = true;
}
}
});
});
server.listen(port, () => console.log(`Listening on port ${port}`));