From bfea897da07953602347acf9238dd1c6f015e0f9 Mon Sep 17 00:00:00 2001 From: Artyom Kornikov / nGragas <66574145+Rush-iam@users.noreply.github.com> Date: Sat, 2 Jul 2022 22:18:47 +0300 Subject: [PATCH] =?UTF-8?q?Python=20Starter=20bot=20-=20=D0=BF=D1=80=D0=B8?= =?UTF-8?q?=D0=B2=D0=B5=D0=B4=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=20int=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20map=5Fsize=20(#6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Python bot init talks with server * Python talk with server up to started match * Python starter bot client done * - Fixed socket read buffer - Named tuple XY and BotInfo * - Revert back to dataclasses (faster) * - Socket - skip server round data to stay in sync * - README - bot comments - optimized Socket performance * - more Type annotations * - fixed map size XY type (str -> int) * better str -> int type conversion --- starter-bot-python/bot.py | 2 +- starter-bot-python/client/bot_base.py | 2 +- starter-bot-python/client/message/extra_types.py | 9 +++++++++ starter-bot-python/client/message/factory.py | 3 +-- starter-bot-python/client/socket_session.py | 2 +- 5 files changed, 13 insertions(+), 5 deletions(-) diff --git a/starter-bot-python/bot.py b/starter-bot-python/bot.py index 4d159fb..13f5611 100644 --- a/starter-bot-python/bot.py +++ b/starter-bot-python/bot.py @@ -10,7 +10,7 @@ class Bot(BotBase): # Старт - def on_match_start(self, match_info: MatchStarted): + def on_match_start(self, match_info: MatchStarted) -> None: # Правила матча self.match_info = match_info self.id = match_info.your_id diff --git a/starter-bot-python/client/bot_base.py b/starter-bot-python/client/bot_base.py index 9f9be74..d49f43c 100644 --- a/starter-bot-python/client/bot_base.py +++ b/starter-bot-python/client/bot_base.py @@ -16,7 +16,7 @@ class BotBase: match_info: MatchStarted = field(init=False) id: int = field(init=False) - def on_match_start(self, match_info: MatchStarted): + def on_match_start(self, match_info: MatchStarted) -> None: raise NotImplementedError def on_update(self, update: Update) -> tuple[int, int]: diff --git a/starter-bot-python/client/message/extra_types.py b/starter-bot-python/client/message/extra_types.py index db3d8d9..e791cc5 100644 --- a/starter-bot-python/client/message/extra_types.py +++ b/starter-bot-python/client/message/extra_types.py @@ -12,8 +12,17 @@ class XY: x: int y: int + def __post_init__(self): + self.x = int(self.x) + self.y = int(self.y) + @dataclass class BotInfo(XY): coins: int id: int + + def __post_init__(self): + super().__post_init__() + self.coins = int(self.coins) + self.id = int(self.id) diff --git a/starter-bot-python/client/message/factory.py b/starter-bot-python/client/message/factory.py index a39114d..dcc3ff9 100644 --- a/starter-bot-python/client/message/factory.py +++ b/starter-bot-python/client/message/factory.py @@ -38,8 +38,7 @@ def load(cls, data: list[str]) -> Message: if container is None: params[name] = real_type(*value) elif container is list: - # map(int, ) assumes that all nested types has only int fields - params[name].append(real_type(*map(int, value))) + params[name].append(real_type(*value)) else: raise Exception(f'cannot handle {command}:{name}:{container}') diff --git a/starter-bot-python/client/socket_session.py b/starter-bot-python/client/socket_session.py index a4e7781..d1e7448 100644 --- a/starter-bot-python/client/socket_session.py +++ b/starter-bot-python/client/socket_session.py @@ -29,7 +29,7 @@ def read(self) -> list[str]: return data.decode().split('\n') - def _find_end_index(self): + def _find_end_index(self) -> int: return self.buffer.find(b'end\n', len(self.buffer) - self._buffer_size) def write(self, data: str) -> None: