e74c2f4634
Added initial ws sync on page load Added start and handoff updates via ws
75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
from time import time
|
|
|
|
import socketio
|
|
from flask import Flask, render_template
|
|
|
|
sio = socketio.Server()
|
|
app = Flask(__name__)
|
|
app.wsgi_app = socketio.WSGIApp(sio, app.wsgi_app)
|
|
|
|
# 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
|
|
|
|
db = {"current_run": None, "run_start_timer": None, "run_data": {}}
|
|
|
|
|
|
@app.route("/")
|
|
def index():
|
|
return render_template('index.html')
|
|
|
|
|
|
@app.route("/start_run/<run_index>")
|
|
def start_run(run_index):
|
|
db["current_run"] = run_index
|
|
starttime = time()
|
|
if run_index in db["run_data"]:
|
|
return "This run is already ran, take another number."
|
|
db["run_data"][run_index] = {"starttime": starttime, "data": {}}
|
|
# TODO send start request to the first person in the chain. Probably a zeus part already written as example
|
|
sio.emit('start_run', run_index)
|
|
return f'Run {run_index} started at {starttime}'
|
|
|
|
|
|
@app.route("/link/start/<run>/<index>")
|
|
def link_start(run, index):
|
|
if db["current_run"] != run:
|
|
return "Wrong run number, check that you update your run", 404
|
|
else:
|
|
run_data = db["run_data"][run]["data"]
|
|
if index in run_data:
|
|
return "you already started in this run. Ignoring this request."
|
|
else:
|
|
run_data[index] = {"id": index, "start": time()}
|
|
sio.emit('link_start', run_data[index])
|
|
return "Success."
|
|
|
|
|
|
@app.route("/link/handoff/<run>/<index>")
|
|
def link_handoff(run, index):
|
|
if db["current_run"] != run:
|
|
return "Wrong run number, check that you updated you run", 404
|
|
else:
|
|
link_data = db["run_data"][run]["data"][index]
|
|
if "handoff" in link_data:
|
|
return "you already handed off control during this run. Ignoring this request"
|
|
else:
|
|
link_data["handoff"] = time()
|
|
sio.emit('link_handoff', link_data)
|
|
return "Success."
|
|
|
|
|
|
@sio.event
|
|
def connect(sid, data):
|
|
if db["current_run"]:
|
|
current_run = db["run_data"][db["current_run"]]
|
|
sio.emit('sync_current_run', current_run, room=sid)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
app.run(host="0.0.0.0", debug=True)
|