-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoreboard.js
92 lines (78 loc) · 2.35 KB
/
scoreboard.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
var currentProblemCount = 0;
function fetchScoreboard(contestJid) {
$.get(config.serverUrl + "?contestJid=" + contestJid, function (data) {
refreshScoreboard(data);
setTimeout(function () {
fetchScoreboard(contestJid);
}, config.refreshIntervalInMs);
});
}
function refreshScoreboardProblems(problemAliases) {
$(".col-problem").remove();
for (var i = 0; i < problemAliases.length; i++) {
var alias = problemAliases[i];
var header = $("<th>");
header.prop("class", "col-score col-problem");
header.html(alias);
$("#headers").append(header);
}
}
function calculateScoreColor(score, problemCount) {
if (score == null) {
return "#eee";
}
var hue = (score * 120.0) / (100.0 * problemCount);
return "hsl(" + hue.toString() + ", 80%, 60%)";
}
function createRow(entry) {
var contestant = config.contestants[entry.contestantJid];
var tr = $("<tr>");
tr.append($("<td>").html(entry.rank));
tr.append($("<td>").html(contestant.username));
tr.append($("<td>").html(contestant.name));
tr.append($("<td>").html(contestant.school));
tr.append($("<td>").html(contestant.province));
tr.append(
$("<td>")
.html(entry["totalScores"])
.css({
"font-weight": "bold",
"background-color": calculateScoreColor(
entry["totalScores"],
currentProblemCount
),
})
);
for (var j = 0; j < entry["scores"].length; j++) {
var score = entry["scores"][j];
tr.append(
$("<td>")
.html(score)
.css({ "background-color": calculateScoreColor(score, 1) })
);
}
return tr;
}
function refreshScoreboardEntries(entries) {
$("#entries").empty();
for (var i = 0; i < entries.length; i++) {
$("#entries").append(createRow(entries[i]));
}
}
function updateProgressBar() {
var now = +new Date();
var progress = now - config.contestStartTimestampInMs;
var percentage = (progress * 100.0) / config.contestDurationInMs;
$("#progress-bar").css({ width: percentage + "%" });
}
function refreshScoreboard(data) {
var scoreboard = data.scoreboard;
var problemAliases = scoreboard.state.problemAliases;
var entries = scoreboard.content.entries;
if (currentProblemCount == 0) {
refreshScoreboardProblems(problemAliases);
}
currentProblemCount = problemAliases.length;
refreshScoreboardEntries(entries);
updateProgressBar();
}