diff --git a/generals/envs/generalsio_client.py b/generals/envs/generalsio_client.py new file mode 100644 index 0000000..43423d5 --- /dev/null +++ b/generals/envs/generalsio_client.py @@ -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)