migration to API only generation, ajax + team totalscore/rank calculation, page title configuration option

This commit is contained in:
Xevion
2020-06-27 18:40:56 -05:00
parent f23b07be82
commit 5e7a659f8c
4 changed files with 72 additions and 24 deletions

View File

@@ -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

View File

@@ -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'])

View File

@@ -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()
})

View File

@@ -3,7 +3,7 @@
<head>
<link rel="stylesheet" type="text/css" href="../static/style.css"/>
<meta charset="UTF-8">
<title>EfTA Trivia Night Scores</title>
<title>{{ title }}</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
@@ -14,7 +14,7 @@
<body>
<header class="ui-header">
<span class="ui-header-title">
EfTA Trivia Night
{{ title }}
</span>
<span class="controls pointer">
<span class="js-scroll-row">
@@ -44,21 +44,6 @@
</tr>
</thead>
<tbody class="js-standings">
{% for team in teams %}
<tr class="ui-row" data-row-index="{{ loop.index - 1}}">
<td class="js-rank">?</td>
<td>{{ team.id }}</td>
{% if team.name | length > 0 %}
<td>{{ team.name }}</td>
{% else %}
<td class="ui-silent-row">Team {{ team.id }}</td>
{% endif %}
<td class="js-total-score">{{ team.scores | sum }}</td>
{% for score in team.scores %}
<td class="small-no-wrap right">{{ score }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>