-
Notifications
You must be signed in to change notification settings - Fork 0
/
oldMain.js
389 lines (374 loc) · 15.4 KB
/
oldMain.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
// TODO: Refactor whole system(with classes preferred).
// TODO: Move to React.js
// TODO: Maybe make live data? :)
Number.prototype.toMoney = function(decimals, decimal_sep, thousands_sep) {
var n = this,
c = isNaN(decimals) ? 2 : Math.abs(decimals),
d = decimal_sep || '.',
t = typeof thousands_sep === 'undefined' ? ',' : thousands_sep,
sign = n < 0 ? '-' : '',
i = parseInt((n = Math.abs(n).toFixed(c))) + '',
j = (j = i.length) > 3 ? j % 3 : 0;
return (
sign +
(j ? i.substr(0, j) + t : '') +
i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + t) +
(c
? d +
Math.abs(n - i)
.toFixed(c)
.slice(2)
: '')
);
};
/* Getting trivia datas from the database */
var xhr = new XMLHttpRequest();
xhr.open('GET', 'trivia_data.php');
xhr.onreadystatechange = function() {
var DONE = 4;
var OK = 200;
if (xhr.readyState === DONE) {
if (xhr.status === OK) {
window.trivias = xhr.responseText;
exportToScreen(xhr.responseText);
} else {
console.log('Error: ' + xhr.status);
}
}
};
xhr.send(null);
/* Loadtime will be used to determine last 24 hours etc. for last 24 hour stats. */
var loadTime = Number(new Date()),
todayStatsTime = loadTime - 24 * 60 * 60 * 1000,
todayTotalCoins = 0,
allTimeTotalCoins = 0;
function exportToScreen(data) {
/* Defining global variables */
var trivias = data.split('\n'),
leaderboard24Data = {},
sortedLeaderboard24Data = [],
leaderboardAllTimeData = {},
sortedLeaderboardAllTimeData = [];
/* Processing each trivia */
trivias.forEach(function(trivia) {
if (trivia.split('-@-').length == 7) {
/* General trivia data structure coming from database ->
hoster-@-question-@-answer-@-timestamp-@-winners(seperated with ,)-@-chatname-@-totalamount
*/
var triviaData = trivia.split('-@-'),
hoster = triviaData[0],
question = triviaData[1],
answer = triviaData[2],
timeStamp = triviaData[3],
winners = triviaData[4],
chatName = triviaData[5],
totalAmount = triviaData[6],
/* Making this data inside a trivia object so it will be more readable in the future */
triviaObj = {
hoster: hoster,
question: question,
answer: answer,
timeStamp: timeStamp,
winners: winners,
chatName: chatName,
totalAmount: totalAmount,
},
winnersArray = winners.split(',');
//console.log(triviaObj);
/* Calculation of last 24 hour trivias START */
if (Number(timeStamp) > todayStatsTime) {
todayTotalCoins += Number(totalAmount);
if (!isNaN(totalAmount)) {
winnersArray.forEach(winner => {
/* If the user has already won a trivia, add the current trivia's reward to user's previous amount. Otherwise, create a new object key-value pair for that user. */
if (leaderboard24Data[winner])
leaderboard24Data[winner] += Math.floor(totalAmount / winnersArray.length);
// Using floor so it will prevent retarded numbers (tho this is not how it should be)
else {
leaderboard24Data[winner] = Math.floor(totalAmount / winnersArray.length); // Using floor so it will prevent retarded numbers (tho this is not how it should be)
}
});
var sortable = [];
for (var user in leaderboard24Data) {
sortable.push([user, leaderboard24Data[user]]);
}
sortable.sort((a, b) => {
return b[1] - a[1];
});
sortedLeaderboard24Data = sortable;
}
var last24exists = false,
last24thIndex = 0;
if (document.querySelector('#last24Trivias').querySelectorAll('th').length > 0) {
for (var i = 0; i < document.querySelector('#last24Trivias').querySelectorAll('th').length; i++) {
title = document.querySelector('#last24Trivias').querySelectorAll('th')[i];
if (title.innerText.split('\n')[0] == hoster) {
last24exists = true;
last24thIndex = i;
}
}
}
if (last24exists == true) {
var last24button = document.createElement('button');
last24button.className = 'triviaButton';
last24button.innerText = new Date(Number(timeStamp)).toLocaleString();
last24button.addEventListener('click', function() {
//alert(`Chat: ${chatName}\nQuestion: ${question}\nAnswer: ${answer}\nWinners: ${winners}\nTotal coins given out: ${totalAmount}`);
if (last24button.parentNode.querySelector('div').className.includes('hidden')) {
last24button.parentNode.querySelector(
'div'
).className = last24button.parentNode
.querySelector('div')
.className.replace('hidden', 'visible');
if (
last24button.parentNode.querySelector('div').getBoundingClientRect().left >
document.body.offsetWidth - 500
)
last24button.parentNode.querySelector('div').className += ' overWidthRight';
} else last24button.parentNode.querySelector('div').className = last24button.parentNode.querySelector('div').className.replace('visible', 'hidden');
});
var div = document.createElement('div');
div.className = 'triviaDetail hidden';
var span = document.createElement('span');
span.innerText = `Chat: ${chatName}\nQuestion: ${question}\nAnswer: ${answer}\nWinners: ${winners}\nTotal coins given out: ${totalAmount}`;
div.append(span);
var triviaButtonDiv = document.createElement('div');
triviaButtonDiv.className = 'triviaButtonDiv';
triviaButtonDiv.appendChild(last24button);
triviaButtonDiv.appendChild(div);
document
.querySelector('#last24Trivias')
.querySelectorAll('td')
[last24thIndex].appendChild(triviaButtonDiv);
document
.querySelector('#last24modList')
.querySelectorAll('th')
[last24thIndex].querySelector('.total').innerText =
Number(
document
.querySelector('#last24modList')
.querySelectorAll('th')
[last24thIndex].querySelector('.total').innerText
) + Number(totalAmount);
} else {
var th = document.createElement('th');
th.innerHTML = `${hoster}<br /><span class="total">${totalAmount}</span>`;
document
.querySelector('#last24Trivias')
.querySelector('#last24modList')
.appendChild(th);
var td = document.createElement('td');
document
.querySelector('#last24Trivias')
.querySelector('#last24triviaList')
.appendChild(td);
for (var i = 0; i < document.querySelector('#last24Trivias').querySelectorAll('th').length; i++) {
title = document.querySelector('#last24Trivias').querySelectorAll('th')[i];
if (title.innerText.split('\n')[0] == hoster) {
last24exists = true;
last24thIndex = i;
}
}
var last24button = document.createElement('button');
last24button.className = 'triviaButton';
last24button.innerText = new Date(Number(timeStamp)).toLocaleString();
last24button.addEventListener('click', () => {
//alert(`Chat: ${chatName}\nQuestion: ${question}\nAnswer: ${answer}\nWinners: ${winners}\nTotal coins given out: ${totalAmount}`);
if (last24button.parentNode.querySelector('div').className.includes('hidden')) {
last24button.parentNode.querySelector(
'div'
).className = last24button.parentNode
.querySelector('div')
.className.replace('hidden', 'visible');
if (
last24button.parentNode.querySelector('div').getBoundingClientRect().left >
document.body.offsetWidth - 500
)
last24button.parentNode.querySelector('div').className += ' overWidthRight';
} else
last24button.parentNode.querySelector(
'div'
).className = last24button.parentNode
.querySelector('div')
.className.replace('visible', 'hidden');
});
var div = document.createElement('div');
div.className = 'triviaDetail hidden';
var span = document.createElement('span');
span.innerText = `Chat: ${chatName}\nQuestion: ${question}\nAnswer: ${answer}\nWinners: ${winners}\nTotal coins given out: ${totalAmount}`;
div.append(span);
var triviaButtonDiv = document.createElement('div');
triviaButtonDiv.className = 'triviaButtonDiv';
triviaButtonDiv.appendChild(last24button);
triviaButtonDiv.appendChild(div);
document
.querySelector('#last24Trivias')
.querySelectorAll('td')
[last24thIndex].appendChild(triviaButtonDiv);
}
}
/* Calculation of last 24 hour trivias END */
/* Sometimes Cancelled trivias get into the server too so we need to filter them out and query all the correct ones */
if (!isNaN(totalAmount)) {
winnersArray.forEach(winner => {
/* Adding user to the all time leaderboard data with username so we can access with username directly */
/* If the user has already won a trivia, add the current trivia's reward to user's previous amount. Otherwise, create a new object key-value pair for that user. */
if (leaderboardAllTimeData[winner])
leaderboardAllTimeData[winner] += Math.floor(totalAmount / winnersArray.length);
// Using floor so it will prevent retarded numbers (tho this is not how it should be)
else {
leaderboardAllTimeData[winner] = Math.floor(totalAmount / winnersArray.length); // Using floor so it will prevent retarded numbers (tho this is not how it should be)
}
});
/* Sorting leaderboard data */
var sortable = [];
for (var user in leaderboardAllTimeData) {
sortable.push([user, leaderboardAllTimeData[user]]);
}
sortable.sort((a, b) => {
return b[1] - a[1];
});
sortedLeaderboardAllTimeData = sortable;
allTimeTotalCoins += Number(totalAmount);
}
/* Check if the user already exists in table */
var exists = false,
thIndex = 0;
if (document.querySelector('#latestTrivias').querySelectorAll('th').length > 0) {
for (var i = 0; i < document.querySelector('#latestTrivias').querySelectorAll('th').length; i++) {
title = document.querySelector('#latestTrivias').querySelectorAll('th')[i];
if (title.innerText.split('\n')[0] == hoster) {
exists = true;
thIndex = i;
}
}
}
/* If the user exists, just add the trivia data to that user's own trivia table */
if (exists == true) {
var button = document.createElement('button');
button.className = 'triviaButton';
button.innerText = new Date(Number(timeStamp)).toLocaleString();
button.addEventListener('click', function() {
//alert(`Chat: ${chatName}\nQuestion: ${question}\nAnswer: ${answer}\nWinners: ${winners}\nTotal coins given out: ${totalAmount}`);
if (button.parentNode.querySelector('div').className.includes('hidden')) {
button.parentNode.querySelector('div').className = button.parentNode
.querySelector('div')
.className.replace('hidden', 'visible');
if (
button.parentNode.querySelector('div').getBoundingClientRect().left >
document.body.offsetWidth - 500
)
button.parentNode.querySelector('div').className += ' overWidthRight';
} else button.parentNode.querySelector('div').className = button.parentNode.querySelector('div').className.replace('visible', 'hidden');
});
var div = document.createElement('div');
div.className = 'triviaDetail hidden';
var span = document.createElement('span');
span.innerText = `Chat: ${chatName}\nQuestion: ${question}\nAnswer: ${answer}\nWinners: ${winners}\nTotal coins given out: ${totalAmount}`;
div.append(span);
var triviaButtonDiv = document.createElement('div');
triviaButtonDiv.className = 'triviaButtonDiv';
triviaButtonDiv.appendChild(button);
triviaButtonDiv.appendChild(div);
document
.querySelector('#latestTrivias')
.querySelectorAll('td')
[thIndex].appendChild(triviaButtonDiv);
document
.querySelector('#modList')
.querySelectorAll('th')
[thIndex].querySelector('.total').innerText =
Number(
document
.querySelector('#modList')
.querySelectorAll('th')
[thIndex].querySelector('.total').innerText
) + Number(totalAmount);
} else {
/* If the user doesn't exist, create that user's table and add the trivia data. */
var th = document.createElement('th');
th.innerHTML = `${hoster}<br /><span class="total">${totalAmount}</span>`;
document
.querySelector('#latestTrivias')
.querySelector('#modList')
.appendChild(th);
var td = document.createElement('td');
document
.querySelector('#latestTrivias')
.querySelector('#triviaList')
.appendChild(td);
for (var i = 0; i < document.querySelector('#latestTrivias').querySelectorAll('th').length; i++) {
title = document.querySelector('#latestTrivias').querySelectorAll('th')[i];
if (title.innerText.split('\n')[0] == hoster) {
exists = true;
thIndex = i;
}
}
var button = document.createElement('button');
button.className = 'triviaButton';
button.innerText = new Date(Number(timeStamp)).toLocaleString();
button.addEventListener('click', function() {
//alert(`Chat: ${chatName}\nQuestion: ${question}\nAnswer: ${answer}\nWinners: ${winners}\nTotal coins given out: ${totalAmount}`);
if (button.parentNode.querySelector('div').className.includes('hidden')) {
button.parentNode.querySelector('div').className = button.parentNode
.querySelector('div')
.className.replace('hidden', 'visible');
if (
button.parentNode.querySelector('div').getBoundingClientRect().left >
document.body.offsetWidth - 500
)
button.parentNode.querySelector('div').className += ' overWidthRight';
} else button.parentNode.querySelector('div').className = button.parentNode.querySelector('div').className.replace('visible', 'hidden');
});
var div = document.createElement('div');
div.className = 'triviaDetail hidden';
var span = document.createElement('span');
span.innerText = `Chat: ${chatName}\nQuestion: ${question}\nAnswer: ${answer}\nWinners: ${winners}\nTotal coins given out: ${totalAmount}`;
div.append(span);
var triviaButtonDiv = document.createElement('div');
triviaButtonDiv.className = 'triviaButtonDiv';
triviaButtonDiv.appendChild(button);
triviaButtonDiv.appendChild(div);
document
.querySelector('#latestTrivias')
.querySelectorAll('td')
[thIndex].appendChild(triviaButtonDiv);
}
}
});
//console.log(sortedLeaderboard24Data);
/* Add the sorted datas into the actual HTML page */
sortedLeaderboard24Data.forEach(userData => {
var tr = document.createElement('tr'),
tdUsername = document.createElement('td'),
tdCoinsWon = document.createElement('td');
tdUsername.innerText = userData[0];
tdCoinsWon.innerText = String(userData[1]);
tr.append(tdCoinsWon);
tr.prepend(tdUsername);
document.querySelector('#leaderboard24').append(tr);
});
sortedLeaderboardAllTimeData.forEach(userData => {
if (userData[0] && userData[1] && userData[0] !== 'cancelled') {
var tr = document.createElement('tr'),
tdUsername = document.createElement('td'),
tdCoinsWon = document.createElement('td');
tdUsername.innerText = userData[0];
tdCoinsWon.innerText = String(userData[1]);
tr.append(tdCoinsWon);
tr.prepend(tdUsername);
document.querySelector('#leaderboardAllTime').append(tr);
}
});
/* Information updates */
document.querySelector(
'#last24TotalCoins'
).innerHTML = `Today, we have given away a total of <span style="font-weight: bold;">${String(
todayTotalCoins.toMoney(0, '', ' ')
)}</span> coins.`;
document.querySelector(
'#allTimeTotalCoins'
).innerHTML = `We have given away a total of <span style="font-weight: bold;">${String(
allTimeTotalCoins.toMoney(0, '', ' ')
)}</span> coins since the start of this site.`;
}