Skip to content

Commit 7e1ba89

Browse files
committed
Use an enum instead of tuple[bool, bool]
1 parent a897bf7 commit 7e1ba89

File tree

5 files changed

+77
-55
lines changed

5 files changed

+77
-55
lines changed

rlbot/interface.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def __init__(self, type: int, data: bytes):
4747
self.data = data
4848

4949

50+
class MsgHandlingResult(IntEnum):
51+
TERMINATED = 0
52+
NO_INCOMING_MSGS = 1
53+
MORE_MSGS_QUEUED = 2
54+
55+
5056
class SocketRelay:
5157
"""
5258
The SocketRelay provides an abstraction over the direct communication with
@@ -250,10 +256,13 @@ def run(self, *, background_thread: bool = False):
250256
else:
251257
self._running = True
252258
while self._running and self.is_connected:
253-
self._running, _ = self.handle_incoming_messages(blocking=True)
259+
self._running = (
260+
self.handle_incoming_messages(blocking=True)
261+
!= MsgHandlingResult.TERMINATED
262+
)
254263
self._running = False
255264

256-
def handle_incoming_messages(self, blocking: bool = False) -> tuple[bool, bool]:
265+
def handle_incoming_messages(self, blocking: bool = False) -> MsgHandlingResult:
257266
"""
258267
Empties queue of incoming messages (should be called regularly, see `run`).
259268
Optionally blocking, ensuring that at least one message will be handled.
@@ -268,30 +277,32 @@ def handle_incoming_messages(self, blocking: bool = False) -> tuple[bool, bool]:
268277
self.socket.setblocking(blocking)
269278
incoming_message = self.read_message()
270279
try:
271-
return self.handle_incoming_message(incoming_message), True
280+
return self.handle_incoming_message(incoming_message)
272281
except flat.InvalidFlatbuffer as e:
273282
self.logger.error(
274283
"Error while unpacking message of type %s (%s bytes): %s",
275284
incoming_message.type.name,
276285
len(incoming_message.data),
277286
e,
278287
)
279-
return False, False
288+
return MsgHandlingResult.TERMINATED
280289
except Exception as e:
281290
self.logger.error(
282291
"Unexpected error while handling message of type %s: %s",
283292
incoming_message.type.name,
284293
e,
285294
)
286-
return False, False
295+
return MsgHandlingResult.TERMINATED
287296
except BlockingIOError:
288297
# No incoming messages and blocking==False
289-
return True, False
298+
return MsgHandlingResult.NO_INCOMING_MSGS
290299
except:
291300
self.logger.error("SocketRelay disconnected unexpectedly!")
292-
return False, False
301+
return MsgHandlingResult.TERMINATED
293302

294-
def handle_incoming_message(self, incoming_message: SocketMessage):
303+
def handle_incoming_message(
304+
self, incoming_message: SocketMessage
305+
) -> MsgHandlingResult:
295306
"""
296307
Handles a messages by passing it to the relevant handlers.
297308
Returns True if the message was NOT a shutdown request (i.e. NONE).
@@ -302,7 +313,7 @@ def handle_incoming_message(self, incoming_message: SocketMessage):
302313

303314
match incoming_message.type:
304315
case SocketDataType.NONE:
305-
return False
316+
return MsgHandlingResult.TERMINATED
306317
case SocketDataType.GAME_PACKET:
307318
if len(self.packet_handlers) > 0:
308319
packet = flat.GamePacket.unpack(incoming_message.data)
@@ -340,7 +351,7 @@ def handle_incoming_message(self, incoming_message: SocketMessage):
340351
case _:
341352
pass
342353

343-
return True
354+
return MsgHandlingResult.MORE_MSGS_QUEUED
344355

345356
def disconnect(self):
346357
if not self.is_connected:

rlbot/managers/bot.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
from typing import Optional
44

55
from rlbot import flat
6-
from rlbot.interface import RLBOT_SERVER_IP, RLBOT_SERVER_PORT, SocketRelay
6+
from rlbot.interface import (
7+
RLBOT_SERVER_IP,
8+
RLBOT_SERVER_PORT,
9+
MsgHandlingResult,
10+
SocketRelay,
11+
)
712
from rlbot.managers.rendering import Renderer
813
from rlbot.utils import fill_desired_game_state
914
from rlbot.utils.logging import DEFAULT_LOGGER, get_logger
@@ -153,23 +158,21 @@ def _packet_processor(self, packet: flat.GamePacket):
153158

154159
def _run(self):
155160
running = True
156-
has_more_messages = True
157161

158162
while running:
159163
# If there might be more messages,
160164
# check for another one with blocking=False
161-
# if there are no more messages, this immediately returns with has_more_messages=False
162-
# after there are no more messages and we've processed the latest packet,
163-
# wait for the next one with blocking=True
164-
running, has_more_messages = self._game_interface.handle_incoming_messages(
165-
blocking=not has_more_messages
166-
)
167-
168-
# if there might be more messages, handle them first
169-
# this ensures that only the latest packet is processed
170-
if self._latest_packet is not None and running and not has_more_messages:
171-
self._packet_processor(self._latest_packet)
172-
self._latest_packet = None
165+
# if there are no more messages, process the latest packet
166+
# then wait for the next message with blocking=True
167+
match self._game_interface.handle_incoming_messages(
168+
blocking=self._latest_packet is None
169+
):
170+
case MsgHandlingResult.TERMINATED:
171+
running = False
172+
case MsgHandlingResult.NO_INCOMING_MSGS:
173+
if self._latest_packet is not None:
174+
self._packet_processor(self._latest_packet)
175+
self._latest_packet = None
173176

174177
def run(
175178
self,

rlbot/managers/hivemind.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
from logging import Logger
21
import os
2+
from logging import Logger
33
from traceback import print_exc
44
from typing import Optional
55

66
from rlbot import flat
7-
from rlbot.interface import RLBOT_SERVER_IP, SocketRelay, RLBOT_SERVER_PORT
7+
from rlbot.interface import (
8+
RLBOT_SERVER_IP,
9+
RLBOT_SERVER_PORT,
10+
MsgHandlingResult,
11+
SocketRelay,
12+
)
813
from rlbot.managers import Renderer
914
from rlbot.utils import fill_desired_game_state
1015
from rlbot.utils.logging import DEFAULT_LOGGER, get_logger
@@ -170,18 +175,19 @@ def _run(self):
170175
while running:
171176
# If there might be more messages,
172177
# check for another one with blocking=False
173-
# if there are no more messages, this immediately returns with has_more_messages=False
174-
# after there are no more messages and we've processed the latest packet,
175-
# wait for the next one with blocking=True
176-
running, has_more_messages = self._game_interface.handle_incoming_messages(
178+
# if there are no more messages, process the latest packet
179+
# then wait for the next message with blocking=True
180+
match self._game_interface.handle_incoming_messages(
177181
blocking=not has_more_messages
178-
)
179-
180-
# if there might be more messages, handle them first
181-
# this ensures that only the latest packet is processed
182-
if self._latest_packet is not None and running and not has_more_messages:
183-
self._packet_processor(self._latest_packet)
184-
self._latest_packet = None
182+
):
183+
case MsgHandlingResult.TERMINATED:
184+
running = False
185+
case MsgHandlingResult.NO_INCOMING_MSGS:
186+
has_more_messages = False
187+
188+
if self._latest_packet is not None:
189+
self._packet_processor(self._latest_packet)
190+
self._latest_packet = None
185191

186192
def run(
187193
self,

rlbot/managers/script.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
from typing import Optional
44

55
from rlbot import flat
6-
from rlbot.interface import RLBOT_SERVER_IP, RLBOT_SERVER_PORT, SocketRelay
6+
from rlbot.interface import (
7+
RLBOT_SERVER_IP,
8+
RLBOT_SERVER_PORT,
9+
MsgHandlingResult,
10+
SocketRelay,
11+
)
712
from rlbot.managers import Renderer
813
from rlbot.utils import fill_desired_game_state
914
from rlbot.utils.logging import DEFAULT_LOGGER, get_logger
@@ -127,18 +132,19 @@ def _run(self):
127132
while running:
128133
# If there might be more messages,
129134
# check for another one with blocking=False
130-
# if there are no more messages, this immediately returns with has_more_messages=False
131-
# after there are no more messages and we've processed the latest packet,
132-
# wait for the next one with blocking=True
133-
running, has_more_messages = self._game_interface.handle_incoming_messages(
135+
# if there are no more messages, process the latest packet
136+
# then wait for the next message with blocking=True
137+
match self._game_interface.handle_incoming_messages(
134138
blocking=not has_more_messages
135-
)
136-
137-
# if there might be more messages, handle them first
138-
# this ensures that only the latest packet is processed
139-
if self._latest_packet is not None and running and not has_more_messages:
140-
self._packet_processor(self._latest_packet)
141-
self._latest_packet = None
139+
):
140+
case MsgHandlingResult.TERMINATED:
141+
running = False
142+
case MsgHandlingResult.NO_INCOMING_MSGS:
143+
has_more_messages = False
144+
145+
if self._latest_packet is not None:
146+
self._packet_processor(self._latest_packet)
147+
self._latest_packet = None
142148

143149
def run(
144150
self,

rlbot/utils/__init__.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ def fill_desired_game_state(
1414
# filling in the blanks with empty states that do nothing.
1515
"""
1616

17-
game_state = flat.DesiredGameState(
18-
match_info=match_info, console_commands=commands
19-
)
17+
game_state = flat.DesiredGameState(match_info=match_info, console_commands=commands)
2018

2119
if balls:
2220
max_entry = max(balls.keys())
@@ -28,8 +26,6 @@ def fill_desired_game_state(
2826
if cars:
2927
max_entry = max(cars.keys())
3028
default_car = flat.DesiredCarState()
31-
game_state.car_states = [
32-
cars.get(i, default_car) for i in range(max_entry + 1)
33-
]
29+
game_state.car_states = [cars.get(i, default_car) for i in range(max_entry + 1)]
3430

3531
return game_state

0 commit comments

Comments
 (0)