Skip to content

Commit 8457112

Browse files
Merge branch 'fix-request-protocol-consume-order' of github.com:cameronangliss/poke-env into none-battleorder-is-pass
2 parents b58839c + 701a62d commit 8457112

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

src/poke_env/environment/pokemon.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ def set_hp_status(self, hp_status: str, store=False):
376376
self.end_effect("yawn")
377377
else:
378378
hp = hp_status
379+
self._status = None
379380

380381
current_hp, max_hp = "".join([c for c in hp if c in "0123456789/"]).split("/")
381382
self._current_hp = int(current_hp)

src/poke_env/player/player.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ async def _handle_battle_message(self, split_messages: List[List[str]]):
262262
:type split_message: str
263263
"""
264264
# Battle messages can be multiline
265+
will_move = False
266+
from_teampreview_request = False
267+
maybe_default_order = False
265268
if (
266269
len(split_messages) > 1
267270
and len(split_messages[1]) > 1
@@ -284,7 +287,7 @@ async def _handle_battle_message(self, split_messages: List[List[str]]):
284287
request = orjson.loads(split_message[2])
285288
battle.parse_request(request)
286289
if battle.move_on_next_request:
287-
await self._handle_battle_request(battle)
290+
will_move = True
288291
battle.move_on_next_request = False
289292
elif split_message[1] == "win" or split_message[1] == "tie":
290293
if split_message[1] == "win":
@@ -304,46 +307,53 @@ async def _handle_battle_message(self, split_messages: List[List[str]]):
304307
"[Invalid choice] Sorry, too late to make a different move"
305308
):
306309
if battle.trapped:
307-
await self._handle_battle_request(battle)
310+
will_move = True
308311
elif split_message[2].startswith(
309312
"[Unavailable choice] Can't switch: The active Pokémon is "
310313
"trapped"
311314
) or split_message[2].startswith(
312315
"[Invalid choice] Can't switch: The active Pokémon is trapped"
313316
):
314317
battle.trapped = True
315-
await self._handle_battle_request(battle)
318+
will_move = True
316319
elif split_message[2].startswith(
317320
"[Invalid choice] Can't switch: You can't switch to an active "
318321
"Pokémon"
319322
):
320-
await self._handle_battle_request(battle, maybe_default_order=True)
323+
will_move = True
324+
maybe_default_order = True
321325
elif split_message[2].startswith(
322326
"[Invalid choice] Can't switch: You can't switch to a fainted "
323327
"Pokémon"
324328
):
325-
await self._handle_battle_request(battle, maybe_default_order=True)
329+
will_move = True
330+
maybe_default_order = True
326331
elif split_message[2].startswith(
327332
"[Invalid choice] Can't move: Invalid target for"
328333
):
329-
await self._handle_battle_request(battle, maybe_default_order=True)
334+
will_move = True
335+
maybe_default_order = True
330336
elif split_message[2].startswith(
331337
"[Invalid choice] Can't move: You can't choose a target for"
332338
):
333-
await self._handle_battle_request(battle, maybe_default_order=True)
339+
will_move = True
340+
maybe_default_order = True
334341
elif split_message[2].startswith(
335342
"[Invalid choice] Can't move: "
336343
) and split_message[2].endswith("needs a target"):
337-
await self._handle_battle_request(battle, maybe_default_order=True)
344+
will_move = True
345+
maybe_default_order = True
338346
elif (
339347
split_message[2].startswith("[Invalid choice] Can't move: Your")
340348
and " doesn't have a move matching " in split_message[2]
341349
):
342-
await self._handle_battle_request(battle, maybe_default_order=True)
350+
will_move = True
351+
maybe_default_order = True
343352
elif split_message[2].startswith(
344353
"[Invalid choice] Incomplete choice: "
345354
):
346-
await self._handle_battle_request(battle, maybe_default_order=True)
355+
will_move = True
356+
maybe_default_order = True
347357
elif split_message[2].startswith(
348358
"[Unavailable choice]"
349359
) and split_message[2].endswith("is disabled"):
@@ -356,25 +366,34 @@ async def _handle_battle_message(self, split_messages: List[List[str]]):
356366
"[Invalid choice] Can't move: You sent more choices than unfainted"
357367
" Pokémon."
358368
):
359-
await self._handle_battle_request(battle, maybe_default_order=True)
369+
will_move = True
370+
maybe_default_order = True
360371
elif split_message[2].startswith(
361372
"[Invalid choice] Can't move: You can only Terastallize once per battle."
362373
):
363-
await self._handle_battle_request(battle, maybe_default_order=True)
374+
will_move = True
375+
maybe_default_order = True
364376
else:
365377
self.logger.critical("Unexpected error message: %s", split_message)
366378
elif split_message[1] == "turn":
367379
battle.parse_message(split_message)
368-
await self._handle_battle_request(battle)
380+
will_move = True
369381
elif split_message[1] == "teampreview":
370382
battle.parse_message(split_message)
371-
await self._handle_battle_request(battle, from_teampreview_request=True)
383+
will_move = True
384+
from_teampreview_request = True
372385
elif split_message[1] == "bigerror":
373386
self.logger.warning("Received 'bigerror' message: %s", split_message)
374387
elif split_message[1] == "uhtml" and split_message[2] == "otsrequest":
375388
await self._handle_ots_request(battle.battle_tag)
376389
else:
377390
battle.parse_message(split_message)
391+
if will_move:
392+
await self._handle_battle_request(
393+
battle,
394+
from_teampreview_request=from_teampreview_request,
395+
maybe_default_order=maybe_default_order,
396+
)
378397

379398
async def _handle_battle_request(
380399
self,

src/poke_env/ps_client/ps_client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from asyncio import CancelledError, Event, Lock, create_task, sleep
88
from logging import Logger
99
from time import perf_counter
10-
from typing import Any, List, Optional, Set
10+
from typing import Any, Dict, List, Optional, Set
1111

1212
import requests
1313
import websockets.client as ws
@@ -85,6 +85,7 @@ def __init__(
8585
self._sending_lock = create_in_poke_loop(Lock)
8686

8787
self.websocket: WebSocketClientProtocol
88+
self.reqs: Dict[str, List[List[str]]] = {}
8889
self._logger: Logger = self._create_logger(log_level)
8990

9091
if start_listening:
@@ -139,7 +140,18 @@ async def _handle_message(self, message: str):
139140
# For battles, this is the zero-th entry
140141
# Otherwise it is the one-th entry
141142
if split_messages[0][0].startswith(">battle"):
143+
# Determine protocol and request
144+
battle_tag = split_messages[0][0][1:]
145+
request = self.reqs.pop(battle_tag, None)
146+
if "|request|" in message:
147+
protocol = None
148+
self.reqs[battle_tag] = split_messages
149+
else:
150+
protocol = split_messages
142151
# Battle update
152+
split_messages = protocol or [[f">{battle_tag}"]]
153+
if request is not None:
154+
split_messages += [request[1]]
143155
await self._handle_battle_message(split_messages) # type: ignore
144156
elif split_messages[0][1] == "challstr":
145157
# Confirms connection to the server: we can login

0 commit comments

Comments
 (0)