Skip to content

Commit baa380e

Browse files
committed
Add run and connect_and_run to MatchManager
1 parent 47e465b commit baa380e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

rlbot/managers/match.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,42 @@ def connect(
9393
rlbot_server_ip=rlbot_server_ip,
9494
rlbot_server_port=rlbot_server_port or self.rlbot_server_port,
9595
)
96-
self.rlbot_interface.run(background_thread=True)
96+
97+
def run(self, *, background_thread: bool = False):
98+
"""
99+
Handle incoming messages until disconnected.
100+
101+
- background_thread: If `True`, a background thread will be started to process messages.
102+
"""
103+
self.rlbot_interface.run(background_thread=background_thread)
104+
105+
def connect_and_run(
106+
self,
107+
*,
108+
wants_match_communications: bool,
109+
wants_ball_predictions: bool,
110+
close_between_matches: bool = True,
111+
rlbot_server_ip: str = RLBOT_SERVER_IP,
112+
rlbot_server_port: Optional[int] = None,
113+
background_thread: bool = False,
114+
):
115+
"""
116+
Connects to the RLBot server specifying the given settings.
117+
118+
- wants_match_communications: Whether match communication messages should be sent to this process.
119+
- wants_ball_predictions: Whether ball prediction messages should be sent to this process.
120+
- close_between_matches: Whether RLBot should close this connection between matches, specifically upon
121+
`StartMatch` and `StopMatch` messages, since RLBot does not actually detect the ending of matches.
122+
- background_thread: If `True`, a background thread will be started to process messages.
123+
"""
124+
self.connect(
125+
wants_match_communications=wants_match_communications,
126+
wants_ball_predictions=wants_ball_predictions,
127+
close_between_matches=close_between_matches,
128+
rlbot_server_ip=rlbot_server_ip,
129+
rlbot_server_port=rlbot_server_port,
130+
)
131+
self.run(background_thread=background_thread)
97132

98133
def wait_for_first_packet(self):
99134
while self.packet is None or self.packet.match_info.match_phase in {
@@ -118,10 +153,11 @@ def start_match(
118153
self.ensure_server_started()
119154

120155
if not self.rlbot_interface.is_connected:
121-
self.connect(
156+
self.connect_and_run(
122157
wants_match_communications=False,
123158
wants_ball_predictions=False,
124159
close_between_matches=False,
160+
background_thread=True,
125161
)
126162

127163
self.rlbot_interface.start_match(config)

tests/many_match.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ def handle_match_comm(comm: flat.MatchComm):
2323
match_manager = MatchManager(RLBOT_SERVER_FOLDER)
2424
match_manager.rlbot_interface.match_comm_handlers.append(handle_match_comm)
2525
match_manager.ensure_server_started()
26-
match_manager.connect(
26+
match_manager.connect_and_run(
2727
wants_match_communications=True,
2828
wants_ball_predictions=False,
2929
close_between_matches=False,
30+
background_thread=False,
3031
)
3132

3233
current_map = -1

0 commit comments

Comments
 (0)