Skip to content

Commit

Permalink
Update chat commands
Browse files Browse the repository at this point in the history
  • Loading branch information
oldnapalm committed Jan 26, 2024
1 parent 35688b0 commit 66afc6c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
4 changes: 1 addition & 3 deletions discord_bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Python 3 only (asyncio)

import asyncio
import json
import os
Expand Down Expand Up @@ -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):
Expand Down
26 changes: 15 additions & 11 deletions zwift_offline.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
db = SQLAlchemy()
db.init_app(app)

online = {}
zc_connect_queue = {}
player_partial_profiles = {}
restarting = False
Expand Down Expand Up @@ -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
Expand All @@ -890,20 +891,20 @@ 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)


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)
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 66afc6c

Please sign in to comment.