BurnySc2 / python-sc2

A StarCraft II bot api client library for Python 3
MIT License
509 stars 157 forks source link

Using client.leave() results in no replay save #128

Closed DuncanDHall closed 2 years ago

DuncanDHall commented 2 years ago

When trying to client.leave() in a bot:

  File "sharpy-sc2/python-sc2/sc2/main.py", line 608, in run_game
    result = asyncio.get_event_loop().run_until_complete(_host_game(map_settings, players, **kwargs))
  File "/usr/local/Cellar/python@3.9/3.9.1_6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "sharpy-sc2/python-sc2/sc2/main.py", line 488, in _host_game
    await client.save_replay(save_replay_as)
  File "sharpy-sc2/python-sc2/sc2/client.py", line 117, in save_replay
    result = await self._execute(save_replay=sc_pb.RequestSaveReplay())
  File "sharpy-sc2/python-sc2/sc2/protocol.py", line 75, in _execute
    raise ProtocolError(f"{response.error}")
sc2.protocol.ProtocolError: ['Not in a game']

Seems like if the saving of a replay were done not in sc2.main._host_game_aiter but instead added to sc2.client.leave that might solve it? That way when a bot calls client.leave, the save happens before await self._execute(leave_game=sc_pb.RequestLeaveGame())

https://github.com/BurnySc2/python-sc2/blob/b54fbb869da8b4442b4fb41996f62d16434b9a93/sc2/main.py#L520-L524

https://github.com/BurnySc2/python-sc2/blob/b54fbb869da8b4442b4fb41996f62d16434b9a93/sc2/client.py#L100-L113

BurnySc2 commented 2 years ago

Thanks! Although not pretty, this should fix it https://github.com/BurnySc2/python-sc2/pull/129 Do you agree?

DuncanDHall commented 2 years ago

Looks like it! Can verify next week when I'm back with my computer