From 27e005ec0782047897ad0a1e1e1da0352872818e Mon Sep 17 00:00:00 2001 From: Jakub Andrysek Date: Fri, 6 Sep 2024 16:07:05 +0200 Subject: [PATCH] Refactor socketio_events.py for consistency and readability --- forester-game-app/app/socketio_events.py | 69 ++++++++++++------------ forester-game-app/games/admin/index.html | 4 +- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/forester-game-app/app/socketio_events.py b/forester-game-app/app/socketio_events.py index 023cd67..fae5dd0 100644 --- a/forester-game-app/app/socketio_events.py +++ b/forester-game-app/app/socketio_events.py @@ -3,83 +3,86 @@ from app.init import socketio, db -socketio_bp = Blueprint('socketio', __name__) +socketio_bp = Blueprint("socketio", __name__) connected_clients = 0 connected_admins = 0 -game_status = 'running' +game_status = "running" -@socketio.on('send_admin_message') +@socketio.on("send_admin_message") def handle_admin_message(message): - emit('admin_messages', message, broadcast=True) + emit("admin_messages", message, broadcast=True) -@socketio.on('connect') +@socketio.on("connect") def handle_connect(): global connected_clients connected_clients += 1 - emit('update_clients', {'count': connected_clients}, broadcast=True) + emit("update_clients", {"count": connected_clients}, broadcast=True) -@socketio.on('disconnect') +@socketio.on("disconnect") def handle_disconnect(): global connected_clients connected_clients -= 1 - emit('update_clients', {'count': connected_clients}, broadcast=True) + emit("update_clients", {"count": connected_clients}, broadcast=True) -@socketio.on('get_game_status') -def handle_game_status(): + +@socketio.on("get_game_status") +def handle_game_status(data): global game_status - emit('game_status', game_status) + emit("game_status", game_status) -@socketio.on('set_game_status') +@socketio.on("set_game_status") def handle_set_game_status(status: str): global game_status game_status = status - emit('game_status', game_status, broadcast=True) + emit("game_status", game_status, broadcast=True) -@socketio.on('get_all_db') +@socketio.on("get_all_db") def handle_get_all_data(data) -> dict: - return {'status': 'ok', 'data': db.get_data()} + return {"status": "ok", "data": db.get_data()} + -@socketio.on('delete_all_db') +@socketio.on("delete_all_db") def handle_delete_all_data(data) -> dict: db.overwrite_data_dict({}) - return {'status': 'ok'} + return {"status": "ok"} + -@socketio.on('set_key') +@socketio.on("set_key") def handle_set_key(json: dict): - key = json.get('key') - value = json.get('value') + key = json.get("key") + value = json.get("value") db.set_data_key(key, value) - return {'status': 'ok'} + return {"status": "ok"} -@socketio.on('set_key_broadcast') +@socketio.on("set_key_broadcast") def handle_set_key_broadcast(json: dict): - key = json.get('key') - value = json.get('value') + key = json.get("key") + value = json.get("value") db.set_data_key(key, value) emit(key, value, broadcast=True) - return {'status': 'ok'} + return {"status": "ok"} -@socketio.on('exists_key') +@socketio.on("exists_key") def handle_exists_key(key: str) -> dict: - return {'status': 'ok', 'exists': db.get_data_key(key) is not None} + return {"status": "ok", "exists": db.get_data_key(key) is not None} -@socketio.on('get_key') +@socketio.on("get_key") def handle_get_key(json: dict) -> dict: - key = json.get('key') - default_value = json.get('defaultValue') - return {'status': 'ok', 'data': db.get_data_key(key, default_value)} + key = json.get("key") + default_value = json.get("defaultValue") + return {"status": "ok", "data": db.get_data_key(key, default_value)} -@socketio.on('delete_key') +@socketio.on("delete_key") def handle_delete_key(key: str) -> dict: db.delete_data_key(key) - return {'status': 'ok'} + return {"status": "ok"} diff --git a/forester-game-app/games/admin/index.html b/forester-game-app/games/admin/index.html index 925a9cd..9e38cda 100644 --- a/forester-game-app/games/admin/index.html +++ b/forester-game-app/games/admin/index.html @@ -80,8 +80,8 @@
Upravit data
Správa dat hry
-

Data hry jsou uložena v počítači: {{ config.EXECUTABLE_DIR }}/{{ config.DATAFILE }}

-

Veškerý informace jsou logovány do souboru: {{ config.EXECUTABLE_DIR }}/{{ config.LOG_FOLDER }}

+

Data hry jsou uložena v počítači: {{ config.ROOT_DIR }}/{{ config.DATAFILE }}

+

Veškerý informace jsou logovány do souboru: {{ config.ROOT_DIR }}/{{ config.LOG_FOLDER }}

Debug: {{ config.DEBUG }}

Root: {{ config.TEMP_DIR }}