예제 #1
0
def handle_fold(data):
    global room_to_gds
    room = data['room']
    game_data = room_to_gds.get_game_data(room)
    print("Current node: " + game_data.player_round.current_node.player.name)
    current_player = game_data.current_player
    print("FOLD")
    if data['username'] is not current_player.name:
        pass
        #error
    if (game_data.game_state == GameDataService.GameState.PREFLOP
            and game_data.current_player
            == game_data.player_round.big_blind.player):
        game_data.big_blind_action = True
    data['action'] = 'fold'
    emit('player action', data, room=room)
    next_node = game_data.player_round.check_next_player()
    next_player = next_node.player
    print(next_player)
    print("node all in: " + str(next_node.is_all_in))
    if next_player == game_data.current_player:
        print("line 62")
        game_data.player_round.remove_current()
        game_data.game_state = GameDataService.GameState(
            game_data.game_state.value + 1)
        run_next_game_state(room)
    else:
        print("line 67")
        game_data.player_round.remove_current()
        game_data.current_player = game_data.player_round.get_next_player(
        ).player
        get_options(room)
예제 #2
0
def current_hand_strength(player, community_cards, room):
    global room_to_gds
    game_data = room_to_gds.get_game_data(room)
    best_hand = get_player_winning_hand(player.cards, community_cards)
    emit('current hand',
         best_hand.serialize(),
         room=game_data.clients[player.name])
예제 #3
0
def broadcast_community_cards(room):
    global room_to_gds
    game_data = room_to_gds.get_game_data(room)
    cards = []
    for c in game_data.community_cards:
        cards.append(c.serialize())
    emit('community cards', {'community_cards': cards}, room=room)
예제 #4
0
def waiting_to_join(data):
    global room_to_gds
    room = data['room']
    game_data = room_to_gds.get_game_data(room)
    json_object = game_data.seralize_waiting_to_join()
    emit('waiting list', {'waiting_list': json_object},
         room=game_data.room_owner)
예제 #5
0
def find_winners(all_players, room):
    global room_to_gds
    game_data = room_to_gds.get_game_data(room)
    players = list(all_players)
    print("FIND_WINNERS")
    middle_cards = game_data.community_cards
    best_hands = [
        get_player_winning_hand(x.cards, middle_cards) for x in players
    ]
    winning_players = [players[0]]
    winning_hands = [best_hands[0]]
    emit('best hand',
         best_hands[0].serialize(),
         room=game_data.clients[players[0].name])
    print(str(players[0]) + " has a " + str(best_hands[0]))
    for i in range(1, len(best_hands)):
        print(str(players[i]) + " has a " + str(best_hands[i]))
        emit('best hand',
             best_hands[i].serialize(),
             room=game_data.clients[players[i].name])
        if best_hands[i] < winning_hands[0]:
            continue
        elif best_hands[i] > winning_hands[0]:
            winning_hands = [best_hands[i]]
            winning_players = [players[i]]
        else:
            winning_hands.append(best_hands[i])
            winning_players.append(players[i])
    for p in winning_players:
        print(p.name)
    return winning_players
예제 #6
0
def get_options(room):
    print("GET OPTIONS")
    global room_to_gds
    game_data = room_to_gds.get_game_data(room)
    print(game_data.current_player.name)
    print(game_data.current_player.current_contribution)
    if game_data.latest_aggressor:
        print(game_data.latest_aggressor.name)
    current_player = game_data.current_player
    highest_current_contribution = game_data.highest_current_contribution
    print(highest_current_contribution)
    print(game_data.big_blind_action)
    if game_data.player_round.length_active == 1:
        print("active length is 1")
        distribute(room)
    else:
        if (((game_data.current_player.current_contribution is not None)
             and (current_player.current_contribution
                  == highest_current_contribution)
             and (game_data.big_blind_action))):
            if game_data.game_state != GameDataService.GameState.WINNER:
                game_data.game_state = GameDataService.GameState(
                    game_data.game_state.value + 1)
            print("line 376: " + str(room))
            run_next_game_state(room)
        else:
            options = []
            options.append("fold")
            if ((game_data.latest_aggressor != current_player)
                    and (current_player !=
                         game_data.player_round.check_next_player().player)
                    and (highest_current_contribution != 0) and
                ((not current_player.current_contribution
                  or current_player.current_contribution <
                  highest_current_contribution) or
                 (game_data.player_round.big_blind.player == current_player
                  and game_data.game_state.value == 1))):
                options.append("raise")
            if ((not current_player.current_contribution
                 or current_player.current_contribution <
                 highest_current_contribution)
                    and highest_current_contribution != 0):
                options.append("call")
            if "call" not in options:
                options.append("check")
            if ("raise" not in options
                    and game_data.latest_aggressor != current_player
                    and highest_current_contribution == 0):
                options.append("bet")
            max_bet_amount = find_max_bet(room)
            emit('options for player', {
                'options': options,
                'max_bet': max_bet_amount,
                'min_bet': game_data.wager_size + highest_current_contribution,
                'highest_contribution': highest_current_contribution
            },
                 room=game_data.clients[current_player.name])
예제 #7
0
def clean_up_poker_table(room):
    global room_to_gds
    print("Clean_Up_Poker_Table")
    game_data = room_to_gds.get_game_data(room)
    try:
        game_data.player_round.start_new_hand()
        game_data.reset()
    except Exception as e:
        print(e)
        print("one player left, cannot restart")
        return
    emit('new game', room=room)
    preflop(room)
예제 #8
0
    def check_client_in_sync(self):
        reference = self.get_reference()

        if self.is_reference():
            self.message("You are the reference")
            return

        max_delay = 100  # in milliseconds
        max_out_of_sync = 5000  # in milliseconds

        delay = self.epoch - reference.epoch

        if not reference.paused:
            reference.update_client_time(reference.time + delay)

        delay_between_players = abs(self.time - reference.time)

        if delay_between_players > max_delay:
            if self.time > reference.time:
                print(
                    f"{self.id}@{self.session} - not in sync - slowing down ({round(delay_between_players)}ms)"
                )
                outofsync = 2
            else:
                print(
                    f"{self.id}@{self.session} - not in sync - speeding up ({round(delay_between_players)}ms)"
                )
                outofsync = 1
        else:
            print(
                f"{self.id}@{self.session} - We are in sync ({round(delay_between_players)}ms)"
            )
            outofsync = 0

        if delay_between_players > max_out_of_sync:
            print(
                f"{self.id}@{self.session} - Syncing client (+{max_out_of_sync}ms)"
            )
            self.sync_client()
        else:
            emit(
                "out of sync",
                {
                    "outofsync": outofsync,
                    "max_out_of_sync": max_out_of_sync,
                    "delay": delay_between_players,
                    "max_delay": max_delay,
                },
                room=self.id,
            )
예제 #9
0
def deal_cards(room):
    global room_to_gds
    game_data = room_to_gds.get_game_data(room)
    game_data.deck.shuffle()
    for p in game_data.player_round.get_current_players():
        pair = [game_data.deck.get_top_card(), game_data.deck.get_top_card()]
        p.set_cards(pair)
        emit('dealt cards', {
            'cards': [{
                'value': pair[0].value_to_str(),
                'suit': pair[0].suit_to_str()
            }, {
                'value': pair[1].value_to_str(),
                'suit': pair[1].suit_to_str()
            }]
        },
             room=game_data.clients[p.name])
예제 #10
0
def apply_result_to_all(room):
    global room_to_gds
    game_data = room_to_gds.get_game_data(room)
    players = game_data.player_round.players
    win_objects = {}
    for p in players:
        p.apply_result()
        win_objects[p.name] = {
            'username': p.name,
            'winnings': p.result if p.result > 0 else 0,
            'hand': [p.cards[0].serialize(), p.cards[1].serialize()],
            'final_bank': p.bank,
        }
        print("Player name: " + p.name + " Bank: " + str(p.bank))
    emit('result', win_objects, room=room)
    eventlet.sleep(10)
    clean_up_poker_table(room)
예제 #11
0
def handle_call(data):
    print("CALL")
    global room_to_gds
    room = data['room']
    print(str(data))
    game_data = room_to_gds.get_game_data(room)
    print("Current node: " + game_data.player_round.current_node.player.name)
    if data['username'] is not game_data.current_player.name:
        pass
        #error
    if (game_data.game_state == GameDataService.GameState.PREFLOP
            and game_data.current_player
            == game_data.player_round.big_blind.player):
        game_data.big_blind_action = True
    game_data.current_player.bet(data['amount'])
    emit('withdraw', {
        'username': game_data.current_player.name,
        'amount': data['amount']
    },
         room=room)
    game_data.current_round_pot += data['amount']
    broadcast_pot(game_data.current_round_pot + game_data.pot, room)
    data['action'] = 'call'
    data['currentContribution'] = game_data.current_player.current_contribution
    emit('player action', data, room=room)
    next_player = game_data.player_round.check_next_player().player
    if next_player == game_data.current_player:
        if game_data.current_player.bank == game_data.current_player.invested:
            print("all_in")
            game_data.number_of_all_ins += 1
            game_data.player_round.all_in_current_node()
        game_data.game_state = GameDataService.GameState(
            game_data.game_state.value + 1)
        run_next_game_state(room)
    else:
        if game_data.current_player.bank == game_data.current_player.invested:
            print("all_in")
            game_data.player_round.all_in_current_node()
            print("Current node: " +
                  game_data.player_round.current_node.player.name)
            game_data.number_of_all_ins += 1
        game_data.current_player = game_data.player_round.get_next_player(
        ).player
        get_options(room)
예제 #12
0
def run_next_game_state(room):
    global room_to_gds
    game_data = room_to_gds.get_game_data(room)
    print('RUN_NEXT_GAME_STATE: ' + str(game_data.game_state.value))
    next_game_state = game_data.game_state
    for p in game_data.player_round.players:
        p.current_contribution = None
    emit('reset current contribution', {}, room=room)
    game_data.highest_current_contribution = 0
    game_data.latest_aggressor = None
    game_data.wager_size = game_data.big_blind_amount
    game_data.pot += game_data.current_round_pot
    broadcast_pot(game_data.pot, room)
    game_data.current_round_pot = 0
    if game_data.player_round.length_active == 1:
        distribute(room)
    else:
        if next_game_state != GameDataService.GameState.WINNER:
            print("line 154: " + str(room))
            run_street(game_data.heads_up, room)
        else:
            distribute(room)
예제 #13
0
 def sync_other_clients(self, client_data):
     self.update_client(client_data)
     self.update_client_in_session()
     emit("sync", client_data, room=self.session, skip_sid=self.id)
예제 #14
0
 def message(self, message):
     emit(
         "message",
         message,
         room=self.id,
     )
예제 #15
0
def broadcast_pot(amount, room):
    emit('pot update', {'pot': amount}, room=room)
예제 #16
0
 def push(self, json):
     emit("sync", json, room=self.id)
예제 #17
0
def handle_raise(data):
    print("RAISE")
    print(str(data))
    global room_to_gds
    room = data['room']

    game_data = room_to_gds.get_game_data(room)
    print("Current node: " + game_data.player_round.current_node.player.name)
    if data['username'] is not game_data.current_player.name:
        pass
        #error
    if (game_data.game_state == GameDataService.GameState.PREFLOP
            and game_data.current_player
            == game_data.player_round.big_blind.player):
        game_data.big_blind_action = True
    if ((data['amount'] >=
         game_data.wager_size + game_data.highest_current_contribution)
            or (game_data.highest_current_contribution == 0)):
        print("latest aggresor: " + game_data.current_player.name)
        game_data.aggressors.append(game_data.current_player)
        game_data.latest_aggressor = game_data.current_player
    # on raise, the amount is the final amount the player wants to be "in" for,
    # not how much more they want to add to there contribution.
    if game_data.current_player.current_contribution is not None:
        game_data.current_round_pot += data[
            'amount'] - game_data.current_player.current_contribution
        emit('withdraw', {
            'username':
            game_data.current_player.name,
            'amount':
            data['amount'] - game_data.current_player.current_contribution
        },
             room=room)
        game_data.current_player.bet(
            data['amount'] - game_data.current_player.current_contribution)
    else:
        game_data.current_player.bet(data['amount'])
        game_data.current_round_pot += data['amount']
        emit('withdraw', {
            'username': game_data.current_player.name,
            'amount': game_data.current_player.current_contribution
        },
             room=room)
    game_data.wager_size = data[
        'amount'] - game_data.highest_current_contribution
    game_data.highest_current_contribution = game_data.current_player.current_contribution
    # we already added data['amount'] to current_player.current_contribution
    if game_data.current_player.bank == game_data.current_player.invested:
        print("all_in")
        game_data.number_of_all_ins += 1
        game_data.player_round.all_in_current_node()
    broadcast_pot(game_data.current_round_pot + game_data.pot, room)
    data['action'] = 'raise'
    data['currentContribution'] = game_data.current_player.current_contribution
    emit('highest contribution',
         {'highest_contribution': game_data.highest_current_contribution},
         room=room)
    emit('player action', data, room=room)
    game_data.current_player = game_data.player_round.get_next_player().player
    print("line 128: " + str(room))
    get_options(room)
예제 #18
0
def preflop(room):
    print('PREFLOP')
    global room_to_gds
    game_data = room_to_gds.get_game_data(room)
    game_data.highest_current_contribution = game_data.big_blind_amount
    if game_data.player_round.small_blind.player.bank <= game_data.small_blind_amount:
        game_data.player_round.small_blind.player.bet(
            game_data.player_round.small_blind.player.bank)
        game_data.player_round.small_blind.is_all_in = True
        game_data.number_of_all_ins += 1
    else:
        game_data.player_round.small_blind.player.bet(
            game_data.small_blind_amount)
    emit('withdraw', {
        'username':
        game_data.player_round.small_blind.player.name,
        'amount':
        game_data.player_round.small_blind.player.current_contribution
    },
         room=room)
    emit('player action', {
        'username':
        game_data.player_round.small_blind.player.name,
        'amount':
        game_data.player_round.small_blind.player.current_contribution,
        'action':
        'small blind',
        'currentContribution':
        game_data.player_round.small_blind.player.current_contribution
    },
         room=room)
    if game_data.player_round.big_blind.player.bank <= game_data.big_blind_amount:
        game_data.player_round.big_blind.player.bet(
            game_data.player_round.big_blind.player.bank)
        game_data.player_round.big_blind.is_all_in = True
        game_data.number_of_all_ins += 1
    else:
        game_data.player_round.big_blind.player.bet(game_data.big_blind_amount)
    emit('withdraw', {
        'username': game_data.player_round.big_blind.player.name,
        'amount': game_data.player_round.big_blind.player.current_contribution
    },
         room=room)
    game_data.current_round_pot += game_data.player_round.small_blind.player.current_contribution
    game_data.current_round_pot += game_data.player_round.big_blind.player.current_contribution
    game_data.current_player = game_data.player_round.current_node.player
    game_data.aggressors.append(game_data.player_round.big_blind.player)
    if game_data.player_round.length_active == 2:
        game_data.heads_up = True
    emit('player action', {
        'username':
        game_data.player_round.big_blind.player.name,
        'amount':
        game_data.player_round.big_blind.player.current_contribution,
        'action':
        'big blind',
        'currentContribution':
        game_data.player_round.big_blind.player.current_contribution
    },
         room=room)
    broadcast_pot(game_data.current_round_pot, room)
    emit('highest contribution',
         {'highest_contribution': game_data.big_blind_amount},
         room=room)
    game_data.wager_size = game_data.big_blind_amount
    deal_cards(room)
    print("Current node: " + game_data.player_round.current_node.player.name)
    get_options(room)
예제 #19
0
 def sync_client(self):
     reference = self.get_reference()
     self.update_client_time(reference.time)
     emit("sync", reference.get_json_profile(), room=self.id)