From 6c49a545605f1f7d94396a77cb01f159ba72a94c Mon Sep 17 00:00:00 2001 From: Sasha Ortiz-Kalina Date: Fri, 12 Dec 2025 22:16:17 -0500 Subject: [PATCH] Sasha Ortiz-Kalina so2787 CBL --- .vscode/settings.json | 5 ++++ server.py | 2 +- static/scoreboard.js | 53 ++++++++++++++++++++++++++++----------- templates/scoreboard.html | 8 +++--- 4 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..9cbf4d29 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "**/templates/*.html": "jinja-html" + } +} diff --git a/server.py b/server.py index 372a266c..6bbc1466 100644 --- a/server.py +++ b/server.py @@ -50,7 +50,7 @@ def increase_score(): for team in scoreboard: if team["id"] == team_id: team["score"] += 1 - + scoreboard.sort(key=lambda t: t['score'], reverse=True) return jsonify(scoreboard=scoreboard) diff --git a/static/scoreboard.js b/static/scoreboard.js index 34ce2009..88b51a89 100644 --- a/static/scoreboard.js +++ b/static/scoreboard.js @@ -8,9 +8,11 @@ function display_scoreboard(scoreboard){ function addTeamView(id, name, score){ var team_template = $("
"); var name_template = $("
"); - var score_template = $("
"); +var score_template = $("
"); +score_template.text(score); var button_template = $("
"); var increase_button = $(""); + increase_button.data("teamId", id); $(increase_button).click(function(){ increase_score(id); }); @@ -24,25 +26,48 @@ function addTeamView(id, name, score){ } function increase_score(id){ - var team_id = {"id": id} + var $scoreEl = $("#score-" + id); + + var $btn = $(":button.increase-button").filter(function(){ return $(this).data("teamId") === id; }); + + + var current = parseInt($scoreEl.text(), 10) || 0; + + // UI update + $scoreEl.text(current + 1); + $btn.prop("disabled", true); + 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), + data: JSON.stringify(team_id), success: function(result){ - - }, + if (result && result.scoreboard) { + scoreboard = result.scoreboard.slice(); // replace local model + display_scoreboard(scoreboard); + } else { + + var $scoreEl = $("#score-" + id); + $scoreEl.text((parseInt($scoreEl.text(), 10) || 0) + 1); + } +}, error: function(request, status, error){ - console.log("Error"); - console.log(request) - console.log(status) - console.log(error) + $scoreEl.text(current); + console.error("Failed to increase score:", status, error); + alert("Could not update score — please try again."); + }, + complete: function(){ + $btn.prop("disabled", false); } }); } -$(document).ready(function(){ - display_scoreboard(scoreboard); -}) + $(document).ready(function(){ + if (typeof scoreboard !== 'undefined') { + display_scoreboard(scoreboard); + } else { + console.warn('scoreboard is undefined on page'); + } + }); diff --git a/templates/scoreboard.html b/templates/scoreboard.html index 7b168c48..5ade6658 100644 --- a/templates/scoreboard.html +++ b/templates/scoreboard.html @@ -2,12 +2,12 @@ - + + -