mirror of
https://github.com/Xevion/trivia.git
synced 2025-12-10 10:09:02 -06:00
created automatic demo altering schedule
This commit is contained in:
@@ -2,4 +2,4 @@ from trivia.create_app import create_app
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = create_app()
|
||||
app.run()
|
||||
app.run(use_reloader=False)
|
||||
|
||||
@@ -20,7 +20,7 @@ class Config(object):
|
||||
DEMO = False
|
||||
DEMO_TEAM_COUNT = 0
|
||||
DEMO_ALTERATION_INTERVAL = 0
|
||||
|
||||
DEMO_MAX_SCORES = 0
|
||||
|
||||
class DemoConfig(Config):
|
||||
# Main Configuration
|
||||
@@ -29,4 +29,5 @@ class DemoConfig(Config):
|
||||
# Demo Configuration
|
||||
DEMO = True
|
||||
DEMO_TEAM_COUNT = 30
|
||||
DEMO_ALTERATION_INTERVAL = 15
|
||||
DEMO_ALTERATION_INTERVAL = 3
|
||||
DEMO_MAX_SCORES = 20
|
||||
|
||||
@@ -28,6 +28,7 @@ def create_app(env=None):
|
||||
if app.config['DEMO']:
|
||||
app.logger.info('Generating Demo Data...')
|
||||
utils.generateDemo()
|
||||
scheduler.add_job(id='altering', func=utils.alterDemo, trigger="interval", seconds=app.config['DEMO_ALTERATION_INTERVAL'])
|
||||
|
||||
utils.refreshScores()
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ Stores important backend application functionality.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
from collections import namedtuple
|
||||
from typing import List
|
||||
|
||||
@@ -69,7 +70,7 @@ def refreshScores() -> None:
|
||||
|
||||
# If invalid or inaccessible, simply do nothing.
|
||||
except json.JSONDecodeError:
|
||||
current_app.logger.error('Scores file could not be opened or parsed.', print_exc=True)
|
||||
current_app.logger.error('Scores file could not be opened or parsed.', exc_info=True)
|
||||
|
||||
|
||||
def generateDemo() -> None:
|
||||
@@ -85,3 +86,22 @@ def generateDemo() -> None:
|
||||
with open(SCORES_FILE, 'w') as file:
|
||||
json.dump(data, file)
|
||||
|
||||
|
||||
def alterDemo() -> None:
|
||||
from trivia.create_app import scheduler
|
||||
app = scheduler.app
|
||||
|
||||
with app.app_context():
|
||||
current_app.logger.debug('Altering Demo Data...')
|
||||
with open(SCORES_FILE, 'r') as file:
|
||||
data = json.load(file)
|
||||
|
||||
if len(data) > 0:
|
||||
if len(data[0]['scores']) >= current_app.config['DEMO_MAX_SCORES']:
|
||||
generateDemo()
|
||||
else:
|
||||
for team in data:
|
||||
team['scores'].append(random.randint(2, 8) if random.random() > 0.25 else 0)
|
||||
|
||||
with open(SCORES_FILE, 'w') as file:
|
||||
json.dump(data, file)
|
||||
|
||||
Reference in New Issue
Block a user