-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
86 lines (72 loc) · 2.04 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
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
const http = require("http");
const path = require("path");
const querystring = require("querystring");
require("dotenv").config();
const { broadcastGameUpdate, socketize, getBytes } = require("./lib/socket");
const { prettykBs } = require("./utils/byteSizeHelpers");
const {
startGame,
finishGame,
resetGame,
writeHistoryToFile,
setMaxPlayers,
setMaskWhiteboards,
setShowNames,
} = require("./lib/game");
const server = http.createServer(app);
socketize(server);
const PORT = process.env.PORT || 5000;
app.use(express.static(`${__dirname}/build`));
// Shitty logger
app.use((req, res, next) => {
console.log("[http]", req.path);
next();
});
app.get("/api/start", (req, res) => {
startGame();
broadcastGameUpdate();
res.send("starting");
});
app.get("/api/finish", (req, res) => {
finishGame();
broadcastGameUpdate();
res.send("finishing");
});
app.get("/api/bytes", (req, res) => {
const [sent, received, averageSent] = getBytes();
res.send(
JSON.stringify(
`total sent: ${prettykBs(sent)}kB av sent/player: ${prettykBs(
averageSent
)}kB total received: ${prettykBs(received)}kB`
)
);
});
app.get("/api/writehistory", async (req, res) => {
await writeHistoryToFile();
res.send("[http] file written");
});
app.post("/api/updateconfig", (req, res) => {
const { maxplayers, mask, names } = req.body;
setMaxPlayers(JSON.parse(maxplayers));
setMaskWhiteboards(JSON.parse(mask));
setShowNames(JSON.parse(names));
const query = querystring.stringify({
maxplayers,
mask,
names,
});
broadcastGameUpdate();
res.redirect("/admin?" + query);
});
app.get("/admin", (req, res) => {
res.sendFile(path.join(__dirname, "admin/index.html"));
});
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build/index.html"));
});
server.listen(PORT, () => console.log(`Server listening on port ${PORT}`));