Skip to content

Commit

Permalink
feat(generalsio): handle basic game progress
Browse files Browse the repository at this point in the history
TODO: enable Agent to make moves
  • Loading branch information
revolko committed Oct 19, 2024
1 parent 026fada commit a1e1b3e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions generals/remote/generalsio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ def __init__(self, agent: Agent, user_id: str):
self.user_id = user_id
self._queue_id = ""

@property
def queue_id(self):
if not self._queue_id:
raise GeneralsIOClientError("Queue ID is not set.\nIs agent in the game lobby?")

return self._queue_id

def _emit_receive(self, *args):
self.emit(*args)
return self.receive()
Expand All @@ -55,3 +62,48 @@ def register_agent(self, username: str) -> None:
if response:
# in case of success the response is empty
raise RegisterAgentError(response)

def join_private_lobby(self, queue_id: str) -> None:
"""
Join (or create) private game lobby.
:param queue_id: Either URL or lobby ID number
"""
self._emit_receive("join_private", (queue_id, self.user_id))
self._queue_id = queue_id

def join_game(self, force_start: bool = True) -> None:
"""
Set force start if requested and wait for the game start.
:param force_start: If set to True, the Agent will set `Force Start` flag
"""
if force_start:
self.emit("set_force_start", (self.queue_id, True))

while True:
event = self.receive()[0]
if event == "game_start":
break

self._start_game()

def _start_game(self) -> None:
"""
Triggered after server starts the game.
TODO: spawn a new thread in which Agent will calculate its moves
"""
winner = False
while True:
event = self.receive()[0]
if event == "game_lost" or event == "game_won":
# server sends game_lost or game_won before game_over
winner = event == "game_won"
break

self._finish_game(winner)

def _finish_game(self, is_winner: bool) -> None:
"""
Triggered after server finishes the game.
:param is_winner: True if Agent won the game
"""
print("game is finished. Am I a winner?", is_winner)

0 comments on commit a1e1b3e

Please sign in to comment.