-
Notifications
You must be signed in to change notification settings - Fork 744
Expand file tree
/
Copy pathscoreboard.js
More file actions
56 lines (52 loc) · 1.69 KB
/
scoreboard.js
File metadata and controls
56 lines (52 loc) · 1.69 KB
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
function display_scoreboard(scoreboard){
// Sort by non-increasing score (stable for ties).
const sorted = scoreboard
.map((team, index) => ({ team, index }))
.sort((a, b) => (b.team.score - a.team.score) || (a.index - b.index))
.map(({ team }) => team);
$("#teams").empty();
$.each(sorted, function(index, team){
addTeamView(team.id, team.name, team.score);
});
}
function addTeamView(id, name, score){
var team_template = $("<div class = row></div>");
var name_template = $("<div class = col-md-5></div>");
var score_template = $("<div class = col-md-2></div>");
var button_template = $("<div class = col-md-2></div>");
var increase_button = $("<button class = increase-button>+</button>");
$(increase_button).click(function(){
increase_score(id);
});
name_template.text(name);
score_template.text(score);
button_template.append(increase_button);
team_template.append(name_template);
team_template.append(score_template);
team_template.append(button_template);
$("#teams").append(team_template);
}
function increase_score(id){
var team_id = {"id": id}
$.ajax({
type: "POST",
url: "increase_score",
dataType : "json",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(team_id),
success: function(result){
// Update the global scoreboard and re-render immediately.
scoreboard = result.scoreboard;
display_scoreboard(scoreboard);
},
error: function(request, status, error){
console.log("Error");
console.log(request)
console.log(status)
console.log(error)
}
});
}
$(document).ready(function(){
display_scoreboard(scoreboard);
})