Ejemplo n.º 1
0
    def test_simple_pot(self):
        """ Test simple 2 player, 1 round pot. """
        potmgr = PotManager()
        potmgr.add({10: self.players[:2]})
        pots = potmgr.pots

        self.assertEquals(1, len(pots))
        self.assertEquals(2, len(pots[0].players))
        self.assertEquals(set(self.players[:2]), set(pots[0].players))
        self.assertEquals(20, pots[0].amount)
Ejemplo n.º 2
0
    def test_one_allin(self):
        """ Test 2 player, one allin. """
        potmgr = PotManager()
        potmgr.add({10: self.players[:2]})
        self.players[0].allin = True
        potmgr.add({10: self.players[:2]})
        pots = potmgr.pots

        self.assertEquals(1, len(pots))
        self.assertEquals(2, len(pots[0].players))
        self.assertEquals(set(self.players[:2]), set(pots[0].players))
        self.assertEquals(40, pots[0].amount)
Ejemplo n.º 3
0
    def __init__(self, limit, players, callback, dealer_index, table=None, deck=None):
        """
        Constructor expects the list of players to all be active, no one
        sitting out. (TODO: add check for this)
        """
        # Every hand played needs a unique ID:
        self.id = ++GAME_ID_COUNTER

        self.dealer_index = dealer_index

        self.table = table
        self.limit = limit
        self._deck = deck

        # A callback method we can call when this game is finished to return
        # control to the object that created us:
        self.callback = callback

        self.aborted = False
        self.finished = False

        # List of players passed in should have empty seats and players
        # sitting out filtered out:
        # TODO: check for empty/sitting out spots:
        self.players = players

        self.pot_mgr = PotManager()

        # Create a new hand starting event and send to each player:
        new_hand_event = NewHandStarted(self.table, self.players, self.players[dealer_index].seat)
        self.table.notify_all(new_hand_event)
Ejemplo n.º 4
0
    def test_two_allin_split_pot(self):
        """ Test 2 players pushing in in different rounds. """
        potmgr = PotManager()
        self.players[2].allin = True
        potmgr.add({15: self.players[:3]})
        self.players[1].allin = True
        potmgr.add({10: self.players[:2]})
        pots = potmgr.pots

        self.assertEquals(2, len(pots))
        self.assertTrue(pots[1].is_main_pot)
        self.assertFalse(pots[0].is_main_pot)
        self.assertEquals(2, len(pots[0].players))
        self.assertEquals(3, len(pots[1].players))
        self.assertEquals(set(self.players[:2]), set(pots[0].players))
        self.assertEquals(set(self.players[:3]), set(pots[1].players))
        self.assertEquals(20, pots[0].amount)
        self.assertEquals(45, pots[1].amount)
Ejemplo n.º 5
0
    def test_two_allin_split_more_complicated(self):
        """ Test 2 players pushing in in different rounds, betting continues.

        Pots are:
           0 : 15 15 15
           1 : 16 16
           1 : 10 10
        """
        potmgr = PotManager()
        self.players[2].allin = True
        potmgr.add({15: [self.players[2]], 31: self.players[:2]})
        self.players[1].allin = True
        potmgr.add({10: self.players[:2]})
        pots = potmgr.pots

        self.assertEquals(2, len(pots))
        self.assertEquals(2, len(pots[0].players))
        self.assertEquals(3, len(pots[1].players))
        self.assertEquals(set(self.players[:2]), set(pots[0].players))
        self.assertEquals(set(self.players[:3]), set(pots[1].players))
        self.assertEquals((31 - 15) * 2 + 20, pots[0].amount)
        self.assertEquals(45, pots[1].amount)
Ejemplo n.º 6
0
class Game(object):

    """ Parent class of all poker games. """

    def __init__(self, limit, players, callback, dealer_index, table=None, deck=None):
        """
        Constructor expects the list of players to all be active, no one
        sitting out. (TODO: add check for this)
        """
        # Every hand played needs a unique ID:
        self.id = ++GAME_ID_COUNTER

        self.dealer_index = dealer_index

        self.table = table
        self.limit = limit
        self._deck = deck

        # A callback method we can call when this game is finished to return
        # control to the object that created us:
        self.callback = callback

        self.aborted = False
        self.finished = False

        # List of players passed in should have empty seats and players
        # sitting out filtered out:
        # TODO: check for empty/sitting out spots:
        self.players = players

        self.pot_mgr = PotManager()

        # Create a new hand starting event and send to each player:
        new_hand_event = NewHandStarted(self.table, self.players, self.players[dealer_index].seat)
        self.table.notify_all(new_hand_event)

    def process_action(self, action):
        """
        Process a player action.

        Assume that any parameters required for the action have already
        been received from the player, parsed, and applied to the action.

        NOTE: Be sure to validate the incoming action exists (i.e. we have
        a record of sending this action to the player) and we validate
        the parameters returned from the player. (to prevent users from
        modifying the client source and returning bogus actions)
        """
        raise NotImplementedError()

    def abort(self):
        """
        Abort the current hand and return control to the object that
        created us.
        """
        logger.warn("Aborting game:")
        self.aborted = True
        self.pot_mgr.refund_all()
        if self.total_value() > 0:
            logger.error("Funds left in pot after refuding all players: " + str(self.pot_mgr.total_value()))
        self.callback()

    def _check_if_finished(self):
        """
        Check if we're trying to do anything but the game has already been
        marked as finished.
        """
        if self.finished:
            raise RounderException("Game is finished.")

    def __get_active_players(self):
        active_players = []
        for p in self.players:
            if not p.folded:
                active_players.append(p)
        return active_players

    active_players = property(__get_active_players, None)