Skip to content

Commit

Permalink
Python Starter bot - приведение к int для map_size (#6)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Rush-iam authored Jul 2, 2022
1 parent 5444357 commit bfea897
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion starter-bot-python/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion starter-bot-python/client/bot_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
9 changes: 9 additions & 0 deletions starter-bot-python/client/message/extra_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 1 addition & 2 deletions starter-bot-python/client/message/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')

Expand Down
2 changes: 1 addition & 1 deletion starter-bot-python/client/socket_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit bfea897

Please sign in to comment.