-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
254 lines (216 loc) · 6.99 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* MIT License
*
* Copyright (c) 2022 Nils Marten Mikk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* eslint-env node, es2021 */
const express = require("express");
const http = require("http");
const path = require("path");
const fs = require("fs");
const websocket = require("ws");
const router = require("./routes/index");
const Game = require("./game");
const messages = require("./public/javascripts/messages");
const types = require("./public/javascripts/types");
const stats = require("./stats");
/** @typedef {import("./public/javascripts/messages").MessageObject} MessageObject} */
/** @typedef {import("./game").MoveResult} MoveResult} */
// Get config file
const config = (() =>
{
try
{
return JSON.parse(fs.readFileSync(path.join(__dirname, "config.json")).toString());
}
catch (err)
{
console.error(`Could not open config file. ${err.message}`);
}
return {};
})();
/**
* Validates the port number. If invalid returns an alternative port number.
*
* @param {number} port Port number
* @param {number=} alt Alternative port number
* @returns {number} A valid port number
*/
function validatePort(port, alt = 3000)
{
return port > 0 && port <= 65535 ? port : alt;
}
// Set port numbers
const httpPort = validatePort(config.httpPort, 8080);
const httpsPort = validatePort(config.httpsPort, 8443);
const app = express();
app.disable("x-powered-by");
app.set("view engine", "ejs");
// Create HTTP server
const httpServer = http.createServer(app);
// Create HTTPS server
let httpsServer = null;
if (config.https === true)
{
const https = require("https");
let options;
try
{
options =
{
key: fs.readFileSync(config.key),
cert: fs.readFileSync(config.cert)
};
}
catch (err)
{
console.log(`Could not open certificate file. ${err.message}\nAdd certificate or disable https in config.json`);
process.exit(1);
}
httpsServer = https.createServer(options, app);
// Redirect http to https
app.use((req, res, next) =>
{
if (req.secure)
next();
else res.redirect(`https://${req.hostname}${req.path}`);
});
}
app.use(express.static(path.join(__dirname, "public")));
// Get statistics
app.get("/statistics", (req, res) =>
{
const statistics = new messages.O_STATISTICS(stats.getPlayersOnline(), stats.getGamesPlayed(), stats.getTimeSpent());
res.json(statistics);
});
app.use("/", router);
// Not found
app.use((req, res) =>
{
res.status(404);
res.render("error",
{
code: "404",
color: "#d5996e",
message: "Resource not found"
});
});
// Server error
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) =>
{
res.status("500");
res.render("error",
{
code: "500",
color: "#6ebbd5",
message: "Server error"
});
});
// ---------- WebSocket ---------- //
const wss = new websocket.Server({ server: (config.https === true ? httpsServer : httpServer) });
let currentGame = new Game();
// On websocket connection received
wss.on("connection", (ws) =>
{
// Update players online
stats.addPlayer();
const game = currentGame;
// Set the player and opponent type
const playerType = game.addPlayer(ws);
const opponentType = playerType === types.PLAYER_1 ? types.PLAYER_2 : types.PLAYER_1;
// On websocket message received
ws.on("message", (msg) =>
{
// Parse message
/** @type {MessageObject} Message object*/
const msgObj = JSON.parse(msg.toString());
// Player sends their username
if (msgObj.type === messages.T_JOIN)
{
// Set the username
game.setName(playerType, msgObj.user.substr(0, 10));
// If all players have joined, send game info to players
if (game.isFull())
{
currentGame = new Game(); // Create a new game for next players
game.start();
ws.send(JSON.stringify(game.getInfo(playerType)));
game.getSocket(opponentType).send(JSON.stringify(game.getInfo(opponentType)));
// Update games played
stats.addGame();
}
}
// Player sends a move
if (msgObj.type === messages.T_MOVE)
{
try
{
/** @type {MoveResult} Move result */
const moveResult = game.move(playerType, msgObj.column);
const opponent = game.getSocket(opponentType);
// Normal move
const message = JSON.stringify(new messages.O_MOVE(msgObj.column, moveResult.row, playerType, moveResult.result));
ws.send(message);
opponent.send(message);
// If game over, close connections
if (moveResult.result !== null)
{
ws.close();
opponent.close();
// Update total time
stats.addTimeSpent(Date.now() - game.getStart());
}
}
catch (err)
{
console.error(err.message);
}
}
});
// On connection closed
ws.on("close", (code) =>
{
const opponent = game.getSocket(opponentType);
game.clearSocket(playerType);
ws.close();
if ((code === 1001 || code === 1006) && opponent !== null)
{
// Update total time
stats.addTimeSpent(Date.now() - game.getStart());
opponent.send(JSON.stringify(messages.O_QUIT));
opponent.close();
}
// Update players online
stats.removePlayer();
});
});
httpServer.listen(httpPort, () =>
{
console.log(`HTTP server listening on port ${httpPort}.`);
});
if (httpsServer !== null)
{
httpsServer.listen(httpsPort, () =>
{
console.log(`HTTPS server listening on port ${httpsPort}`);
});
}