This repository was archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhoster.js
More file actions
164 lines (134 loc) · 3.69 KB
/
hoster.js
File metadata and controls
164 lines (134 loc) · 3.69 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
const fs = require("fs");
const rw = require("./reader_writer.js");
const config = require("./config.json");
const gameInterface = require("./game_interface.js");
var reservedPorts = [];
module.exports.reservePort = function(cb)
{
var reservedPort = config.gamePortRange.first.toString();
var usedPorts = gameInterface.getUsedPorts().concat(reservedPorts);
while (usedPorts.includes(reservedPort.toString()) === true)
{
reservedPort++;
if (reservedPort > config.gamePortRange.last)
{
cb(`There are no free ports.`);
return;
}
}
reservedPorts.push(reservedPort);
cb(null, reservedPort, config.ip);
};
module.exports.releasePort = function(port)
{
reservedPorts.splice(reservedPorts.indexOf(port), 1);
};
module.exports.checkGameName = function(id, name, gameType, cb)
{
try
{
validateNameFormat(name);
}
catch(err)
{
cb(err, null);
return;
}
if (gameInterface.isGameNameUsed(name, gameType) === true)
{
rw.log("general", `validateName() Error: This name is already used by a different game. Input was: ${name}`);
cb(`The game name ${name} is already used by a different game. Please choose one that's free.`);
}
//send back the first cue of the assisted hosting so that the user
//can start picking settings
cb(null);
};
module.exports.validateMapfile = function(mapfile, gameType, cb)
{
var path;
switch(gameType)
{
case "dom4":
path = config.dom4DataPath;
break;
case "dom5":
path = config.dom5DataPath;
break;
default:
cb("The game type is incorrect. Cannot determine the path to validate the map.", null);
return;
}
if (fs.existsSync(`${path}/maps/${mapfile}`) === false)
{
cb("The map file could not be found.");
return;
}
cb(null, fs.readdirSync(`${path}/maps`).find(function(map)
{
return mapfile.toLowerCase() === map.toLowerCase();
}));
};
module.exports.validateMod = function(mod, gameType, cb)
{
var path;
switch(gameType)
{
case "dom4":
path = config.dom4DataPath;
break;
case "dom5":
path = config.dom5DataPath;
break;
default:
cb("The game type is incorrect. Cannot determine the path to validate the mods.", null);
return;
}
if (fs.existsSync(`${path}/mods/${mod}`) === false)
{
cb(`The mod file ${mod} could not be found.`);
return;
}
cb(null);
};
module.exports.releaseAllPorts = function()
{
reservedPorts = [];
rw.log("general", `Released reserved assistted hosting instance ports.`);
};
function reservePort()
{
var reservedPort = config.gamePortRange.first.toString();
var usedPorts = gameInterface.getUsedPorts().concat(reservedPorts);
while(usedPorts.includes(reservedPort.toString()) === true)
{
reservedPort++;
if (reservedPort > config.gamePortRange.last)
{
return null;
}
}
return reservedPort;
}
function validateNameFormat(name)
{
if (name == null)
{
rw.log("general", `validateName() Error: Game name is null. Input was: ${name}`);
throw "Game name MUST be specified.";
}
if (name.length > 24)
{
rw.log("general", `validateName() Error: Game name ${name} is too long. Input was: ${name}`);
throw `Game name ${name} is too long. It must be within 24 characters.`;
}
if (/[^0-9a-zA-Z_~]/.test(name) === true)
{
rw.log("general", `validateName() Error: Invalid characters. Input was: ${name}`);
throw `The game name ${name} contains invalid characters. Only letters, numbers and underscores are allowed.`;
}
if (name === "dom4" || name === "dom5" || name === "coe4")
{
rw.log("general", `validateName() Error: Reserved keyword. Input was: ${name}`);
throw "This is a reserved keyword, please choose a different one.";
}
}