Skip to content

Commit

Permalink
feat(generalsio): implement GeneralsIO client
Browse files Browse the repository at this point in the history
  • Loading branch information
revolko committed Oct 18, 2024
1 parent 48341d3 commit 550021b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions generals/envs/generalsio_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from socketio import SimpleClient

from generals.agents.agent import Agent


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


class RegisterAgentError(GeneralsBotError):
"""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):
super().__init__()
self.connect('https://botws.generals.io')

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

def register_agent(self, user_id: str, 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', (user_id, username))
if response:
# in case of success the response is empty
raise RegisterAgentError(response)

0 comments on commit 550021b

Please sign in to comment.