rm cah_ext
This commit is contained in:
parent
7029eedafc
commit
5e4dbe7b50
@ -1,3 +0,0 @@
|
|||||||
#!/usr/bin/env python3.12
|
|
||||||
|
|
||||||
from . import constructors
|
|
@ -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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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
|
|
Loading…
x
Reference in New Issue
Block a user