mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-13 14:13:18 -06:00
migration to API only generation, ajax + team totalscore/rank calculation, page title configuration option
This commit is contained in:
@@ -14,6 +14,7 @@ configs = {
|
|||||||
|
|
||||||
class Config(object):
|
class Config(object):
|
||||||
# Main Configuration
|
# Main Configuration
|
||||||
|
APPLICATION_TITLE = 'EfTA Trivia Night'
|
||||||
SCORE_FILE = 'scores.json'
|
SCORE_FILE = 'scores.json'
|
||||||
POLLING_INTERVAL = 5
|
POLLING_INTERVAL = 5
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
@@ -26,7 +27,7 @@ class Config(object):
|
|||||||
DEMO_MAX_SCORES = 0
|
DEMO_MAX_SCORES = 0
|
||||||
|
|
||||||
|
|
||||||
def ConfigDeprecated(Config):
|
class ConfigDeprecated(Config):
|
||||||
CONVERT_OLD = True
|
CONVERT_OLD = True
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,4 +15,4 @@ def index():
|
|||||||
"""
|
"""
|
||||||
from trivia.utils import teams
|
from trivia.utils import teams
|
||||||
scoreCount = max([len(team.scores) for team in teams]) if len(teams) > 0 else 0
|
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'])
|
||||||
|
|||||||
@@ -64,24 +64,86 @@ function sortUsingNestedText(parent, childSelector, keySelector) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Sorts all Teams, rearranging them by Rank
|
// Sorts all Teams, rearranging them by Rank
|
||||||
function SortTeams(topTeam = 5) {
|
function sortTeams(topTeam = 5) {
|
||||||
// Sort by total score
|
// Sort by total score
|
||||||
sortUsingNestedText($(".js-standings"), '.ui-row', 'td.js-total-score')
|
sortUsingNestedText($(".js-standings"), '.ui-row', 'td.js-total-score')
|
||||||
|
|
||||||
// Push
|
// Push
|
||||||
console.log(parseInt($('.js-standings').find('>:first-child').data('row-index')))
|
console.log(parseInt($('.js-standings').find('>:first-child').data('row-index')))
|
||||||
|
// TODO: Fix scrolling functionality
|
||||||
// while ( !== topTeam) {
|
// 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
|
// Client Initialization
|
||||||
$().ready(function () {
|
$().ready(function () {
|
||||||
// Setup all click functions
|
// Setup all click functions
|
||||||
$(".js-scroll-row").on("click", ToggleAutoscroll);
|
$(".js-scroll-row").on("click", ToggleAutoscroll);
|
||||||
$(".js-refresh").on("click", ToggleAutorefresh);
|
$(".js-refresh").on("click", ToggleAutorefresh);
|
||||||
|
|
||||||
ToggleAutoscroll();
|
// ToggleAutoscroll();
|
||||||
ToggleAutorefresh();
|
// ToggleAutorefresh();
|
||||||
SortTeams();
|
// sortTeams();
|
||||||
|
refresh()
|
||||||
})
|
})
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<link rel="stylesheet" type="text/css" href="../static/style.css"/>
|
<link rel="stylesheet" type="text/css" href="../static/style.css"/>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>EfTA Trivia Night Scores</title>
|
<title>{{ title }}</title>
|
||||||
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
||||||
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
|
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
|
||||||
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
|
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
|
||||||
@@ -14,7 +14,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<header class="ui-header">
|
<header class="ui-header">
|
||||||
<span class="ui-header-title">
|
<span class="ui-header-title">
|
||||||
EfTA Trivia Night
|
{{ title }}
|
||||||
</span>
|
</span>
|
||||||
<span class="controls pointer">
|
<span class="controls pointer">
|
||||||
<span class="js-scroll-row">
|
<span class="js-scroll-row">
|
||||||
@@ -44,21 +44,6 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody class="js-standings">
|
<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>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user