This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.js
359 lines (327 loc) · 11.7 KB
/
helpers.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
const config = require("./config.json");
const emotes = config.emotes;
var exports = module.exports = {};
// Writes something to stdout
exports.log = (text, err=false) => {
if (err) {
console.error("[" + (new Date()).toISOString() + "] " + text);
} else {
console.log("[" + (new Date()).toISOString() + "] " + text);
}
}
// Given a string (game), returns the name of the closest matching game in config.json.
exports.normalizeGameName = (game) => {
process = (g) => g.toLowerCase().replace(/\W/g, "").replace("littlebigplanet", "lbp").replace("psv", "v");
game = process(game);
for (var name in config.games) {
if (config.games[name].aliases.includes(game) || process(name) === game) {
return name;
}
}
return null;
}
// Given a game name and a category string, returns the closest matching category name in config.json.
exports.normalizeCategory = (normalizedGame, category) => {
g = config.games[normalizedGame];
if (g === undefined || g.categories === undefined) {
return "Any%";
}
if (category === null) {
return Object.keys(g.categories)[0];
}
process = (c) => c.toLowerCase().replace(/\W|plus/g, "").replace("newgame", "ng");
normalizedCategory = process(category);
for (var name in g.categories) {
if (g.categories[name].includes(normalizedCategory) || process(name) === normalizedCategory) {
return name;
}
}
return null;
}
// Given a game name and level string, return the closest matching level name in config.json.
exports.normalizeLevel = (normalizedGame, level) => {
g = config.games[normalizedGame];
if (level == null) {
if (g === undefined || g.levels === undefined) {
return "<select level>";
}
return Object.keys(g.levels)[0];
}
process = (l) => l.toLowerCase().replace(/&/g, "and").replace(/\W|the/g, "");
normalizedLevel = process(level);
for (var name in g.levels) {
if (g.levels[name].includes(normalizedLevel) || process(name) === normalizedLevel) {
return name;
}
}
return null;
}
// Gets a user's username string (unless it's FireThieff, then it returns "bean")
exports.username = (message) => {
if (message.author.id === "159245797328814081") {
return "bean";
}
if (message.member !== null) {
return message.member.displayName;
}
return message.author.username;
}
// Gets a formatted string for @ing a user
exports.mention = (user) => {
return "<@" + user.id + ">";
}
// Formats a time in seconds in H:mm:ss.xx
exports.formatTime = (time) => {
if (time === -1) {
return "--:--:--.--";
}
var hrs = Math.floor(time / 3600);
var min = Math.floor((time - (hrs * 3600)) / 60);
var sec = Math.round((time - (hrs * 3600) - (min * 60)) * 100) / 100;
var result = (hrs < 10 ? "0" : "") + hrs;
result += ":" + (min < 10 ? "0" + min : min);
result += ":" + (sec < 10 ? "0" + sec : sec);
if (sec % 1 === 0) {
result += ".0";
}
if ((sec * 10) % 1 === 0) {
result += "0";
}
return result;
}
// Converts a number to its place, e.g. 1 -> 1st, 2 -> 2nd, etc.
exports.formatPlace = (place) => {
placeDigit = place % 10;
if (placeDigit > 3 || (3 < place % 100 && place % 100 < 21)) {
return place + "th";
} else if (placeDigit === 1) {
return place + "st";
} else if (placeDigit === 2) {
return place + "nd";
}
return place + "rd";
}
// Helper for removing an object (value) from an array (arr)
exports.arrayRemove = (arr, value) => {
return arr.filter((element) => {
return element != value;
});
}
// e.g. 1 --> " 1"
exports.addSpaces = (input, outputLength) => {
var spacesString = "";
for (let i = 0; i < outputLength - input.length; i++) {
spacesString += " ";
}
return spacesString + input;
}
// Returns either ":..._place:" or ":checkered_flag:"
exports.placeEmote = (place) => {
switch (place) {
case 0:
return emotes.firstPlace;
case 1:
return emotes.secondPlace;
case 2:
return emotes.thirdPlace;
default:
return emotes.finished;
}
}
exports.defaultStatObj = (id, g, c) => {
return { user_id: `${id}`, game: `${g}`, category: `${c}`, races: 0, gold: 0, silver: 0, bronze: 0, ffs: 0, elo: 1500, pb: -1 };
}
// Update a user's stats in statObj based on their race results
exports.calculatePlayerStats = (statObj, ffd, racePlace, doneTime) => {
statObj.races++;
if (ffd) {
statObj.ffs++;
} else {
if (racePlace === 0) {
statObj.gold++;
} else if (racePlace === 1) {
statObj.silver++;
} else if (racePlace === 2) {
statObj.bronze++;
}
if (statObj.category !== "Individual Levels" && statObj.category !== "Individual Levels (Co-op)" && (statObj.pb === -1 || doneTime < statObj.pb)) {
statObj.pb = doneTime;
}
}
}
// Calculate Elo diffs by treating each pair of racers in the race as a 1v1 matchup.
// See https://en.wikipedia.org/wiki/Elo_rating_system
exports.calculateEloDiffs = (stats, teamMap, raceRankings, ffs) => {
oldElos = new Map();
prevTeam = teamMap.get(stats.values().next().value.user_id);
maxElo = Number.MIN_VALUE;
eloAccum = 0;
teamCount = 0;
calculateTeamElo = () => {
elo = (maxElo * (teamCount - 1) + eloAccum) / (2 * teamCount - 1);
oldElos.set("!team " + prevTeam, elo);
maxElo = Number.MIN_VALUE;
eloAccum = 0;
teamCount = 0;
}
raceRankings.forEach((id) => {
curTeam = teamMap.get(id);
stat = stats.get(id);
if (prevTeam !== "" && prevTeam !== curTeam) {
calculateTeamElo();
}
if (curTeam === "") {
// Individual; store Elo
oldElos.set(id, stat.elo);
} else {
// Team member; accumulate Elo to average with whole team
teamCount++;
eloAccum += stat.elo;
if (stat.elo > maxElo) {
maxElo = stat.elo;
}
}
prevTeam = curTeam;
});
if (prevTeam !== "") {
calculateTeamElo();
}
eloDiffs = new Map();
raceRankings.forEach((id1, p1Place) => {
actualId1 = id1;
if (teamMap.get(id1) !== "") {
actualId1 = "!team " + teamMap.get(id1);
}
actualScore = 0;
expectedScore = 0;
alreadyCompared = new Set();
raceRankings.forEach((id2, p2Place) => {
// Don't compare the player against themselves
if (id1 === id2 || (teamMap.get(id1) !== "" && teamMap.get(id2) === teamMap.get(id1))) {
return;
}
// If user is on a team, compare against averaged team elo instead of individual
actualId2 = id2;
if (teamMap.get(id2) !== "") {
actualId2 = "!team " + teamMap.get(id2);
}
if (alreadyCompared.has(actualId2)) {
return;
}
alreadyCompared.add(actualId2);
expectedDiff = 1.0 / (1 + Math.pow(10, (oldElos.get(actualId2) - oldElos.get(actualId1)) / 400));
expectedScore += expectedDiff;
if (ffs.includes(id1)) {
if (ffs.includes(id2)) {
// If both players forfeited, those two players won't affect each other's scores
actualScore += expectedDiff;
} else {
// Loss gives 0 points
}
} else if (p1Place < p2Place) {
// Ahead of opponent, count as win
actualScore++;
} else {
// Loss gives 0 points
}
});
eloDiffs.set(actualId1, 32 * (actualScore - expectedScore));
});
return eloDiffs;
}
// Builds and returns a map of discord id -> stat object for a given game/category. If functions for determining
// a user's forfeit status and finish time are provided, the stat objects will be updated to reflect the results.
exports.retrievePlayerStats = (raceRankings, retrieveStatsSql, game, category, teamMap, ffFunc=null, dtimeFunc=null) => {
stats = new Map();
place = -1;
prevTeam = "";
raceRankings.forEach((id, i) => {
statObj = retrieveStatsSql.get(id, game, category);
if (!statObj) {
statObj = exports.defaultStatObj(id, game, category);
}
curTeam = teamMap.get(id);
if (curTeam === "" || curTeam !== prevTeam) {
place++;
}
if (ffFunc !== null && dtimeFunc !== null) {
exports.calculatePlayerStats(statObj, ffFunc(id, i), place, dtimeFunc(id, i));
}
stats.set(id, statObj);
prevTeam = curTeam;
});
return stats;
}
// Runs a function for everyone on a given entrant's team.
exports.doForWholeTeam = (raceState, id, proc) => {
entrant = raceState.entrants.get(id);
if (entrant.team !== "") {
raceState.entrants.forEach((e) => {
if (e.team === entrant.team) {
proc(e);
}
});
} else {
proc(entrant);
}
}
// Iterates over a collection of Entrants and calls individualEntrantFunc on all entrants with no team,
// teamNameFunc for the first player found on a team, and teamEntrantFunc for every player on a team
exports.forEachWithTeamHandling = (collection, individualEntrantFunc, teamNameFunc, teamEntrantFunc) => {
entrantsAlreadyOnTeam = [];
collection.forEach((entrant) => {
if (entrantsAlreadyOnTeam.includes(entrant)) {
return;
}
if (entrant.team === "") {
individualEntrantFunc(entrant);
} else {
teamNameFunc(entrant);
teamEntrantFunc(entrant);
entrantsAlreadyOnTeam.push(entrant);
collection.forEach((entrantTeamSearch) => {
if (entrantTeamSearch.team === entrant.team && !entrantsAlreadyOnTeam.includes(entrantTeamSearch)) {
teamEntrantFunc(entrantTeamSearch);
entrantsAlreadyOnTeam.push(entrantTeamSearch);
}
});
}
});
}
// Returns true if there is exactly one team registered (and no individuals)
exports.isOneTeamRegistered = (raceState) => {
foundTeam = "";
oneTeam = true;
raceState.entrants.forEach((entrant) => {
if (entrant.team === "" || (foundTeam !== "" && entrant.team !== foundTeam)) {
oneTeam = false;
}
foundTeam = entrant.team;
});
return oneTeam;
}
// The following code is based on https://github.com/intesso/decode-html to avoid additional dependencies ---------
// (license: https://github.com/intesso/decode-html/blob/master/LICENSE)
exports.decodeHTML = (text) => {
entityPattern = /&([a-z]+);/ig;
entities = { 'amp': '&', 'apos': '\'', 'lt': '<', 'gt': '>', 'quot': '"', 'nbsp': ' ' };
return text.replace(entityPattern, (match, entity) => {
entity = entity.toLowerCase();
if (entities.hasOwnProperty(entity)) {
return entities[entity];
}
// return original string if there is no matching entity (no replace)
return match;
});
};
// ----------------------------------------------------------------------------------------------------------------
// Send an error message to discord (with some special handling depending on what the error is)
exports.sendErrorMessage = (e, path, message) => {
errMsg = "Error reaching " + path + ": ";
if (e.message.startsWith("connect ETIMEDOUT")) {
errMsg += "Connection timed out.";
} else {
errMsg += e.message;
}
message.channel.send(errMsg);
}