diff --git a/server.py b/server.py index 372a266c..28237876 100644 --- a/server.py +++ b/server.py @@ -5,58 +5,57 @@ scoreboard = [ { - "id": 1, - "name": "Boston Bruins", - "score": 7 + "id": 1, + "name": "Boston Bruins", + "score": 7 }, { - "id": 2, - "name": "Tampa Bay Lightning", - "score": 5 + "id": 2, + "name": "Tampa Bay Lightning", + "score": 5 }, { - "id": 3, - "name": "Toronto Maple Leafs", - "score": 2 + "id": 3, + "name": "Toronto Maple Leafs", + "score": 2 }, { - "id": 4, - "name": "Florida Panthers", - "score": 1 + "id": 4, + "name": "Florida Panthers", + "score": 1 }, { - "id": 5, - "name": "Buffalo Sabres", - "score": 1 + "id": 5, + "name": "Buffalo Sabres", + "score": 1 }, ] @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']) def increase_score(): global scoreboard - json_data = request.get_json() - team_id = json_data["id"] - + json_data = request.get_json() + team_id = int(json_data["id"]) + for team in scoreboard: if team["id"] == team_id: team["score"] += 1 + scoreboard = sorted(scoreboard, key=lambda x: x["score"], reverse=True) + return jsonify(scoreboard=scoreboard) if __name__ == '__main__': - app.run(debug = True) - - - - + app.run(debug=True) diff --git a/static/scoreboard.js b/static/scoreboard.js index 34ce2009..d2a954c9 100644 --- a/static/scoreboard.js +++ b/static/scoreboard.js @@ -1,17 +1,17 @@ -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){ +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_button).click(function () { increase_score(id); }); name_template.text(name); @@ -23,26 +23,26 @@ 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) { + 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); -}) +});