Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More robust battle tracking #666

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

cameronangliss
Copy link
Contributor

@cameronangliss cameronangliss commented Dec 27, 2024

Fixes #616

Currently we consume the messages in the order they come in, which is request and then protocol message. We really should update the battle object with the protocol first, and then update with the battle request - and now we do! This is better because it prevents us from accidentally overwriting something that is definitely true with our interpretation of the protocol, which currently has bugs. Specifically, this guarantees that we can correctly track Zoroark if it's on our team, and we resolve several other bugs that have remained silent until now.

@cameronangliss cameronangliss force-pushed the fix-request-protocol-consume-order branch from 6a9b635 to 6c22de7 Compare December 27, 2024 02:38
Copy link

codecov bot commented Dec 27, 2024

Codecov Report

Attention: Patch coverage is 43.51852% with 61 lines in your changes missing coverage. Please review.

Project coverage is 84.94%. Comparing base (f458350) to head (62b2ec9).
Report is 124 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #666      +/-   ##
==========================================
+ Coverage   83.38%   84.94%   +1.56%     
==========================================
  Files          39       42       +3     
  Lines        3918     4438     +520     
==========================================
+ Hits         3267     3770     +503     
- Misses        651      668      +17     

@cameronangliss cameronangliss force-pushed the fix-request-protocol-consume-order branch 8 times, most recently from f404a69 to ae0777b Compare December 29, 2024 08:52
@cameronangliss cameronangliss marked this pull request as ready for review December 30, 2024 02:33
@caymansimpson
Copy link
Contributor

Love this change -- long overdue! Have you written tests to show this works? I'm looking at this and comparing this to Showdown protocol, but might be easier for hsahovic to if add tests (also so if anyone after you messes up, we'll know)

@cameronangliss
Copy link
Contributor Author

Yes, will get to that hopefully tomorrow, I'll see when I can find time.

@cameronangliss
Copy link
Contributor Author

@hsahovic this is all set! Passing to you

@@ -262,6 +262,9 @@ async def _handle_battle_message(self, split_messages: List[List[str]]):
:type split_message: str
"""
# Battle messages can be multiline
will_move = False
from_teampreview_request = False
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think these two could be named a bit more explicitly, for instance something like should_process_request and should_process_teampreview_request

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's this: f13727b

split_messages = protocol or [[f">{battle_tag}"]]
if request is not None:
split_messages += [request[1]]
await self._handle_battle_message(split_messages) # type: ignore
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my understanding is that the changes in the client are to not treat requests and protocol updates separately, is that correct?
if so, i think this logic should live in Player._handle_battle_message, as the client should be agnostic to implementation details of the battling protocol
additionally, requests should be stored in battle objects instead of the protocol / player objects

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly. It is to swap the order that they are consumed. I would argue that the logic should be in the client since _handle_battle_message is built to take only one split_messages at a time. I tried to make it so we didn't have to combine the protocol and request as I have done here, but I wasn't able to get that to work unfortunately. The logic inside of _handle_battle_message was fighting back at me hahaha

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stated simply, this is the change:

BEFORE: we consume messages as they come. Showdown sends the request before the protocol, so we consume request and then protocol.

AFTER: we consume messages as they come, EXCEPT for the request-protocol pair, which we swap the order of upon receiving them.

Refer to the PR description for why this is a good idea

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see - in this case let's pause on this one for now; i want to take a look at possible alternatives

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. One path forward I can see is to do this change and then make an effort towards the ps-client rework that we were talking about the other day. Let me know if you have any other questions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed this concern now, hopefully you like the change! I moved this logic to player.py.

@cameronangliss cameronangliss reopened this Feb 1, 2025
@cameronangliss cameronangliss changed the title Fix request/protocol order of consumption More robust battle tracking Feb 1, 2025
@cameronangliss
Copy link
Contributor Author

@hsahovic I put elements of #659 into this PR because it ended up that this PR needed the changes from it, and also it needed the changes from this PR in order to handle Zoroark better. I've rebranded this as an effort to improve the battle tracking in poke-env. Also, I finally got around to pulling the logic that reverses the order of the request and protocol into player.py and out of ps_client.py like you asked!🥳 Let me know what you think!

@cameronangliss cameronangliss force-pushed the fix-request-protocol-consume-order branch from cebbf4d to 0ce7f67 Compare February 1, 2025 03:24
@cameronangliss
Copy link
Contributor Author

I'm realizing that even though this seems to pass tests, this PR is NOT ready for merging. This PR is exposing bugs with the way that we handle Zoroark which show up (sparsely, but it does show up). We need to rewrite the way we process the request in order to correct for the mistakes we're making, since the request can get us out of this mess by telling us the ground truth of the battle.

@cameronangliss cameronangliss marked this pull request as draft February 6, 2025 04:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

force_switch is forced before the events of a battle are processed
3 participants