async def advance_from_voting( room_name: str, game_mapper: GameEventMapper = Depends(get_game_mapper), token: str = Depends(JWTBearer()), conman: ConnectionManager = Depends(get_websocket_connection_manager), ): user = decodeJWT(token) if user is None: raise HTTPException(404, "User not found with this token.") room = game_mapper.get_game(room_name) if room is None: raise HTTPException(404, "Room does not exist.") if user.user_id not in room.session.player_lookup.keys(): raise HTTPException(404, "User not in the game room.") if room.session.state == GameStates.PLAYERS_INSPECTING_RESULT: room.on_new_event(AdvanceRoundAfterVoting(), user.user_id) else: raise HTTPException(400, "You can't do that in this game state.") await broadcast_event( room_name, conman, f"{user.user_id} has started a new round!", ) return GameStatePlayerView.from_game_state(room.session, user.user_id)
async def select_winner( room_name: str, winner: SelectWinningSubmission, game_mapper: GameEventMapper = Depends(get_game_mapper), token: str = Depends(JWTBearer()), conman: ConnectionManager = Depends(get_websocket_connection_manager), ): user = decodeJWT(token) if user is None: raise HTTPException(404, "User not found with this token.") room = game_mapper.get_game(room_name) if room is None: raise HTTPException(404, "Room does not exist.") try: room.on_new_event(winner, user.user_id) except GameHasEnded as e: pass # It's fine. Just proceed. # TODO: event broadcast shall be triggered somehow inside from the SM await broadcast_event( room_name, conman, f"{user.user_id} voted for the winner", ) return GameStatePlayerView.from_game_state(room.session, user.user_id)
async def get_token_header(x_token: str = Header(...), db=Depends(get_db)): token = decodeJWT(x_token) if token is None: raise HTTPException(status_code=400, detail="X-Token header invalid") user = get_user_by_username(db, token.user_id) if user is None: raise HTTPException(status_code=404, detail="No user is associated with the token")
def refresh( room_name: str, game_mapper: GameEventMapper = Depends(get_game_mapper), token: str = Depends(JWTBearer()), ): user = decodeJWT(token) if user is None: raise HTTPException(404, "User not found with this token.") room = game_mapper.get_game(room_name) if room is None: raise HTTPException(404, "Room does not exist.") if user.user_id not in room.session.player_lookup.keys(): raise HTTPException(404, "User not in the game room.") return GameStatePlayerView.from_game_state(room.session, user.user_id)
async def websocket_endpoint( websocket: WebSocket, room_name: str, token, db: Session = Depends(get_db), manager: ConnectionManager = Depends(get_websocket_connection_manager), ): decoded_content = decodeJWT(token) if decoded_content is None: raise Exception("No valid credentials!") username = decoded_content.user_id username = get_user_by_username(db, username).username await manager.connect(websocket=websocket, room_name=room_name, username=username) try: while True: data = await websocket.receive_text() # await manager.send_personal_message( # room_name, # username, # WebSocketMessage( # message=f"You wrote: {data}", # sender=SENDER_TYPES.PERSONAL, # topic=room_name, # ), # ) await manager.broadcast( room_name, WebSocketMessage( message=f"[{username}] {data}", sender=SENDER_TYPES.SYSTEM, topic=room_name, ), ) except WebSocketDisconnect: await manager.disconnect(room_name=room_name, username=username, websocket=websocket) await manager.broadcast( room_name, WebSocketMessage( message=f"[{username}] disconnected", sender=SENDER_TYPES.SYSTEM, topic=room_name, ), )
async def join_game( room_name: str, game_mapper: GameEventMapper = Depends(get_game_mapper), token: str = Depends(JWTBearer()), conman: ConnectionManager = Depends(get_websocket_connection_manager), ): user = decodeJWT(token) if user is None: raise HTTPException(404, "User not found with this token.") room = game_mapper.get_game(room_name) if room is None: raise HTTPException(404, "Room does not exist.") room.session.player_join(user.user_id) await broadcast_event(room_name, conman, f"{user.user_id} has joined the game!") return GameStatePlayerView.from_game_state(room.session, user.user_id)
async def submit_cards( room_name: str, cards: List[WhiteCard], game_mapper: GameEventMapper = Depends(get_game_mapper), token: str = Depends(JWTBearer()), conman: ConnectionManager = Depends(get_websocket_connection_manager), ): user = decodeJWT(token) if user is None: raise HTTPException(404, "User not found with this token.") room = game_mapper.get_game(room_name) if room is None: raise HTTPException(404, "Room does not exist.") room.on_new_event( PlayerSubmitCards(submitting_user=user.user_id, cards=cards), user.user_id, ) await broadcast_event(room_name, conman, f"{user.user_id} has submitted cards!") return GameStatePlayerView.from_game_state(room.session, user.user_id)
async def leave_game( room_name: str, game_mapper: GameEventMapper = Depends(get_game_mapper), token: str = Depends(JWTBearer()), conman: ConnectionManager = Depends(get_websocket_connection_manager), ): user = decodeJWT(token) if user is None: raise HTTPException(404, "User not found with this token.") room = game_mapper.get_game(room_name) if room is None: raise HTTPException(404, "Room does not exist.") # This kinda deserves a test: if room.session.state != GameStates.FINISHED: room.session.player_leaves(user.user_id) if len(room.session.players) == 0: game_mapper.end_game(room_name) await broadcast_event(room_name, conman, f"{user.user_id} has left the game!") return LeaveResponse()
def is_my_login_valid(token: str = Depends(JWTBearer())): user = decodeJWT(token) if user is None: raise HTTPException(404, "User not found with this token.") return LoginCheckResponse(success=True)