-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.mjs
142 lines (134 loc) · 4.13 KB
/
server.mjs
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
/**
* @file server.js
* The entry point for the WebWars server.
* Starts the server that runs the game and serves assets.
*/
import { rmSync } from "node:fs";
import { join } from "node:path";
import commandLineArgs from "command-line-args";
import commandLineUsage from "command-line-usage";
import esMain from "es-main";
import { logLevels, defaultLogLevel, setLogLevel, setLogFilepath } from "#src/logging/logger.mjs";
import Controller from "#src/mvc/controller.mjs";
import FrontEndData from "#src/models/frontEndData.mjs";
import MapManager from "#src/models/mapManager.mjs";
export const optionDefinitions = [
// MARK: Generic
{
name: "help",
alias: "h",
type: Boolean,
description: "Prints this usage guide then exits",
typeLabel: "",
},
// MARK: Server
{
name: "port",
alias: "p",
type: Number,
defaultValue: 80,
description: "The port to run the game server on (default: 80)",
typeLabel: "{underline number}",
},
// MARK: Logging
{
name: "log-level",
alias: "l",
type: String,
defaultValue: defaultLogLevel,
description: `The level to log at, out of: ${logLevels
.map((level, i) => `${level} or ${i}`)
.join(", ")} (default: ${defaultLogLevel})`,
typeLabel: "{underline level string or number}",
},
{
name: "log-file",
alias: "f",
type: String,
defaultValue: "logs/WebWars.log",
description: "The log file to write to (default: logs/WebWars.log)",
typeLabel: "{underline filepath}",
},
{
name: "no-log-file",
alias: "n",
type: Boolean,
description: "Prevents logs from being output to a file",
typeLabel: "",
},
{
name: "keep-log-file",
alias: "k",
type: Boolean,
description:
"The game will delete any existing log file on start-up. Specify this option to append to the log file " +
"instead. No log file will ever be deleted if --no-log-file is specified.",
typeLabel: "",
},
// MARK: Game
{
name: "map-pack",
alias: "m",
type: String,
defaultValue: "./default-map-pack",
description: "The path to the map pack to load on start-up (default: ./default-map-pack)",
typeLabel: "{underline folder path}",
},
];
export const usageSections = [
{
header: "WebWars",
content: "Web-based implementation of a moddable Advance Wars engine",
},
{
header: "Options",
optionList: optionDefinitions,
},
];
/**
* Parses the command-line arguments given by the user and returns them.
* If the help argument is given, the usage documentation will be printed, and the game will close.
* @returns {Object} The options provided by the user.
*/
export function getCommandLineArguments() {
const options = commandLineArgs(optionDefinitions);
if (options.help) {
console.log(commandLineUsage(usageSections));
process.exit();
}
setLogLevel(options["log-level"]);
if (!options["no-log-file"]) {
if (!options["keep-log-file"]) {
rmSync(options["log-file"], { force: true });
}
setLogFilepath(options["log-file"]);
}
return options;
}
// Don't run the actual game if we're importing this module.
if (esMain(import.meta)) {
const options = getCommandLineArguments();
new Controller({
port: options["port"],
files: [
{
path: "WebWars.html",
root: join(import.meta.dirname, "public"),
url: "/",
},
],
folders: [
{
path: join(import.meta.dirname, "shared"),
url: "/",
},
{
path: join(import.meta.dirname, "public"),
url: "/",
},
],
onServerUp: port => console.log(`Open http://localhost:${port} in your browser to open the game!`),
models: new Set([FrontEndData, MapManager]),
mapPackPath: options["map-pack"],
});
}