Esempio n. 1
0
def start_round():
    try:
        game = Game.query.filter_by(status=2).first()

        if game is not None:
            rounds = Round.query.order_by(Round.id).all()
            print(game.id, file=sys.stderr)
            current_round = rounds[-1]
            print(current_round, file=sys.stderr)

            initial_player = PlayerInGame.query.filter_by(
                position=1, game_id=game.id).first()
            current_round.current_player_id = initial_player.player_id

            db.session.commit()
            data = {'message': 'Redireciona'}

            try:
                message_app(data, game.id)
            except Exception as e:
                return jsonify({'message': str(e)}), 400

            return jsonify({"message": "Round começou!"}), 200

        else:
            return jsonify({"message": "Não existe jogo ativo."}), 500
    except Exception as e:
        print(e, file=sys.stderr)
        return jsonify({"message": "Erro ao tentar recuperar round!"}), 500
Esempio n. 2
0
def start_game():
    try:
        game_starting = Game.query.filter_by(status=1).first()
        if game_starting is None:
            return jsonify({"message": "No game is initializing"}), 406

        players = PlayerInGame.query.filter_by(game_id=game_starting.id).all()

        for player in players:
            player.money = 10000  # Valor Inicial de dinheiro dos jogadores

        game_starting.status = 2  # Status do jogo mudado para "Em Progresso"
        game_starting.continued = 1

        db.session.commit()

        data = {'message': 'Reconhecer'}

        try:
            message_app(data, game_starting.id)
        except Exception as e:
            print(str(e))
            return jsonify({'message': str(e)}), 400

        return jsonify({"message": "Game Started"}), 200

    except Exception as e:
        return jsonify({
            "error": "Error on starting Game",
            "message": str(e)
        }), 500
Esempio n. 3
0
def check_active_players(game_id):
    players = PlayerInGame.query.filter_by(game_id = game_id, is_playing_match = True).all()
    number_of_players = len(players)
    print(number_of_players, file=sys.stderr)
    print(players[0].player_id, file=sys.stderr)

    if number_of_players < 2:
        url = base_gateway_url + 'get_user_by_id?user_id=' + str(players[0].player_id)
        get_player_request = requests.request("GET", url)
        print(get_player_request.status_code, file=sys.stderr)
        
        if get_player_request.status_code == 200:
            data = get_player_request.json()
            print(data, file=sys.stderr)

            data = {
                'message': 'Fugiram',
                'winner': data['player']['name']
            }

            message_app(data, game_id)

            game = Game.query.filter_by(status = 2).first()
            game.continued = 4
            db.session.commit()

            check_endgame(players[0].player_id, game_id)
Esempio n. 4
0
def raise_bet():
    try:
        data = request.get_json()
        game_id = data['game_id']
        player_id = data['player_id']
        round_id = data['round_id']
        new_bet = int(data['value'])

        player_in_game = PlayerInGame.query.filter_by(
            game_id=game_id, player_id=player_id).first()
        current_round = Round.query.filter_by(id=round_id).first()

        if player_in_game is not None:
            if current_round.current_player_id == player_in_game.player_id:
                if new_bet > player_in_game.bet:
                    money_difference = new_bet - player_in_game.bet

                    if player_in_game.money > money_difference:
                        player_in_game.bet = new_bet
                        player_in_game.money -= money_difference

                        current_round.last_player_raised_bet = player_id
                        current_round.bet = new_bet
                        current_round.total_bet_prize += money_difference

                        db.session.commit()

                        set_current_player_id(player_id, round_id)

                        data = {'message': 'Novo turno'}

                        try:
                            message_app(data, game_id)
                        except Exception as e:
                            return jsonify({'message': str(e)}), 400

                        return jsonify({"message": "Aposta Aumentada!"}), 200
                    else:
                        return jsonify({
                            "message":
                            "Você não possui dinheiro para aumentar tanto a aposta. Tente pagá-la."
                        }), 500

                else:
                    return jsonify({
                        "message":
                        "Valor passado é menor ou igual ao valor da sua aposta."
                    }), 400
            else:
                return jsonify({"message": "Não é a sua vez"}), 400
        else:
            return jsonify({"message": "Jogador não está no jogo!"}), 400

    except Exception as e:
        return jsonify({
            "error": "Erro ao tentar aumentar a aposta!",
            "message": str(e)
        }), 400
Esempio n. 5
0
def redirect_round():
    game = Game.query.filter_by(status=2).first()

    data = {'message': 'Redireciona'}

    try:
        message_app(data, game.id)
    except Exception as e:
        return jsonify({'message': str(e)}), 400

    return jsonify({'message': 'Redirecionou'}), 200
Esempio n. 6
0
def check_endgame(winner_id, game_id):
    players = PlayerInGame.query.filter_by().all()
    defeated_players = PlayerInGame.query.filter_by(money = 0).all()

    if len(defeated_players) == (len(players) - 1):
        url = base_gateway_url + 'get_user_by_id?user_id=' + str(winner_id)
        get_player_request = requests.request("GET", url)
        data = get_player_request.json()

        game = Game.query.filter_by(id = game_id).first()
        game.winner = winner_id
        game.status = 3

        db.session.commit()

        data = {
            'message': 'Endgame',
            'winner': data['player']['name']
        }

        message_app(data, game_id)
    else:

        previous_round = Round.query.filter_by().all()
        previous_round = previous_round[-1]
        round_data = Round(game_id = game_id, small_blind = 250, big_blind = 500, bet = 500, current_player_id= -1)
        players_in_game = PlayerInGame.query.filter_by(game_id = game_id).all()

        for player in players_in_game:
            player.bet = 0
            player.is_playing_match = True
        
        winner = PlayerInGame.query.filter_by(player_id = winner_id).first()
        winner.money = winner.money + previous_round.total_bet_prize

        db.session.add(round_data)
        db.session.commit()

        data = {
            'message': 'NovoRound',
        }

        message_app(data, game_id)
Esempio n. 7
0
def leave_match():
    try:
        data = request.get_json()

        game_id = data['game_id']
        player_id = data['player_id']
        round_id = data['round_id']
        player_in_game = PlayerInGame.query.filter_by(
            game_id=game_id, player_id=player_id).first()
        round_data = Round.query.filter_by(id=round_id).first()

        if player_in_game is not None:
            if round_data.current_player_id == player_in_game.player_id:
                player_in_game.is_playing_match = False
                db.session.commit()

                data = {'message': 'Novo turno'}

                set_current_player_id(player_id, round_id)

                try:
                    message_app(data, game_id)
                except Exception as e:
                    return jsonify({'message': str(e)}), 400

                check_last_player_bet(round_id, player_id, game_id)
                check_active_players(game_id)

                return jsonify({"message": "Jogador fugiu da partida!"}), 200
            else:
                return jsonify({"message": "Não é a vez do Jogador"}), 400
        else:
            return jsonify({"message": "Jogador não está no jogo!"}), 400

    except Exception as e:
        return jsonify({
            "error": "Erro ao tentar tirar jogador da partida!",
            "message": str(e)
        }), 400
Esempio n. 8
0
def post_game_participate():
    try:
        game_participate_json = request.get_json()
        player_id = game_participate_json['player_id']
        device_id = game_participate_json['device_id']

        try:
            game_starting = Game.query.filter_by(status=1).first()
        except Exception as e:
            print(str(e))

        # Caso não haja um jogo no estado Iniciando
        if game_starting is None:

            game_in_progress = Game.query.filter_by(status=2).first()

            # Caso não haja um jogo nem iniciando e nem em progresso, inicia-se um
            if game_in_progress is None:
                new_game = Game()
                db.session.add(new_game)

                db.session.flush()
                db.session.refresh(new_game)

                db.session.add(
                    PlayerInGame(game_id=new_game.id,
                                 player_id=player_id,
                                 device_id=device_id))

                db.session.commit()

                data = {'message': 'Atualiza'}

                try:
                    message_app(data, new_game.id)
                except Exception as e:
                    print(str(e))
                    return jsonify({'message': str(e)}), 400

                return jsonify({
                    "message": "Player added to game",
                    "game_id": new_game.id
                }), 200

            # Caso haja um jogo em progresso
            else:
                return jsonify({"message": "Game in Progress"}), 406

        # Caso haja um jogo no estado Iniciando
        else:
            db.session.add(
                PlayerInGame(game_id=game_starting.id,
                             device_id=device_id,
                             player_id=player_id))
            db.session.commit()

            data = {'message': 'Atualiza'}

            try:
                message_app(data, game_starting.id)
            except Exception as e:
                print(str(e))

                return jsonify({'message': str(e)}), 400

        return jsonify({
            "message": "Player added to game",
            "game_id": game_starting.id
        }), 200

    except:
        return jsonify({"message": "Error on adding player to game"}), 500
Esempio n. 9
0
def pay_bet():
    try:
        data = request.get_json()
        game_id = data['game_id']
        player_id = data['player_id']
        round_id = data['round_id']

        player_in_game = PlayerInGame.query.filter_by(
            game_id=game_id, player_id=player_id).first()
        current_round = Round.query.filter_by(id=round_id).first()

        if player_in_game is not None:
            if current_round.current_player_id == player_in_game.player_id:
                money_difference = current_round.bet - player_in_game.bet

                # Caso seja All In
                if player_in_game.money < money_difference:
                    current_round.total_bet_prize += player_in_game.money
                    player_in_game.bet += player_in_game.money
                    player_in_game.money = 0

                    db.session.commit()

                    check_last_player_bet(round_id, player_id, game_id)
                    set_current_player_id(player_id, round_id)

                    data = {'message': 'Novo turno'}

                    try:
                        message_app(data, game_id)
                    except Exception as e:
                        return jsonify({'message': str(e)}), 40
                    return jsonify({"message": "All In!"}), 200
                else:
                    current_round.total_bet_prize += money_difference
                    player_in_game.money -= money_difference
                    player_in_game.bet = current_round.bet

                    db.session.commit()
                    check_last_player_bet(round_id, player_id, game_id)
                    set_current_player_id(player_id, round_id)

                    data = {'message': 'Novo turno'}

                    try:
                        message_app(data, game_id)
                    except Exception as e:
                        print(str(e), file=sys.stderr)
                        return jsonify({'message': str(e)}), 400
                    return jsonify({"message": "Aposta paga!"}), 200
            else:
                return jsonify({"message": "Não é a sua vez"}), 400
        else:
            return jsonify({"message": "Jogador não está no jogo!"}), 400

    except Exception as e:
        print(str(e), file=sys.stderr)
        return jsonify({
            "error": "Erro ao tentar pagar a aposta!",
            "message": str(e)
        }), 400
Esempio n. 10
0
def check_last_player_bet(round_id, player_id, game_id):
    print('checklastplayerbet', file = sys.stderr)
    round_data = Round.query.filter_by(id = round_id).first()
    game = Game.query.filter_by(status = 2).first()
    

    if round_data.last_player_raised_bet == player_id:
        print('requisisao', file = sys.stderr)
        url = base_gateway_url + 'get_round_cards_number?round_id=' + str(round_id)
        round_cards_request = requests.request("GET", url)
        cards_data = round_cards_request.json()
        print(cards_data, file=sys.stderr)
        print(cards_data['number'], file=sys.stderr)
        print(cards_data['number']['number'], file=sys.stderr)


        if cards_data['number']['number'] < 5:
            round_data.distribute_cards = True
            game.continued = 3
            db.session.commit()
        else:
            url = base_gateway_url + 'get_winner'
            player_list = PlayerInGame.query.filter_by(game_id = game_id).all()
            
            request_data = {
                'round_id': round_id,
                'players': []
            }

            for player in player_list:
                player = { 'id': player.player_id }
                request_data['players'].append(player)

            print('\n\n\nRequest_data', file = sys.stderr)
            


            winner_request = requests.request("POST", url, json = request_data,
                                            headers = {'Accept': 'application/json', 'content-type' : 'application/json'})

            print(winner_request.status_code, file = sys.stderr)


            if winner_request.status_code == 200:
                request_data = winner_request.json()
                print(request_data, file = sys.stderr)

                url = base_gateway_url + 'get_user_by_id?user_id=' + str(request_data['winner'])
                get_player_request = requests.request("GET", url)

                player_data = get_player_request.json()

                data = {
                    'message': 'Endround',
                    'winner': player_data['player']['name']
                }

                message_app(data, game_id)

                game.continued = 4
                db.session.commit()

                check_endgame(request_data['winner'], game_id)