From 2fc774d85cb36c8859a0824d2824f824ead8377a Mon Sep 17 00:00:00 2001 From: Russell Ballestrini Date: Sun, 28 Jul 2024 13:59:25 -0400 Subject: [PATCH] /activity cancel modified: README.rst modified: app.py --- README.rst | 5 +++++ app.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 47e2fcd..2baa833 100644 --- a/README.rst +++ b/README.rst @@ -165,6 +165,11 @@ The server expects to load the YAML file out of the S3 bucket you specify in you ``/activity info`` +3. **Cancel an Activity**: Use the ``/activity cancel`` command to display cancel the current activity running in the room. + + ``/activity cancel`` + + Contributing ------------ diff --git a/app.py b/app.py index ba7a76b..3e976e2 100644 --- a/app.py +++ b/app.py @@ -312,6 +312,10 @@ def handle_message(data): gevent.spawn(display_activity_info, room_name, data["username"]) # Exit early since we're displaying activity info return + if command.startswith("/activity cancel"): + gevent.spawn(cancel_activity, room_name, data["username"]) + # Exit early since we're canceling the activity + return if command.startswith("/activity"): s3_file_path = command.split(" ", 1)[1].strip() gevent.spawn(start_activity, room_name, s3_file_path, data["username"]) @@ -1741,6 +1745,40 @@ def start_activity(room_name, s3_file_path, username): room=room_name, ) + +def cancel_activity(room_name, username): + with app.app_context(): + room = get_room(room_name) + activity_state = ActivityState.query.filter_by(room_id=room.id).first() + + if not activity_state: + socketio.emit( + "message", + { + "id": None, + "username": "System", + "content": "No active activity found to cancel.", + }, + room=room_name, + ) + return + + # Delete the activity state + db.session.delete(activity_state) + db.session.commit() + + # Emit a message indicating the activity has been canceled + socketio.emit( + "message", + { + "id": None, + "username": "System", + "content": "Activity has been canceled.", + }, + room=room_name, + ) + + def handle_activity_response(room_name, user_response, username): with app.app_context(): room = get_room(room_name) @@ -1890,12 +1928,18 @@ def handle_activity_response(room_name, user_response, username): or activity_state.attempts >= activity_state.max_attempts ): if next_section_and_step: - current_section_id, current_step_id = next_section_and_step.split(":") + current_section_id, current_step_id = next_section_and_step.split( + ":" + ) next_section = next( - s for s in activity_content["sections"] if s["section_id"] == current_section_id + s + for s in activity_content["sections"] + if s["section_id"] == current_section_id ) next_step = next( - s for s in next_section["steps"] if s["step_id"] == current_step_id + s + for s in next_section["steps"] + if s["step_id"] == current_step_id ) else: # Move to the next step or section