Exemplo n.º 1
0
    def test_privatize_hands(self):
        """Test hiding opponents' hands.
        """
        g = Game()
        g.add_player(uuid4(), 'p0')
        g.add_player(uuid4(), 'p1')
        gs = g

        p0, p1 = gs.players

        latrine, insula, jack, road = cm.get_cards(['Latrine', 'Insula', 'Jack', 'Road'])
        p0.hand.set_content([latrine, insula])
        p1.hand.set_content([jack, road])

        gs_private = g.privatized_game_state_copy('p0')
        p0, p1 = gs_private.players

        self.assertIn(jack, p1.hand)
        self.assertIn(Card(-1), p1.hand)
        self.assertNotIn(road, p1.hand)

        self.assertIn(latrine, p0.hand)
        self.assertIn(insula, p0.hand)

        self.assertEqual(len(p0.hand), 2)
        self.assertEqual(len(p1.hand), 2)
Exemplo n.º 2
0
    def test_start_frames(self):
        game = Game()
        game.add_player(0, 'p0')
        game.add_player(1, 'p1')
        game.start()

        self.encode_decode_compare_game(game)
Exemplo n.º 3
0
def simple_n_player(n, clientele=[], buildings=[], deck=None):
    """N-player game with nothing in hands, stockpiles,
    or common piles. Player 1 (p1) goes first.

    One frame of take_turn_stacked is pushed, and the game
    is pumped, so it's ready for the first player (p1) to 
    thinker or lead.
    """
    g = Game()

    players = ['p{0:d}'.format(i+1) for i in range(n)]

    for p in players:
        uid = uuid4().int
        g.add_player(uid, p)

    if deck is None:
        d = TestDeck()
    else:
        d = deck

    for p, p_clientele, p_buildings in \
            izip_longest(g.players, clientele, buildings, fillvalue=[]):

        p.clientele.set_content([getattr(d, c.replace(' ','')) for c in p_clientele])

        for b in p_buildings:
            foundation = getattr(d, b)
            b = Building(foundation, foundation.material, [], None, True)
            p.buildings.append(b)

    g.controlled_start()

    return g
Exemplo n.º 4
0
    def test_add_player_with_same_name(self):
        """Adding two players with the same name raises GTRError.
        """
        g = Game()
        g.add_player(uuid4(), 'p1')

        with self.assertRaises(GTRError):
            g.add_player(uuid4(), 'p1')
Exemplo n.º 5
0
    def test_add_player(self):

        g = Game()
        g.add_player(uuid4(), 'p1')

        self.assertEqual(len(g.players), 1)

        g.add_player(uuid4(), 'p2')

        self.assertEqual(len(g.players), 2)
Exemplo n.º 6
0
    def test_add_too_many_players(self):
        """Adding a sixth player raises GTRError.
        """
        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')
        g.add_player(uuid4(), 'p3')
        g.add_player(uuid4(), 'p4')
        g.add_player(uuid4(), 'p5')

        with self.assertRaises(GTRError):
            g.add_player(uuid4(), 'p6')
Exemplo n.º 7
0
    def _create_game(self, user):
        """Create a new game."""
        game = Game()
        self.games.append(game)
        game_id = len(self.games)-1
        lg.info('Creating new game {0:d}'.format(game_id))
        game.game_id = game_id
        game.host = user

        username = self._userinfo(user)['name']
        player_index = game.add_player(user, username)

        return game_id
Exemplo n.º 8
0
    def _create_game(self, user):
        """Create a new game."""
        game = Game()
        self.games.append(game)
        game_id = len(self.games) - 1
        lg.info('Creating new game {0:d}'.format(game_id))
        game.game_id = game_id
        game.host = user

        username = self._userinfo(user)['name']
        player_index = game.add_player(user, username)

        return game_id
Exemplo n.º 9
0
    def test_two_player(self):

        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')

        gs = g
        gs.library.set_content(cm.get_cards(['Bar', 'Circus']))

        first = g._init_pool(len(gs.players))

        self.assertEqual(first, 0)
        self.assertIn('Bar', gs.pool)
        self.assertIn('Circus', gs.pool)
Exemplo n.º 10
0
    def test_add_too_many_players(self):
        """Adding a sixth player raises GTRError.
        """
        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')
        g.add_player(uuid4(), 'p3')
        g.add_player(uuid4(), 'p4')
        g.add_player(uuid4(), 'p5')

        with self.assertRaises(GTRError):
            g.add_player(uuid4(), 'p6')
Exemplo n.º 11
0
def decode_game(obj):
    """Decode a dictionary made with encode() into a Game object.
    """
    try:
        players = obj['players']
        jacks = obj['jacks']
        library = obj['library']
        pool = obj['pool']
    except KeyError as e:
        raise GTREncodingError(e.message)

    game_dict = copy.deepcopy(obj)

    game_dict['players'] = [decode_player(p) for p in players]

    game_dict['jacks'] = decode_zone(jacks, 'jacks')
    game_dict['library'] = decode_zone(library, 'library')
    game_dict['pool'] = decode_zone(pool, 'pool')

    g = Game()

    for k, v in game_dict.items():
        setattr(g, k, v)

    return g
Exemplo n.º 12
0
    def test_frame(self):
        game = Game()
        game.add_player(0, 'p0')
        game.add_player(1, 'p1')
        game.start()

        frame = game._current_frame
        frame_dict = encode.encode(frame)
        frame_recovered = encode.decode_frame(frame_dict)
        frame_dict2 = encode.encode(frame_recovered)
        self.assertEqual(str(frame_dict), str(frame_dict2))
        
        for frame in game.stack.stack:
            frame_dict = encode.encode(frame)
            frame_recovered = encode.decode_frame(frame_dict)
            frame_dict2 = encode.encode(frame_recovered)
            self.assertEqual(str(frame_dict), str(frame_dict2))
Exemplo n.º 13
0
    def test_privatize_library(self):
        """Test hiding the library.
        """
        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')
        gs = g

        gs.library.set_content(cm.get_cards(
            ['Circus', 'Circus', 'Circus', 'Circus Maximus', 'Circus', 'Circus',
             'Ludus Magna', 'Ludus Magna', 'Statue', 'Coliseum',
             ]))

        gs_private = g.privatized_game_state_copy('p1')

        self.assertFalse(gs_private.library.contains('Circus'))
        self.assertEqual(gs_private.library, Zone([Card(-1)]*len(gs_private.library), name='library'))
Exemplo n.º 14
0
    def test_privatize_fountain_card(self):
        """Test hiding the card revealed with the fountain.
        """
        g = Game()
        g.add_player(uuid4(), 'p0')
        g.add_player(uuid4(), 'p1')

        gs = g
        p0, p1 = gs.players

        latrine, insula, statue, road = cm.get_cards(['Latrine', 'Insula', 'Statue', 'Road'])
        p0.fountain_card = latrine

        gs_private = g.privatized_game_state_copy('p1')
        p0, p1 = gs_private.players

        self.assertEqual(p0.fountain_card, Card(-1))
Exemplo n.º 15
0
    def test_privatize_vaults(self):
        """Test hiding all vaults.
        """
        g = Game()
        g.add_player(uuid4(), 'p0')
        g.add_player(uuid4(), 'p1')
        gs = g

        p0, p1 = gs.players

        latrine, insula, statue, road = cm.get_cards(['Latrine', 'Insula', 'Statue', 'Road'])
        p0.vault.set_content([latrine, insula])
        p1.vault.set_content([statue, road])

        gs_private = g.privatized_game_state_copy('p1')
        p0, p1 = gs_private.players

        self.assertEqual(p0.vault, Zone([Card(-1)]*2, name='vault'))
        self.assertEqual(p1.vault, Zone([Card(-1)]*2, name='vault'))
Exemplo n.º 16
0
def simple_n_player(n):
    """N-player game with nothing in hands, stockpiles,
    or common piles. Player 1 (p1) goes first.

    One frame of take_turn_stacked is pushed, and the game
    is pumped, so it's ready for the first player (p1) to 
    thinker or lead.
    """
    g = Game()

    players = ['p{0:d}'.format(i+1) for i in range(n)]

    for p in players:
        uid = uuid4().int
        g.add_player(uid, p)

    g.controlled_start()

    return g
Exemplo n.º 17
0
    def test_add_player_after_start(self):
        """Attempting to add a player after the game has started throws
        a GTRError.
        """
        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')

        g.start()

        with self.assertRaises(GTRError):
            g.add_player(uuid4(), 'p3')
Exemplo n.º 18
0
    def test_start_again(self):
        """Starting an already-started game raises a GTRError.
        """

        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')

        g.start()

        with self.assertRaises(GTRError):
            g.start()
Exemplo n.º 19
0
    def create_game(self, user_id):
        """Create a new game, return the new game ID."""
        userdict = yield self.db.retrieve_user(user_id)

        game_id = yield self.db.create_game_with_host(user_id)
        # If another coroutine locks this game between getting the id from
        # the DB and acquiring the lock, then blocks so acquiring the lock
        # times out, we raise an exception and the game never gets stored.
        # The DB will contain an empty stub for this game ID.
        try:
            lock = self._game_locks[game_id]
        except KeyError:
            lock = locks.Lock()
            self._game_locks[game_id] = lock
        
        try:
            with (yield lock.acquire(GTRServer.GAME_WAIT_TIMEOUT)):
                username = userdict['username']

                lg.info('Creating new game {0:d} with host {1}'.format(
                    game_id, username))

                game = Game()
                game.game_id = game_id
                game.host = username

                lg.debug('Adding player {0!s} with ID {1!s}'.format(username, user_id))

                player_index = game.add_player(user_id, username)

                if len(game.game_log):
                    yield self.db.append_log_messages(game_id, game.game_log)

                yield self._store_game(game)

                raise gen.Return(game_id)

        except gen.TimeoutError:
            raise GTRError('Timeout acquiring lock to create game.')
Exemplo n.º 20
0
    def test_add_player_with_same_name(self):
        """Adding two players with the same name raises GTRError.
        """
        g = Game()
        g.add_player(uuid4(), 'p1')

        with self.assertRaises(GTRError):
            g.add_player(uuid4(), 'p1')
Exemplo n.º 21
0
    def test_add_player_after_start(self):
        """Attempting to add a player after the game has started throws
        a GTRError.
        """
        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')

        g.start()

        with self.assertRaises(GTRError):
            g.add_player(uuid4(), 'p3')
Exemplo n.º 22
0
    def test_start_again(self):
        """Starting an already-started game raises a GTRError.
        """

        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')

        g.start()

        with self.assertRaises(GTRError):
            g.start()
Exemplo n.º 23
0
    def test_start(self):
        """Starting a game puts orders and jacks in the hands of the players,
        populates the pool, and sets the turn number to 1
        """

        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')

        g.start()
        
        self.assertEqual(len(g.jacks), 4)

        # We don't know how many cards because of tiebreakers in determining
        # who goes first but each player at least needs 4 cards + 1 in the pool
        self.assertTrue(len(g.library) <= 134)
        self.assertTrue(len(g.library) > 0)

        for p in g.players:
            self.assertIn('Jack', p.hand)

        self.assertTrue(g.started)
        self.assertEqual(g.expected_action, message.THINKERORLEAD)
Exemplo n.º 24
0
    def test_add_player(self):

        g = Game()
        g.add_player(uuid4(), 'p1')

        self.assertEqual(len(g.players), 1)

        g.add_player(uuid4(), 'p2')

        self.assertEqual(len(g.players), 2)
Exemplo n.º 25
0
    def test_five_player(self):
        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')
        g.add_player(uuid4(), 'p3')
        g.add_player(uuid4(), 'p4')
        g.add_player(uuid4(), 'p5')

        gs = g
        gs.library.set_content(cm.get_cards(['Statue', 'Circus', 'Dock', 'Dock', 'Ludus Magna']))

        first = g._init_pool(len(gs.players))

        self.assertEqual(first, 1)
        self.assertIn('Statue', gs.pool)
        self.assertIn('Circus', gs.pool)
        self.assertIn('Ludus Magna', gs.pool)
        self.assertEqual(gs.pool.count('Dock'), 2)
Exemplo n.º 26
0
def simple_n_player(n):
    """N-player game with nothing in hands, stockpiles,
    or common piles. Player 1 (p1) goes first.

    One frame of take_turn_stacked is pushed, and the game
    is pumped, so it's ready for the first player (p1) to 
    thinker or lead.
    """
    g = Game()

    players = ['p{0:d}'.format(i + 1) for i in range(n)]

    for p in players:
        uid = uuid4().int
        g.add_player(uid, p)

    g.controlled_start()

    return g
Exemplo n.º 27
0
    def test_resolve_tie(self):
        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')
        g.add_player(uuid4(), 'p3')

        gs = g
        gs.library.set_content(cm.get_cards(
            ['Circus', 'Circus', 'Circus', 'Circus Maximus', 'Circus', 'Circus',
             'Ludus Magna', 'Ludus Magna', 'Statue', 'Coliseum',
             ]))


        first = g._init_pool(len(gs.players))

        self.assertEqual(first, 2)
        z = Zone(cm.get_cards(
                ['Circus', 'Circus', 'Circus', 'Circus Maximus', 'Circus',
                 'Circus', 'Ludus Magna', 'Ludus Magna', 'Statue', 'Coliseum']))
        self.assertTrue(z.equal_contents(gs.pool))
Exemplo n.º 28
0
    def test_start(self):
        """Starting a game puts orders and jacks in the hands of the players,
        populates the pool, and sets the turn number to 1
        """

        g = Game()
        g.add_player(uuid4(), 'p1')
        g.add_player(uuid4(), 'p2')

        g.start()

        self.assertEqual(len(g.jacks), 4)

        # We don't know how many cards because of tiebreakers in determining
        # who goes first but each player at least needs 4 cards + 1 in the pool
        self.assertTrue(len(g.library) <= 134)
        self.assertTrue(len(g.library) > 0)

        for p in g.players:
            self.assertIn('Jack', p.hand)

        self.assertTrue(g.started)
        self.assertEqual(g.expected_action, message.THINKERORLEAD)
Exemplo n.º 29
0
 def test_library(self):
     game = Game()
     game._init_library()
     self.jacks = Zone([Card(i) for i in range(6)])
     self.encode_decode_compare_game(game)
Exemplo n.º 30
0
 def test_sites(self):
     game = Game()
     game._init_sites(2)
     self.encode_decode_compare_game(game)
Exemplo n.º 31
0
 def test_player(self):
     game = Game()
     game.add_player(0, 'p0')
     self.encode_decode_compare_game(game)
Exemplo n.º 32
0
 def test_all_common_piles(self):
     game = Game()
     game.add_player(0, 'p0')
     game.add_player(1, 'p1')
     game._init_common_piles(2)
     self.encode_decode_compare_game(game)