mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-08 00:08:45 -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
|
# Setup a scheduler for automatically refreshing data
|
||||||
scheduler = BackgroundScheduler()
|
scheduler = BackgroundScheduler()
|
||||||
|
scheduler.start()
|
||||||
scheduler.add_job(func=utils.refreshScores, trigger="interval", seconds=5)
|
scheduler.add_job(func=utils.refreshScores, trigger="interval", seconds=5)
|
||||||
|
|
||||||
utils.refreshScores()
|
utils.refreshScores()
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ routes.py
|
|||||||
|
|
||||||
Handles user frontend routes.
|
Handles user frontend routes.
|
||||||
"""
|
"""
|
||||||
|
from flask import render_template
|
||||||
|
|
||||||
from trivia import app
|
from trivia import app
|
||||||
|
|
||||||
@@ -14,4 +15,6 @@ def index():
|
|||||||
|
|
||||||
:return:
|
: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 json
|
||||||
import os
|
import os
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from trivia import Team
|
from trivia import Team
|
||||||
|
|
||||||
@@ -12,8 +13,9 @@ from trivia import Team
|
|||||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
DATA_DIR = os.path.join(BASE_DIR, 'data')
|
DATA_DIR = os.path.join(BASE_DIR, 'data')
|
||||||
|
|
||||||
# data: List[Team] = []
|
# Initialize global data/tracking vars
|
||||||
teams = []
|
lastChange: int = -1
|
||||||
|
teams: List[Team] = []
|
||||||
|
|
||||||
|
|
||||||
def refreshScores() -> None:
|
def refreshScores() -> None:
|
||||||
@@ -23,9 +25,17 @@ def refreshScores() -> None:
|
|||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
global lastChange
|
||||||
|
filepath = os.path.join(DATA_DIR, 'scores.json')
|
||||||
|
curChange = os.stat(filepath).st_mtime
|
||||||
|
|
||||||
|
if lastChange < curChange:
|
||||||
try:
|
try:
|
||||||
|
# Update tracking var
|
||||||
|
lastChange = curChange
|
||||||
|
|
||||||
print('Attempting to load and parse scores file.')
|
print('Attempting to load and parse scores file.')
|
||||||
with open(os.path.join(DATA_DIR, 'scores.json')) as file:
|
with open(filepath) as file:
|
||||||
temp = json.load(file)
|
temp = json.load(file)
|
||||||
|
|
||||||
# Place all values into Team object for jinja
|
# Place all values into Team object for jinja
|
||||||
@@ -37,8 +47,10 @@ def refreshScores() -> None:
|
|||||||
) for team in temp
|
) for team in temp
|
||||||
]
|
]
|
||||||
print(f'Successfully loaded ({len(temp)} teams).')
|
print(f'Successfully loaded ({len(temp)} teams).')
|
||||||
|
|
||||||
global teams
|
global teams
|
||||||
teams = temp
|
teams = temp
|
||||||
|
|
||||||
# If invalid or inaccessible, simply do nothing.
|
# If invalid or inaccessible, simply do nothing.
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
print('Scores file could not be opened or parsed.')
|
print('Scores file could not be opened or parsed.')
|
||||||
|
|||||||
Reference in New Issue
Block a user