mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-06 07:16:38 -06:00
fix scheduler not running, template rendering route implementation, implement file change modifed checks
This commit is contained in:
@@ -1 +1,2 @@
|
||||
Flask~=1.1.2
|
||||
Flask~=1.1.2
|
||||
APScheduler~=3.6.3
|
||||
@@ -15,7 +15,7 @@ from trivia import routes, api, utils
|
||||
|
||||
# Setup a scheduler for automatically refreshing data
|
||||
scheduler = BackgroundScheduler()
|
||||
scheduler.start()
|
||||
scheduler.add_job(func=utils.refreshScores, trigger="interval", seconds=5)
|
||||
|
||||
utils.refreshScores()
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ routes.py
|
||||
|
||||
Handles user frontend routes.
|
||||
"""
|
||||
from flask import render_template
|
||||
|
||||
from trivia import app
|
||||
|
||||
@@ -14,4 +15,6 @@ def index():
|
||||
|
||||
:return:
|
||||
"""
|
||||
return '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)
|
||||
|
||||
@@ -5,6 +5,7 @@ Stores important backend application functionality.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from typing import List
|
||||
|
||||
from trivia import Team
|
||||
|
||||
@@ -12,8 +13,9 @@ from trivia import Team
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
DATA_DIR = os.path.join(BASE_DIR, 'data')
|
||||
|
||||
# data: List[Team] = []
|
||||
teams = []
|
||||
# Initialize global data/tracking vars
|
||||
lastChange: int = -1
|
||||
teams: List[Team] = []
|
||||
|
||||
|
||||
def refreshScores() -> None:
|
||||
@@ -23,22 +25,32 @@ def refreshScores() -> None:
|
||||
:return:
|
||||
"""
|
||||
|
||||
try:
|
||||
print('Attempting to load and parse scores file.')
|
||||
with open(os.path.join(DATA_DIR, 'scores.json')) as file:
|
||||
temp = json.load(file)
|
||||
global lastChange
|
||||
filepath = os.path.join(DATA_DIR, 'scores.json')
|
||||
curChange = os.stat(filepath).st_mtime
|
||||
|
||||
# Place all values into Team object for jinja
|
||||
temp = [
|
||||
Team(
|
||||
id=team['teamno'],
|
||||
name=team['teamname'],
|
||||
scores=team['scores']
|
||||
) for team in temp
|
||||
]
|
||||
print(f'Successfully loaded ({len(temp)} teams).')
|
||||
global teams
|
||||
teams = temp
|
||||
# If invalid or inaccessible, simply do nothing.
|
||||
except json.JSONDecodeError:
|
||||
print('Scores file could not be opened or parsed.')
|
||||
if lastChange < curChange:
|
||||
try:
|
||||
# Update tracking var
|
||||
lastChange = curChange
|
||||
|
||||
print('Attempting to load and parse scores file.')
|
||||
with open(filepath) as file:
|
||||
temp = json.load(file)
|
||||
|
||||
# Place all values into Team object for jinja
|
||||
temp = [
|
||||
Team(
|
||||
id=team['teamno'],
|
||||
name=team['teamname'],
|
||||
scores=team['scores']
|
||||
) for team in temp
|
||||
]
|
||||
print(f'Successfully loaded ({len(temp)} teams).')
|
||||
|
||||
global teams
|
||||
teams = temp
|
||||
|
||||
# If invalid or inaccessible, simply do nothing.
|
||||
except json.JSONDecodeError:
|
||||
print('Scores file could not be opened or parsed.')
|
||||
|
||||
Reference in New Issue
Block a user