mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-16 14:13:38 -06:00
Fix non-existent data/demo file issues
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
from flask_apscheduler import APScheduler
|
from flask_apscheduler import APScheduler
|
||||||
|
|
||||||
from trivia.config import configs
|
from trivia.config import configs
|
||||||
|
|
||||||
scheduler: APScheduler = None
|
scheduler: Optional[APScheduler] = None
|
||||||
|
|
||||||
|
|
||||||
def create_app(env=None):
|
def create_app(env=None):
|
||||||
|
|||||||
@@ -31,9 +31,11 @@ def lastModified() -> float:
|
|||||||
"""
|
"""
|
||||||
returns epoch time of last modification to the scores file.
|
returns epoch time of last modification to the scores file.
|
||||||
"""
|
"""
|
||||||
if current_app.config['DEBUG']:
|
if os.path.exists(SCORES_FILE):
|
||||||
print(datetime.fromtimestamp(os.path.getmtime(SCORES_FILE)), datetime.fromtimestamp(time.time()))
|
if current_app.config['DEBUG']:
|
||||||
return os.path.getmtime(SCORES_FILE)
|
print(datetime.fromtimestamp(os.path.getmtime(SCORES_FILE)), datetime.fromtimestamp(time.time()))
|
||||||
|
return os.path.getmtime(SCORES_FILE)
|
||||||
|
return -1
|
||||||
|
|
||||||
|
|
||||||
def refreshScores() -> None:
|
def refreshScores() -> None:
|
||||||
@@ -66,7 +68,7 @@ def refreshScores() -> None:
|
|||||||
scores=team['scores']
|
scores=team['scores']
|
||||||
) for team in temp
|
) for team in temp
|
||||||
]
|
]
|
||||||
temp.sort(key=lambda team : sum(team.scores), reverse=True)
|
temp.sort(key=lambda team: sum(team.scores), reverse=True)
|
||||||
current_app.logger.debug(f'Successfully loaded ({len(temp)} teams).')
|
current_app.logger.debug(f'Successfully loaded ({len(temp)} teams).')
|
||||||
|
|
||||||
global teams
|
global teams
|
||||||
@@ -91,6 +93,7 @@ def generateDemo() -> None:
|
|||||||
} for i in range(current_app.config['DEMO_TEAM_COUNT'])
|
} for i in range(current_app.config['DEMO_TEAM_COUNT'])
|
||||||
]
|
]
|
||||||
|
|
||||||
|
os.makedirs(DATA_DIR, exist_ok=True)
|
||||||
with open(SCORES_FILE, 'w') as file:
|
with open(SCORES_FILE, 'w') as file:
|
||||||
json.dump(convertTo(data) if current_app.config['CONVERT_OLD'] else data, file)
|
json.dump(convertTo(data) if current_app.config['CONVERT_OLD'] else data, file)
|
||||||
|
|
||||||
@@ -106,8 +109,12 @@ def alterDemo() -> None:
|
|||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
current_app.logger.debug('Altering Demo Data...')
|
current_app.logger.debug('Altering Demo Data...')
|
||||||
with open(SCORES_FILE, 'r') as file:
|
|
||||||
data = convertFrom(json.load(file)) if current_app.config['CONVERT_OLD'] else json.load(file)
|
if os.path.exists(SCORES_FILE):
|
||||||
|
with open(SCORES_FILE, 'r') as file:
|
||||||
|
data = convertFrom(json.load(file)) if current_app.config['CONVERT_OLD'] else json.load(file)
|
||||||
|
else:
|
||||||
|
data = []
|
||||||
|
|
||||||
if len(data) > 0:
|
if len(data) > 0:
|
||||||
if len(data[0]['scores']) >= current_app.config['DEMO_MAX_SCORES']:
|
if len(data[0]['scores']) >= current_app.config['DEMO_MAX_SCORES']:
|
||||||
|
|||||||
Reference in New Issue
Block a user