From 66afc6c1c8251a608c72b94e0eba1612614198b3 Mon Sep 17 00:00:00 2001 From: oldnapalm <38410858+oldnapalm@users.noreply.github.com> Date: Thu, 25 Jan 2024 21:21:17 -0300 Subject: [PATCH] Update chat commands --- discord_bot.py | 4 +--- zwift_offline.py | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/discord_bot.py b/discord_bot.py index 7bf3cff0..ca07aa0e 100644 --- a/discord_bot.py +++ b/discord_bot.py @@ -1,5 +1,3 @@ -# Python 3 only (asyncio) - import asyncio import json import os @@ -39,7 +37,7 @@ async def on_message(self, message): elif message.content == '!ping': await message.channel.send('pong') elif message.channel == self.channel and not message.author.bot and not message.content.startswith('!'): - zwift_offline.send_message_to_all_online(message.content, message.author.name) + zwift_offline.send_message(message.content, message.author.name) class DiscordThread(threading.Thread): diff --git a/zwift_offline.py b/zwift_offline.py index ea193889..ac88b54c 100644 --- a/zwift_offline.py +++ b/zwift_offline.py @@ -146,6 +146,7 @@ db = SQLAlchemy() db.init_app(app) +online = {} zc_connect_queue = {} player_partial_profiles = {} restarting = False @@ -870,7 +871,7 @@ def enqueue_player_update(player_id, wa_bytes): player_update_queue[player_id] = list() player_update_queue[player_id].append(wa_bytes) -def send_message_to_all_online(message, sender='Server'): +def send_message(message, sender='Server', recipients=online.keys()): player_update = udp_node_msgs_pb2.WorldAttribute() player_update.server_realm = udp_node_msgs_pb2.ZofflineConstants.RealmID player_update.wa_type = udp_node_msgs_pb2.WA_TYPE.WAT_SPA @@ -890,7 +891,7 @@ def send_message_to_all_online(message, sender='Server'): player_update.payload = chat_message.SerializeToString() player_update_s = player_update.SerializeToString() - for receiving_player_id in online.keys(): + for receiving_player_id in recipients: enqueue_player_update(receiving_player_id, player_update_s) @@ -898,12 +899,12 @@ def send_restarting_message(): global restarting global restarting_in_minutes while restarting: - send_message_to_all_online('Restarting / Shutting down in %s minutes. Save your progress or continue riding until server is back online' % restarting_in_minutes) + send_message('Restarting / Shutting down in %s minutes. Save your progress or continue riding until server is back online' % restarting_in_minutes) time.sleep(60) restarting_in_minutes -= 1 if restarting and restarting_in_minutes == 0: message = 'See you later! Look for the back online message.' - send_message_to_all_online(message) + send_message(message) discord.send_message(message) time.sleep(6) os.kill(os.getpid(), signal.SIGINT) @@ -932,7 +933,7 @@ def cancel_restart_server(): restarting = False restarting_in_minutes = 0 message = 'Restart of the server has been cancelled. Ride on!' - send_message_to_all_online(message) + send_message(message) discord.send_message(message) return redirect(url_for('user_home', username=current_user.username)) @@ -3145,11 +3146,14 @@ def relay_worlds_attributes(): chat_message.ParseFromString(player_update.payload) if chat_message.player_id in online: state = online[chat_message.player_id] - if chat_message.message == '.regroup': - regroup_ghosts(chat_message.player_id, True) - return '', 201 - if chat_message.message == '.startline': - logger.info('course %s road %s isForward %s roadTime %s route %s' % (get_course(state), road_id(state), is_forward(state), state.roadTime, state.route)) + if chat_message.message.startswith('.'): + command = chat_message.message[1:] + if command == 'regroup': + regroup_ghosts(chat_message.player_id, True) + elif command == 'position': + logger.info('course %s road %s isForward %s roadTime %s route %s' % (get_course(state), road_id(state), is_forward(state), state.roadTime, state.route)) + else: + send_message('Invalid command: %s' % command, recipients=[chat_message.player_id]) return '', 201 discord.send_message(chat_message.message, chat_message.player_id) for receiving_player_id in online.keys(): @@ -3668,7 +3672,7 @@ def check_columns(table_class, table_name): def send_server_back_online_message(): time.sleep(30) message = "We're back online. Ride on!" - send_message_to_all_online(message) + send_message(message) discord.send_message(message)