From 5e4dbe7b50f956c817a4578b77f5643f690ae272 Mon Sep 17 00:00:00 2001 From: codey Date: Sun, 19 Jan 2025 13:30:04 -0500 Subject: [PATCH] rm cah_ext --- cah_ext/__init__.py | 3 - cah_ext/constructors.py | 63 ------------------- cah_ext/websocket_conn.py | 123 -------------------------------------- 3 files changed, 189 deletions(-) delete mode 100644 cah_ext/__init__.py delete mode 100644 cah_ext/constructors.py delete mode 100644 cah_ext/websocket_conn.py diff --git a/cah_ext/__init__.py b/cah_ext/__init__.py deleted file mode 100644 index b4ecc12..0000000 --- a/cah_ext/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env python3.12 - -from . import constructors \ No newline at end of file diff --git a/cah_ext/constructors.py b/cah_ext/constructors.py deleted file mode 100644 index f5a6ba5..0000000 --- a/cah_ext/constructors.py +++ /dev/null @@ -1,63 +0,0 @@ -#!/usr/bin/env python3.12 - -class CAHClient: - """CAH Client Constructor""" - def __init__(self, - resource: str, - platform: str, - csid: str, - connected_at: int, - players: list, - games: list): - self.resource: str = resource - self.platform: str = platform - self.csid: str = csid - self.connected_at: int = connected_at - self.players: list = players - self.games: list = games - - def __iter__(self): - return [value for value in self.__dict__.values() if isinstance(value, int) or isinstance(value, float)].__iter__() - -class CAHGame: - """CAH Game Constructor""" - def __init__(self, - _id: str, - rounds: int, - resources: list[dict], - players: list[dict], - created_at: int, - state: int, - started_at: int, - state_changed_at: int, - ): - self.id: str = id - self.rounds: int = rounds - self.resources: list[dict] = resources - self.players: list[dict] = players - self.created_at: int = created_at - self.state: int = state - self.started_at: int = started_at - self.state_changed_at: int = state_changed_at - - def __iter__(self): - return [value for value in self.__dict__.values() if isinstance(value, int) or isinstance(value, float)].__iter__() - -class CAHPlayer: - """CAH Player Constructor""" - def __init__(self, - _id: str, - current_game: CAHGame, - platform: str, - related_resource: str, - joined_at: str, - handle: str): - self.id = id - self.current_game = current_game - self.platform = platform - self.related_resource = related_resource - self.joined_at = joined_at - self.handle = handle - - - \ No newline at end of file diff --git a/cah_ext/websocket_conn.py b/cah_ext/websocket_conn.py deleted file mode 100644 index 33c7d8a..0000000 --- a/cah_ext/websocket_conn.py +++ /dev/null @@ -1,123 +0,0 @@ -#!/usr/bin/env python3.12 -# pylint: disable=bare-except, broad-exception-caught, invalid-name - -import time -import logging -from fastapi import WebSocket -from cah_ext.constructors import CAHClient - -class ConnectionManager: - """WS Connection Manager""" - def __init__(self): - self.active_connections: dict = {} - - def get_connection_by_ws(self, websocket: WebSocket) -> WebSocket: - """Get Connection by WS""" - return self.active_connections.get(websocket) - - def get_connection_by_csid(self, csid: str) -> WebSocket: - """Get Connection by CSID""" - for connection in self.active_connections: - if connection.get('csid') == csid: - return connection - - def get_connection_by_resource_label(self, resource: str): - """Get Connection by Resource Label""" - for connection in self.active_connections: - try: - if connection.get('client').get('resource') == resource: - return connection - except: - continue - - async def send_client_and_game_lists(self, state, websocket: WebSocket): - """Send Client and Game Lists""" - clients = [] - - for _, client in self.active_connections.items(): - logging.debug("Client: %s", client) - _client = client.get('client') - clients.append({ - 'resource': _client.resource, - 'platform': _client.platform, - 'connected_at': _client.connected_at, - }) - - await websocket.send_json({ - "event": "client_list", - "ts": int(time.time()), - "data": { - "clients": clients - } - }) - await websocket.send_json({ - "event": "game_list", - "ts": int(time.time()), - "data": - { - "games": state.get_games() - } - }) - - async def connect(self, websocket: WebSocket): - """Process WS Client Connection""" - await websocket.accept() - self.active_connections[websocket] = { - 'client': None, - 'websocket': websocket, - } - - async def handshake_complete(self, - state, - websocket: WebSocket, - csid: str, - handshakedClient: CAHClient): - """Process Handshake""" - if websocket in self.active_connections: - self.active_connections.pop(websocket) - self.active_connections[websocket] = { - 'websocket': websocket, - 'csid': csid, - 'client': handshakedClient, - } - - await self.broadcast({ - "event": "client_connected", - "ts": int(time.time()), - "data": { - "connected_resource": handshakedClient.resource, - "connected_platform": handshakedClient.platform, - } - }) - - await self.send_client_and_game_lists(state, - websocket) - - async def disconnect(self, state, websocket: WebSocket, csid: str = None): # pylint: disable=unused-argument - """Process WS Client Disconnection""" - disconnected = self.get_connection_by_ws(websocket) - disconnected_client = disconnected.get('client') - disconnected_resource = disconnected_client.resource - disconnected_games = [str(game.id) for game in disconnected_client.games] - await self.broadcast({ - "event": "client_disconnected", - "ts": int(time.time()), - "data": { - "disconnected_resource": disconnected_resource, - } - }) - await state.remove_resource(disconnected_games, disconnected_resource) - self.active_connections.pop(websocket) - - - async def send(self, message: str, websocket: WebSocket): - """Send WS Client some data""" - await websocket.send_json(message) - - async def broadcast(self, message: str): - """Broadcast data to all connected WS clients""" - for connection in self.active_connections: - try: - await connection.send_json(message) - except: - continue \ No newline at end of file