BurnySc2 / python-sc2

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

sc2.protocol.ProtocolError: ['Not in a game'] #96

Open BurnySc2 opened 3 years ago

BurnySc2 commented 3 years ago

Minimal bot example provided by SoupCatcher

import sc2
from sc2 import BotAI, Race
from sc2.player import Bot

class NoBot(BotAI):
  async def on_step(self, iteration):
    return

class ResignBot(BotAI):
  async def on_step(self, iteration):
    if iteration == 10:
      await self._client.leave()

def main():
    sc2.run_game(
        sc2.maps.get("EverDream506"),
        [Bot(Race.Random, ResignBot()), Bot(Race.Random, NoBot())],
        realtime=False,
        save_replay_as="test.SC2Replay"
    )

if __name__ == "__main__":
    main()

with trace:

Traceback (most recent call last):
  File "resign.py", line 24, in <module>
    main()
  File "resign.py", line 16, in main
    sc2.run_game(
  File ".../sc2/main.py", line 596, in run_game
    result = asyncio.get_event_loop().run_until_complete(
  File "/usr/local/***/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File ".../sc2/main.py", line 483, in _host_game
    await client.save_replay(save_replay_as)
  File ".../sc2/client.py", line 117, in save_replay
    result = await self._execute(save_replay=sc_pb.RequestSaveReplay())
  File ".../sc2/protocol.py", line 77, in _execute
    raise ProtocolError(f"{response.error}")
sc2.protocol.ProtocolError: ['Not in a game']

The game ended correctly, yet there is still an error trace, which does not belong there.

SoupCatcher commented 3 years ago

In normal operations the replay is saved before calling client.leave():

https://github.com/BurnySc2/python-sc2/blob/fa4933a1bf89540a052482b1a394c8d6206d7491/sc2/main.py#L482-L485

In the example in the OP the bot has already called client.leave() when we get to this code.