Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Flask
from flask import render_template
from flask import Response, request, jsonify
from flask import request, jsonify
app = Flask(__name__)

scoreboard = [
Expand Down Expand Up @@ -36,21 +36,30 @@
]


def sort_scoreboard_in_place():
scoreboard.sort(key=lambda team: team["score"], reverse=True)


@app.route('/')
def show_scoreboard():
sort_scoreboard_in_place()
return render_template('scoreboard.html', scoreboard = scoreboard)

@app.route('/increase_score', methods=['GET', 'POST'])
@app.route('/increase_score', methods=['POST'])
def increase_score():
global scoreboard

json_data = request.get_json()
team_id = json_data["id"]
json_data = request.get_json(silent=True) or {}
team_id = json_data.get("id")
if team_id is None:
return jsonify(error="Missing required field 'id'"), 400

for team in scoreboard:
if team["id"] == team_id:
team["score"] += 1
break

sort_scoreboard_in_place()
return jsonify(scoreboard=scoreboard)


Expand All @@ -59,4 +68,3 @@ def increase_score():




12 changes: 10 additions & 2 deletions static/scoreboard.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
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(scoreboard, function(index, team){
$.each(sorted, function(index, team){
addTeamView(team.id, team.name, team.score);
});
}
Expand Down Expand Up @@ -32,7 +38,9 @@ function increase_score(id){
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");
Expand Down