-
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.
Browse files
Browse the repository at this point in the history
…ackend/socketio/create-game Hi @wingtkw , As I described above I will merge the PR first , you are still welcome to leave comment at here, or create another issue / discussion thread for any question about learning my code at here
- Loading branch information
Showing
14 changed files
with
572 additions
and
369 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,35 @@ | ||
from typing import Optional | ||
|
||
from app.domain import Game | ||
from app.dto import RtcRoomMsgData | ||
|
||
|
||
class AbstractRepository: | ||
class AbstractGameRepository: | ||
async def save(self, game: Game): | ||
raise NotImplementedError("AbstractRepository.save") | ||
raise NotImplementedError("AbstractGameRepository.save") | ||
|
||
async def get_game(self, game_id: str) -> Game: | ||
raise NotImplementedError("AbstractRepository.get_game") | ||
raise NotImplementedError("AbstractGameRepository.get_game") | ||
|
||
|
||
class AbstractRtcRoomRepository: | ||
async def save(self, data: RtcRoomMsgData): | ||
raise NotImplementedError("AbstractRtcRoomRepository.save") | ||
|
||
async def get(self, game_id: str) -> Optional[RtcRoomMsgData]: | ||
raise NotImplementedError("AbstractRtcRoomRepository.get") | ||
|
||
|
||
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 InMemoryGameRepository() | ||
|
||
|
||
def get_rtc_room_repository(): | ||
from .in_mem import InMemoryRtcRoomRepository | ||
|
||
return InMemoryRepository() | ||
return InMemoryRtcRoomRepository() |
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
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.