Skip to content

Commit

Permalink
Refactor socketio_events.py for consistency and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubAndrysek committed Sep 6, 2024
1 parent 9548287 commit 27e005e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
69 changes: 36 additions & 33 deletions forester-game-app/app/socketio_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
4 changes: 2 additions & 2 deletions forester-game-app/games/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ <h5 class="card-title">Upravit data</h5>
<h5 class="card-title">Správa dat hry</h5>
<button id="download-button" class="btn btn-primary mb-2">Stáhnout data hry</button>
<button id="clear-button" class="btn btn-danger mb-2">Vymazat data hry</button>
<p class="card-text">Data hry jsou uložena v počítači: <strong>{{ config.EXECUTABLE_DIR }}/{{ config.DATAFILE }}</strong></p>
<p class="card-text">Veškerý informace jsou logovány do souboru: <strong>{{ config.EXECUTABLE_DIR }}/{{ config.LOG_FOLDER }}</strong></p>
<p class="card-text">Data hry jsou uložena v počítači: <strong>{{ config.ROOT_DIR }}/{{ config.DATAFILE }}</strong></p>
<p class="card-text">Veškerý informace jsou logovány do souboru: <strong>{{ config.ROOT_DIR }}/{{ config.LOG_FOLDER }}</strong></p>
<p class="card-text">Debug: <strong>{{ config.DEBUG }}</strong></p>
<p class="card-text">Root: <strong>{{ config.TEMP_DIR }}</strong></p>
</div>
Expand Down

0 comments on commit 27e005e

Please sign in to comment.