This repository has been archived by the owner on Feb 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
577 lines (511 loc) · 16 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
const express = require('express');
const fs = require('fs');
const bodyParser = require('body-parser');
const app = express();
const csv = require('fast-csv');
const StatsManager = require('./scripts/stats').StatsManager;
const PORT = 8000;
var defaultMap, settings;
var users, logins, anyChanges, turnChangeTime;
var gameStarted;
var statsManager = new StatsManager();
function userAttemptAdminError(command) {
log(`User attempted admin command ${command}`);
}
function makeError(error) {
return {error: error};
}
function createUnit(units, loc, type, user, hp, delayed) {
var id;
if (units) {
id = units[units.length - 1].id + 1;
} else {
id = 0;
}
var unit = {
id: id,
loc: loc,
type: type,
user: user,
hp: hp
};
if (type == "Carrier") {
unit.airfieldId = id+1000;
}
if (gameStarted && delayed) {
unit.deployTime = parseInt(statsManager.getProperties(type)["Turns to Deploy"]);
} else {
unit.deployTime = 0;
}
var refuelTime = statsManager.getProperties(type)["Turns to refuel"];
if (refuelTime != "n/a") {
unit.fuelLeft = parseInt(refuelTime);
}
return unit;
}
function restrictMapView(mapJSON, user) {
var units = [];
for (var unit of mapJSON.units) {
if (unit.user == user && unit.deployTime == 0) {
units.push(unit);
var range = statsManager.getProperties(unit.type)["Vision"]*1000;
for (var unit2 of mapJSON.units) {
if (unit2 != unit) {
var [unit1x, unit1y] = unit.loc;
var [unit2x, unit2y] = unit2.loc;
if (Math.hypot(unit1x-unit2x, unit1y-unit2y) <= range && unit2.deployTime == 0) {
units.push(unit2);
}
}
}
}
}
mapJSON.units = units;
return mapJSON;
}
function getUnitById(units, id) {
for (var unit of units) {
if (unit.id == id) {
return unit;
}
}
return null;
}
function startGame() {
gameStarted = true;
turnChangeTime[users[0]] = (new Date()).getTime() + settings.turnTime;
turnChangeTime[users[1]] = (new Date()).getTime() + settings.turnTime * 2;
}
function getAirfieldById(mapJSON, airfieldId) {
for (var airfield of mapJSON.airfields) {
if (airfield.id == airfieldId) {
return airfield;
}
}
return null;
}
function moveAirfield(mapJSON, airfieldId, loc) {
getAirfieldById(mapJSON, airfieldId).loc = loc;
}
function attemptMove(mapJSON, id, newLocation) {
changeOccured();
for (var unit of mapJSON.units) {
if (unit.id == id) {
if (unit.type == "Carrier") {
moveAirfield(mapJSON, unit.airfieldId, newLocation);
}
unit.loc = newLocation;
return;
}
}
log(`Invalid move made: id ${id} not found`);
}
function exitAirfield(mapJSON, airfieldId, unitId) {
changeOccured();
var airfield = getAirfieldById(mapJSON, airfieldId);
var unitToDelete = -1;
for (var unitId_ in airfield.units) {
var unit = airfield.units[unitId_];
if (unit.id == unitId) {
unitToDelete = unit;
mapJSON.units.push(createUnit(mapJSON.units, airfield.loc, unit.type, unit.user, unit.hp, false));
break;
}
}
if (unitToDelete == -1) {
log(`Invalid unit (${unitId}) deletion from airfield ${airfieldId}`);
} else {
deleteUnit(airfield, unitToDelete.id)
}
}
function hasCompatAirfieldAffil(airfield, user) {
if (airfield.units.length == 0) {
return true;
} else {
return airfield.units[0].user == user;
}
}
function getNearestAirfield(mapJSON, unit) {
var user = unit.user;
var loc = unit.loc;
var airfieldFound = false;
var searchRadius = 0;
var closestAirfield = null;
var closestAirfieldDistance = null;
for (var airfield of mapJSON.airfields) {
if (hasCompatAirfieldAffil(airfield, user)) {
var d = Math.hypot(airfield.loc[0] - loc[0], airfield.loc[1] - loc[1]);
if (closestAirfieldDistance == null || d < closestAirfieldDistance) {
closestAirfield = airfield;
closestAirfieldDistance = d;
}
}
}
return closestAirfield;
}
function returnToAirfield(mapJSON, unitId) {
var unit = getUnitById(mapJSON.units, unitId);
var airfield = getNearestAirfield(mapJSON, unit);
if (airfield != null){
unit.loc = null;
changeOccured();
deleteUnit(mapJSON, unitId);
airfield.units.push(unit);
} else {
log(`Failed return to airfield as no airfield was found`);
changeOccured();
deleteUnit(mapJSON, unitId);
}
}
function deleteUnit(unitContainer, id) {
unitContainer.units = unitContainer.units.filter(u => u.id != id);
}
function handleSync(reqBody, mapJSON) {
var changes = reqBody.changes;
var response = new Object();
response.notifications = notifications[reqBody.username];
notifications[reqBody.username] = [];
if (reqBody.username == "admin" || changes.length==0) {
for (var change of changes) {
// Admin changes
switch (change.type) {
case "add":
changeOccured();
mapJSON.units.push(createUnit(mapJSON.units, change.loc, change.unitType, change.user, 100, true));
adminNotification(`Added a ${change.unitType} for ${change.user}`);
break;
case "move":
attemptMove(mapJSON, change.unitId, change.newLocation);
break;
case "delete":
changeOccured();
var unit = getUnitById(mapJSON.units, change.unitId);
if (unit == null) {
log(`Invalid delete made: id ${change.unitId} not found`);
} else {
deleteUnit(mapJSON, change.unitId);
}
break;
case "setTurnTime":
settings.turnTime = change.time*1000;
break;
case "startTurnChanging":
mapJSON.gameStarted = true;
startGame();
break;
case "reset":
fs.copyFile('data/map.json', 'data/backup-map.json', (err) => {
if (err) throw err;
});
defaultMap = fs.readFileSync('default-map.json');
defaultMapJSON = JSON.parse(defaultMap);
mapJSON = defaultMapJSON;
updateTurnChangeTimeFromFile(mapJSON.turnChangeTime);
gameStarted = mapJSON.gameStarted;
init()
break;
case "returnToAirfield":
returnToAirfield(mapJSON, change.unitId);
break;
case "exitAirfield":
exitAirfield(mapJSON, change.airfieldId, change.unitId);
break;
default:
log(`Unusual change requested: ${change.type}`);
}
}
updateTurnChangeTimeInFile(mapJSON.turnChangeTime);
var mapRaw = JSON.stringify(mapJSON);
response.turnTime = settings.turnTime;
response.anyChanges = anyChanges[reqBody.username];
response.usersList = users;
if (firstSync[reqBody.username] || reqBody.firstSync) {
firstSync[reqBody.username] = false;
response.anyChanges = true;
response.statsData = statsManager.getData();
}
if (reqBody.username == "admin") {
response.mapState = mapJSON;
} else {
checkForMissingUsers(mapJSON);
response.mapState = restrictMapView(mapJSON, reqBody.username);
response.nextTurnChange = turnChangeTime[reqBody.username];
response.isCorrectTurn = getNextTurnUser() == reqBody.username;
}
// log(`time: ${(new Date()).getTime()}, Blufor: ${turnChangeTime[users[0]]}, Opfor: ${turnChangeTime[users[1]]}`)
fs.writeFileSync('data/map.json', mapRaw);
} else {
userAttemptAdminError(change.type);
response = makeError("Error: User attempted admin move");
}
return response;
}
function advanceTurnTimer(mapJSON, offset) {
var previousUser;
if (offset === undefined) {
offset = 0;
}
var usersCount = users.length;
var nextUser = getNextTurnUser();
for (var unit of mapJSON.units) {
if (unit.user == nextUser) {
if (unit.deployTime > 0) {
unit.deployTime--;
if (unit.deployTime == 0) {
anyChanges[nextUser] = true;
}
}
if (unit.fuelLeft != null) {
if (unit.fuelLeft == 0) {
addNotification([notifications[unit.user]], `A ${unit.type} was returned to the nearest airfield.`);
returnToAirfield(mapJSON, unit.id)
}
unit.fuelLeft -= 1;
}
}
}
var nextUserIndex = users.indexOf(nextUser);
var i = (nextUserIndex+1) % usersCount;
turnChangeTime[users[i]] = (new Date()).getTime()+parseInt(settings.turnTime) + offset;
while (i != nextUserIndex) {
previousUser = users[i];
i = (i+1) % usersCount;
turnChangeTime[users[i]] = turnChangeTime[previousUser]+parseInt(settings.turnTime);
}
updateTurnChangeTimeInFile(mapJSON.turnChangeTime);
}
function checkForMissingUsers(mapJSON) {
if (gameStarted) {
var t = (new Date()).getTime();
var u = getNextTurnUser();
if (turnChangeTime[u] + 2000 < t){
advanceTurnTimer(mapJSON, -2000);
}
}
}
function getLastTurnUser() {
if (!gameStarted) {
return "";
}
var lastUser = users[0];
for (var u of users) {
if (turnChangeTime[u] > turnChangeTime[lastUser]) {
lastUser = u;
}
}
return lastUser;
}
function getNextTurnUser() {
if (!gameStarted) {
return "";
}
var nextUser = users[0];
for (var u of users) {
if (turnChangeTime[u] < turnChangeTime[nextUser]) {
nextUser = u;
}
}
return nextUser;
}
function attemptAttack(attacker, defender) {
var dodgeChance = statsManager.getDodgeChance(attacker.type, defender.type);
if (Math.random()*100>=dodgeChance) {
defender.hp -= statsManager.getAttackStrength(attacker.type, defender.type);
attacker.hp -= statsManager.getDefenceStrength(attacker.type, defender.type);
return true;
} else {
return false;
}
}
function advanceGameTime(mapJSON) {
mapJSON["currentTime"] += 1;
}
function addNotification(lists, message) {
adminNotification(message);
for (var list of lists) {
list.push(message);
}
}
function adminNotification(message) {
notifications["admin"].push(`[${getLastTurnUser()}] `+message);
}
function handleTurnChange(reqBody, mapJSON) {
var nextUser = getNextTurnUser();
var isCorrectTurn = reqBody.username == nextUser;
var response = new Object();
if (isCorrectTurn) {
if (nextUser == users[users.length-1]) {
advanceGameTime(mapJSON);
}
var d = new Date();
advanceTurnTimer(mapJSON, 0);
var changes = reqBody.changes;
response.notifications = notifications[reqBody.username];
notifications[reqBody.username] = [];
for (var change of changes) {
// User changes
let unit;
switch (change.type) {
case "move":
attemptMove(mapJSON, change.unitId, change.newLocation);
break;
case "attack":
changeOccured();
var attacker = getUnitById(mapJSON.units, change.attackerId);
var defender = getUnitById(mapJSON.units, change.defenderId);
if (attacker == null || defender == null) {
addNotification([response.notifications, notifications[defender.user]], `Attacking ${attacker.type} missed the defending ${defender.type}.`);
} else if (!attemptAttack(attacker, defender)) {
addNotification([response.notifications, notifications[defender.user]], `Attacking ${attacker.type} missed the defending ${defender.type}.`);
} else {
if (attacker.hp <= 0) {
deleteUnit(mapJSON, change.attackerId);
addNotification([response.notifications, notifications[defender.user]], `Attacking ${attacker.type} was killed by the defending ${defender.type}.`);
}
if (defender.hp <= 0) {
deleteUnit(mapJSON, change.defenderId);
addNotification([response.notifications, notifications[defender.user]], `Attacking ${attacker.type} killed the defending ${defender.type}.`);
}
}
break;
case "returnToAirfield":
unit = getUnitById(mapJSON.units, change.unitId);
returnToAirfield(mapJSON, change.unitId);
addNotification([response.notifications], `A ${unit.type} was returned to the nearest airfield.`);
break;
case "exitAirfield":
unit = getUnitById(getAirfieldById(mapJSON, change.airfieldId).units, change.unitId);
exitAirfield(mapJSON, change.airfieldId, change.unitId);
addNotification([response.notifications], `A ${unit.type} exited its airfield.`);
break;
default:
log(`Unusual change requested: ${change.type}`);
}
}
updateTurnChangeTimeInFile(mapJSON.turnChangeTime);
var mapRaw = JSON.stringify(mapJSON);
checkForMissingUsers(mapJSON);
response.nextTurnChange = turnChangeTime[reqBody.username];
response.turnTime = isCorrectTurn;
response.anyChanges = anyChanges[reqBody.username];
response.usersList = users;
if (reqBody.username == "admin") {
response.mapState = mapJSON;
} else {
response.mapState = restrictMapView(mapJSON, reqBody.username);
}
// log(`time: ${(new Date()).getTime()}, test1: ${turnChangeTime.test1}, test2: ${turnChangeTime.test2}`);
fs.writeFileSync('data/map.json', mapRaw);
} else {
log(`Out of turn - turn change. user: ${reqBody.username}. next user: ${nextUser} at time ${(new Date()).getTime()}`);
// response = makeError(`out of turn - turn change. user: ${reqBody.username}. next user: ${nextUser}`)
response.mapState = restrictMapView(mapJSON);
response.nextTurnChange = turnChangeTime[reqBody.username];
response.isCorrectTurn = isCorrectTurn;
response.anyChanges = anyChanges[reqBody.username];
if (reqBody.username == "admin") {
response.usersList = users;
}
}
return response;
}
function changeOccured() {
for (var key in anyChanges) {
anyChanges[key] = true
}
}
function init() {
for (user of users) {
anyChanges[user] = true;
firstSync[user] = true;
notifications[user] = [];
}
}
function updateTurnChangeTimeInFile(times) {
for (user of users) {
if (turnChangeTime[user] == 0) {
times[user] = 0;
} else {
times[user] = turnChangeTime[user] - (new Date()).getTime();
}
}
}
function updateTurnChangeTimeFromFile(times) {
for (user of users) {
if (times[user] == 0) {
turnChangeTime[user] = 0;
} else {
turnChangeTime[user] = (new Date()).getTime() + times[user];
}
}
}
function log(message) {
t = new Date();
message = `[${t.toLocaleString()}] ${message}`;
console.log(message);
if (!fs.existsSync("data/server.log")) {
fs.writeFileSync('data/server.log', message);
} else {
fs.appendFileSync('data/server.log', "\n"+message);
}
}
users = ["Blufor", "Opfor"];
const passwordsLocation = "/var/thereIsProbablyAMoreSecureWayOfDoingThis.json"
if (!fs.existsSync(passwordsLocation)) {
throw `Missing passwords file.
Create the file at /var/thereIsProbablyAMoreSecureWayOfDoingThis.json with the following format:
{
"admin": "ADMIN_PASSWORD",
"Blufor": "BLUFOR_PASSWORD",
"Opfor": "OPFOR_PASSWORD"
}`;
}
// Luckily this doesn't need to be secure as it will be on a private network and its just a game
logins = JSON.parse(fs.readFileSync(passwordsLocation));
anyChanges = {"admin": true};
turnChangeTime = {};
firstSync = {"admin": true};
notifications = {"admin": []};
init()
// File reading
settings = JSON.parse(fs.readFileSync('settings.json'));
if (!fs.existsSync("data")) {
fs.mkdirSync("data");
}
if (fs.existsSync("data/map.json")) {
let mapJSON = JSON.parse(fs.readFileSync('data/map.json'));
gameStarted = mapJSON.gameStarted;
updateTurnChangeTimeFromFile(mapJSON.turnChangeTime);
} else {
defaultMapRaw = fs.readFileSync('default-map.json');
let defaultMap = JSON.parse(defaultMapRaw)
gameStarted = defaultMap.gameStarted;
updateTurnChangeTimeFromFile(defaultMap.turnChangeTime);
fs.writeFileSync('data/map.json', defaultMapRaw);
}
app.use(express.static('dist'));
app.use(bodyParser.json());
app.use('/res', express.static('res'));
app.post('/server.js', function (req, res, next) {
var rawdata = fs.readFileSync('data/map.json');
var mapJSON = JSON.parse(rawdata);
var username = req.body.username;
if (logins[username] == req.body.password) {
var response;
if (req.body.requestType == "sync") {
response = handleSync(req.body, mapJSON);
} else if (req.body.requestType == "turnChange") {
response = handleTurnChange(req.body, mapJSON);
} else {
log(`Invalid request made: ${req.body.requestType}`);
response = makeError("Invalid request made");
}
} else {
log(`Incorrect password for user, ${username}, entered`);
response = makeError("Wrong password");
}
res.send(JSON.stringify(response));
anyChanges[username] = false;
next();
})
app.listen(PORT);