def __init__(self, postId, authorName, minKarma, durationHours, endTime, pifOptions={}, pifEntries={}, karmaFail={}): logging.debug('Building holdem poker PIF [%s]', postId) if len(pifOptions) < 1: deck = poker_util.new_deck() flop_cards = list() for i in range(3): card = poker_util.deal_card(deck) flop_cards.append(card) flop_cards = poker_util.order_cards(flop_cards) river_card = poker_util.deal_card(deck) turn_card = poker_util.deal_card(deck) # remove 3 cards (ie never dealt) so these dont appear as hold cards # and so users cant infer river and flop cards from looking at all dealt hands for i in range(3): poker_util.deal_card(deck) pifOptions['FlopCards'] = poker_util.order_cards(flop_cards) pifOptions['RiverCard'] = river_card pifOptions['TurnCard'] = turn_card pifOptions['ExtraInfo'] = '' remaining_cards = list() while True: try: remaining_cards.append(poker_util.deal_card(deck)) except IndexError: break # make a set of possible two card combinations for dealing # note that two users can be dealt the same card, but not the exact same combination of cards # make into a list to prevent issues with serialising / storage of hands pifOptions['hands'] = list( itertools.combinations(remaining_cards, 2)) random.shuffle(pifOptions['hands']) # convert to json to avoid issues with persistence to dynamodb pifOptions['hands'] = json.dumps(pifOptions['hands']) BasePIF.__init__(self, postId, authorName, 'holdem-poker', minKarma, durationHours, endTime, pifOptions, pifEntries, karmaFail)
def handle_entry(self, comment, user, command_parts): logging.info('User [%s] entered to PIF [%s]', user, self.postId) deck = poker_util.new_deck() user_hand = list() for i in range(5): card = poker_util.deal_card(deck) user_hand.append(card) user_hand = poker_util.order_cards(user_hand) entry_details = dict() entry_details['CommentId'] = comment.id entry_details['UserHand'] = user_hand entry_details['HandScore'] = poker_util.hand_score(user_hand) self.pifEntries[user.name] = entry_details comment.reply( entry_template.format(user.name, poker_util.format_card(user_hand[0]), poker_util.format_card(user_hand[1]), poker_util.format_card(user_hand[2]), poker_util.format_card(user_hand[3]), poker_util.format_card(user_hand[4]), poker_util.determine_hand(user_hand))) comment.save()
def __init__(self, postId, authorName, minKarma, durationHours, endTime, pifOptions={}, pifEntries={}, karmaFail={}): logging.debug('Building single-deck poker PIF [%s]', postId) if len(pifOptions) < 1: deck = poker_util.new_deck() shared_cards = list() for i in range(3): card = poker_util.deal_card(deck) shared_cards.append(card) shared_cards = poker_util.order_cards(shared_cards) pifOptions['Deck'] = deck pifOptions['SharedCards'] = shared_cards BasePIF.__init__(self, postId, authorName, 'poker', minKarma, durationHours, endTime, pifOptions, pifEntries, karmaFail)
def handle_entry(self, comment, user, command_parts): logging.info('User [%s] entered to PIF [%s]', user, self.postId) hands = json.loads(self.pifOptions['hands']) if not self.pifOptions['hands']: comment.reply( "Sorry, I'm out of cards. Time to wrap this thing up.") comment.save() self.finalize() return user_hand = hands.pop() user_hand = poker_util.order_cards(user_hand) entry_details = dict() entry_details['CommentId'] = comment.id entry_details['UserHoleCards'] = user_hand self.pifEntries[user.name] = entry_details comment.reply( entry_template.format( user.name, poker_util.format_card(user_hand[0]), poker_util.format_card(user_hand[1]), )) comment.save() if not hands: self.finalize() else: # persist hands minus the one dealt self.pifOptions['hands'] = json.dumps(hands)
def test_poker(): deck = poker_util.new_deck() tied_winners = list() curr_max_score = 0 shared_cards = list() for i in range(3): shared_cards.append(poker_util.deal_card(deck)) print('--------------------') print("Shared cards: {}".format(poker_util.order_cards(shared_cards))) while len(deck) > 2: hand = list() for card in shared_cards: hand.append(card) for i in range(2): hand.append(poker_util.deal_card(deck)) ordered = poker_util.order_cards(hand) print("{} - {} - {}".format(ordered, poker_util.determine_hand(hand), poker_util.hand_score(hand))) if poker_util.hand_score(ordered) > curr_max_score: tied_winners = list() tied_winners.append(ordered) curr_max_score = poker_util.hand_score(ordered) elif poker_util.hand_score(ordered) == curr_max_score: tied_winners.append(ordered) print('--------------------') if len(tied_winners) == 1: print("{} - {} - {}".format(tied_winners[0], poker_util.determine_hand(tied_winners[0]), poker_util.hand_score(tied_winners[0]))) else: print("{} hands tied for win: {} - {} - {}".format( len(tied_winners), tied_winners[0], poker_util.determine_hand(tied_winners[0]), poker_util.hand_score(tied_winners[0])))
def handle_entry(self, comment, user, command_parts): logging.info('User [%s] entered to PIF [%s]', user, self.postId) deck = self.pifOptions['Deck'] if len(deck) < 2: comment.reply( "Sorry, I'm out of cards. Time to wrap this thing up.") comment.save() self.finalize() return shared_cards = self.pifOptions['SharedCards'] user_hand = list() user_cards = list() for card in shared_cards: user_hand.append(card) for i in range(2): card = poker_util.deal_card(deck) user_cards.append(card) user_hand.append(card) user_hand = poker_util.order_cards(user_hand) # Gotta put the deck back with fewer cards self.pifOptions['Deck'] = deck entry_details = dict() entry_details['CommentId'] = comment.id entry_details['UserCards'] = user_cards entry_details['UserHand'] = user_hand entry_details['HandScore'] = poker_util.hand_score(user_hand) self.pifEntries[user.name] = entry_details comment.reply( entry_template.format(user.name, poker_util.format_card(user_cards[0]), poker_util.format_card(user_cards[1]), poker_util.format_card(user_hand[0]), poker_util.format_card(user_hand[1]), poker_util.format_card(user_hand[2]), poker_util.format_card(user_hand[3]), poker_util.format_card(user_hand[4]), poker_util.determine_hand(user_hand))) comment.save() if len(deck) < 2: self.finalize()
def determine_winner(self): curr_max_score = 0 tied_winners = list() for entrant in self.pifEntries.keys(): # determine the entrants best possible hand by combining their hole cards with # the flop, turn and river cards entrant_best_score = 0 entrant_card_pool = self.pifEntries[entrant]['UserHoleCards'] entrant_card_pool.extend(self.pifOptions['FlopCards']) entrant_card_pool.append(self.pifOptions['TurnCard']) entrant_card_pool.append(self.pifOptions['RiverCard']) for possible_hand in itertools.combinations(entrant_card_pool, 5): hand_score = poker_util.hand_score(possible_hand) if hand_score > entrant_best_score: entrant_best_score = hand_score self.pifEntries[entrant][ 'BestHand'] = poker_util.order_cards( list(possible_hand)) self.pifEntries[entrant]['HandScore'] = hand_score if entrant_best_score > curr_max_score: if self.postId != get_comment( self.pifEntries[entrant]['CommentId']).submission.id: continue tied_winners = list() tied_winners.append(entrant) curr_max_score = self.pifEntries[entrant]['HandScore'] elif self.pifEntries[entrant]['HandScore'] == curr_max_score: tied_winners.append(entrant) if len(tied_winners) == 1: self.pifWinner = tied_winners[0] logging.info('User [%s] has won PIF [%s]', self.pifWinner, self.postId) else: self.pifWinner = random.choice(tied_winners) logging.info("{} players tied for first", len(tied_winners)) self.pifOptions[ 'ExtraInfo'] = "{} players tied for first, a winner was selected randomly".format( len(tied_winners)) logging.info('User [%s] has won PIF [%s]', self.pifWinner, self.postId)