This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard.js
265 lines (238 loc) · 8.15 KB
/
leaderboard.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
const sizes = ['small', 'medium', 'large'];
const numInputsPerSize = [300, 300, 300];
// const broken_files = ['large-135', 'medium-219', 'large-219', 'large-218', 'large-208', 'large-224', 'medium-225', 'large-235']
let teamSet = new Set();
let firebaseData = null;
var dict = {}
async function getFirebaseData() {
const response = await fetch('https://www.dl.dropboxusercontent.com/s/6bnhgv9qk59z2au/cs170-project-spring-2021-default-rtdb-export.json?dl=1');
const data = await response.json();
return data;
}
async function loadTeams() {
teamSet = new Set();
if (firebaseData == null) {
firebaseData = await getFirebaseData();
}
for (let key in firebaseData['leaderboard']) {
const item = firebaseData['leaderboard'][key];
teamSet.add(item["leaderboard_name"]);
}
return teamSet;
}
async function loadAutocomplete(input) {
if (!teamSet.size) {
teams = await loadTeams();
console.warn("Teams not loaded, check pullLeaderboard functions.");
}
new Awesomplete(input, {list: Array.from(teamSet)});
}
function round(x) {
return Math.round((x + Number.EPSILON) * 10000) / 10000;
}
async function pullLeaderboard(graphName) {
if (firebaseData == null) {
firebaseData = await getFirebaseData();
}
const entries = [];
for (let key in firebaseData['leaderboard']) {
const entry = firebaseData['leaderboard'][key];
if (entry['input'] == graphName) {
entries.push([entry['leaderboard_name'], round(entry['score'])]);
}
}
return entries.sort((elem1, elem2) => elem2[1] - elem1[1]);
}
async function pullFullLeaderboard(firebase) {
if (firebaseData == null) {
firebaseData = await getFirebaseData();
}
const leaderboards = {};
for (let key in firebaseData['leaderboard']) {
const entry = firebaseData['leaderboard'][key];
const name = entry['leaderboard_name'];
const score = round(entry['score']);
const inputName = entry['input'];
if (!leaderboards.hasOwnProperty(inputName)) {
leaderboards[inputName] = [];
}
leaderboards[inputName].push([name, score]);
}
return leaderboards;
}
async function computeFullLeaderboard() {
const leaderboards = await pullFullLeaderboard();
const namesAndRanks = {};
let totalInputs = 0;
for (let i = 0; i < sizes.length; i++) {
const size = sizes[i];
for (let j = 1; j <= numInputsPerSize[i]; j++) {
const graphName = `${size}-${j}`;
// if (broken_files.includes(graphName)) {continue;}
const leaderboard = leaderboards[graphName].sort((elem1, elem2) => elem2[1] - elem1[1]);
const ranks = getRanks(leaderboard);
for (let i = 0; i < leaderboard.length; i++) {
entry = leaderboard[i];
name = entry[0];
score = entry[1];
if (!namesAndRanks.hasOwnProperty(name)) {
namesAndRanks[name] = [];
}
namesAndRanks[name].push([graphName, ranks[i]]);
}
totalInputs++;
}
}
const finalEntries = [];
const not_full_outputs = [];
for (let name in namesAndRanks) {
const base_scores = namesAndRanks['WelcomeToTheLeaderboard'];
const scores = namesAndRanks[name];
test = scores;
if (scores.length == totalInputs) {
const average = scores.reduce((a, b) => a + b[1], 0) / totalInputs;
finalEntries.push([name, round(average)]);
} else {
not_full_outputs.push(name);
var score = 0;
for (let i = 0; i < base_scores.length; i++) {
var found = false;
for (let j = 0; j < scores.length; j++) {
if (base_scores[i][0] === scores[j][0]) {
score = score + scores[j][1];
found = true;
}
}
if (!found) {
score = score + base_scores[i][1];
}
}
const average = score / totalInputs;
finalEntries.push([name, round(average)]);
}
}
return [namesAndRanks, finalEntries.sort((elem1, elem2) => elem1[1] - elem2[1])];
}
function getRanks(sortedEntries) {
let currentRank = 0;
let prevValue = -1;
const ranks = [];
for (let i = 0; i < sortedEntries.length; i++) {
const entry = sortedEntries[i];
if (entry[1] != prevValue) {
currentRank = i + 1;
prevValue = entry[1];
}
ranks.push(currentRank);
}
return ranks;
}
function formatTable(sortedEntries, header, addRanks) {
const table = document.createElement('table');
const thead = document.createElement('thead');
const headerElements = [];
header.forEach((elem) => headerElements.push(`<th>${elem}</th>`));
thead.innerHTML = headerElements.join('\n');
table.appendChild(thead);
table.className = 'table';
let ranks = null;
if (addRanks) {
ranks = getRanks(sortedEntries);
}
for (let i = 0; i < sortedEntries.length; i++) {
const row = document.createElement('tr');
var currentRank = 0
entry = sortedEntries[i];
if (addRanks) {
currentRank = ranks[i];
const rank = document.createElement('th');
rank.innerHTML = currentRank;
row.appendChild(rank);
}
for (let j = 0; j < entry.length; j++) {
const elem = document.createElement('td');
elem.innerHTML = entry[j];
row.appendChild(elem);
}
table.appendChild(row);
}
console.log(dict)
return table;
}
function createLeaderboard(leaderboardEntries, header, title) {
for (let i = 0; i < leaderboardEntries.length; i++) {
const entry = leaderboardEntries[i];
entry[0] = `<span class="team-link"><a onclick="moveToTeam('${entry[0]}')">${entry[0]}</a></span>`;
}
document.getElementById('table-title').innerHTML = title;
document.getElementById('table').appendChild(
formatTable(leaderboardEntries, header, true)
);
}
function createTeamView(teamEntries, teamName) {
for (let i = 0; i < teamEntries.length; i++) {
const entry = teamEntries[i];
entry[0] = `<span class="leaderboard-link"><a onclick="moveToLeaderboard('${entry[0]}')">${entry[0]}</a></span>`;
}
document.getElementById('table-title').innerHTML = `Team <code>${teamName}</code>`;
document.getElementById('table').appendChild(
formatTable(teamEntries, ["Input Name", "Rank"], false)
);
}
async function generateRanksForTeam(teamName) {
fullLeaderboardResults = await computeFullLeaderboard();
namesAndRanks = fullLeaderboardResults[0];
return namesAndRanks.hasOwnProperty(teamName) ? namesAndRanks[teamName] : null;
}
async function generateTeamView(teamName) {
const entries = await generateRanksForTeam(teamName);
document.getElementById("table").innerHTML = '';
if (entries == null) {
const div = document.createElement('h1');
div.innerHTML = 'Invalid team name';
div.className = 'invalid';
document.getElementById("table").appendChild(div);
} else {
createTeamView(entries, teamName);
}
}
async function generateLeaderboard(graphName) {
let entries = null;
let header = null;
let title = null;
if (graphName === null) {
fullLeaderboardResults = await computeFullLeaderboard();
entries = fullLeaderboardResults[1];
header = ["#", "Team Name", "Average Rank"];
title = "";
} else {
entries = await pullLeaderboard(graphName);
if (entries.length > 0) {
header = ["#", "Team Name", "Score"];
title = `<code>${graphName}.in</code>`;
} else {
header = [];
title = "";
}
}
document.getElementById("table").innerHTML = '';
if (entries.length == 0) {
const div = document.createElement('h1');
div.innerHTML = 'Invalid input name';
div.className = 'invalid';
document.getElementById("table").appendChild(div);
} else {
createLeaderboard(entries, header, title);
}
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
return null;
}