You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
4.4 KiB
173 lines
4.4 KiB
#!/usr/bin/python3
|
|
|
|
from flask import Flask,render_template,session,request,redirect,abort
|
|
import json
|
|
import sys
|
|
import logging
|
|
import datetime
|
|
import toml
|
|
import os
|
|
|
|
SECRET_KEY = '!Êrû÷FwÙxIER'
|
|
CONFIG_FILE = 'config.toml'
|
|
CONFIG_LOG_FILE = 'config_log.json'
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = SECRET_KEY
|
|
app.logger.setLevel(logging.DEBUG)
|
|
SERVER_NAME = '127.0.0.1'
|
|
|
|
config = {}
|
|
with open(CONFIG_FILE) as f:
|
|
config = toml.load(f)
|
|
print(config)
|
|
if not config["orgs"]:
|
|
app.logger.error("Orgs part of config file missing")
|
|
sys.exit(1)
|
|
if not config["teams"]:
|
|
app.logger.error("Teams part of config file missing")
|
|
sys.exit(1)
|
|
if not config["times"]:
|
|
app.logger.error("Times part of config file missing")
|
|
sys.exit(1)
|
|
|
|
order = [{"id":i,"value":str(i+1)+"."} for i in range(len(config["teams"]))]
|
|
order.append({"id":"t", "value":"Třezalka"})
|
|
order.append({"id":"n", "value":"Neběžel"})
|
|
|
|
|
|
def log_state(config):
|
|
with open(CONFIG_LOG_FILE) as f:
|
|
log = json.load(f)
|
|
if type(log) != list:
|
|
raise ValueError('Config log file must contain list!')
|
|
log.append(config)
|
|
os.rename(CONFIG_LOG_FILE,CONFIG_LOG_FILE+'.old')
|
|
with open(CONFIG_LOG_FILE,'w') as f:
|
|
json.dump(log,f)
|
|
|
|
|
|
log_state(config)
|
|
|
|
|
|
|
|
def check_form(form,config):
|
|
errors = []
|
|
for t in config["teams"]:
|
|
if "poradi1_"+str(t['id']) not in form:
|
|
errors.append("Tým "+t['name']+" nemá vyplněné pořadí u 1. člověka")
|
|
if "poradi2_"+str(t['id']) not in form:
|
|
errors.append("Tým "+t['name']+" nemá vyplněné pořadí u 2. člověka")
|
|
|
|
return errors
|
|
|
|
def team_points(per1,order1,per2,order2,config):
|
|
# FIXME hardcoded interactions
|
|
if (per1 not in ['n','t'] and per2 not in ['n','t']) and \
|
|
((per1 == 'Kodein' and per2 == 'Paralen') or \
|
|
(per1 == 'Penicilin' and per2 == 'Strepsils')):
|
|
order1 = min(int(order1),int(order2))
|
|
order2 = order1
|
|
|
|
pts = 0
|
|
# Pokud neběžel první, neběžel ani druhý
|
|
if order1 == 'n':
|
|
return config['no_pts']
|
|
|
|
# První měl třezalku
|
|
if order1 == 't':
|
|
pts += config['base_pts']
|
|
|
|
# První běžel normálně
|
|
else:
|
|
org = get_org_by_id(per1,config)
|
|
pts +=config['base_pts']*config['order_coeff'][int(order1)]*org['coeff']
|
|
# Druhý neběžěl, body za oba jsou body prvního, můžeme vrátit
|
|
if order2 == 'n':
|
|
return pts
|
|
|
|
# Druhý měl třezalku
|
|
if order2 == 't':
|
|
pts += config['base_pts']
|
|
|
|
# Druhý běžel normálně
|
|
else:
|
|
org = get_org_by_id(per2,config)
|
|
pts += config['base_pts']*config['order_coeff'][int(order2)]*org['coeff']
|
|
return pts
|
|
|
|
def get_org_by_id(aid,config):
|
|
for o in config["orgs"]:
|
|
if o["id"] == aid:
|
|
return o
|
|
|
|
@app.route('/')
|
|
def hello_world():
|
|
return 'Hello, World!'
|
|
|
|
@app.route('/form',methods=['GET','POST'])
|
|
def form_page():
|
|
if request.method == 'GET':
|
|
return render_template("form.html",
|
|
round = config["times"][config['round']],
|
|
people = config["orgs"],
|
|
order = order,
|
|
teams = config["teams"],
|
|
)
|
|
|
|
if request.method == 'POST':
|
|
print(request.form)
|
|
errors = check_form(request.form,config)
|
|
if errors:
|
|
return render_template("form.html",
|
|
round = config["times"][config['round']],
|
|
people = config["orgs"],
|
|
order = order,
|
|
teams = config["teams"],
|
|
errors = errors
|
|
)
|
|
for team in config["teams"]:
|
|
order1 = request.form['poradi1_'+str(team['id'])]
|
|
order2 = request.form['poradi2_'+str(team['id'])]
|
|
per1 = request.form.get('clovek1_'+str(team['id']),None)
|
|
per2 = request.form.get('clovek2_'+str(team['id']),None)
|
|
team["points"] += team_points(per1,order1,per2,order2,config)
|
|
if team["points"] <= 0:
|
|
team["emerg"] += 1
|
|
team["points"] = 100
|
|
if "zachranka_"+str(team['id']) in request.form and\
|
|
team['emerg_remain'] > 0:
|
|
team["points"] = 100
|
|
team['emerg_remain'] -= 1
|
|
config["round"] += 1
|
|
log_state(config)
|
|
return render_template("form.html",
|
|
round = config["times"][config['round']],
|
|
people = config["orgs"],
|
|
order = order,
|
|
teams = config["teams"],
|
|
errors = [],
|
|
success = True
|
|
)
|
|
|
|
|
|
|
|
@app.route('/tablo')
|
|
def tablo():
|
|
return render_template("tablo.html",teams=config["teams"])
|
|
|
|
@app.route('/rollback',methods=['GET','POST'])
|
|
def rollback():
|
|
global config
|
|
with open(CONFIG_LOG_FILE) as f:
|
|
log = json.load(f)
|
|
if request.method == 'GET':
|
|
return render_template("rollback.html",log=log)
|
|
|
|
if request.method == 'POST':
|
|
version = request.form['version']
|
|
config = log[int(version)]
|
|
return render_template('rollback.html',log=log,success=True)
|
|
|
|
|