created automatic demo altering schedule

This commit is contained in:
Xevion
2020-06-21 07:32:54 -05:00
parent 0e08c6837d
commit 4fd1ecb1c4
4 changed files with 26 additions and 4 deletions

View File

@@ -2,4 +2,4 @@ from trivia.create_app import create_app
if __name__ == '__main__': if __name__ == '__main__':
app = create_app() app = create_app()
app.run() app.run(use_reloader=False)

View File

@@ -20,7 +20,7 @@ class Config(object):
DEMO = False DEMO = False
DEMO_TEAM_COUNT = 0 DEMO_TEAM_COUNT = 0
DEMO_ALTERATION_INTERVAL = 0 DEMO_ALTERATION_INTERVAL = 0
DEMO_MAX_SCORES = 0
class DemoConfig(Config): class DemoConfig(Config):
# Main Configuration # Main Configuration
@@ -29,4 +29,5 @@ class DemoConfig(Config):
# Demo Configuration # Demo Configuration
DEMO = True DEMO = True
DEMO_TEAM_COUNT = 30 DEMO_TEAM_COUNT = 30
DEMO_ALTERATION_INTERVAL = 15 DEMO_ALTERATION_INTERVAL = 3
DEMO_MAX_SCORES = 20

View File

@@ -28,6 +28,7 @@ def create_app(env=None):
if app.config['DEMO']: if app.config['DEMO']:
app.logger.info('Generating Demo Data...') app.logger.info('Generating Demo Data...')
utils.generateDemo() utils.generateDemo()
scheduler.add_job(id='altering', func=utils.alterDemo, trigger="interval", seconds=app.config['DEMO_ALTERATION_INTERVAL'])
utils.refreshScores() utils.refreshScores()

View File

@@ -5,6 +5,7 @@ Stores important backend application functionality.
""" """
import json import json
import os import os
import random
from collections import namedtuple from collections import namedtuple
from typing import List from typing import List
@@ -69,7 +70,7 @@ def refreshScores() -> None:
# If invalid or inaccessible, simply do nothing. # If invalid or inaccessible, simply do nothing.
except json.JSONDecodeError: 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: def generateDemo() -> None:
@@ -85,3 +86,22 @@ def generateDemo() -> None:
with open(SCORES_FILE, 'w') as file: with open(SCORES_FILE, 'w') as file:
json.dump(data, 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)