class PredictRankSocket(PokerSocket): ws = "" predict_rank_data = [] def __init__(self, playerName, connect_url, pokerbot): self.pokerbot = pokerbot self.playerName = playerName self.md5_name = hashlib.md5(self.playerName).hexdigest() self.connect_url = connect_url self.table = None self.players = [] self.predict_rank_data = [] def resetGame(self): return False def resetRound(self): return False def getAction(self, data): state = utils.get_current_state(self.table, self.players) minBet = data['self']['minBet'] player_index = utils.get_index_from_player_list( data['self']['playerName'], self.players) # Change hand and board to object Card # hand = [] # board = [] # print(state.player_state[seat_num].cards) # for card in state.player_state[seat_num].cards: # hand.append(utils.getCard(card)) # for card in state.table_state.board: # board.append(utils.getCard(card)) # state.player_state[seat_num] = state.player_state[seat_num]._replace(cards=hand) # state.table_state = state.table_state._replace(board=board) print("state: {}".format(state)) action, amount = self.players[player_index].do_action( state, minBet, player_index) return action, amount def takeAction(self, action, data): # Get number of players and table info if action == "__game_start": # print("Game Start") table_number = data['tableNumber'] self.table = Table(table_number) return False elif action == "__new_round": print("New Round") self.predict_rank_data = [] table_data = data['table'] players_data = data['players'] if self.table is None: # raise ("Error: Table is None.") table_number = table_data['tableNumber'] self.table = Table(table_number) self.table.update_table_status(data) if len(self.players) == 0: # first time join the game for i, p in enumerate(players_data): if p['playerName'] == self.md5_name: self.players.append( Player(p['playerName'], p['chips'], self.pokerbot)) else: self.players.append(Player(p['playerName'], p['chips'])) for i, p in enumerate(self.players): p.update_by_state(players_data[i]) return False elif action == "__show_action": player_index = utils.get_index_from_player_list( data['action']['playerName'], self.players) self.players[player_index].update_action(data['action']) # get predict rank data state = utils.get_current_state(self.table, self.players) rank_info = utils.get_rank_data(state, player_index) self.predict_rank_data.append(rank_info) # update table and player self.table.update_table_status(data) for i, p in enumerate(self.players): p.update_by_state(data['players'][i]) # record some states return False elif action == "__bet": print("Bet") action, amount = self.getAction(data) print "action: {}".format(action) print "action amount: {}".format(amount) self.ws.send( json.dumps({ "eventName": "__action", "data": { "action": action, "playerName": self.playerName, "amount": amount } })) return False elif action == "__action": print("Action") action, amount = self.getAction(data) print "action: {}".format(action) print "action amount: {}".format(amount) self.ws.send( json.dumps({ "eventName": "__action", "data": { "action": action, "playerName": self.playerName, "amount": amount } })) return False elif action == "__deal": print "Deal Cards" self.table.update_table_status(data) for i, p in enumerate(self.players): p.update_by_state(data['players'][i]) return False elif action == "__start_reload": print "Reload" # self.ws.send(json.dumps({ # "eventName": "__reload", # })) # self.ws.send(json.dumps({ # "eventName": "__reload", # })) return False elif action == "__round_end": print "Round End" # update card and rank for each player evaluator = HandEvaluator() for player in data['players']: for i, rank_data in enumerate(self.predict_rank_data): if rank_data.player_name == player['playerName']: if not player[ 'isOnline']: # if player is not online, delete the record self.predict_rank_data.remove(rank_data) else: hand = [] for card in player['cards']: hand.append(utils.getCard(card)) board = self.predict_rank_data[i].board self.predict_rank_data[i] = self.predict_rank_data[ i]._replace(cards=hand) # hand, board = utils.str_list_to_card(self.predict_rank_data[i].cards, # self.predict_rank_data[i].board) rank = evaluator.evaluate_hand(hand, board) # print("rank: {}".format(rank)) self.predict_rank_data[i] = self.predict_rank_data[ i]._replace(rank=rank) utils.state_to_csv(utils.PREDICT_RANK_DATA, "log/", self.predict_rank_data) return False elif action == "__game_over": print "Game Over" self.table = None self.players = [] self.ws.send( json.dumps({ "eventName": "__join", "data": { "playerName": self.playerName } })) return True def doListen(self): # self.ws = create_connection(self.connect_url) # self.ws.send(json.dumps({ # "eventName": "__join", # "data": { # "playerName": self.playerName # } # })) while True: try: self.ws = create_connection(self.connect_url) self.ws.send( json.dumps({ "eventName": "__join", "data": { "playerName": self.playerName } })) terminal = False while not terminal: try: result = self.ws.recv() msg = json.loads(result) event_name = msg["eventName"] data = msg["data"] print event_name print data terminal = self.takeAction(event_name, data) # except WebSocketConnectionClosedException: except Exception as e: print e.message # self.ws.close() # self.ws = create_connection(self.connect_url) self.ws.send( json.dumps({ "eventName": "__join", "data": { "playerName": self.playerName } })) self.ws.close() except Exception, e: print e.message self.doListen()
def takeAction(self, action, data): # Get number of players and table info if action == "__game_start": # print("Game Start") table_number = data['tableNumber'] self.table = Table(table_number) return False elif action == "__new_round": print("New Round") self.predict_rank_data = [] table_data = data['table'] players_data = data['players'] if self.table is None: # raise ("Error: Table is None.") table_number = table_data['tableNumber'] self.table = Table(table_number) self.table.update_table_status(data) if len(self.players) == 0: # first time join the game for i, p in enumerate(players_data): if p['playerName'] == self.md5_name: self.players.append( Player(p['playerName'], p['chips'], self.pokerbot)) else: self.players.append(Player(p['playerName'], p['chips'])) for i, p in enumerate(self.players): p.update_by_state(players_data[i]) return False elif action == "__show_action": player_index = utils.get_index_from_player_list( data['action']['playerName'], self.players) self.players[player_index].update_action(data['action']) # get predict rank data state = utils.get_current_state(self.table, self.players) rank_info = utils.get_rank_data(state, player_index) self.predict_rank_data.append(rank_info) # update table and player self.table.update_table_status(data) for i, p in enumerate(self.players): p.update_by_state(data['players'][i]) # record some states return False elif action == "__bet": print("Bet") action, amount = self.getAction(data) print "action: {}".format(action) print "action amount: {}".format(amount) self.ws.send( json.dumps({ "eventName": "__action", "data": { "action": action, "playerName": self.playerName, "amount": amount } })) return False elif action == "__action": print("Action") action, amount = self.getAction(data) print "action: {}".format(action) print "action amount: {}".format(amount) self.ws.send( json.dumps({ "eventName": "__action", "data": { "action": action, "playerName": self.playerName, "amount": amount } })) return False elif action == "__deal": print "Deal Cards" self.table.update_table_status(data) for i, p in enumerate(self.players): p.update_by_state(data['players'][i]) return False elif action == "__start_reload": print "Reload" # self.ws.send(json.dumps({ # "eventName": "__reload", # })) # self.ws.send(json.dumps({ # "eventName": "__reload", # })) return False elif action == "__round_end": print "Round End" # update card and rank for each player evaluator = HandEvaluator() for player in data['players']: for i, rank_data in enumerate(self.predict_rank_data): if rank_data.player_name == player['playerName']: if not player[ 'isOnline']: # if player is not online, delete the record self.predict_rank_data.remove(rank_data) else: hand = [] for card in player['cards']: hand.append(utils.getCard(card)) board = self.predict_rank_data[i].board self.predict_rank_data[i] = self.predict_rank_data[ i]._replace(cards=hand) # hand, board = utils.str_list_to_card(self.predict_rank_data[i].cards, # self.predict_rank_data[i].board) rank = evaluator.evaluate_hand(hand, board) # print("rank: {}".format(rank)) self.predict_rank_data[i] = self.predict_rank_data[ i]._replace(rank=rank) utils.state_to_csv(utils.PREDICT_RANK_DATA, "log/", self.predict_rank_data) return False elif action == "__game_over": print "Game Over" self.table = None self.players = [] self.ws.send( json.dumps({ "eventName": "__join", "data": { "playerName": self.playerName } })) return True
def extract_log(location, log_path): file_list = [] msg_list = [] log_list = [] for filename in os.listdir(location): file_list.append(filename) for filename in reversed(file_list): print filename f = open(os.path.join(location, filename), "r") for line in f: if any(event in line for event in EVENT_LIST): s = re.search(r'({.*})', line).group(1) msg = json.loads(s) msg_list.append(msg) table_dict = get_table_dict(msg_list) for table_number in table_dict.keys(): table_data = table_dict[table_number] table_obj = None players = [] log = [] for msg in table_data: event_name = msg["eventName"] data = msg["data"] if table_obj is None: table_obj = Table(table_number) if len(players) == 0: for i, p in enumerate(data["players"]): players.append(Player(p['playerName'], 3000)) if event_name == "__show_action": player_index = get_index_from_player_list(data['action']['playerName'], players) players[player_index].update_action(data['action'], data["table"]["roundName"]) # update table and player table_obj.update_table_status(data) for i, p in enumerate(players): p.update_by_state(data['players'][i]) # get predict rank data state = get_current_state(table_obj, players) log_info = get_log_data(state, player_index) log.append(log_info) elif event_name == "__round_end": if table_obj is not None: for player in data['players']: for i, log_data in enumerate(log): if log_data.player_name == player['playerName']: if player['isOnline'] and not player['folded'] and not player['isHuman']: # if player is not online, delete the record # if player['isOnline'] and not player['isHuman']: # if player is not online and not human, delete the record log_list.append(log_data._replace(win_money=player['winMoney'] / (1.0 * log[i].big_blind))) # state_to_csv(LOG_DATA, "log/no fold/", log) log = [] # table_obj.update_table_status(data) # for i, p in enumerate(players): # p.update_by_state(data['players'][i]) for p in players: p.new_round() elif event_name == "__game_over": players = [] table_obj = None log = [] # state_to_csv(LOG_DATA, "log/{}/".format(log_path), log_list) state_to_csv(LOG_DATA, "log/no fold/", log_list)
learning_rate = 0.00001 model = Sequential() # model.add(Convolution1D(int(hidden_size), 3, activation='relu', input_shape=(nb_frames, grid_size))) # model.add(Dropout(0.2)) model.add( Dense(int(hidden_size), activation='relu', input_shape=(nb_frames, grid_size))) model.add(Dropout(0.2)) model.add(Dense(int(hidden_size / 2), activation='relu')) model.add(Dropout(0.2)) model.add(LSTM(int(lstm_size))) model.compile(Adam(lr=learning_rate), 'MSE') model._make_predict_function() # controller for human meat bag # h = PlayerControl(1, t) trainers = [] for i in range(1, seats + 1): trainers.append(Train()) while True: t = Table(seats, training=True, quiet=True) print('starting ai players') # fill the rest of the table with ai players for i in range(1, seats + 1): p = PlayerControl(i, t, model=model, train=trainers[i - 1]) t.run_game()
def takeAction(self, action, data): # Get number of players and table info if action == "__game_start": # print("Game Start") table_number = data['tableNumber'] self.table = Table(table_number) return False elif action == "__new_round": # print("New Round") table_data = data['table'] players_data = data['players'] if self.table is None: # raise ("Error: Table is None.") table_number = table_data['tableNumber'] self.table = Table(table_number) self.table.update_table_status(data) if len(self.players) == 0: # first time join the game for i, p in enumerate(players_data): if p['playerName'] == self.md5_name: self.players.append( Player(p['playerName'], p['chips'], self.pokerbot)) else: self.players.append(Player(p['playerName'], p['chips'])) for i, p in enumerate(self.players): p.new_round() p.update_by_state(players_data[i]) return False elif action == "__show_action": player_index = utils.get_index_from_player_list( data['action']['playerName'], self.players) self.players[player_index].update_action( data['action'], data['table']['roundName']) # update table and player self.table.update_table_status(data) for i, p in enumerate(self.players): p.update_by_state(data['players'][i]) return False elif action == "__bet": # print("Bet") action, amount = self.getAction(data) # print "action: {}".format(action) # print "action amount: {}".format(amount) self.ws.send( json.dumps({ "eventName": "__action", "data": { "action": action, "playerName": self.playerName, "amount": amount } })) return False elif action == "__action": # print("Action") action, amount = self.getAction(data) # print "action: {}".format(action) # print "action amount: {}".format(amount) self.ws.send( json.dumps({ "eventName": "__action", "data": { "action": action, "playerName": self.playerName, "amount": amount } })) return False elif action == "__deal": print "Deal Cards" self.table.update_table_status(data) for i, p in enumerate(self.players): p.update_by_state(data['players'][i]) player_index = utils.get_index_from_player_list( self.md5_name, self.players) print "hands: {}, board: {}".format( data['players'][player_index]['cards'], data['table']['board']) return False elif action == "__start_reload": # print "Reload" # self.ws.send(json.dumps({ # "eventName": "__reload", # })) # self.ws.send(json.dumps({ # "eventName": "__reload", # })) return False elif action == "__round_end": print "Round End" state = utils.get_current_state(self.table, self.players) player_index = utils.get_index_from_player_list( self.md5_name, self.players) player_data = data['players'] win_money = player_data[player_index]['winMoney'] evaluator = HandEvaluator() rank = evaluator.evaluate_hand( state.player_state[player_index].hand, state.table_state.board) chips = player_data[player_index]['chips'] print "hands: {}, board: {}, rank: {}".format( state.player_state[player_index].hand, state.table_state.board, rank) print "win money: {}, remain chips: {}".format(win_money, chips) return False elif action == "__game_over": print "Game Over" self.table = None self.players = [] return True
class TMSocket(PokerSocket): ws = "" def __init__(self, playerName, connect_url, pokerbot): self.pokerbot = pokerbot self.playerName = playerName self.md5_name = hashlib.md5(self.playerName).hexdigest() self.connect_url = connect_url self.table = None self.players = [] def resetGame(self): return False def resetRound(self): return False def getAction(self, data): state = utils.get_current_state(self.table, self.players) minBet = data['self']['minBet'] player_index = utils.get_index_from_player_list( data['self']['playerName'], self.players) # Change hand and board to object Card # hand = [] # board = [] # print(state.player_state[seat_num].cards) # for card in state.player_state[seat_num].cards: # hand.append(utils.getCard(card)) # for card in state.table_state.board: # board.append(utils.getCard(card)) # state.player_state[seat_num] = state.player_state[seat_num]._replace(cards=hand) # state.table_state = state.table_state._replace(board=board) # print("state: {}".format(state)) action, amount = self.players[player_index].do_action( state, minBet, player_index) return action, amount def takeAction(self, action, data): # Get number of players and table info if action == "__game_start": # print("Game Start") table_number = data['tableNumber'] self.table = Table(table_number) return False elif action == "__new_round": # print("New Round") table_data = data['table'] players_data = data['players'] if self.table is None: # raise ("Error: Table is None.") table_number = table_data['tableNumber'] self.table = Table(table_number) self.table.update_table_status(data) if len(self.players) == 0: # first time join the game for i, p in enumerate(players_data): if p['playerName'] == self.md5_name: self.players.append( Player(p['playerName'], p['chips'], self.pokerbot)) else: self.players.append(Player(p['playerName'], p['chips'])) for i, p in enumerate(self.players): p.new_round() p.update_by_state(players_data[i]) return False elif action == "__show_action": player_index = utils.get_index_from_player_list( data['action']['playerName'], self.players) self.players[player_index].update_action( data['action'], data['table']['roundName']) # update table and player self.table.update_table_status(data) for i, p in enumerate(self.players): p.update_by_state(data['players'][i]) return False elif action == "__bet": # print("Bet") action, amount = self.getAction(data) # print "action: {}".format(action) # print "action amount: {}".format(amount) self.ws.send( json.dumps({ "eventName": "__action", "data": { "action": action, "playerName": self.playerName, "amount": amount } })) return False elif action == "__action": # print("Action") action, amount = self.getAction(data) # print "action: {}".format(action) # print "action amount: {}".format(amount) self.ws.send( json.dumps({ "eventName": "__action", "data": { "action": action, "playerName": self.playerName, "amount": amount } })) return False elif action == "__deal": print "Deal Cards" self.table.update_table_status(data) for i, p in enumerate(self.players): p.update_by_state(data['players'][i]) player_index = utils.get_index_from_player_list( self.md5_name, self.players) print "hands: {}, board: {}".format( data['players'][player_index]['cards'], data['table']['board']) return False elif action == "__start_reload": # print "Reload" # self.ws.send(json.dumps({ # "eventName": "__reload", # })) # self.ws.send(json.dumps({ # "eventName": "__reload", # })) return False elif action == "__round_end": print "Round End" state = utils.get_current_state(self.table, self.players) player_index = utils.get_index_from_player_list( self.md5_name, self.players) player_data = data['players'] win_money = player_data[player_index]['winMoney'] evaluator = HandEvaluator() rank = evaluator.evaluate_hand( state.player_state[player_index].hand, state.table_state.board) chips = player_data[player_index]['chips'] print "hands: {}, board: {}, rank: {}".format( state.player_state[player_index].hand, state.table_state.board, rank) print "win money: {}, remain chips: {}".format(win_money, chips) return False elif action == "__game_over": print "Game Over" self.table = None self.players = [] return True def doListen(self): try: while True: self.ws = create_connection(self.connect_url) self.ws.send( json.dumps({ "eventName": "__join", "data": { "playerName": self.playerName } })) terminal = False while not terminal: try: result = self.ws.recv() msg = json.loads(result) event_name = msg["eventName"] data = msg["data"] # print event_name # print data terminal = self.takeAction(event_name, data) # except WebSocketConnectionClosedException: except Exception as e: print e.message terminal = True self.ws.close() except Exception, e: print e.message self.doListen()
from holdem import Table, TableProxy, PlayerControl, PlayerControlProxy seats = 8 # start an table with 8 seats t = Table(seats) tp = TableProxy(t) # controller for human meat bag h = PlayerControl("localhost", 8001, 1, False) hp = PlayerControlProxy(h) print('starting ai players') # fill the rest of the table with ai players for i in range(2, seats + 1): p = PlayerControl("localhost", 8000 + i, i, True) pp = PlayerControlProxy(p) tp.run_game()