2019-09-20 23:50:13 +02:00
|
|
|
from datetime import datetime
|
2019-09-20 14:15:20 +02:00
|
|
|
|
2019-09-20 23:50:13 +02:00
|
|
|
import socketio
|
2019-09-20 14:47:55 +02:00
|
|
|
from flask import Flask, render_template
|
|
|
|
|
2019-09-20 23:50:13 +02:00
|
|
|
sio = socketio.Server()
|
2019-09-20 14:15:20 +02:00
|
|
|
app = Flask(__name__)
|
2019-09-20 23:50:13 +02:00
|
|
|
app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app)
|
2019-09-20 14:15:20 +02:00
|
|
|
|
|
|
|
# We request users to send the run number to prevent accidentially getting old requests from a previous run
|
|
|
|
# This maybe will give to much errors from people forgetting to change their number
|
|
|
|
# Remove it?
|
|
|
|
|
|
|
|
# TODO
|
|
|
|
# Save and read from a file to persist over reboots
|
|
|
|
# Save progress through time during 1 run
|
|
|
|
# Save result of different runs
|
|
|
|
|
2019-09-20 14:47:55 +02:00
|
|
|
db = {"current_run": 0, "run_start_timer": None, "run_data": {}}
|
|
|
|
|
2019-09-20 14:15:20 +02:00
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def index():
|
2019-09-20 14:47:55 +02:00
|
|
|
return render_template('index.html')
|
|
|
|
|
2019-09-20 14:15:20 +02:00
|
|
|
|
|
|
|
@app.route("/start_run/<run_index>")
|
|
|
|
def start_run(run_index):
|
|
|
|
db["current_run"] = run_index
|
2019-09-20 23:50:13 +02:00
|
|
|
starttime = datetime.now()
|
|
|
|
if run_index in db["run_data"]:
|
|
|
|
return "This run is already ran, take another number."
|
2019-09-20 14:47:55 +02:00
|
|
|
db["run_data"][run_index] = {"starttime": starttime, "data": {}}
|
2019-09-20 14:15:20 +02:00
|
|
|
# TODO send start request to the first person in the chain. Probably a zeus part already written as example
|
2019-09-20 23:50:13 +02:00
|
|
|
sio.emit('start_run', run_index)
|
2019-09-20 14:15:20 +02:00
|
|
|
return f'Run {run_index} started at {starttime}'
|
|
|
|
|
2019-09-20 14:47:55 +02:00
|
|
|
|
2019-09-20 14:55:11 +02:00
|
|
|
@app.route("/link/start/<run>/<index>")
|
|
|
|
def link_start(run, index):
|
2019-09-20 14:15:20 +02:00
|
|
|
if db["current_run"] != run:
|
|
|
|
return "Wrong run number, you are probably behind.", 404
|
|
|
|
else:
|
2019-09-20 23:50:13 +02:00
|
|
|
run_data = db["run_data"][run]["data"]
|
|
|
|
if index in run_data:
|
2019-09-20 17:00:44 +02:00
|
|
|
return "you already started in this run. Ignoring this request."
|
|
|
|
else:
|
2019-09-20 23:50:13 +02:00
|
|
|
run_data[index] = {"start": datetime.now()}
|
|
|
|
sio.emit('link_start', index)
|
2019-09-20 17:00:44 +02:00
|
|
|
return "Success."
|
2019-09-20 14:15:20 +02:00
|
|
|
|
|
|
|
|
2019-09-20 14:55:11 +02:00
|
|
|
@app.route("/link/handoff/<run>/<index>")
|
|
|
|
def link_handoff(run, index):
|
2019-09-20 14:15:20 +02:00
|
|
|
if db["current_run"] != run:
|
|
|
|
return "Wrong run number, you are probably behind.", 404
|
|
|
|
else:
|
2019-09-20 17:00:44 +02:00
|
|
|
link_data = db["run_data"][run]["data"][index]
|
|
|
|
if link_data["handoff"]:
|
|
|
|
return "you already handed off control during this run. Ignoring this request"
|
|
|
|
else:
|
2019-09-20 23:50:13 +02:00
|
|
|
link_data["handoff"] = datetime.now()
|
2019-09-20 17:00:44 +02:00
|
|
|
return "Success."
|
2019-09-20 14:15:20 +02:00
|
|
|
|
2019-09-20 14:47:55 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2019-09-20 23:50:13 +02:00
|
|
|
app.run(host="0.0.0.0", debug=True)
|