moar moar
This commit is contained in:
@ -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)
|
||||
|
Reference in New Issue
Block a user