Skip to content

Commit

Permalink
Python starter bot - fixed bot freeze after 2+ rounds timeout (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rush-iam authored Jul 18, 2022
1 parent a0f7e69 commit 6f50c56
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion starter-bot-python/client/socket_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class SocketSession:
_buffer_size = 8192
_timeout_move = 'move\noffset 0 0\nend\n'

def __init__(self, host: str, port: int):
self.socket = socket.socket()
Expand All @@ -25,12 +26,13 @@ def read(self) -> list[str]:

if len(self.buffer) > 0:
logging.warning('skipping round, seems like your bot had timed out')
self.write(self._timeout_move)
return self.read()

return data.decode().split('\n')

def _find_end_index(self) -> int:
return self.buffer.find(b'end\n', len(self.buffer) - self._buffer_size)
return self.buffer.find(b'end\n', len(self.buffer) - self._buffer_size - 4)

def write(self, data: str) -> None:
self.socket.sendall(data.encode())

0 comments on commit 6f50c56

Please sign in to comment.