-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#37] feature: http server emits event to socketio server on creating…
… game - socketio server collects the events, to know all the ongoing games - http server maintains a shared socketio client sending all relavant events - rename `AbstractRepository` to `AbstractGameRepository` - TODO, implement room repository for real-time communication Signed-off-by: T.H. <[email protected]>
- Loading branch information
1 parent
7bba139
commit 5b17c85
Showing
11 changed files
with
392 additions
and
314 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
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,38 @@ | ||
import logging | ||
import socketio | ||
|
||
from app.config import LOG_FILE_PATH, RTC_HOST, RTC_PORT | ||
from app.constant import RealTimeCommConst as RtcConst | ||
from app.domain import Game | ||
|
||
_logger = logging.getLogger(__name__) | ||
_logger.setLevel(logging.WARNING) | ||
_logger.addHandler(logging.FileHandler(LOG_FILE_PATH["REST"], mode="a")) | ||
|
||
|
||
class AbsEventEmitter: | ||
async def create_game(self, game: Game): | ||
raise NotImplementedError("AbsEventEmitter.create_game") | ||
|
||
|
||
class SocketIoEventEmitter(AbsEventEmitter): | ||
def __init__(self): | ||
self._url = "http://%s:%s" % (RTC_HOST, RTC_PORT) | ||
self._namespace = RtcConst.NAMESPACE | ||
self._client = socketio.AsyncSimpleClient(logger=True) | ||
|
||
async def create_game(self, game: Game): | ||
data = {"gameID": game.id, "players": [p.id for p in game.players]} | ||
try: | ||
if not self._client.connected: | ||
await self._client.connect(self._url, namespace=self._namespace) | ||
await self._client.emit(RtcConst.EVENTS.NEW_ROOM.value, data=data) | ||
except Exception as e: | ||
_logger.error("send new-room event: %s", e) | ||
# TODO, reconnect if connection inactive | ||
|
||
async def deinit(self): | ||
try: | ||
await self._client.disconnect() | ||
except Exception as e: | ||
_logger.error("error on deinit: %s", e) |
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
from app.domain import Game | ||
|
||
|
||
class AbstractRepository: | ||
class AbstractGameRepository: | ||
async def save(self, game: Game): | ||
raise NotImplementedError("AbstractRepository.save") | ||
|
||
async def get_game(self, game_id: str) -> Game: | ||
raise NotImplementedError("AbstractRepository.get_game") | ||
|
||
|
||
def get_repository(): | ||
def get_game_repository(): | ||
# TODO | ||
# - make it configurable at runtime | ||
# - set max limit of concurrent and ongoing games to save | ||
from .in_mem import InMemoryRepository | ||
from .in_mem import InMemoryGameRepository | ||
|
||
return InMemoryRepository() | ||
return InMemoryGameRepository() |
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
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
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
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
Oops, something went wrong.