From 6e0c81f57c26bbe3b7fb873a43478804cc0268ef Mon Sep 17 00:00:00 2001 From: perast Date: Sat, 14 Nov 2020 21:18:10 +0100 Subject: [PATCH 1/2] Replace Werkzeug server with gevent --- zwift_offline.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/zwift_offline.py b/zwift_offline.py index 0c68308..1bd6822 100644 --- a/zwift_offline.py +++ b/zwift_offline.py @@ -22,6 +22,7 @@ from shutil import copyfile import jwt from flask import Flask, request, jsonify, g, redirect, render_template, url_for, flash, session, abort, make_response, send_file, send_from_directory from flask_login import UserMixin, AnonymousUserMixin, LoginManager, login_user, current_user, login_required +from gevent.pywsgi import WSGIServer from google.protobuf.descriptor import FieldDescriptor from protobuf_to_dict import protobuf_to_dict, TYPE_CALLABLE_MAP from flask_sqlalchemy import sqlalchemy, SQLAlchemy @@ -1485,7 +1486,9 @@ def run_standalone(passed_online, passed_global_pace_partners, passed_ghosts_ena def load_user(uid): return User.query.get(int(uid)) - app.run(ssl_context=('%s/cert-zwift-com.pem' % SSL_DIR, '%s/key-zwift-com.pem' % SSL_DIR), port=443, threaded=True, host='0.0.0.0') # debug=True, use_reload=False) + server = WSGIServer(('0.0.0.0', 443), app, certfile='%s/cert-zwift-com.pem' % SSL_DIR, keyfile='%s/key-zwift-com.pem' % SSL_DIR) + server.serve_forever() + #app.run(ssl_context=('%s/cert-zwift-com.pem' % SSL_DIR, '%s/key-zwift-com.pem' % SSL_DIR), port=443, threaded=True, host='0.0.0.0') # debug=True, use_reload=False) if __name__ == "__main__": From 9931d8aa782b83bafb02742853518b121b9358e3 Mon Sep 17 00:00:00 2001 From: perast Date: Tue, 17 Nov 2020 14:16:14 +0100 Subject: [PATCH 2/2] Added button / api to reload bots Added bots to .gitignore --- .gitignore | 3 ++- cdn/static/web/launcher/user_home.html | 3 +++ standalone.py | 5 +++++ zwift_offline.py | 10 ++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index fcee6fe..3a8d460 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ storage/* !storage/force_dir_in_git.txt example-requests/* zoffline.log -migrate_auth.sql \ No newline at end of file +migrate_auth.sql +bots \ No newline at end of file diff --git a/cdn/static/web/launcher/user_home.html b/cdn/static/web/launcher/user_home.html index f2ea73f..c1bea00 100644 --- a/cdn/static/web/launcher/user_home.html +++ b/cdn/static/web/launcher/user_home.html @@ -20,6 +20,9 @@ {% elif is_admin %} Cancel restart {% endif %} + {% if is_admin %} + Reload bots + {% endif %} {% endif %} diff --git a/standalone.py b/standalone.py index 6bc2803..8015117 100755 --- a/standalone.py +++ b/standalone.py @@ -402,7 +402,12 @@ def load_bots(): bot.position = 0 def play_bots(): + global global_bots while True: + if zwift_offline.reload_pacer_bots: + zwift_offline.reload_pacer_bots = False + global_bots.clear() + load_bots() for bot_id in global_bots.keys(): bot = global_bots[bot_id] if bot.position < len(bot.route.states) - 1: bot.position += 1 diff --git a/zwift_offline.py b/zwift_offline.py index 2689bb7..ab0c730 100644 --- a/zwift_offline.py +++ b/zwift_offline.py @@ -99,6 +99,7 @@ app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 db = SQLAlchemy(app) online = {} global_pace_partners = {} +global_bots = {} ghosts_enabled = {} player_update_queue = {} player_ids = {} @@ -106,6 +107,7 @@ player_partial_profiles = {} save_ghost = None restarting = False restarting_in_minutes = 0 +reload_pacer_bots = False class User(UserMixin, db.Model): player_id = db.Column(db.Integer, primary_key=True) @@ -384,6 +386,14 @@ def cancel_restart_server(): send_message_to_all_online('Restart of the server has been cancelled. Ride on!') return redirect('/user/%s/' % current_user.username) +@app.route("/reloadbots") +@login_required +def reload_bots(): + global reload_pacer_bots + if bool(current_user.is_admin): + reload_pacer_bots = True + return redirect('/user/%s/' % current_user.username) + @app.route("/upload//", methods=["GET", "POST"]) @login_required def upload(username):