moar moar
This commit is contained in:
parent
3d554f5ad9
commit
6265342393
@ -37,3 +37,4 @@ class CAHGame:
|
|||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return [value for value in self.__dict__.values() if isinstance(value, int) or isinstance(value, float)].__iter__()
|
return [value for value in self.__dict__.values() if isinstance(value, int) or isinstance(value, float)].__iter__()
|
||||||
|
|
@ -20,7 +20,7 @@ class ConnectionManager:
|
|||||||
|
|
||||||
async def send_client_and_game_lists(self, state, websocket: WebSocket):
|
async def send_client_and_game_lists(self, state, websocket: WebSocket):
|
||||||
clients = []
|
clients = []
|
||||||
games = [game for game in state.games]
|
games = [game.__dict__ for game in state.games]
|
||||||
|
|
||||||
for ws, client in self.active_connections.items():
|
for ws, client in self.active_connections.items():
|
||||||
print(f"Client: {client}")
|
print(f"Client: {client}")
|
||||||
@ -29,7 +29,7 @@ class ConnectionManager:
|
|||||||
'resource': _client.resource,
|
'resource': _client.resource,
|
||||||
'platform': _client.platform,
|
'platform': _client.platform,
|
||||||
'connected_at': _client.connected_at,
|
'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({
|
await websocket.send_json({
|
||||||
@ -44,7 +44,7 @@ class ConnectionManager:
|
|||||||
"ts": int(time.time()),
|
"ts": int(time.time()),
|
||||||
"data":
|
"data":
|
||||||
{
|
{
|
||||||
"games": [str(game.id) for game in games],
|
"games": state.get_games()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
|
|||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
import json
|
import json
|
||||||
|
import traceback
|
||||||
import random
|
import random
|
||||||
from cah.constructors import CAHClient, CAHGame
|
from cah.constructors import CAHClient, CAHGame
|
||||||
from cah.websocket_conn import ConnectionManager
|
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):
|
async def cah_handshake(self, websocket: WebSocket, data):
|
||||||
"""Handshake"""
|
"""Handshake"""
|
||||||
@ -118,10 +137,11 @@ class CAH(FastAPI):
|
|||||||
"success": True,
|
"success": True,
|
||||||
"resource": resource,
|
"resource": resource,
|
||||||
"platform": platform,
|
"platform": platform,
|
||||||
"games": [str(game.id) for game in self.games],
|
"games": self.get_games(),
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
async def create_game(self, websocket, data: str):
|
async def create_game(self, websocket, data: str):
|
||||||
data = data.get('data')
|
data = data.get('data')
|
||||||
if not data.get('rounds') or not str(data.get('rounds')).isnumeric():
|
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_uuid = str(uuid.uuid4())
|
||||||
game = CAHGame(id=game_uuid,
|
game = CAHGame(id=game_uuid,
|
||||||
rounds=rounds,
|
rounds=rounds,
|
||||||
players=[client,],
|
players=[vars(client),],
|
||||||
created_at=int(time.time()),
|
created_at=int(time.time()),
|
||||||
state=-1,
|
state=-1,
|
||||||
started_at=0,
|
started_at=0,
|
||||||
state_changed_at=int(time.time()))
|
state_changed_at=int(time.time()))
|
||||||
self.games.append(game)
|
self.games.append(game)
|
||||||
client.current_game = game
|
client.current_game = game.id
|
||||||
await websocket.send_json({
|
await websocket.send_json({
|
||||||
"event": "create_game_response",
|
"event": "create_game_response",
|
||||||
"ts": int(time.time()),
|
"ts": int(time.time()),
|
||||||
"data": {
|
"data": {
|
||||||
"success": True,
|
"success": True,
|
||||||
"createdGame": client.current_game.id,
|
"createdGame": client.current_game,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
await self.connection_manager.send_client_and_game_lists(self, websocket)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user