mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-15 02:13:30 -06:00
Team named tuple, sheduler and refreshScores implementation with singleton Teams data
This commit is contained in:
@@ -1,10 +1,21 @@
|
||||
"""
|
||||
__init__.py
|
||||
"""
|
||||
from collections import namedtuple
|
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from flask import Flask
|
||||
|
||||
# initialize Flask object
|
||||
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.
|
||||
"""
|
||||
import os
|
||||
import json
|
||||
import os
|
||||
|
||||
from typing import List
|
||||
from trivia import Team
|
||||
|
||||
# Generate paths
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
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.
|
||||
|
||||
@@ -31,14 +31,14 @@ def refresh() -> None:
|
||||
# Place all values into Team object for jinja
|
||||
temp = [
|
||||
Team(
|
||||
rank=-1,
|
||||
id=team['teamno'],
|
||||
name=team['teamname'],
|
||||
scores=team['scores']
|
||||
) for team in temp
|
||||
]
|
||||
print(f'Successfully loaded ({len(temp)} teams).')
|
||||
data = temp
|
||||
global teams
|
||||
teams = temp
|
||||
# If invalid or inaccessible, simply do nothing.
|
||||
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