Skip to content

Commit

Permalink
feat(generalsio): implement GeneralsIO client
Browse files Browse the repository at this point in the history
Ignoring mypy type checking for socketio library as it does not define
(and does not plan to) typing stubs.
  • Loading branch information
revolko authored and strakam committed Oct 25, 2024
1 parent a0a7109 commit 0ca4c43
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions generals/remote/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .generalsio_client import GeneralsBotError, GeneralsIOClient, GeneralsIOClientError

__all__ = [
"GeneralsBotError",
"GeneralsIOClientError",
"GeneralsIOClient",
]
57 changes: 57 additions & 0 deletions generals/remote/generalsio_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from socketio import SimpleClient # type: ignore

from generals.agents.agent import Agent


class GeneralsBotError(Exception):
"""Base generals-bot exception
TODO: find a place for exceptions
"""

pass


class GeneralsIOClientError(GeneralsBotError):
"""Base GeneralsIOClient exception"""

pass


class RegisterAgentError(GeneralsIOClientError):
"""Registering bot error"""

def __init__(self, msg: str) -> None:
super().__init__()
self.msg = msg

def __str__(self) -> str:
return f"Failed to register the agent. Error: {self.msg}"


class GeneralsIOClient(SimpleClient):
"""
Wrapper around socket.io client to enable Agent to join
GeneralsIO lobby.
"""

def __init__(self, agent: Agent, user_id: str):
super().__init__()
self.connect("https://botws.generals.io")
self.user_id = user_id
self._queue_id = ""

def _emit_receive(self, *args):
self.emit(*args)
return self.receive()

def register_agent(self, username: str) -> None:
"""
Register Agent to GeneralsIO platform.
You can configure one Agent per `user_id`. `user_id` should be handled as secret.
:param user_id: secret ID of Agent
:param username: agent username, must be prefixed with `[Bot]`
"""
event, response = self._emit_receive("set_username", (self.user_id, username))
if response:
# in case of success the response is empty
raise RegisterAgentError(response)

0 comments on commit 0ca4c43

Please sign in to comment.