moar moar

This commit is contained in:
codey 2024-09-18 21:37:50 -04:00
parent 3d554f5ad9
commit 6265342393
3 changed files with 30 additions and 8 deletions

View File

@ -37,3 +37,4 @@ class CAHGame:
def __iter__(self):
return [value for value in self.__dict__.values() if isinstance(value, int) or isinstance(value, float)].__iter__()

View File

@ -20,7 +20,7 @@ class ConnectionManager:
async def send_client_and_game_lists(self, state, websocket: WebSocket):
clients = []
games = [game for game in state.games]
games = [game.__dict__ for game in state.games]
for ws, client in self.active_connections.items():
print(f"Client: {client}")
@ -29,7 +29,7 @@ class ConnectionManager:
'resource': _client.resource,
'platform': _client.platform,
'connected_at': _client.connected_at,
'current_game': _client.current_game.id if _client.current_game else None,
'current_game': _client.current_game if _client.current_game else None,
})
await websocket.send_json({
@ -44,7 +44,7 @@ class ConnectionManager:
"ts": int(time.time()),
"data":
{
"games": [str(game.id) for game in games],
"games": state.get_games()
}
})

View File

@ -4,6 +4,7 @@ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
import time
import uuid
import json
import traceback
import random
from cah.constructors import CAHClient, CAHGame
from cah.websocket_conn import ConnectionManager
@ -82,6 +83,24 @@ class CAH(FastAPI):
})
def get_games(self):
try:
_games: list = []
for game in self.games:
print(f"Adding {game}")
print(f"Players: {game.players}")
_games.append({
'id': game.id,
'rounds': game.rounds,
'players': game.players,
'created_at': game.created_at,
'state': game.state,
'started_at': game.started_at,
'state_changed_at': game.state_changed_at,
})
return _games
except:
print(traceback.format_exc())
async def cah_handshake(self, websocket: WebSocket, data):
"""Handshake"""
@ -118,10 +137,11 @@ class CAH(FastAPI):
"success": True,
"resource": resource,
"platform": platform,
"games": [str(game.id) for game in self.games],
"games": self.get_games(),
},
})
async def create_game(self, websocket, data: str):
data = data.get('data')
if not data.get('rounds') or not str(data.get('rounds')).isnumeric():
@ -139,18 +159,19 @@ class CAH(FastAPI):
game_uuid = str(uuid.uuid4())
game = CAHGame(id=game_uuid,
rounds=rounds,
players=[client,],
players=[vars(client),],
created_at=int(time.time()),
state=-1,
started_at=0,
state_changed_at=int(time.time()))
self.games.append(game)
client.current_game = game
client.current_game = game.id
await websocket.send_json({
"event": "create_game_response",
"ts": int(time.time()),
"data": {
"success": True,
"createdGame": client.current_game.id,
"createdGame": client.current_game,
}
})
await self.connection_manager.send_client_and_game_lists(self, websocket)