Skip to content

Commit

Permalink
Added StreamRecorder to RoRClient, removed send_multiline_chat
Browse files Browse the repository at this point in the history
  • Loading branch information
danmackey committed Sep 8, 2023
1 parent 2d669db commit 2f46b02
Showing 1 changed file with 46 additions and 37 deletions.
83 changes: 46 additions & 37 deletions ror_server_bot/ror_bot/ror_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio
import logging
import math

from .commands import chat_command_factory, COMMAND_PREFIX
from .enums import AuthStatus, RoRClientEvents
from .models import RoRClientConfig
from .models import RoRClientConfig, Vector3
from .ror_connection import RoRConnection
from .stream_recorder import StreamRecorder

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -43,15 +43,15 @@ def try_next(self, delta: float) -> str | None:
if self._time >= self._delay:
self._time = 0

idx = self._idx
message = self._messages[self._idx]

if idx == len(self._messages) - 1:
is_last_idx = self._idx == len(self._messages) - 1

if is_last_idx:
self._idx = 0
else:
self._idx += 1

message = self._messages[idx]

return f'{self._color}ANNOUNCEMENT: {message}'
else:
return None
Expand All @@ -74,6 +74,8 @@ def __init__(self, client_config: RoRClientConfig) -> None:
port=client_config.server.port,
)

self.stream_recorder = StreamRecorder(self.server)

self._announcements_enabled = client_config.announcements.enabled
self._announcements_handler = AnnouncementsHandler(
delay=client_config.announcements.delay,
Expand Down Expand Up @@ -133,19 +135,30 @@ async def _on_frame_step(self, delta: float):

async def _on_chat(self, uid: int, msg: str):
if msg.startswith(COMMAND_PREFIX):
logger.info('User %d sent command: %r', uid, msg)
try:
command = chat_command_factory(
msg[len(COMMAND_PREFIX):]
)
except ValueError as e:
logger.warning(e)
await self.send_private_chat(uid, f'Invalid command: {msg}')
else:
# Wait a bit before executing the command to give the
# impression that the bot is typing the response.
await asyncio.sleep(0.2)
await command.execute(self, uid)
await self._perform_command(uid, msg)

async def _perform_command(self, uid: int, msg: str):
logger.info('User %d sent command: %r', uid, msg)
try:
command = chat_command_factory(msg[len(COMMAND_PREFIX):])
except ValueError as exc:
logger.warning(exc)
await self.send_chat(f'Invalid command: {msg}')
return

# Wait a bit before executing the command to give the
# impression that the bot is typing the response.
await asyncio.sleep(0.2)

try:
await command.execute(self, uid)
except Exception as exc:
logger.error(
'Error executing command: %r',
exc,
exc_info=True,
stacklevel=2
)

async def send_chat(self, message: str):
"""Send a chat message to the server.
Expand All @@ -162,24 +175,6 @@ async def send_private_chat(self, uid: int, message: str):
"""
await self.server.send_private_chat(uid, message)

async def send_multiline_chat(self, message: str):
"""Sends a multiline message to the game chat.
:param message: The message to send.
"""
max_line_len = 100
if len(message) > max_line_len:
logger.debug('[CHAT] multiline_message=%r', message)

total_lines = math.ceil(len(message) / max_line_len)
for i in range(total_lines):
line = message[max_line_len*i:max_line_len*(i+1)]
if i > 0:
line = f'| {line}'
await self.send_chat(line)
else:
await self.send_chat(message)

async def kick(self, uid: int, reason: str = 'No reason given'):
"""Kicks a user from the server.
Expand Down Expand Up @@ -213,3 +208,17 @@ async def send_game_cmd(self, cmd: str):
:param cmd: The command to send.
"""
await self.server.send_game_cmd(cmd)

async def move_bot(self, position: Vector3):
"""Move the bot to a position.
:param position: The position to move the bot to, in meters.
"""
await self.server.move_bot(position)

async def rotate_bot(self, rotation: float):
"""Rotate the bot to a rotation.
:param rotation: The new rotation of the bot, in radians.
"""
await self.server.rotate_bot(rotation)

0 comments on commit 2f46b02

Please sign in to comment.