Exemple #1
0
async def all_messages(game_id: int, user=Depends(manager)):
    with db_session:

        def user_data(obj_player):
            c_user = obj_player.user
            return {
                "id": c_user.id,
                "username": c_user.username,
                "useralias": c_user.useralias
            }

        game = Game.get(id=game_id)
        Player.user_player(user, 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")
        chats = game.chats.order_by(lambda c: desc(c.date))
        return {
            'data': [{
                "content": m.content,
                "date": m.date,
                "send_by": user_data(m.player)
            } for m in chats]
        }
Exemple #2
0
async def play_crucio(player_id: int, game_id: int, user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)
        current_player = Player.user_player(user, game_id)
        victim_player = Player.select(
            lambda p: p.id == player_id and p.game.id == game_id).first()
        deck = game.board.spell_fields.split(",")
        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")
        if not victim_player:
            raise HTTPException(
                status_code=400,
                detail="The victim player does not belong to this game")
        if game.status["phase"] != "spell play":
            raise HTTPException(status_code=400,
                                detail="Its not time for playing spells!")
        if current_player["current_position"] != "minister":
            raise HTTPException(status_code=400,
                                detail=f"This player is not the minister")
        if game.board.de_proc == 0 or deck[game.board.de_proc - 1] != "crucio":
            raise HTTPException(status_code=400,
                                detail="The crucio spell is not available")
        victim_user = User.select(
            lambda u: u.id == victim_player.user.id).first()
        role = victim_player.role
        return {
            "role": role,
            "player_id": player_id,
            "player_alias": victim_user.useralias
        }
Exemple #3
0
async def play(proc: ProcM, 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")

        current_player = Player.user_player(user, game_id)

        if game.status["phase"] == "minister play":
            if current_player["current_position"] != "minister":
                raise HTTPException(status_code=404,
                                    detail="This player is not the minister")
            cards = game.board.deck.split(',')[:3]
            if proc.card in cards:
                cards = game.board.deck.split(',')
                cards.remove(proc.card)
                game.board.deck = ','.join(cards)
            else:
                raise HTTPException(
                    status_code=400,
                    detail="The input card was not one of the options")
            msg = f'{proc.card} card discarded successfully'
            # PASS THE TURN #####################
            game.status["phase"] = "headmaster play"
            #####################################

        elif game.status["phase"] == "headmaster play":
            if current_player["current_position"] != "headmaster":
                raise HTTPException(status_code=404,
                                    detail="This player is not the headmaster")
            cards = game.board.deck.split(',')[:2]
            if proc.card in cards:
                cards = game.board.deck.split(',')[2:]
                game.board.deck = ','.join(cards)
                if proc.card == 'phoenix':
                    game.board.po_proc += 1
                else:
                    game.board.de_proc += 1
                # IMPORTANT! HERE GOES THE LOGIC FOR SPELL ACTIVATION
                # PASS THE TURN ###########
                spell_fields = game.board.spell_fields.split(",")
                spells = ["divination", "avadakedavra", "imperius", "crucio"]
                if game.board.de_proc != 0 and spell_fields[game.board.de_proc
                                                            - 1] in spells:
                    game.status["phase"] = "spell play"
                else:
                    Player.reassign_minister(game)
                #####################################
                msg = f'{proc.card} card played successfully'
            else:
                raise HTTPException(
                    status_code=400,
                    detail="The input card was not one of the options")

        else:
            raise HTTPException(
                status_code=400,
                detail="It is not a phase for playing a proclamation")

        return {"message": msg}
Exemple #4
0
async def get_proclamations(game_id: int, user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)

        current_player = Player.user_player(user, game_id)

        if game is None:
            raise HTTPException(status_code=404, detail="Game not found")
        if game.status["phase"] == "minister play":
            if current_player["current_position"] != "minister":
                raise HTTPException(status_code=404,
                                    detail="This player is not the minister")
            cards = game.board.deck.split(',')[:3]
            data = {"data": cards}
        elif game.status["phase"] == "headmaster play":
            if current_player["current_position"] != "headmaster":
                raise HTTPException(status_code=404,
                                    detail="This player is not the headmaster")
            cards = game.board.deck.split(',')[:2]
            data = {"data": cards}
        else:
            raise HTTPException(
                status_code=400,
                detail="It is not a phase for geting a proclamation")
        return data
Exemple #5
0
async def get_current_player(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="The game does not exist")

        return Player.user_player(user, game_id)
Exemple #6
0
async def left_game(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.started:
            raise HTTPException(status_code=400,
                                detail="The Game is already started")

        current_player = Player.user_player(user, game_id)
        player_obj = Player.get(id=current_player["id"])
        player_obj.delete()

    return {"message": 'game left successfully'}
Exemple #7
0
async def expelliarmus(in_vote: VoteM, game_id: int, user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)
        current_player = Player.user_player(user, game_id)
        username = User.get(id=current_player["user"]).username
        detail = f"The player {username} has played expelliarmus!"

        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")
        if current_player["current_position"] != "minister" and current_player[
                "current_position"] != "headmaster":
            raise HTTPException(
                status_code=400,
                detail="this player is not the minister nor the headmaster")
        if game.board.de_proc < 5 or game.status["phase"] != "headmaster play":
            raise HTTPException(status_code=400,
                                detail="It is not time for expelliarmus!!!")

        if "headmaster_expelliarmus" not in game.status.keys():
            if current_player["current_position"] == 'headmaster':
                game.status["headmaster_expelliarmus"] = in_vote.vote
            else:
                raise HTTPException(
                    status_code=400,
                    detail="The headmaster must play the expelliarmus first!")
        else:
            if current_player["current_position"] == 'minister':
                game.status["minister_expelliarmus"] = in_vote.vote
            else:
                raise HTTPException(
                    status_code=400,
                    detail="The minister must confirm the expelliarmus!")
            detail = "The expelliarmus has failed!"
            if game.status["minister_expelliarmus"] and game.status[
                    "headmaster_expelliarmus"]:
                cards = game.board.deck.split(',')[2:]
                game.board.deck = ','.join(cards)
                detail = "the expelliarmus was played succesfully!, cards discarded"
                Player.reassign_minister(game)

        return {detail}
Exemple #8
0
async def play_divination(game_id: int, user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)
        current_player = Player.user_player(user, game_id)
        deck = game.board.spell_fields.split(",")
        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")
        if game.status["phase"] != "spell play":
            raise HTTPException(status_code=400,
                                detail="Its not time for playing spells!")
        if current_player["current_position"] != "minister":
            raise HTTPException(status_code=400,
                                detail=f"This player is not the minister")
        if game.board.de_proc == 0 or deck[game.board.de_proc -
                                           1] != "divination":
            raise HTTPException(status_code=400,
                                detail="The divination spell is not available")
        return {"data": game.board.deck.split(",")[:3]}
Exemple #9
0
async def kill_player(player_id: PlayerM, game_id: int, user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)
        current_player = Player.user_player(user, game_id)
        victim_player = Player.select(
            lambda p: p.id == player_id.id and p.game.id == game_id).first()
        deck = game.board.spell_fields.split(",")
        if game is None:
            raise HTTPException(status_code=404, detail="Game not found")
        if game.status["phase"] != "spell play":
            raise HTTPException(status_code=400,
                                detail="Its not time for playing spells!")
        if not game.board.de_proc or deck[game.board.de_proc -
                                          1] != 'avadakedavra':
            raise HTTPException(
                status_code=400,
                detail="The avadakedavra spell is not available")
        if not victim_player:
            raise HTTPException(
                status_code=400,
                detail="The victim player does not belong to this game")
        if current_player["current_position"] != "minister":
            raise HTTPException(status_code=404,
                                detail="This player is not the minister")
        victim_player.alive = False
        if victim_player.is_voldemort:
            game.status = {
                "info": "game ended",
                "winner": "Phoenix Order",
                "detail": "voldemort killed"
            }
        else:
            Player.reassign_minister(game)
        victim_user = User.select(
            lambda u: u.id == victim_player.user.id).first()
        return {
            "avadakedavra": "succeed!",
            "dead_player_id": player_id.id,
            "dead_player_alias": victim_user.useralias
        }
Exemple #10
0
async def play_imperius(obj_player: PlayerM,
                        game_id: int,
                        user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)
        current_player = Player.user_player(user, game_id)
        objective_player = game.players.select(
            lambda p: p.id == obj_player.id and p.game.id == game.id).first()
        board = game.board.spell_fields.split(",")
        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")
        if game.status["phase"] != "spell play":
            raise HTTPException(status_code=400,
                                detail="Its not time for playing spells!")
        if current_player["current_position"] != "minister":
            raise HTTPException(status_code=400,
                                detail=f"This player is not the minister")
        if game.board.de_proc == 0 or board[game.board.de_proc -
                                            1] != "imperius":
            raise HTTPException(status_code=400,
                                detail="The imperius spell is not available")
        if not objective_player:
            raise HTTPException(
                status_code=400,
                detail="The objective player does not belong to this game")
        if not objective_player.alive:
            raise HTTPException(status_code=400,
                                detail="The objective player is dead")
        game.status["temporal_minister"] = objective_player.id
        game.status["return_minister"] = current_player["id"]
        return {
            "message":
            f"The player {objective_player.id} ({objective_player.user.username}) is going to be the "
            f"next minister!"
        }
Exemple #11
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}