-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathostracodMultiplayer.js
More file actions
194 lines (171 loc) · 7.06 KB
/
ostracodMultiplayer.js
File metadata and controls
194 lines (171 loc) · 7.06 KB
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
const express = require("express");
const pathUtils = require("path");
const favicon = require("serve-favicon");
const logger = require("morgan");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const mustacheExpress = require("mustache-express");
const session = require("express-session");
const http = require("http");
const https = require("https");
const fs = require("fs");
const expressWs = require("express-ws");
class OstracodMultiplayer {
constructor() {
this.expressApp = null;
this.mode = null;
this.basePath = null;
this.configDirectory = null;
this.serverConfig = null;
this.gameConfig = null;
this.consumerViewsDirectory = null;
this.localViewsDirectory = null;
this.gameDelegate = null;
this.server = null;
this.shouldListenImmediately = true;
}
listen() {
const portNumber = this.serverConfig.port;
this.server.listen(portNumber, () => {
console.log("Listening on port " + portNumber + ".");
});
}
initializeServer(basePath, gameDelegate, routerList) {
this.expressApp = express();
this.mode = this.expressApp.get("env");
this.basePath = basePath;
this.gameDelegate = gameDelegate;
this.configDirectory = pathUtils.join(basePath, "ostracodMultiplayerConfig");
this.serverConfig = JSON.parse(fs.readFileSync(
pathUtils.join(this.configDirectory, "serverConfig.json"),
"utf8",
));
this.gameConfig = JSON.parse(fs.readFileSync(
pathUtils.join(this.configDirectory, "gameConfig.json"),
"utf8",
));
this.localViewsDirectory = pathUtils.join(__dirname, "views");
this.consumerViewsDirectory = pathUtils.join(this.basePath, "views");
for (const module of this.gameConfig.pageModules) {
module.viewPath = pageUtils.getConsumerViewPath(module.viewFile);
delete module.viewFile;
}
this.gameConfig.pageModules.push({
name: "chat",
buttonLabel: "Chat",
title: "Chat",
viewPath: pageUtils.getLocalViewPath("chatModule.html"),
shouldShowOnLoad: false,
});
this.gameConfig.pageModules.push({
name: "onlinePlayers",
buttonLabel: "Players",
title: "Online Players",
viewPath: pageUtils.getLocalViewPath("onlinePlayersModule.html"),
shouldShowOnLoad: true,
});
if (this.mode === "development") {
console.log("WARNING: APPLICATION RUNNING IN DEVELOPMENT MODE!");
console.log("CACHING TURNED OFF");
console.log("ERROR REPORTING TURNED ON");
console.log("DEBUG SPAM TURNED ON");
console.log("SIMULATED LAG TURNED ON");
console.log("AUTHENTICATION TURNED OFF");
console.log("HTTPS TURNED OFF");
this.expressApp.disable("view cache");
this.expressApp.use(logger("dev"));
} else if (this.mode === "production") {
console.log("Application running in production mode.");
} else {
console.log("WARNING: UNRECOGNIZED APPLICATION MODE! (" + this.mode + ")");
console.log("PLEASE USE \"development\" OR \"production\"");
return false;
}
if (this.mode === "development") {
this.server = http.createServer(this.expressApp);
} else {
const privateKey = fs.readFileSync(
pathUtils.join(this.configDirectory, "ssl.key"), "utf8",
);
const certificate = fs.readFileSync(
pathUtils.join(this.configDirectory, "ssl.crt"), "utf8",
);
const credentials = { key: privateKey, cert: certificate };
const tempPath = pathUtils.join(this.configDirectory, "ssl.ca-bundle");
if (fs.existsSync(tempPath)) {
credentials.ca = fs.readFileSync(tempPath);
}
this.server = https.createServer(credentials, this.expressApp);
}
expressWs(this.expressApp, this.server);
this.expressApp.set("views", this.localViewsDirectory);
this.expressApp.engine("html", mustacheExpress());
let sessionSecret;
if ("secret" in this.serverConfig) {
sessionSecret = this.serverConfig.secret;
} else {
const secretPath = pathUtils.join(this.configDirectory, "sessionSecret.txt");
if (fs.existsSync(secretPath)) {
sessionSecret = fs.readFileSync(secretPath, "utf8");
} else {
sessionSecret = "";
while (sessionSecret.length < 30) {
sessionSecret += Math.floor(Math.random() * 10);
}
fs.writeFileSync(secretPath, sessionSecret);
}
}
const faviconPath = pathUtils.join(this.configDirectory, "public", "favicon.ico");
if (fs.existsSync(faviconPath)) {
this.expressApp.use(favicon(faviconPath));
}
this.expressApp.use(bodyParser.json());
this.expressApp.use(bodyParser.urlencoded({ extended: false }));
this.expressApp.use(cookieParser());
this.expressApp.use(express.static(pathUtils.join(__dirname, "public")));
this.expressApp.use(express.static(pathUtils.join(basePath, "public")));
this.expressApp.set("trust proxy", 1);
this.expressApp.use(session({
secret: sessionSecret,
resave: false,
saveUninitialized: true,
cookie: { maxAge: 24 * 60 * 60 * 1000 },
}));
const router = require("./router");
this.expressApp.use("/", router);
for (const router of routerList) {
this.expressApp.use("/", router);
}
// Catch 404 and forward to error handler.
this.expressApp.use((req, res, next) => {
const tempError = new Error("Not Found");
tempError.status = 404;
next(tempError);
});
// Error handler.
// eslint-disable-next-line no-unused-vars
this.expressApp.use((error, req, res, next) => {
const tempParameters = { message: error.message };
if (this.mode === "development") {
tempParameters.error = error;
}
if (error.status) {
res.status(error.status);
} else {
res.status(500);
}
res.render("error.html", tempParameters);
});
dbUtils.initialize();
gameUtils.initialize();
if (this.shouldListenImmediately) {
this.listen();
}
return true;
}
}
const ostracodMultiplayer = new OstracodMultiplayer();
module.exports = { ostracodMultiplayer };
const { dbUtils } = require("./dbUtils");
const { pageUtils } = require("./pageUtils");
const { gameUtils } = require("./gameUtils");