Ejemplo n.º 1
0
    def __init__(self, _id: int, money: int, controlled: bool, name: str,
                 play: Play, net: BaseNetwork):

        self.id: int = _id
        self.name: str = name
        self.money: int = money
        self.money_start_of_hand: int = money
        self.gived: int = 0
        self.in_pot: int = 0
        self.wins: int = 0
        self.in_game: bool = False
        self.in_play: bool = True
        self.re_seat: Players = None
        self.cards: CardsPair = CardsPair()
        self.hand: Hand = None
        self.controlled: bool = controlled
        self.lose_time: int = None
        self.play: Play = play
        self.network: BaseNetwork = net
Ejemplo n.º 2
0
 def setUp(self):
     self.none = CardsPair()
     self.half = CardsPair(Card('KS'))
     self.full = CardsPair(Card('QS'), Card('TH'))
Ejemplo n.º 3
0
class CardsPairTest(TestCase):
    def setUp(self):
        self.none = CardsPair()
        self.half = CardsPair(Card('KS'))
        self.full = CardsPair(Card('QS'), Card('TH'))

    def test_initialized(self):
        self.assertFalse(self.none.initialized())
        self.assertFalse(self.none.half_initialized())

        self.assertFalse(self.half.initialized())
        self.assertTrue(self.half.half_initialized())

        self.assertTrue(self.full.initialized())
        self.assertFalse(self.full.half_initialized())

    def test_raise_third_card_add(self):
        with self.assertRaises(CanNotAddAnotherCard):
            self.full.set(Card('AS'))

        self.half.set(Card('TC'))
        with self.assertRaises(CanNotAddAnotherCard):
            self.half.set(Card('AS'))

        self.none.set(Card('2D'))
        self.none.set(Card('3D'))
        with self.assertRaises(CanNotAddAnotherCard):
            self.none.set(Card('AS'))

    def test_suitability(self):
        suited = CardsPair(Card('AS'), Card('QS'))
        self.assertEqual(suited.suitability, Suitability.Suited)

        offsuited = CardsPair(Card('AS'), Card('QC'))
        self.assertEqual(offsuited.suitability, Suitability.Offsuited)

    def test_first_always_stronger(self):
        weak = Card('2C')
        strong = Card('AD')

        pair = CardsPair(strong, weak)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair(weak, strong)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair(weak)
        self.assertEqual(pair.first, weak)
        self.assertEqual(pair.second, None)

        pair.set(strong)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair(strong)
        self.assertEqual(pair.first, strong)

        pair.set(weak)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair()
        self.assertEqual(pair.first, None)
        self.assertEqual(pair.second, None)

        pair.set(weak)
        self.assertEqual(pair.first, weak)
        self.assertEqual(pair.second, None)

        pair.set(strong)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair()
        pair.set(strong)
        pair.set(weak)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

    def test_drop(self):
        self.full.drop()
        self.assertFalse(self.full.initialized())
        self.assertFalse(self.full.half_initialized())

        self.half.drop()
        self.assertFalse(self.half.initialized())
        self.assertFalse(self.half.half_initialized())

        self.none.drop()
        self.assertFalse(self.none.initialized())
        self.assertFalse(self.none.half_initialized())

    def test_str_not_initialized(self):
        self.full.str()
        self.full.long_str()

        with self.assertRaises(NotInitializedCards):
            self.half.str()
        with self.assertRaises(NotInitializedCards):
            self.half.long_str()

        with self.assertRaises(NotInitializedCards):
            self.none.str()
        with self.assertRaises(NotInitializedCards):
            self.none.long_str()

    def test_equal(self):
        pair1 = CardsPair(Card('AS'), Card('5D'))
        pair2 = CardsPair(Card('AS'), Card('5C'))
        pair3 = CardsPair(Card('AS'), Card('5D'))
        self.assertNotEqual(pair1, pair2)
        self.assertEqual(pair1, pair3)

    def test_str(self):
        self.assertRegex(str(self.full), '.. ..')
        self.assertRegex(str(self.half), '.. \?\?')
        self.assertRegex(str(self.none), '\?\? \?\?')

    def test_hash(self):
        pair1 = CardsPair(Card('AS'), Card('5D'))
        pair2 = CardsPair(Card('AS'), Card('5C'))
        pair3 = CardsPair(Card('AS'), Card('5D'))
        self.assertEqual(len({pair1, pair2, pair3}), 2)

    def test_can_not_add_same_card(self):
        for card1 in Card.cards_52():

            with self.assertRaises(InitializeWithSameCard):
                CardsPair(card1, card1)

            with self.assertRaises(InitializeWithSameCard):
                pair = CardsPair(card1)
                pair.set(card1)

            with self.assertRaises(InitializeWithSameCard):
                pair = CardsPair()
                pair.set(card1)
                pair.set(card1)

            for card2 in Card.cards_52():
                if card1 != card2:
                    CardsPair(card1, card2)

                    pair = CardsPair(card1)
                    pair.set(card2)

                    pair = CardsPair(card2)
                    pair.set(card1)

                    pair = CardsPair()
                    pair.set(card1)
                    pair.set(card2)

                    pair = CardsPair()
                    pair.set(card2)
                    pair.set(card1)
                else:
                    with self.assertRaises(InitializeWithSameCard):
                        CardsPair(card1, card2)

                    with self.assertRaises(InitializeWithSameCard):
                        CardsPair(card2, card1)

                    with self.assertRaises(InitializeWithSameCard):
                        pair = CardsPair(card1)
                        pair.set(card2)

                    with self.assertRaises(InitializeWithSameCard):
                        pair = CardsPair(card2)
                        pair.set(card1)

                    with self.assertRaises(InitializeWithSameCard):
                        pair = CardsPair()
                        pair.set(card1)
                        pair.set(card2)

                    with self.assertRaises(InitializeWithSameCard):
                        pair = CardsPair()
                        pair.set(card2)
                        pair.set(card1)

    def test_all_combinations(self):

        pairs = []
        for card1 in Card.cards_52():
            for card2 in Card.cards_52():
                if card1 != card2:
                    pairs += [CardsPair(card1, card2)]

                    pair = CardsPair(card1)
                    pair.set(card2)
                    pairs += [pair]

                    pair = CardsPair(card2)
                    pair.set(card1)
                    pairs += [pair]

                    pair = CardsPair()
                    pair.set(card1)
                    pair.set(card2)
                    pairs += [pair]

                    pair = CardsPair()
                    pair.set(card2)
                    pair.set(card1)
                    pairs += [pair]

        pairs2 = CardsPair.get_all_pairs()
        self.assertEqual(len(pairs2), len(set(pairs)))
Ejemplo n.º 4
0
    def test_first_always_stronger(self):
        weak = Card('2C')
        strong = Card('AD')

        pair = CardsPair(strong, weak)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair(weak, strong)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair(weak)
        self.assertEqual(pair.first, weak)
        self.assertEqual(pair.second, None)

        pair.set(strong)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair(strong)
        self.assertEqual(pair.first, strong)

        pair.set(weak)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair()
        self.assertEqual(pair.first, None)
        self.assertEqual(pair.second, None)

        pair.set(weak)
        self.assertEqual(pair.first, weak)
        self.assertEqual(pair.second, None)

        pair.set(strong)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)

        pair = CardsPair()
        pair.set(strong)
        pair.set(weak)
        self.assertEqual(pair.first, strong)
        self.assertEqual(pair.second, weak)
Ejemplo n.º 5
0
    def test_suitability(self):
        suited = CardsPair(Card('AS'), Card('QS'))
        self.assertEqual(suited.suitability, Suitability.Suited)

        offsuited = CardsPair(Card('AS'), Card('QC'))
        self.assertEqual(offsuited.suitability, Suitability.Offsuited)
Ejemplo n.º 6
0
    def test_all_combinations(self):

        pairs = []
        for card1 in Card.cards_52():
            for card2 in Card.cards_52():
                if card1 != card2:
                    pairs += [CardsPair(card1, card2)]

                    pair = CardsPair(card1)
                    pair.set(card2)
                    pairs += [pair]

                    pair = CardsPair(card2)
                    pair.set(card1)
                    pairs += [pair]

                    pair = CardsPair()
                    pair.set(card1)
                    pair.set(card2)
                    pairs += [pair]

                    pair = CardsPair()
                    pair.set(card2)
                    pair.set(card1)
                    pairs += [pair]

        pairs2 = CardsPair.get_all_pairs()
        self.assertEqual(len(pairs2), len(set(pairs)))
Ejemplo n.º 7
0
    def test_can_not_add_same_card(self):
        for card1 in Card.cards_52():

            with self.assertRaises(InitializeWithSameCard):
                CardsPair(card1, card1)

            with self.assertRaises(InitializeWithSameCard):
                pair = CardsPair(card1)
                pair.set(card1)

            with self.assertRaises(InitializeWithSameCard):
                pair = CardsPair()
                pair.set(card1)
                pair.set(card1)

            for card2 in Card.cards_52():
                if card1 != card2:
                    CardsPair(card1, card2)

                    pair = CardsPair(card1)
                    pair.set(card2)

                    pair = CardsPair(card2)
                    pair.set(card1)

                    pair = CardsPair()
                    pair.set(card1)
                    pair.set(card2)

                    pair = CardsPair()
                    pair.set(card2)
                    pair.set(card1)
                else:
                    with self.assertRaises(InitializeWithSameCard):
                        CardsPair(card1, card2)

                    with self.assertRaises(InitializeWithSameCard):
                        CardsPair(card2, card1)

                    with self.assertRaises(InitializeWithSameCard):
                        pair = CardsPair(card1)
                        pair.set(card2)

                    with self.assertRaises(InitializeWithSameCard):
                        pair = CardsPair(card2)
                        pair.set(card1)

                    with self.assertRaises(InitializeWithSameCard):
                        pair = CardsPair()
                        pair.set(card1)
                        pair.set(card2)

                    with self.assertRaises(InitializeWithSameCard):
                        pair = CardsPair()
                        pair.set(card2)
                        pair.set(card1)
Ejemplo n.º 8
0
 def test_hash(self):
     pair1 = CardsPair(Card('AS'), Card('5D'))
     pair2 = CardsPair(Card('AS'), Card('5C'))
     pair3 = CardsPair(Card('AS'), Card('5D'))
     self.assertEqual(len({pair1, pair2, pair3}), 2)
Ejemplo n.º 9
0
 def test_equal(self):
     pair1 = CardsPair(Card('AS'), Card('5D'))
     pair2 = CardsPair(Card('AS'), Card('5C'))
     pair3 = CardsPair(Card('AS'), Card('5D'))
     self.assertNotEqual(pair1, pair2)
     self.assertEqual(pair1, pair3)
Ejemplo n.º 10
0
    def process_actions(self, lines):
        while True:

            try:
                line = next(lines).strip()
            except StopIteration:
                return

            if not line:
                return

            match = self.parser.find_dealt_cards.search(line)

            if match is not None:
                name = match.group(1)
                first_card = Card(match.group(2).upper())
                second_card = Card(match.group(3).upper())
                pair = CardsPair(first_card, second_card)
                self.game.curr_hand.set_cards(name, pair)
                continue

            match = self.parser.find_fold.search(line)

            if match is not None:
                name = match.group(1)
                self.game.curr_hand.add_decision(name, Event.Fold, 0)
                continue

            match = self.parser.find_call.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.total_pot += money
                self.game.curr_hand.add_decision(name, Event.Call,
                                                 self.call_amount)
                continue

            match = self.parser.find_call_2.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace('\xa0', ''))
                self.total_pot += money
                self.game.curr_hand.add_decision(name, Event.Call,
                                                 self.call_amount)
                continue

            match = self.parser.find_check.search(line)

            if match is not None:
                name = match.group(1)
                self.game.curr_hand.add_decision(name, Event.Check, 0)
                continue

            match = self.parser.find_bet.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.call_amount = money
                self.total_pot += money
                self.game.curr_hand.add_decision(name, Event.Raise, money)
                continue

            match = self.parser.find_bet_2.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace('\xa0', ''))
                self.call_amount = money
                self.total_pot += money
                self.game.curr_hand.add_decision(name, Event.Raise, money)
                continue

            match = self.parser.find_raise.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.total_pot += money
                self.call_amount = self.game.curr_hand.get_player(name).gived(
                    self.game.curr_hand.curr_step) + money
                try:
                    self.game.curr_hand.add_decision(name, Event.Raise,
                                                     self.call_amount)
                except ValueError:
                    print('Can not add decision: ' + line)
                    raise
                continue

            match = self.parser.find_raise_2.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace('\xa0', ''))
                self.total_pot += money
                self.call_amount = self.game.curr_hand.get_player(name).gived(
                    self.game.curr_hand.curr_step) + money
                try:
                    self.game.curr_hand.add_decision(name, Event.Raise,
                                                     self.call_amount)
                except ValueError:
                    print('Can not add decision: ' + line)
                    raise
                continue

            match = self.parser.find_did_not_show.search(line)

            if match is not None:
                continue

            match = self.parser.find_win_money.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.game.curr_hand.add_decision(name, Event.WinMoney, money)
                self.game.curr_hand.add_winner(name)
                continue

            match = self.parser.find_win_money_2.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace('\xa0', ''))
                self.game.curr_hand.add_decision(name, Event.WinMoney, money)
                self.game.curr_hand.add_winner(name)
                continue

            match = self.parser.find_show_cards.search(line)

            if match is not None:
                name = match.group(1)
                card1 = Card(match.group(2).upper())
                card2 = Card(match.group(3).upper())
                pair = CardsPair(card1, card2)
                self.game.curr_hand.set_cards(name, pair)
                self.game.curr_hand.goes_to_showdown = True
                continue

            match = self.parser.find_muck_cards.search(line)

            if match is not None:
                name = match.group(1)
                card1 = Card(match.group(2).upper())
                card2 = Card(match.group(3).upper())
                pair = CardsPair(card1, card2)
                self.game.curr_hand.set_cards(name, pair)
                self.game.curr_hand.add_loser(name)
                continue

            raise ValueError('Undefined action: ' + line)
Ejemplo n.º 11
0
    def process_summary(self, text: str) -> None:
        every_line = iter(text.strip().split('\n'))
        line = next(every_line).strip()

        if not line.startswith('Total pot'):
            raise ValueError(f'Bad first line of summary: {text}')

        if 'Main pot' in line:
            match = self.parser.find_total_pot_with_main_pot.search(line)
        else:
            match = self.parser.find_total_pot.search(line)

        try:
            total_pot = int(match.group(1))
        except AttributeError:
            raise ValueError(f'Bad total pot: {line}')

        self.game.curr_hand.total_pot = total_pot

        line = next(every_line)

        if line.startswith('Board'):
            line = next(every_line)

        if not line.startswith('Seat'):
            raise ValueError(f'Bad second/third line of summary: {text}')

        while line.startswith('Seat'):

            if line.endswith("folded before Flop (didn't bet)") or \
                    line.endswith('folded before Flop') or \
                    line.endswith('folded on the Flop') or \
                    line.endswith('folded on the Turn') or \
                    line.endswith('folded on the River'):

                try:
                    line = next(every_line)
                except StopIteration:
                    return

                continue

            if ' (button) ' in line:
                line = line.replace(' (button) ', ' ')
            if ' (big blind) ' in line:
                line = line.replace(' (big blind) ', ' ')
            if ' (small blind) ' in line:
                line = line.replace(' (small blind) ', ' ')

            match = self.parser.find_collected_pot_summary.search(line)

            if match is not None:

                name = match.group(1)
                win_player_cards = self.game.curr_hand.get_player(name).cards
                if win_player_cards is not None and win_player_cards.initialized(
                ):
                    self.game.curr_hand.add_winner(name)

            else:

                match = self.parser.find_lost.search(line)

                if match is not None:
                    name = match.group(1)
                    card1 = Card(match.group(2).upper())
                    card2 = Card(match.group(3).upper())
                    self.game.curr_hand.set_cards(name,
                                                  CardsPair(card1, card2))
                    self.game.curr_hand.add_loser(name)

                else:

                    match = self.parser.find_won.search(line)

                    if match is not None:
                        name = match.group(1)
                        card1 = Card(match.group(2).upper())
                        card2 = Card(match.group(3).upper())
                        self.game.curr_hand.set_cards(name,
                                                      CardsPair(card1, card2))
                        self.game.curr_hand.add_winner(name)

                    else:

                        match = self.parser.find_mucked_cards.search(line)

                        if match is not None:
                            name = match.group(1)
                            card1 = Card(match.group(2).upper())
                            card2 = Card(match.group(3).upper())
                            self.game.curr_hand.set_cards(
                                name, CardsPair(card1, card2))
                            self.game.curr_hand.add_loser(name)

                        else:
                            raise ValueError(
                                f'Bad summary processing line: {line}')

            try:
                line = next(every_line)
            except StopIteration:
                return

        self.process_actions(every_line)
Ejemplo n.º 12
0
    def process_actions(self, lines):
        while True:

            try:
                line = next(lines).strip()
            except StopIteration:
                return

            match = self.parser.find_dealt_cards.search(line)

            if match is not None:
                name = match.group(1)
                first_card = Card(match.group(2).upper())
                second_card = Card(match.group(3).upper())
                pair = CardsPair(first_card, second_card)
                self.game.curr_hand.set_cards(name, pair)
                continue

            match = self.parser.find_uncalled_bet.search(line)

            if match is not None:
                money = int(match.group(1))
                name = match.group(2)
                self.game.curr_hand.add_decision(name, Event.ReturnMoney,
                                                 money)
                continue

            match = self.parser.find_collect_pot.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2))
                self.game.curr_hand.add_decision(name, Event.WinMoney, money)
                continue

            match = self.parser.find_collect_side_pot.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2))
                self.game.curr_hand.add_decision(name, Event.WinMoney, money)
                continue

            match = self.parser.find_collect_side_pot_n.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2))
                self.game.curr_hand.add_decision(name, Event.WinMoney, money)
                continue

            match = self.parser.find_collect_main_pot.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2))
                self.game.curr_hand.add_decision(name, Event.WinMoney, money)
                continue

            match = self.parser.find_show_cards.search(line)

            if match is not None:
                name = match.group(1)
                cards = match.group(2)

                if len(cards) == 5:
                    card1, card2 = map(str.upper, cards.split())
                    pair = CardsPair(Card(card1), Card(card2))

                elif len(cards) == 2:
                    only_card = Card(cards.upper())
                    pair = CardsPair(only_card)

                else:
                    raise ValueError(f'Bad cards shown: {line}')

                self.game.curr_hand.set_cards(name, pair)
                continue

            match = self.parser.find_is_connected.search(line)

            if match is not None:
                name = match.group(1)
                self.game.curr_hand.add_decision(name, Event.Connected, 0)
                continue

            match = self.parser.find_is_disconnected.search(line)

            if match is not None:
                name = match.group(1)
                try:
                    self.game.curr_hand.add_decision(name, Event.Disconnected,
                                                     0)
                except ValueError:
                    pass
                continue

            match = self.parser.find_is_sitting_out.search(line)

            if match is not None:
                name = match.group(1)
                try:
                    self.game.curr_hand.add_decision(name, Event.Disconnected,
                                                     0)
                except ValueError:
                    pass
                continue

            match = self.parser.find_said.search(line)

            if match is not None:
                name = match.group(1)
                msg = match.group(2)
                try:
                    self.game.curr_hand.add_decision(name, Event.ChatMessage,
                                                     0, msg)
                except ValueError:
                    self.game.curr_hand.add_decision(name,
                                                     Event.ObserverChatMessage,
                                                     0, msg)
                continue

            match = self.parser.find_observer_said.search(line)

            if match is not None:
                name = match.group(1)
                msg = match.group(2)
                self.game.curr_hand.add_decision(name,
                                                 Event.ObserverChatMessage, 0,
                                                 msg)
                continue

            match = self.parser.find_finished.search(line)

            if match is not None:
                name = match.group(1)
                place = match.group(2)
                self.game.curr_hand.add_decision(name, Event.FinishGame, 0,
                                                 place)
                match = self.parser.find_place.search(place)
                self.game.curr_hand.players_left = int(match.group(1))
                continue

            match = self.parser.find_received.search(line)

            if match is not None:
                name = match.group(1)
                place = match.group(2)
                earn = int(match.group(3).replace('.', ''))
                self.game.curr_hand.add_decision(name, Event.FinishGame, earn,
                                                 place)
                match = self.parser.find_place.search(place)
                self.game.curr_hand.players_left = int(match.group(1))
                continue

            match = self.parser.find_received_fpp.search(line)

            if match is not None:
                name = match.group(1)
                place = match.group(2)
                earn = int(match.group(3))
                self.game.curr_hand.add_decision(name, Event.FinishGame, earn,
                                                 place)
                match = self.parser.find_place.search(place)
                self.game.curr_hand.players_left = int(match.group(1))
                continue

            match = self.parser.find_winner.search(line)

            if match is not None:
                name = match.group(1)
                earn = int(match.group(2).replace('.', ''))
                self.game.curr_hand.add_decision(name, Event.FinishGame, earn,
                                                 '1st')
                continue

            match = self.parser.find_does_not_show.search(line)

            if match is not None:
                continue

            match = self.parser.find_has_returned.search(line)

            if match is not None:
                name = match.group(1)
                try:
                    self.game.curr_hand.add_decision(name, Event.Connected, 0)
                except ValueError:
                    pass
                continue

            match = self.parser.find_has_timed_out.search(line)

            if match is not None:
                continue

            match = self.parser.find_timed_disconnected.search(line)

            if match is not None:
                name = match.group(1)
                self.game.curr_hand.add_decision(name, Event.Disconnected, 0)
                continue

            match = self.parser.find_timed_being_disconnected.search(line)

            if match is not None:
                name = match.group(1)
                self.game.curr_hand.add_decision(name, Event.Disconnected, 0)
                continue

            match = self.parser.find_finished_the_tournament.search(line)

            if match is not None:
                continue

            match = self.parser.find_eliminated_and_bounty.search(line)

            if match is not None:
                continue

            match = self.parser.find_eliminated_and_bounty_first.search(line)

            if match is not None:
                continue

            match = self.parser.find_eliminated_and_bounty_split.search(line)

            if match is not None:
                continue

            match = self.parser.find_rebuy_and_receive_chips.search(line)

            if match is not None:
                continue

            match = self.parser.find_rebuy_for_starcoins.search(line)

            if match is not None:
                continue

            match = self.parser.find_addon_and_receive_chips.search(line)

            if match is not None:
                continue

            match = self.parser.find_addon_for_starcoins.search(line)

            if match is not None:
                continue

            match = self.parser.find_skip_break_and_resuming.search(line)

            if match is not None:
                continue

            match = self.parser.find_wins_entry_to_tournament.search(line)

            if match is not None:
                continue

            match = self.parser.find_add_chips.search(line)

            if match is not None:
                continue

            if match is not None:
                name = match.group(1)
                self.game.curr_hand.add_decision(name, Event.Disconnected, 0)
                continue

            match = self.parser.find_shows_in_show_down.search(line)

            if match is not None:
                name = match.group(1)
                card1 = Card(match.group(2).upper())
                card2 = Card(match.group(3).upper())
                self.game.curr_hand.set_cards(name, CardsPair(card1, card2))
                continue

            match = self.parser.find_fold_showing_cards.search(line)

            if match is not None:
                name = match.group(1)
                cards = match.group(2)

                if len(cards) == 5:
                    card1, card2 = map(str.upper, cards.split())
                    pair = CardsPair(Card(card1), Card(card2))

                elif len(cards) == 2:
                    only_card = Card(cards.upper())
                    pair = CardsPair(only_card)

                else:
                    raise ValueError(f'Bad cards shown: {line}')

                self.game.curr_hand.set_cards(name, pair)
                continue

            match = self.parser.find_mucks_hand.search(line)

            if match is not None:
                continue

            match = self.parser.find_action.search(line)

            try:
                name = match.group(1)
                action = match.group(2)
            except AttributeError:
                print('Cannot parse line:', line)
                raise

            try:
                result, money = self.parse_action(
                    self.game.curr_hand.get_player(name),
                    self.game.curr_hand.curr_step, action)
            except ValueError:
                print('Bad action: ' + line)
                raise

            self.game.curr_hand.add_decision(name, result, money)
Ejemplo n.º 13
0
    def process_actions(self, lines):
        while True:

            try:
                line = next(lines).strip()
            except StopIteration:
                return

            if not line:
                return

            match = self.parser.find_dealt_cards.search(line)

            if match is not None:
                name = match.group(1)
                first_card = Card(match.group(2).upper())
                second_card = Card(match.group(3).upper())
                pair = CardsPair(first_card, second_card)
                self.game.curr_hand.set_cards(name, pair)
                continue

            match = self.parser.find_fold.search(line)

            if match is not None:
                name = match.group(1)
                try:
                    self.game.curr_hand.add_decision(name, Event.Fold, 0)
                except ValueError:
                    pass
                continue

            match = self.parser.find_call.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.total_pot += money
                self.game.curr_hand.add_decision(name, Event.Call,
                                                 self.call_amount)
                continue

            match = self.parser.find_check.search(line)

            if match is not None:
                name = match.group(1)
                self.game.curr_hand.add_decision(name, Event.Check, 0)
                continue

            match = self.parser.find_bet.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.call_amount = money
                self.total_pot += money
                self.game.curr_hand.add_decision(name, Event.Raise, money)
                continue

            match = self.parser.find_raise.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.total_pot += money
                self.call_amount = self.game.curr_hand.get_player(name).gived(
                    self.game.curr_hand.curr_step) + money
                self.game.curr_hand.add_decision(name, Event.Raise,
                                                 self.call_amount)
                continue

            match = self.parser.find_all_in.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.total_pot += money
                self.call_amount = self.game.curr_hand.get_player(name).gived(
                    self.game.curr_hand.curr_step) + money
                self.game.curr_hand.add_decision(name, Event.Raise,
                                                 self.call_amount)
                continue

            match = self.parser.find_did_not_show.search(line)

            if match is not None:
                continue

            match = self.parser.find_win_money.search(line)

            if match is not None:
                name = match.group(1)
                money = int(match.group(2).replace(',', ''))
                self.game.curr_hand.add_decision(name, Event.WinMoney, money)
                self.game.curr_hand.add_winner(name)
                continue

            match = self.parser.find_show_cards.search(line)

            if match is not None:
                name = match.group(1)
                card1 = Card(match.group(2).upper())
                card2 = Card(match.group(3).upper())
                pair = CardsPair(card1, card2)
                self.game.curr_hand.set_cards(name, pair)
                self.game.curr_hand.goes_to_showdown = True
                continue

            match = self.parser.find_finished.search(line)

            if match is not None:
                name = match.group(1)
                place = match.group(2)
                self.game.curr_hand.add_decision(name, Event.FinishGame, 0,
                                                 place)
                continue

            match = self.parser.find_knocked_out.search(line)

            if match is not None:
                continue

            match = self.parser.find_join_game.search(line)

            if match is not None:
                continue

            match = self.parser.find_use_bank_time.search(line)

            if match is not None:
                continue

            match = self.parser.find_did_not_respond.search(line)

            if match is not None:
                continue

            match = self.parser.find_not_respond_disconnected.search(line)

            if match is not None:
                name = match.group(1)
                self.game.curr_hand.add_decision(name, Event.Disconnected, 0)
                continue

            match = self.parser.find_moved_from_other_table.search(line)

            if match is not None:
                continue

            match = self.parser.find_break.search(line)

            if match is not None:
                continue

            match = self.parser.find_activate_bank.search(line)

            if match is not None:
                continue

            match = self.parser.find_reconnected.search(line)

            if match is not None:
                continue

            match = self.parser.find_disconnected_wait.search(line)

            if match is not None:
                continue

            match = self.parser.find_level_moves.search(line)

            if match is not None:
                continue

            match = self.parser.find_chat_message.search(line)

            if match is not None:
                name = match.group(1)
                message = match.group(2)
                try:
                    self.game.curr_hand.add_decision(name, Event.ChatMessage,
                                                     0, message)
                except ValueError:
                    self.game.curr_hand.add_decision(name,
                                                     Event.ObserverChatMessage,
                                                     0, message)
                continue

            match = self.parser.find_end_of_hand.search(line)

            if match is not None:
                self.game.curr_hand.total_pot = self.total_pot
                break

            raise ValueError('Undefined action: ' + line)