-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generalsio): implement GeneralsIO client
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |