예제 #1
0
async def choose_headmaster(headmaster: PlayerM,
                            game_id: int,
                            user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)
        if game is None:
            raise HTTPException(status_code=404, detail="Game not found")
        if not game.started:
            raise HTTPException(status_code=400, detail="Game is not started")
        status = game.status
        player = Player.get(id=status["minister"])
        if status["phase"] != "propose":
            raise HTTPException(
                status_code=400,
                detail="The headmaster only can be elected in the propose phase"
            )
        if player.user.id != user["id"]:
            raise HTTPException(
                status_code=400,
                detail="Only the minister can propose a headmaster")
        new_hm = Player.get(id=headmaster.id)
        if new_hm is None:
            raise HTTPException(status_code=400,
                                detail="The selected player does not exist")
        if new_hm.id == status["minister"]:
            raise HTTPException(
                status_code=400,
                detail="The minister can not be the headmaster")
        if not new_hm.choosable:
            raise HTTPException(
                status_code=400,
                detail="The player has been headmaster in the previous round")
        if new_hm.game.id != game_id:
            raise HTTPException(
                status_code=400,
                detail="The player does not belong to this game")
        if not new_hm.alive:
            raise HTTPException(
                status_code=400,
                detail="The player cannot be headmaster because is dead")
        Player.reset_choosable()
        status["headmaster"] = int(headmaster.id)
        # PASS THE TURN ####################
        status["phase"] = "vote"
        #####################################
        game.status = status
        new_hm.current_position = "headmaster"
        new_hm.choosable = False
        return {
            "message":
            f'The player number {new_hm.id}: {new_hm.user.username} was proposed as headmaster'
        }
예제 #2
0
async def vote(in_vote: VoteM, game_id: int, user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)
        if game is None:
            raise HTTPException(status_code=404, detail="Game not found")
        if game.status["phase"] != "vote":
            raise HTTPException(status_code=400,
                                detail="It is not the vote phase")

        current_player = Player.user_player(user, game_id)
        vote_arr = [{
            "player": current_player["id"],
            "user": user["id"],
            "username": user["username"],
            "vote": in_vote.vote
        }]

        if 'votes' in game.status.keys():
            for v in game.status["votes"]:
                if v["user"] == user["id"]:
                    raise HTTPException(status_code=400,
                                        detail="This player already voted")
            game.status["votes"] = game.status["votes"] + vote_arr
        else:
            game.status["votes"] = vote_arr

        pid = current_player["id"]
        username = user["username"]
        player_msg = f"Player: {pid} ({username}) successfully voted"
        general_msg = "election in progress"
        if len(game.status["votes"]) == game.players.select(
                lambda p: p.alive).count():
            nox_votes = 0
            lumos_votes = 0
            for v in game.status["votes"]:
                if v["vote"]:
                    lumos_votes += 1
                else:
                    nox_votes += 1
            if lumos_votes > nox_votes:
                # PASS THE TURN ######################
                game.board.caos = 0
                game.status["phase"] = "minister play"
                general_msg = "election succeed"
                new_hm = Player.get(id=game.status["headmaster"])
                if new_hm and (game.player_amount == 5 or game.player_amount == 6) \
                        and game.board.de_proc > 3 and new_hm.is_voldemort:
                    game.status = {
                        "info": "game ended",
                        "winner": "Death Eaters",
                        "detail": "voldemort headmaster"
                    }
                    return {"vote": player_msg, "election": general_msg}
                ######################################
            else:
                # PASS THE TURN #####################
                game.board.caos = game.board.caos + 1
                general_msg = "election failed"
                # ACTIONS TO DO IF CAOS IS EQUAL TO 3
                if game.board.caos == 3:
                    deck = game.board.deck.split(',')
                    first_card = deck[:1]
                    if first_card[0] == 'death':
                        game.board.de_proc += 1
                    else:
                        game.board.po_proc += 1
                    if 'caos' in game.status.keys():
                        game.status["caos"] = game.status["caos"] + first_card
                    else:
                        game.status["caos"] = first_card
                    game.board.deck = ','.join(deck[1:])
                    game.board.caos = 0
                    general_msg = "government caos"
                    Player.reset_choosable()
                #####################################
                Player.reassign_minister(game)
        return {"vote": player_msg, "election": general_msg}