From 5e7a659f8c4a7516a039ea9585d266be983b071d Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 27 Jun 2020 18:40:56 -0500 Subject: [PATCH] migration to API only generation, ajax + team totalscore/rank calculation, page title configuration option --- trivia/config.py | 3 +- trivia/routes.py | 2 +- trivia/static/script.js | 72 ++++++++++++++++++++++++++++++++++--- trivia/templates/index.html | 19 ++-------- 4 files changed, 72 insertions(+), 24 deletions(-) diff --git a/trivia/config.py b/trivia/config.py index 210e123..1a8e1ac 100644 --- a/trivia/config.py +++ b/trivia/config.py @@ -14,6 +14,7 @@ configs = { class Config(object): # Main Configuration + APPLICATION_TITLE = 'EfTA Trivia Night' SCORE_FILE = 'scores.json' POLLING_INTERVAL = 5 DEBUG = False @@ -26,7 +27,7 @@ class Config(object): DEMO_MAX_SCORES = 0 -def ConfigDeprecated(Config): +class ConfigDeprecated(Config): CONVERT_OLD = True diff --git a/trivia/routes.py b/trivia/routes.py index 758419f..8338cb4 100644 --- a/trivia/routes.py +++ b/trivia/routes.py @@ -15,4 +15,4 @@ def index(): """ from trivia.utils import teams scoreCount = max([len(team.scores) for team in teams]) if len(teams) > 0 else 0 - return render_template('index.html', scoreCount=scoreCount, teams=teams) + return render_template('index.html', scoreCount=scoreCount, teams=teams, title=current_app.config['APPLICATION_TITLE']) diff --git a/trivia/static/script.js b/trivia/static/script.js index a7511bd..0faf654 100644 --- a/trivia/static/script.js +++ b/trivia/static/script.js @@ -64,24 +64,86 @@ function sortUsingNestedText(parent, childSelector, keySelector) { } // Sorts all Teams, rearranging them by Rank -function SortTeams(topTeam = 5) { +function sortTeams(topTeam = 5) { // Sort by total score sortUsingNestedText($(".js-standings"), '.ui-row', 'td.js-total-score') // Push console.log(parseInt($('.js-standings').find('>:first-child').data('row-index'))) + // TODO: Fix scrolling functionality // while ( !== topTeam) { - // $(".ui-row:first").appendTo("tbody") + // $(".ui-row:first").appendTo("tbody") // } } +// Returns the current Team ID at the top of the score +function currentTopTeam() { + return parseInt($('.js-standings').find('>:first-child').data('row-index')); +} + +// Sorting comparator for Team JSON objects (post 'total' field calculation) +function ScoreComparator(a, b) { + if (a.total < b.total) + return 1; + if (a.total > b.total) + return -1; + return a.id - b.id; +} + +// Formally refreshes all data in the table with the server while preserving scroll position. +function refresh() { + // Remember the team at the top + let pretop = currentTopTeam() + + // Send AJAX GET request + // TODO: Implement If-Modified Header + $.ajax({type: "GET", url: "/api/scores/", dataType: "json"}).done(function (teams) { + // Calculate Team Score Total + for (let i = 0; i < teams.length; i++) { + teams[i].total = 0 + for (let j = 0; j < teams[i].scores.length; j++) + teams[i].total += teams[i].scores[j] + } + + teams = teams.sort(ScoreComparator) + + // Calculate Team Rank + teams[0].rank = "1" + let prevRank = 1; + let prevTie = false; + for (let i = 1; i < teams.length; i++) { + // If current and previous teams have equal scores, mark them as being tied + if (teams[i].total === teams[i - 1].total) { + teams[i].rank = `T${prevRank}` + + // Checks if this is the first tie item, if not it updates the first item in the 'tie sequence' + if (!prevTie) { + prevTie = true; + teams[i - 1].rank = `T${teams[i - 1].rank}` + } + } else { + prevTie = false; + prevRank++; + teams[i].rank = prevRank.toString() + } + } + + // Ensure a space (or char T) exists at the start of each Rank + for (let i = 0; i < teams.length; i++) { + teams[i].rank = teams[i].rank.padStart(2) + } + }) +} + + // Client Initialization $().ready(function () { // Setup all click functions $(".js-scroll-row").on("click", ToggleAutoscroll); $(".js-refresh").on("click", ToggleAutorefresh); - ToggleAutoscroll(); - ToggleAutorefresh(); - SortTeams(); + // ToggleAutoscroll(); + // ToggleAutorefresh(); + // sortTeams(); + refresh() }) \ No newline at end of file diff --git a/trivia/templates/index.html b/trivia/templates/index.html index 0377418..333baab 100644 --- a/trivia/templates/index.html +++ b/trivia/templates/index.html @@ -3,7 +3,7 @@ - EfTA Trivia Night Scores + {{ title }} @@ -14,7 +14,7 @@
- EfTA Trivia Night + {{ title }} @@ -44,21 +44,6 @@ - {% for team in teams %} - - ? - {{ team.id }} - {% if team.name | length > 0 %} - {{ team.name }} - {% else %} - Team {{ team.id }} - {% endif %} - {{ team.scores | sum }} - {% for score in team.scores %} - {{ score }} - {% endfor %} - - {% endfor %}