-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathutil.js
More file actions
485 lines (415 loc) · 15 KB
/
Copy pathutil.js
File metadata and controls
485 lines (415 loc) · 15 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
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
const { newStaff } = require('./models');
const util = require('util');
const { Webhook } = require('discord-webhook-node');
const tempy = require('tempy');
/**
* Debugging function to send a game to Discord after it's been identified to be cyclic
*/
const debugSendGame = (game, message = '') => {
const _game = Object.assign({}, game);
delete _game.unsentReports;
const webhook = new Webhook(process.env.DISCORDPRIVATEDEVELOPERS);
const gameStr = util.inspect(_game, { showHidden: true, depth: null, colors: false });
tempy.write.task(
gameStr,
filename => {
if (message) webhook.send(message);
webhook.sendFile(filename);
},
{ extension: '.txt' }
);
};
module.exports.debugSendGame = debugSendGame;
const identified = [];
/**
* Check if a game is cyclic (JSON stringify fails on a cyclic object)
* @param {*} game game object
* @param {string} phase identifier of when this was detected
*/
const testGameObject = game => {
if (identified.indexOf(game.general.uid) !== -1) return;
try {
// eslint-disable-next-line no-unused-vars
const str = JSON.stringify(game);
} catch (e) {
debugSendGame(game, `Cyclic game object detected, stack trace: \n${e.stack}`);
identified.push(game.general.uid);
}
};
module.exports.testGameObject = testGameObject;
/**
* @param {object} game - game to act on.
* @return {object} game
*/
const secureGame = game => {
const _game = Object.assign({}, game);
delete _game.private;
delete _game.remakeData;
delete _game.guesses;
delete _game.unsentReports;
return _game;
};
const combineInProgressChats = (game, userName) =>
userName && game.gameState.isTracksFlipped
? game.private.seatedPlayers.find(player => player.userName === userName).gameChats.concat(game.chats)
: game.private.unSeatedGameChats.concat(game.chats);
const combineCommandChats = (game, user, commandChats) => (commandChats[user] ? game.chats.concat(commandChats[user]) : game.chats);
module.exports.combineCommandChats = combineCommandChats;
/**
* @param {object} game - game to act on.
* @param {boolean} noChats - remove chats for client to handle.
*/
module.exports.sendInProgressGameUpdate = (game, noChats = false) => {
if (!game || !io.sockets.adapter.rooms[game.general.uid]) {
return;
}
// DEBUG ONLY
// console.log(game.general.status, 'TimedMode:', game.gameState.timedModeEnabled, 'TimerId:', game.private.timerId ? 'exists' : 'null');
const seatedPlayerNames = game.publicPlayersState.map(player => player.userName);
const roomSockets = Object.keys(io.sockets.adapter.rooms[game.general.uid].sockets).map(sockedId => io.sockets.connected[sockedId]);
const playerSockets = roomSockets.filter(
socket =>
socket &&
socket.handshake.session.passport &&
Object.keys(socket.handshake.session.passport).length &&
seatedPlayerNames.includes(socket.handshake.session.passport.user)
);
const observerSockets = roomSockets.filter(
socket => (socket && !socket.handshake.session.passport) || (socket && !seatedPlayerNames.includes(socket.handshake.session.passport.user))
);
playerSockets.forEach(sock => {
const _game = Object.assign({}, game);
const { user } = sock.handshake.session.passport;
if (!game.gameState.isCompleted && game.gameState.isTracksFlipped) {
const privatePlayer = _game.private.seatedPlayers.find(player => user === player.userName);
if (!_game || !privatePlayer) {
return;
}
_game.playersState = privatePlayer.playersState;
_game.cardFlingerState = privatePlayer.cardFlingerState || [];
}
_game.chats = combineCommandChats(_game, user, game.private.commandChats);
if (noChats) {
delete _game.chats;
sock.emit('gameUpdate', secureGame(_game), true);
} else {
_game.chats = combineInProgressChats(_game, user);
sock.emit('gameUpdate', secureGame(_game));
}
});
let chatWithHidden = game.chats;
if (!noChats && game.private && game.private.hiddenInfoChat && game.private.hiddenInfoSubscriptions.length) {
chatWithHidden = [...chatWithHidden, ...game.private.hiddenInfoChat];
}
if (observerSockets.length) {
observerSockets.forEach(sock => {
const _game = Object.assign({}, game);
const user = sock.handshake.session.passport ? sock.handshake.session.passport.user : null;
if (user && game.private && game.private.hiddenInfoSubscriptions && game.private.hiddenInfoSubscriptions.includes(user)) {
// AEM status is ensured when adding to the subscription list
_game.chats = chatWithHidden;
}
if (noChats) {
delete _game.chats;
sock.emit('gameUpdate', secureGame(_game), true);
} else {
_game.chats = combineInProgressChats(_game);
_game.chats = combineCommandChats(_game, user, game.private.commandChats);
sock.emit('gameUpdate', secureGame(_game));
}
testGameObject(_game);
});
}
};
module.exports.sendInProgressModChatUpdate = (game, chat, specificUser) => {
if (!io.sockets.adapter.rooms[game.general.uid]) {
return;
}
const roomSockets = Object.keys(io.sockets.adapter.rooms[game.general.uid].sockets).map(sockedId => io.sockets.connected[sockedId]);
if (roomSockets.length) {
roomSockets.forEach(sock => {
if (sock && sock.handshake && sock.handshake.passport && sock.handshake.passport.user) {
const { user } = sock.handshake.session.passport;
if (game.private.hiddenInfoSubscriptions.includes(user)) {
// AEM status is ensured when adding to the subscription list
if (!specificUser) {
// single message
sock.emit('gameModChat', chat);
} else if (specificUser === user) {
// list of messages
chat.forEach(msg => sock.emit('gameModChat', msg));
}
}
}
});
}
};
module.exports.sendPlayerChatUpdate = (game, chat) => {
if (!io.sockets.adapter.rooms[game.general.uid]) {
return;
}
const roomSockets = Object.keys(io.sockets.adapter.rooms[game.general.uid].sockets).map(sockedId => io.sockets.connected[sockedId]);
roomSockets.forEach(sock => {
if (sock) {
sock.emit('playerChatUpdate', chat);
}
});
};
module.exports.sendCommandChatsUpdate = game => {
if (!io.sockets.adapter.rooms[game.general.uid]) {
return;
}
const roomSockets = Object.keys(io.sockets.adapter.rooms[game.general.uid].sockets).map(sockedId => io.sockets.connected[sockedId]);
roomSockets.forEach(sock => {
if (sock) {
const _game = Object.assign({}, game);
const user = sock.handshake?.session?.passport?.user;
if (user) {
_game.chats = combineCommandChats(_game, user, game.private.commandChats);
sock.emit('gameUpdate', secureGame(_game));
}
}
});
};
module.exports.secureGame = secureGame;
const getStaffRole = (user, modUserNames, editorUserNames, adminUserNames) => {
if (modUserNames.includes(user) || newStaff.modUserNames.includes(user)) {
return 'moderator';
} else if (editorUserNames.includes(user) || newStaff.editorUserNames.includes(user)) {
return 'editor';
} else if (adminUserNames && adminUserNames.includes(user)) {
return 'admin';
}
return '';
};
module.exports.getStaffRole = getStaffRole;
const handleAEMMessages = (dm, user, modUserNames, editorUserNames, adminUserNames) => {
const dmClone = Object.assign({}, dm);
if (getStaffRole(user, modUserNames, editorUserNames, adminUserNames)) {
dmClone.messages = dmClone.aemOnlyMessages;
}
delete dmClone.aemOnlyMessages;
delete dmClone.subscribedPlayers;
return dmClone;
};
module.exports.handleAEMMessages = handleAEMMessages;
module.exports.sendInProgressModDMUpdate = (dm, modUserNames, editorUserNames, adminUserNames) => {
for (const user of dm.subscribedPlayers) {
try {
io.sockets.sockets[
Object.keys(io.sockets.sockets).find(
socketId => io.sockets.sockets[socketId].handshake.session.passport && io.sockets.sockets[socketId].handshake.session.passport.user === user
)
].emit('inProgressModDMUpdate', handleAEMMessages(dm, user, modUserNames, editorUserNames, adminUserNames));
} catch (e) {
console.log('err', e);
}
}
};
const avg = (accounts, accessor) => accounts.reduce((prev, curr) => prev + accessor(curr), 0) / accounts.length;
// Calculates the bias in elo points
const probToEloPoints = p => -400 * Math.log10(1 / p - 1);
// The probability of this team winning in this game size, given perfectly equal teams, in terms of elo points
const winnerBiasPoints = game => {
const liberalBias = game.gameState.isCompleted === 'liberal' ? 1 : -1;
const fascistBias = game.gameState.isCompleted === 'liberal' ? -1 : 1;
if (game.general.rebalance6p) {
return probToEloPoints(0.5 + 0.03 * fascistBias);
} else if (game.general.rebalance7p) {
return probToEloPoints(0.5 + 0.01 * fascistBias);
} else if (game.general.rebalance9p2f) {
return probToEloPoints(0.5 + 0.07 * fascistBias);
} else {
switch (game.general.playerCount) {
case 5:
return probToEloPoints(0.5 + 0.04 * fascistBias);
case 6:
return probToEloPoints(0.5 + 0.07 * liberalBias);
case 7:
return probToEloPoints(0.5 + 0.02 * fascistBias);
case 8:
return probToEloPoints(0.5 + 0.04 * liberalBias);
case 9:
return probToEloPoints(0.5 + 0.08 * fascistBias);
case 10:
return probToEloPoints(0.5 + 0.04 * fascistBias);
default:
return 0;
}
}
};
module.exports.rateEloGame = (game, accounts, winningPlayerNames) => {
const size = game.general.playerCount;
// The default starting elo is 1600 (totally arbitrary but now we are stuck with it)
const defaultELO = 1600;
// The maximum change for rainbow games is rk
const rk = 9;
// The maximum change for non-rainbow games is nk
const nk = 4;
// Choose the right factor
const k = size * (game.general.rainbowgame ? rk : nk); // non-rainbow games are capped at k/r
// Sort the players into winners and losers
const winningAccounts = accounts.filter(account => winningPlayerNames.includes(account.username));
const winningSize = winningPlayerNames.length;
const losingAccounts = accounts.filter(account => !winningPlayerNames.includes(account.username));
const losingSize = size - winningSize;
// Construct some basic statistics for each team
const averageRatingWinners = avg(winningAccounts, a => a.eloOverall || defaultELO);
const averageRatingWinnersSeason = avg(winningAccounts, a => a.eloSeason || defaultELO);
const averageRatingLosers = avg(losingAccounts, a => a.eloOverall || defaultELO);
const averageRatingLosersSeason = avg(losingAccounts, a => a.eloSeason || defaultELO);
// Elo Formula
const bias = winnerBiasPoints(game);
const winFactor = k / winningSize;
const loseFactor = -k / losingSize;
// P is the degree to which the new win surprised us, given our current ratings
// Bias is applied within the sigmoid to ensure that elo is conserved even in situations with huge team differences
const p = 1 / (1 + Math.pow(10, (averageRatingWinners - averageRatingLosers + bias) / 400));
const pSeason = 1 / (1 + Math.pow(10, (averageRatingWinnersSeason - averageRatingLosersSeason + bias) / 400));
// Now we will use our 'supprisedness' p to correct the player rankings
const ratingUpdates = {};
const date = new Date(); // ensure we use the same date for each player
accounts.forEach(account => {
const eloOverall = account.eloOverall ? account.eloOverall : defaultELO;
const eloSeason = account.eloSeason ? account.eloSeason : defaultELO;
// If this player won, use the win factor. If they lost, use the lost factor.
const factor = winningPlayerNames.includes(account.username) ? winFactor : loseFactor;
const change = p * factor;
const changeSeason = pSeason * factor;
const xpChange = change > 0 ? change / 1.5 : 1;
const xpChangeSeason = changeSeason > 0 ? changeSeason / 1.5 : 1;
account.eloOverall = eloOverall + change;
account.maxElo = Math.max(account.maxElo, account.eloOverall);
account.pastElo.push({
date,
value: account.eloOverall
});
account.xpOverall = (account.xpOverall || 0) + xpChange;
account.eloSeason = eloSeason + changeSeason;
account.xpSeason = (account.xpSeason || 0) + xpChangeSeason;
if (account.xpOverall >= 10.0) {
account.isRainbowOverall = true;
account.dateRainbowOverall = new Date();
}
if (account.xpSeason >= 10.0) {
account.isRainbowSeason = true;
}
account.save();
ratingUpdates[account.username] = { change, changeSeason, xpChange, xpChangeSeason };
});
return ratingUpdates;
// Future work: Someone should make this a single function, applied twice: once to overall and once to seasonal.
};
module.exports.destroySession = username => {
if (process.env.NODE_ENV !== 'production') {
const Mongoclient = require('mongodb').MongoClient;
let mongoClient;
Mongoclient.connect('mongodb://localhost:27017', { useNewUrlParser: true }, (err, client) => {
mongoClient = client;
});
if (!mongoClient) {
console.log('WARN: No mongo connection, cannot destroy user session.');
return;
}
mongoClient
.db('secret-hitler-app')
.collection('sessions')
.findOneAndDelete({ 'session.passport.user': username }, err => {
if (err) {
try {
console.log(err, 'err in logoutuser');
} catch (error) {}
}
});
}
};
class LineGuess {
/**
* @type number[]
*/
regs;
/**
* @type number|null
*/
hit;
/**
* @param {{regs: number[], hit: (number|null)}} o
*/
constructor(o = { regs: [], hit: null }) {
this.regs = o.regs;
this.hit = o.hit;
}
/**
* @return {string} - A string representation of the guess, can be passed to parse.
*/
toString() {
return this.regs
.map(reg => {
const newReg = reg === 10 ? 0 : reg;
return reg === this.hit ? `${newReg}h` : `${newReg}`;
})
.join('');
}
/**
* @param {LineGuess} other - the guess to compare this to.
* @return {boolean} - whether the guesses are equal.
*/
equals(other) {
if (this.hit !== other.hit) {
return false;
}
if (this.regs.length !== other.regs.length) {
return false;
}
for (let i = 0; i < this.regs.length; i++) {
if (this.regs[i] !== other.regs[i]) {
return false;
}
}
return true;
}
/**
* Parses a string guess into a structured format.
*
* @param {string} guess - the guess string.
* @return {LineGuess|null} - the resulting guess, or null if it is invalid.
*/
static parse(guess) {
const fasRegex = /(\dh?)/gi;
const result = new LineGuess();
const m = guess.match(fasRegex);
if (!m) {
return null;
}
for (const match of m) {
let seat = parseInt(match[0]);
seat = seat === 0 ? 10 : seat;
if (result.regs.includes(seat)) {
return null;
}
result.regs.push(seat);
if (match.length === 2) {
if (result.hit) {
return null;
}
result.hit = seat;
}
}
result.regs.sort((a, b) => a - b);
return result;
}
/**
* @param {LineGuess} other - the guess to find the difference of this to.
* @return {[number, boolean]} - the number of fas the same and whether hit is the same.
*/
difference(other) {
const fasSame = this.regs.reduce((accum, f) => accum + other.regs.includes(f), 0);
return [fasSame, this.hit === other.hit];
}
}
module.exports.LineGuess = LineGuess;
// tacks on "/64" to IPv6 ips; needed to properly ban IPv6 ips
module.exports.handleDefaultIPv6Range = ip => {
// check if there is NOT a : or there IS a / (ie. it's not IPv6 or it already has a CIDR range)
return ip.indexOf(':') === -1 || ip.indexOf('/') !== -1 ? ip : ip + '/64';
};