From 0d6c7e5b459f4d6476470b7d8965190ef042ce0e Mon Sep 17 00:00:00 2001 From: Salia Nahshal Date: Thu, 12 Dec 2024 10:40:02 -0500 Subject: [PATCH] Fixed scoreboard update & sort --- server.py | 13 ++++--- static/scoreboard.js | 52 +++++++++++++------------ templates/scoreboard.html | 80 +++++++++++++++++++++------------------ 3 files changed, 79 insertions(+), 66 deletions(-) diff --git a/server.py b/server.py index 372a266c..4531572a 100644 --- a/server.py +++ b/server.py @@ -38,19 +38,22 @@ @app.route('/') def show_scoreboard(): - return render_template('scoreboard.html', scoreboard = scoreboard) + 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() + team_id = json_data["id"] + for team in scoreboard: if team["id"] == team_id: team["score"] += 1 + # Sort the scoreboard by score in descending order + scoreboard = sorted(scoreboard, key=lambda x: x["score"], reverse=True) + return jsonify(scoreboard=scoreboard) diff --git a/static/scoreboard.js b/static/scoreboard.js index 34ce2009..f9231242 100644 --- a/static/scoreboard.js +++ b/static/scoreboard.js @@ -1,19 +1,21 @@ -function display_scoreboard(scoreboard){ +function display_scoreboard(scoreboard) { $("#teams").empty(); - $.each(scoreboard, function(index, team){ + $.each(scoreboard, function (index, team) { addTeamView(team.id, team.name, team.score); }); } -function addTeamView(id, name, score){ - var team_template = $("
"); - var name_template = $("
"); - var score_template = $("
"); - var button_template = $("
"); - var increase_button = $(""); - $(increase_button).click(function(){ +function addTeamView(id, name, score) { + var team_template = $("
"); + var name_template = $("
"); + var score_template = $("
"); + var button_template = $("
"); + var increase_button = $(""); + + $(increase_button).click(function () { increase_score(id); }); + name_template.text(name); score_template.text(score); button_template.append(increase_button); @@ -23,26 +25,28 @@ function addTeamView(id, name, score){ $("#teams").append(team_template); } -function increase_score(id){ - var team_id = {"id": id} +function increase_score(id) { + var team_id = { id: id }; + $.ajax({ type: "POST", - url: "increase_score", - dataType : "json", + url: "increase_score", + dataType: "json", contentType: "application/json; charset=utf-8", - data : JSON.stringify(team_id), - success: function(result){ - + data: JSON.stringify(team_id), + success: function (result) { + // Re-render the scoreboard with the updated data + display_scoreboard(result.scoreboard); + }, + error: function (request, status, error) { + console.log("Error"); + console.log(request); + console.log(status); + console.log(error); }, - error: function(request, status, error){ - console.log("Error"); - console.log(request) - console.log(status) - console.log(error) - } }); } -$(document).ready(function(){ +$(document).ready(function () { display_scoreboard(scoreboard); -}) +}); diff --git a/templates/scoreboard.html b/templates/scoreboard.html index 7b168c48..8d4d8e8a 100644 --- a/templates/scoreboard.html +++ b/templates/scoreboard.html @@ -1,42 +1,48 @@ - - - - - - - - - - -
-
- 2020 NHL Scoreboard -
-
-
-
+ + + + + + + + + + + +
+
+ 2020 NHL Scoreboard +
+
+
+
- +
+ + \ No newline at end of file