def post(self, gamePassCode, playerId): try: game_action_tracker = create_game_action_tracker().load( request.get_json()) game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") current_player_move = get_game_player_moves(game.gameId) if current_player_move is None: raise ResourceNotFoundException( message="No Current Moves found") if current_player_move.playerId != playerId or game_action_tracker.playerId != playerId: raise FieldValidationException( message="Requested player is unable to start an action.") elif current_player_move.gameActionTrackerId is not None: raise FieldValidationException( message= "Unable to start a new action while an action is in progress." ) create_game_action_tracker(game_action_tracker) result = GameActionTrackerSchema().dump(game_action_tracker) current_player_move.gameActionTrackerId = result.gameActionTrackerId update_game_player_moves(current_player_move) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def get(self,gamePassCode,playerId): try: game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") current_player_move = get_game_player_moves(game.gameId) if current_player_move is None: raise ResourceNotFoundException(message="No Current Moves found") result = GamePlayerMovesSchema().dump(current_player_move) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def post(self,gamePassCode): try: gameFound = get_game_by_gamepasscode(gamePassCode) if gameFound is None: raise ResourceNotFoundException(message="Game Not Found") game = update_game_schema().load(request.get_json()) if game.gamePassCode != gameFound.gamePassCode: raise FieldValidationException(message="GamePassCode in request body doesn't match url") elif game.gameStatus == Enum.GameStatus.WaitingToStart: raise FieldValidationException(message="Game cannot be reset to Waiting To Start.") elif gameFound.gameStatus == Enum.GameStatus.Completed: raise FieldValidationException(message="Game is complete.No longer can be updated.") if gameFound.gameStatus == Enum.GameStatus.WaitingToStart and game.gameStatus == Enum.GameStatus.InProgress: try: cards = get_cards() players = get_players_by_gameid(gameFound.gameId) if not is_game_allowed_to_start(gameFound): raise FieldValidationException(message="Not enough players to start the game") create_game_cards(gameFound,players,cards) create_game_player_moves(gameFound) except ValidationError as e: raise ResourceValidationException(e) gameFound = update_game(game) result = GameSchema().dump(gameFound) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def get(self, cardId): card = get_card(cardId) if card is None: raise ResourceNotFoundException(message="Card Not Found") else: result = CardSchema().dump(card) return jsonify(result)
def get(self, gamePlayActionId): gamePlayAction = get_game_play_action(gamePlayActionId) if gamePlayAction is None: raise ResourceNotFoundException( message="Game Play Action Not Found") else: result = GamePlayActionSchema().dump(gamePlayAction) return jsonify(result)
def get(self, gamePassCode, playerId, gameActionTrackerId): try: game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") game_action_tracker = get_game_action_tracker( game.gameId, gameActionTrackerId) if game_action_tracker is None: raise ResourceNotFoundException( message="Game Action Tracker Not Found") result = GameActionTrackerSchema().dump(game_action_tracker) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def get(self,gamePassCode): try: game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") else: result = GameSchema().dump(game) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def post(self, gamePassCode, playerId, gameActionTrackerId): try: game_action_tracker = update_game_action_tracker().load( request.get_json()) game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") current_player_move = get_game_player_moves(game.gameId) if current_player_move is None: raise ResourceNotFoundException( message="No Current Moves found") if current_player_move.playerId != playerId: raise FieldValidationException( message="Requested player is unable to start an action.") except ValidationError as e: raise ResourceValidationException(e)
def get(self, gamePassCode, playerId): try: gameFound = get_game_by_gamepasscode(gamePassCode) if gameFound is None: raise ResourceNotFoundException(message="Game Not Found") playerCards = get_game_cards_on_hand(gameFound.gameId, playerId) result = GameCardSchema(many=True).dump(playerCards) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def get(self, gamePassCode): try: game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") gameCards = get_game_cards_in_play(game.gameId) result = GameCardSchema(many=True).dump(gameCards) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def post(self, gamePassCode): try: game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") player = login_player_schema().load(request.get_json()) playerFound = get_player_by_player_name(game.gameId, player.playerName) if playerFound is None: raise ResourceNotFoundException(message="Player Not Found") if check_password_hash(playerFound.playerPassCode, player.playerPassCode): player_result = PlayerSchema().dump(playerFound) result = create_tokens(player_result) session["gameId"] = game.gameId session["playerId"] = player_result["playerId"] return result, 200 else: raise ResourceNotFoundException(message="Player Not Found") except ValidationError as e: raise ResourceValidationException(e)
def get(self): gameId = session.get("gameId") playerId = session.get("playerId") if gameId is None or playerId is None: raise FieldValidationException(message="No Session exists.") playerFound = get_player_by_player_id(gameId, playerId) if playerFound is None: raise ResourceNotFoundException(message="Player Not Found") player_result = PlayerSchema().dump(playerFound) result = create_tokens(player_result) return make_response(result, 200)
def post(self, gamePassCode, playerId): try: gameFound = get_game_by_gamepasscode(gamePassCode) if gameFound is None: raise ResourceNotFoundException(message="Game Not Found") player_cards_on_hand = get_game_cards_on_hand( gameFound.gameId, playerId) game_play_actions = get_game_play_actions() game_cards_in_play = get_game_cards_in_play(gameFound.gameId) pre_move_check_list = get_pre_move_check_list( player_cards_on_hand, game_cards_in_play, game_play_actions) result = PreMoveCheckSchema(many=True).dump(pre_move_check_list) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def post(self,gamePassCode,playerId): try: updated_player_move = update_player_moves().load(request.get_json()) game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") current_player_move = get_game_player_moves(game.gameId) if current_player_move is None: raise FieldValidationException(message="No Current Moves found") if not is_player_moves_status_valid(current_player_move,updated_player_move): raise FieldValidationException(message="Invalid Game Status") if not (is_player_valid(current_player_move,playerId) and is_player_valid(updated_player_move,playerId)): raise FieldValidationException(message="Player not allowed to update") if not is_game_action_tracker_valid(current_player_move): raise FieldValidationException(message="Cannot progress without performing an action.") updated_player_move.totalGameMoveCount = current_player_move.totalGameMoveCount updated_player_move.gameActionTrackerId = current_player_move.gameActionTrackerId updated_player_move.gameId = current_player_move.gameId if (updated_player_move.gameMoveStatus == GameMoveStatus.WaitingForPlayerToBeginMove and current_player_move.numberOfMovesPlayed == MAX_NUMBER_OF_MOVES) or updated_player_move.gameMoveStatus == GameMoveStatus.SkipYourTurn: currentPlayerGameOrder = get_player_game_order(game.players,current_player_move.currentPlayerId) updated_player_move.currentPlayerId = get_next_player_id(game.players,1) if len(game.players) == currentPlayerGameOrder else get_next_player_id(game.players,currentPlayerGameOrder+1) updated_player_move.numberOfRounds +=1 updated_player_move.numberOfMovesPlayed = 1 if current_player_move.numberOfMovesPlayed == MAX_NUMBER_OF_MOVES else (current_player_move.numberOfMovesPlayed + 1) gamePlayerMove = update_game_player_moves(updated_player_move) result = GamePlayerMovesSchema().dump(gamePlayerMove) return jsonify(result) except ValidationError as e: raise ResourceValidationException(e)
def post(self, gamePassCode): try: #Get game and all current players that are part of the game game = get_game_by_gamepasscode(gamePassCode) if game is None: raise ResourceNotFoundException(message="Game Not Found") if not is_player_allowed_to_join(game): raise FieldValidationException( message="No more players can join the game") #create the player player = register_player_schema().load(request.get_json()) player.gameId = game.gameId player.gamePassCode = game.gamePassCode player.playerPassCode = generate_password_hash( player.playerPassCode) #evaluate their order based on other players' order player.playerGameOrder = 1 if len( game.players) == 0 else max(p.playerGameOrder for p in game.players) + 1 #check if user name exists if len([ x for x in game.players if x.playerName.lower() == player.playerName.lower() ]) > 0: raise FieldValidationException( message="Player Name already exists") add_player(player) player_result = PlayerSchema().dump(player) session["gameId"] = game.gameId session["playerId"] = player_result["playerId"] result = create_tokens(player_result) return result, 200 except ValidationError as e: raise ResourceValidationException(e)