예제 #1
0
    def test_update_game_calls_send_actions(self):
        cells = [
            Cell(0, 5, 10, Vec2(0, 0), Vec2(1, 1)),
            Cell(1, 5, 10, Vec2(0, 0), Vec2(1, 1)),
            Cell(2, 5, 10, Vec2(0, 0), Vec2(1, 1))
        ]
        player = Player(0, "", 10, True, cells)
        game = Game(0, 0, 0, [player], Resources([], [], []), Map(0, 0), [])

        cells[0].target = Vec2(2, 2)
        cells[0].burst()

        cells[1].split()
        cells[1].trade(3)

        class MockApi:
            def send_actions(self, game_id, actions):
                self.actions = actions

        api = MockApi()

        update_game(api, game, lambda x: None)

        self.assertEqual(len(api.actions), 2)
        self.assertTrue(api.actions[0].target.almost_equals(Vec2(2, 2)))
        self.assertTrue(api.actions[0].burst)

        self.assertTrue(api.actions[1].split)
        self.assertEqual(3, api.actions[1].trade)
예제 #2
0
    def test_trade_sets_actions_trade(self):
        cell = Cell(1, 2, 3, Vec2(4, 5), Vec2(6, 7))

        cell.trade(42)

        actions = cell.actions()
        self.assertEqual(42, actions.trade)
        self.assertIsNotNone(actions.target)
예제 #3
0
    def test_burst_sets_actions_burst(self):
        cell = Cell(1, 2, 3, Vec2(4, 5), Vec2(6, 7))

        cell.burst()

        actions = cell.actions()
        self.assertTrue(actions.burst)
        self.assertIsNotNone(actions.target)
예제 #4
0
    def test_actions_sets_actions_target(self):
        cell = Cell(1, 2, 3, Vec2(4, 5), Vec2(6, 7))
        cell.move(Vec2(6, 7))

        actions = cell.actions()

        self.assertEqual(1, actions.cell_id)
        self.assertTrue(Vec2(6, 7).almost_equals(actions.target))
예제 #5
0
    def dispatch(self, request, *args, **kwargs):
        # get the game by the id
        self.game = Game.get_game(kwargs['game_id'])
        user = get_user(request)
        # check to see if the game is open and available for this user
        # if this player is the creator, just return
        if self.game.p1 == user or self.game.p2 == user:
            return super(GameView, self).dispatch(request, *args, **kwargs)

        # if there is no opponent and the game is not yet completed,
        # set the opponent as this user
        if not self.game.p2 and self.game.winner == 0:
            self.game.p2 = user
            Cell.create_new_board(self.game.id, self.game.num_rows,
                                  self.game.num_cols, self.game.p1,
                                  self.game.p2)
            self.game.save()
            return super(GameView, self).dispatch(request, *args, **kwargs)
        else:
            messages.add_message(request, messages.ERROR,
                                 'Sorry, the selected game is not available.')
            return redirect('/lobby/')
예제 #6
0
    def test_parse(self):
        obj = {
            "id": 1,
            "mass": 12,
            "radius": 8,
            "position": {
                "x": 1241,
                "y": 442
            },
            "target": {
                "x": 1448,
                "y": 1136
            }
        }

        cell = Cell.parse(obj)

        self.assertEqual(1, cell.id)
        self.assertEqual(12, cell.mass)
        self.assertEqual(8, cell.radius)
        self.assertTrue(cell.position.almost_equals(Vec2(1241, 442)))
        self.assertTrue(cell.target.almost_equals(Vec2(1448, 1136)))
예제 #7
0
    def test_actions_do_nothing(self):
        cell = Cell(1, 2, 3, Vec2(4, 5), Vec2(6, 7))

        actions = cell.actions()

        self.assertIsNone(actions)