mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-17 08:13:37 -06:00
Team named tuple, sheduler and refreshScores implementation with singleton Teams data
This commit is contained in:
@@ -1,10 +1,21 @@
|
|||||||
"""
|
"""
|
||||||
__init__.py
|
__init__.py
|
||||||
"""
|
"""
|
||||||
|
from collections import namedtuple
|
||||||
|
|
||||||
|
from apscheduler.schedulers.background import BackgroundScheduler
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
# initialize Flask object
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
from trivia import routes, api
|
# Simple fake 'class' for passing to jinja templates
|
||||||
|
Team = namedtuple('Team', ['id', 'name', 'scores'])
|
||||||
|
|
||||||
|
from trivia import routes, api, utils
|
||||||
|
|
||||||
|
# Setup a scheduler for automatically refreshing data
|
||||||
|
scheduler = BackgroundScheduler()
|
||||||
|
scheduler.add_job(func=utils.refreshScores, trigger="interval", seconds=5)
|
||||||
|
|
||||||
|
utils.refreshScores()
|
||||||
|
|
||||||
|
|||||||
@@ -3,20 +3,20 @@ utils.py
|
|||||||
|
|
||||||
Stores important backend application functionality.
|
Stores important backend application functionality.
|
||||||
"""
|
"""
|
||||||
import os
|
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
from typing import List
|
|
||||||
from trivia import Team
|
from trivia import Team
|
||||||
|
|
||||||
# Generate paths
|
# Generate paths
|
||||||
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] = None
|
# data: List[Team] = []
|
||||||
|
teams = []
|
||||||
|
|
||||||
|
|
||||||
def refresh() -> None:
|
def refreshScores() -> None:
|
||||||
"""
|
"""
|
||||||
Refreshes scores data safely.
|
Refreshes scores data safely.
|
||||||
|
|
||||||
@@ -31,14 +31,14 @@ def refresh() -> None:
|
|||||||
# Place all values into Team object for jinja
|
# Place all values into Team object for jinja
|
||||||
temp = [
|
temp = [
|
||||||
Team(
|
Team(
|
||||||
rank=-1,
|
|
||||||
id=team['teamno'],
|
id=team['teamno'],
|
||||||
name=team['teamname'],
|
name=team['teamname'],
|
||||||
scores=team['scores']
|
scores=team['scores']
|
||||||
) for team in temp
|
) for team in temp
|
||||||
]
|
]
|
||||||
print(f'Successfully loaded ({len(temp)} teams).')
|
print(f'Successfully loaded ({len(temp)} teams).')
|
||||||
data = temp
|
global teams
|
||||||
|
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