-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
393 lines (301 loc) · 11.4 KB
/
main.mjs
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
import {html, LitElement} from "./lib/lit-element.mjs";
class ApiError {
constructor(status, statusText) {
this.status = status;
this.statusText = statusText;
}
}
async function request(url, options) {
return await fetch(url, options).then(async (response) => {
const contentType = response.headers.get('Content-Type');
if (contentType) {
if (contentType.includes('json')) {
if (response.ok) {
return response.json();
}
}
}
if (response.ok) {
return response.text();
}
throw new ApiError(response.status, response.statusText);
}).catch((errorInfo) => {
console.error(errorInfo);
throw errorInfo;
});
}
async function getCompetition() {
return request('competition-state/competition-summary.json');
}
function getValidScoreCounts(roundScores) {
const validScoreCounts = [];
for (const robotScores of roundScores) {
let validCount = 0;
for (const score of robotScores) {
if (score.isValid) {
validCount++;
}
}
validScoreCounts.push(validCount);
}
return validScoreCounts;
}
function roundToTwoDecimalPlaces(number) {
return Math.round((number + Number.EPSILON) * 100) / 100;
}
class CompetitionResults extends LitElement {
static get properties() {
return {
competitionInfo: {type: Object}
};
}
createRenderRoot() {
return this;
}
async fetchCompetitionInfo() {
try {
this.competitionInfo = await getCompetition();
} catch (apiError) {
if (apiError.status === 404) {
this.competitionInfo = {};
}
}
}
render() {
if (!this.competitionInfo) {
this.fetchCompetitionInfo();
return html`<div>Loading...</div>`;
}
if (!this.competitionInfo.name) {
return null;
}
return html`${this.renderHeader()}
${this.renderCompetitionResults()}
${this.renderDoubleElimination()}
${this.renderSwiss()}`;
}
renderHeader() {
if (this.competitionInfo.name)
return html`<h1>${`${this.competitionInfo.name}`}</h1>`
}
renderCompetitionResults() {
const deInfo = this.competitionInfo.doubleEliminationTournament;
if (!deInfo) {
return null;
}
const robotCount = deInfo.robots.length;
let firstPlaceRobot = deInfo.noLossQueue.length + deInfo.oneLossQueue.length === 1
? deInfo.noLossQueue[0] || deInfo.oneLossQueue[0]
: null;
let secondPlaceRobot = deInfo.eliminatedRobots[robotCount - 2];
let thirdPlaceRobot = deInfo.eliminatedRobots[robotCount - 3];
if (!thirdPlaceRobot) {
return null;
}
return html`<ul>
<li>Winner: ${firstPlaceRobot ? firstPlaceRobot.name : '???'}</li>
<li>2nd place: ${secondPlaceRobot ? secondPlaceRobot.name : '???'}</li>
<li>3rd place: ${thirdPlaceRobot ? thirdPlaceRobot.name : '???'}</li>
</ul>`;
}
renderRobots(robots) {
if (!Array.isArray(robots)) {
return null;
}
return html`<h2>Robots</h2>
<ol>${robots.map(r => this.renderRobot(r))}</ol>`;
}
renderRobot(robot) {
return html`<li>${`${robot.name}`}</li>`;
}
renderSwiss() {
const swissInfo = this.competitionInfo.swissSystemTournament;
if (!swissInfo || !swissInfo.games) {
return null;
}
return html`<h2>Swiss-system tournament</h2>
${this.renderSwissScoreboard()}
${this.renderSwissGamesList()}
${this.renderSwissGamePointExplanation()}`;
}
renderSwissGamesList() {
const swissInfo = this.competitionInfo.swissSystemTournament;
if (!swissInfo || !swissInfo.games) {
return null;
}
const {roundCount, byes} = swissInfo;
const rounds = [];
const gamesPerRound = Math.floor(swissInfo.robots.length / 2);
for (const [index, game] of swissInfo.games.entries()) {
const roundIndex = Math.floor(index / gamesPerRound);
if (!rounds[roundIndex]) {
rounds[roundIndex] = [];
}
rounds[roundIndex].push(game);
}
const reversedRounds = rounds.slice().reverse();
return html`${reversedRounds.map((r, index) => this.renderSwissGamesRound(rounds.length - index, roundCount, r, byes[rounds.length - index - 1]))}`
}
renderSwissGamesRound(roundNumber, roundsInTotal, games, bye) {
return html`<h3>Round ${roundNumber} of ${roundsInTotal}</h3>
<ul>
${games.map(g => this.renderGamesListItem(g))}
${this.renderBye(bye)}
</ul>`
}
renderBye(bye) {
if (!bye) {
return null;
}
const robot = this.competitionInfo.robots.find(r => r.id === bye.robotID);
if (!robot) {
return null;
}
return html`<li>Bye: ${robot.name} | bye = 1 point</li>`;
}
renderGamesListItem(game, gameType) {
let robotsText = `${game.robots[0].name} vs ${game.robots[1].name}`;
const {status} = game;
if (status.result === 'unknown' && game.rounds.length === 0 || !game.rounds[0].hasEnded) {
return html`<li>${robotsText}</li>`;
}
const {result} = status;
let roundsText = '';
for (const round of game.rounds) {
if (!round.hasEnded) {
continue;
}
const validScoreCounts = getValidScoreCounts(round.scores);
roundsText += ` (${validScoreCounts[0]} - ${validScoreCounts[1]})`
}
if (game.freeThrows) {
roundsText += ` (${game.freeThrows.scores[0]} - ${game.freeThrows.scores[1]})`
}
if (status.result === 'unknown') {
return html`<li>${robotsText} | ${roundsText}</li>`;
}
const resultContent = result === 'won'
? html`<b>${status.winner.name} ${result}</b>`
: `${result}`;
let pointsText = '';
if (!gameType) {
const roundCount = game.rounds.length;
pointsText += ' (';
if (result === 'tied') {
pointsText += '0.5 points';
} else {
if (roundCount === 2) {
pointsText += '1 point';
} else {
if (status.roundWinCount === 2 && status.roundTieCount === 1) {
pointsText += '0.9 points';
} else if (status.roundWinCount === 2 && status.roundLossCount === 1) {
pointsText += '0.8 points';
} else if (status.roundWinCount === 1 && status.roundTieCount === 2) {
pointsText += '0.7 points';
}
}
}
pointsText += ')';
}
return html`<li>${robotsText} | ${roundsText} | ${resultContent}${pointsText}</li>`;
}
renderSwissGamePointExplanation() {
return html`<h3>Swiss-system tournament game point system</h3>
<table>
<thead><th>Result</th><th>Robot 1 (winner) points</th><th>Robot 2 points</th></thead>
<tbody>
<tr><td>2 out of 2 round wins</td><td>1</td><td>0</td></tr>
<tr><td>2 out of 3 round wins and 1 tied round</td><td>0.9</td><td>0.1</td></tr>
<tr><td>2 out of 3 round wins and 1 lost round</td><td>0.8</td><td>0.2</td></tr>
<tr><td>1 out of 3 round wins and 2 tied rounds</td><td>0.7</td><td>0.3</td></tr>
<tr><td>Tie</td><td>0.5</td><td>0.5</td></tr>
</tbody>
</table>`;
}
renderSwissScoreboard() {
const swissInfo = this.competitionInfo.swissSystemTournament;
if (!swissInfo) {
return null;
}
const orderedScores = swissInfo.robotScores.slice();
orderedScores.sort((a, b) => {
if (a.score === b.score) {
return b.tieBreakScore - a.tieBreakScore;
}
return b.score - a.score;
});
return html`<h3>Scoreboard</h3>
<table>
<thead><tr><th>Name</th><th>Score</th><th>Tiebreak score</th></tr></thead>
<tbody>${orderedScores.map(s => this.renderSwissScoreboardRow(s))}</tbody>
</table>`
}
renderSwissScoreboardRow(robotScore) {
return html`<tr>
<td>${robotScore.robot.name}</td>
<td>${roundToTwoDecimalPlaces(robotScore.score)}</td>
<td>${roundToTwoDecimalPlaces(robotScore.tieBreakScore)}</td>
</tr>`;
}
renderDoubleElimination() {
const deInfo = this.competitionInfo.doubleEliminationTournament;
if (!deInfo) {
return null;
}
return html`<h2>Double elimination tournament</h2>
${this.renderDoubleEliminationQueues(deInfo)}`;
}
renderDoubleEliminationGames(deInfo) {
return html`<h2>Double elimination games</h2>
<ul>${deInfo.games.map(g => this.renderGamesListItem(g, deInfo.gameTypes[g.id]))}</ul>`
}
renderDoubleEliminationQueues(deInfo) {
const {games, gameTypes} = deInfo;
const noLossGames = [];
const oneLossGames = [];
const finalGames = [];
for (const game of games) {
const gameType = gameTypes[game.id];
if (gameType === 'noLoss') {
noLossGames.push(game);
} else if (gameType === 'oneLoss') {
oneLossGames.push(game);
} else if (gameType.endsWith('Final')) {
finalGames.push(game);
}
}
return html`${this.renderDoubleEliminationFinalGames(finalGames)}
<h3>No games lost</h3>
<ul>${noLossGames.map(g => this.renderGamesListItem(g, gameTypes[g.id]))}</ul>
${this.renderDoubleEliminationNextGames(deInfo.noLossQueue)}
<h3>1 game lost</h3>
<ul>${oneLossGames.map(g => this.renderGamesListItem(g, gameTypes[g.id]))}</ul>
${this.renderDoubleEliminationNextGames(deInfo.oneLossQueue)}
<h3>Eliminated</h3>
<ul>${deInfo.eliminatedRobots.map(r => this.renderRobot(r))}</ul>`
}
renderDoubleEliminationFinalGames(games) {
if (games.length === 0) {
return null;
}
const deInfo = this.competitionInfo.doubleEliminationTournament;
const {gameTypes} = deInfo;
return html`<h3>Final games</h3>
<ul>${games.map(g => this.renderGamesListItem(g, gameTypes[g.id]))}</ul>`;
}
renderDoubleEliminationNextGames(robots) {
const matches = [];
for (let i = 0; i < robots.length; i += 2) {
matches.push(robots.slice(i, i+ 2));
}
return html`<ul>${matches.map(m => this.renderMatch(m))}</ul>`;
}
renderMatch(robots) {
if (robots.length === 1) {
return html`<li>${robots[0].name}</li>`;
}
return html`<li>${robots[0].name} vs ${robots[1].name}</li>`;
}
}
customElements.define('competition-results', CompetitionResults);