-
Notifications
You must be signed in to change notification settings - Fork 1
/
league.js
392 lines (383 loc) · 14.4 KB
/
league.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
/*****************************************
*
* DA Circuit
*
* Developed by Irate in 2015
*
* Copyright by Duel Academy - duelacademy.net
*
*****************************************
**/
"use strict"; // require global strict
(function(o) {
var noES5 = false;
if (!Object.hasOwnProperty || !Object.propertyIsEnumerable || !Object.create || !Object.keys || !Object.freeze || !Array.prototype.forEach || !Array.prototype.sort) {
noES5 = true;
}
o.noES5 = noES5;
})(window);
function Map(o) {
var p;
if (!(this instanceof Map)) {
return new Map(o);
}
for (p in o) {
if (o.hasOwnProperty(p) && o.propertyIsEnumerable(p) && !(p in this)) {
this[p] = o[p];
}
}
return this;
}
Map.prototype.map = function(s) {
return this[s] || this[s.toLowerCase()] || this[s.toUpperCase()] || null;
};
var rankMap = Map({
0: {
rank: "Slifer Red",
elo: 500,
rankID: 0
},
1: {
rank: "Ra Yellow",
elo: 750,
rankID: 1
},
2: {
rank: "Obelisk Blue",
elo: 1000,
rankID: 2
},
3: {
rank: "Society of Light",
elo: 1250,
rankID: 3
},
"slifer": {
rank: "Slifer Red",
elo: 500,
rankID: 0
},
"ra": {
rank: "Ra Yellow",
elo: 750,
rankID: 1
},
"obelisk": {
rank: "Obelisk Blue",
elo: 1000,
rankID: 2
},
"sol": {
rank: "Society of Light",
elo: 1250,
rankID: 3
},
"slifer red": {
rank: "Slifer Red",
elo: 500,
rankID: 0
},
"ra yellow": {
rank: "Ra Yellow",
elo: 750,
rankID: 1
},
"obelisk blue": {
rank: "Obelisk Blue",
elo: 1000,
rankID: 2
},
"society of light": {
rank: "Society of Light",
elo: 1250,
rankID: 3
}
}),
dbTopic = "/t41235-",
entries = {},
matches = [],
entry,
iter,
sortArray = [];
function calculateNewElo(opts) {
if (!opts) {
console.log('Argument not specified for `calculateNewElo(opts)`');
return;
}
var previousWinnerElo = entries[opts.winner].elo,
previousLoserElo = entries[opts.loser].elo,
staticPointsGained = 15,
staticPointsLost = 15,
pointsGained,
pointsLost,
newWinnerElo,
newLoserElo,
rankDifference = entries[opts.winner].rankID - entries[opts.loser].rankID,
winnerWLRatio = (entries[opts.winner].wins / (entries[opts.winner].wins + entries[opts.winner].losses)) * 100,
loserWLRatio = (entries[opts.loser].wins / (entries[opts.loser].wins + entries[opts.loser].losses)) * 100,
winnerWLString = winnerWLRatio.toFixed(1).replace(/\.0$/, '') + '%',
loserWLString = loserWLRatio.toFixed(1).replace(/\.0$/, '') + '%',
hardCapGainPercentage = 60,
hardCapLossPercentage = 40;
entries[opts.winner].winLossHistory.push({
status: 1,
versus: opts.loser
});
entries[opts.loser].winLossHistory.push({
status: 0,
versus: opts.winner
});
if (rankDifference > 0) { // higher rank won vs lower rank
pointsGained = Math.floor(staticPointsGained / (rankDifference + (rankDifference * 0.1)));
pointsLost = (rankDifference >= 2) ? Math.floor(pointsGained / rankDifference) : Math.ceil(pointsGained / 2);
entries[opts.winner].lowerPlayed++;
entries[opts.winner].lowerWon++;
entries[opts.loser].higherPlayed++;
entries[opts.loser].higherLost++;
} else if (rankDifference < 0) { // lower rank won vs higher rank
pointsGained = (rankDifference <= -2) ? Math.round(staticPointsGained * Math.abs(rankDifference)) : Math.round(staticPointsGained * 1.5);
pointsLost = pointsGained;
entries[opts.winner].higherPlayed++;
entries[opts.winner].higherWon++;
entries[opts.loser].lowerPlayed++;
entries[opts.loser].lowerLost++;
} else if (rankDifference === 0) { // same rank
pointsGained = staticPointsGained;
pointsLost = staticPointsLost;
entries[opts.winner].equalPlayed++;
entries[opts.winner].equalWon++;
entries[opts.loser].equalPlayed++;
entries[opts.loser].equalLost++;
}
if (Math.round(winnerWLRatio) > hardCapGainPercentage) {
pointsGained = pointsGained + 10;
} else if (Math.round(winnerWLRatio) < hardCapLossPercentage) {
pointsGained = pointsGained - 10;
} else if (Math.round(winnerWLRatio) >= 50) {
pointsGained = pointsGained + (Math.round(winnerWLRatio - 50));
} else if (Math.round(winnerWLRatio) <= 50) {
pointsGained = pointsGained - (Math.round(50 - winnerWLRatio));
}
pointsGained = (pointsGained <= 0) ? 1 : pointsGained;
if (Math.round(loserWLRatio) > hardCapGainPercentage) {
pointsLost = pointsLost - 10;
} else if (Math.round(loserWLRatio) < hardCapLossPercentage) {
pointsLost = pointsLost + 10;
} else if (Math.round(loserWLRatio) >= 50) {
pointsLost = pointsLost - (Math.round(loserWLRatio - 50));
} else if (Math.round(loserWLRatio) <= 50) {
pointsLost = pointsLost + (Math.round(50 - loserWLRatio));
}
newWinnerElo = previousWinnerElo + pointsGained;
newLoserElo = previousLoserElo - pointsLost;
entries[opts.winner].elo = newWinnerElo;
entries[opts.winner].winratio = winnerWLString;
entries[opts.loser].elo = newLoserElo;
entries[opts.loser].winratio = loserWLString;
entries[opts.winner].eloDifference = newWinnerElo - rankMap.map((entries[opts.winner].lastDataCopies[0] && entries[opts.winner].lastDataCopies[0].rankID || entries[opts.winner].rankID)).elo;
entries[opts.loser].eloDifference = newLoserElo - rankMap.map((entries[opts.loser].lastDataCopies[0] && entries[opts.loser].lastDataCopies[0].rankID || entries[opts.loser].rankID)).elo;
}
function createWinLossStreak(user) {
if (!user.winLossHistory || user.winLossHistory.length === 0) {
return null;
}
var index = 0,
arrayLength = user.winLossHistory.length,
returnObject = {
wins: [],
losses: []
},
count = 0,
statusString, temp, previousTemp, samePlayed = {};
for (index, arrayLength; index < arrayLength; index++) {
temp = user.winLossHistory[index];
previousTemp = user.winLossHistory[index - 1];
if (samePlayed[temp.versus] === undefined) {
samePlayed[temp.versus] = 1;
} else {
samePlayed[temp.versus]++;
}
if (index === 0) {
count = 1;
statusString = (temp.status === 1) ? "wins" : "losses";
continue;
}
if (previousTemp && (temp.status === previousTemp.status)) {
count++;
} else {
returnObject[statusString].push(count);
count = 1;
statusString = (temp.status === 1) ? "wins" : "losses";
}
if (index === arrayLength - 1) {
returnObject[statusString].push(count);
}
}
if (!returnObject.wins.length) {
returnObject.wins = [0];
}
if (!returnObject.losses.length) {
returnObject.losses = [0];
}
user.samePlayed = samePlayed;
user.highestWinStreak = Math.max.apply({}, returnObject.wins);
user.highestLossStreak = Math.max.apply({}, returnObject.losses);
return user;
}
function makeTable(sourceArray) {
var returnHTML = "";
sourceArray.forEach(function(v, i) {
returnHTML = returnHTML + '<tr class="rankingTable rank_' + (i + 1) + '"><td class="rankingTableUser">' + v.username + '</td><td class="rankingTableWins">' + v.wins + '</td><td class="rankingTableLosses">' + v.losses + '</td><td class="rankingTableElo">' + v.elo + '</td></tr>';
});
return returnHTML;
}
function sortAfter(sourceArray, sortString, ascDesc) {
return sourceArray.sort(function(a, b) {
if (sortString === "username") {
return (ascDesc ? ((b.username[0].toLowerCase() < a.username[0].toLowerCase() ? 1 : -1)) : ((b.username[0].toLowerCase() < a.username[0].toLowerCase()) ? -1 : 1));
} else {
return (ascDesc ? b[sortString] - a[sortString] : a[sortString] - b[sortString]);
}
});
}
function adjustRank(user) {
var id = user.rankID;
switch (id) {
case 0:
{
if (user.elo > rankMap.map(1).elo) {
id = 1;
}
break;
}
case 1:
{
if (user.elo > rankMap.map(2).elo) {
id = 2;
}
if (user.elo < rankMap.map(0).elo) {
id = 0;
}
break;
}
case 2:
{
if (user.elo > rankMap.map(3).elo) {
id = 3;
}
if (user.elo < rankMap.map(1).elo) {
id = 1;
}
break;
}
case 3:
{
if (user.elo < rankMap.map(2).elo) {
id = 2;
}
break;
}
default:
{
console.log("No user specified to update.");
break;
}
}
user.rankID = id;
return user;
}
function createDataCopy(user) {
var userCopy = Object.create({}),
userKeys = Object.keys(user);
userKeys.forEach(function(v) {
Object.defineProperty(userCopy, v, {
enumerable: true,
writeable: true,
configurable: true,
value: user[v]
});
});
userCopy.hasOwnProperty("lastDataCopies") && delete userCopy.lastDataCopies;
return userCopy;
}
$(function() {
if (window.noES5) {
$('#loading').html('<span class="fatal">We have detected a fatal error: your browser does not support EcmaScript 5 features. Download a new browser or update your current browser to view this page.</span>');
} else {
$.get(dbTopic, function(data, status, xhr) {
if (xhr.status !== 200) {
console.log('Error "' + status + '"\nHTTP Request failed with Error Status of ' + xhr.status + ', please check your connection settings or connect your system administrator.');
}
data = $(data);
entries = $.parseJSON($('#entryTable td', data).html().replace(/<br>/gi, ''));
matches = $.parseJSON($('#matchTable td', data).html().replace(/<br>/gi, '')).matches;
for (entry in entries) {
if (entries.hasOwnProperty(entry) && entries.propertyIsEnumerable(entry)) {
entries[entry].username = entry;
entries[entry].rankID = rankMap.map(entries[entry].rank).rankID;
entries[entry].elo = rankMap.map(entries[entry].rank).elo;
entries[entry].wins = 0;
entries[entry].losses = 0;
// data for statistics below
entries[entry].gamesPlayed = 0;
entries[entry].higherPlayed = 0;
entries[entry].lowerPlayed = 0;
entries[entry].equalPlayed = 0;
entries[entry].higherWon = 0;
entries[entry].lowerWon = 0;
entries[entry].equalWon = 0;
entries[entry].higherLost = 0;
entries[entry].lowerLost = 0;
entries[entry].equalLost = 0;
entries[entry].eloDifference = 0;
entries[entry].highestWinStreak = 0;
entries[entry].highestLossStreak = 0;
entries[entry].winLossHistory = [];
// end of statistics
entries[entry].lastDataCopies = [];
}
}
matches.forEach(function(v, i) {
if (!v.winner || !v.loser) {
console.log('Invalid entry; winner or loser not given.');
} else if (!(v.winner in entries)) {
console.log('Invalid entry for match "' + v.winner + '" versus "' + v.loser + '"; winner not registered in database.');
} else if (!(v.loser in entries)) {
console.log('Invalid entry for match "' + v.winner + '" versus "' + v.loser + '"; loser not registered in database.');
} else {
entries[v.winner].wins++;
entries[v.loser].losses++;
entries[v.winner].gamesPlayed++;
entries[v.loser].gamesPlayed++;
calculateNewElo({
winner: v.winner,
loser: v.loser,
index: i
});
entries[v.winner].lastDataCopies.push(Object.freeze(createDataCopy(entries[v.winner])));
entries[v.loser].lastDataCopies.push(Object.freeze(createDataCopy(entries[v.loser])));
adjustRank(entries[v.winner]);
adjustRank(entries[v.loser]);
}
});
for (iter in entries) {
if (entries.hasOwnProperty(iter) && entries.propertyIsEnumerable(iter)) {
createWinLossStreak(entries[iter]);
sortArray.push(entries[iter]);
}
}
$('#eloDisplay tbody').html(makeTable(sortAfter(sortArray, "elo", true)));
$('#loading').css('display', 'none');
$('#tableHead').css('display', 'block');
/*$('div[class^="ranking"]').on('click', function(e) {
if ($(this).hasClass('asc')) {
$('#eloDisplay tbody').html(makeTable(sortAfter(sortArray, $(this).removeClass('asc').attr('class').split('ranking')[0], false)));
$(this).addClass('desc');
} else {
$('#eloDisplay tbody').html(makeTable(sortAfter(sortArray, $(this).removeClass('desc').attr('class').split('ranking')[0], true)));
$(this).addClass('asc');
}
});*/
});
}
});