Пример #1
0
 def construct_turn_manager(self, avatars, locations):
     self.avatar_manager = DummyAvatarManager(avatars)
     self.game_state = GameState(InfiniteMap(), self.avatar_manager)
     self.turn_manager = ConcurrentTurnManager(
         game_state=self.game_state, end_turn_callback=lambda: None)
     for index, location in enumerate(locations):
         self.game_state.add_avatar(index, "", location)
     return self.turn_manager
Пример #2
0
def run_game(port):
    global worker_manager

    print("Running game...")
    settings = pickle.loads(os.environ['settings'])
    api_url = os.environ.get('GAME_API_URL', 'http://localhost:8000/players/api/games/')
    generator = getattr(map_generator, settings['GENERATOR'])(settings)
    player_manager = AvatarManager()
    game_state = generator.get_game_state(player_manager)
    turn_manager = ConcurrentTurnManager(game_state=game_state, end_turn_callback=send_world_update, completion_url=api_url+'complete/')
    WorkerManagerClass = WORKER_MANAGERS[os.environ.get('WORKER_MANAGER', 'local')]
    worker_manager = WorkerManagerClass(game_state=game_state, users_url=api_url, port=port)
    worker_manager.start()
    turn_manager.start()
Пример #3
0
def run_game():
    global worker_manager

    print("Running game...")
    my_map = map_generator.generate_map(10, 10, 0.1)
    player_manager = AvatarManager()
    game_state = GameState(my_map, player_manager)
    turn_manager = ConcurrentTurnManager(game_state=game_state, end_turn_callback=send_world_update)
    WorkerManagerClass = WORKER_MANAGERS[os.environ.get('WORKER_MANAGER', 'local')]
    worker_manager = WorkerManagerClass(
        game_state=game_state,
        users_url=os.environ.get('GAME_API_URL', 'http://localhost:8000/players/api/games/')
    )
    worker_manager.start()
    turn_manager.start()
Пример #4
0
def run_game(port):
    global worker_manager

    print("Running game...")
    settings = pickle.loads(os.environ['settings'])

    api_url = os.environ.get('GAME_API_URL', 'http://localhost:8000/players/api/games/')
    generator = getattr(map_generator, settings['GENERATOR'])(settings)
    player_manager = AvatarManager()
    game_state = generator.get_game_state(player_manager)

    turn_manager = ConcurrentTurnManager(game_state=game_state, end_turn_callback=send_world_update, completion_url=api_url+'complete/')
    WorkerManagerClass = WORKER_MANAGERS[os.environ.get('WORKER_MANAGER', 'local')]
    worker_manager = WorkerManagerClass(game_state=game_state, users_url=api_url, port=port)

    worker_manager.start()
    turn_manager.start()
Пример #5
0
def run_game():
    global worker_manager

    print("Running game...")
    my_map = map_generator.generate_map(10, 10, 0.1)
    player_manager = AvatarManager()
    game_state = GameState(my_map, player_manager)
    turn_manager = ConcurrentTurnManager(game_state=game_state,
                                         end_turn_callback=send_world_update)
    WorkerManagerClass = WORKER_MANAGERS[os.environ.get(
        'WORKER_MANAGER', 'local')]
    worker_manager = WorkerManagerClass(
        game_state=game_state,
        users_url=os.environ.get('GAME_API_URL',
                                 'http://localhost:8000/players/api/games/'))
    worker_manager.start()
    turn_manager.start()
Пример #6
0
 def construct_turn_manager(self, avatars, locations):
     self.avatar_manager = DummyAvatarManager(avatars)
     self.game_state = MockGameState(InfiniteMap(), self.avatar_manager)
     self.turn_manager = ConcurrentTurnManager(game_state=self.game_state,
                                               end_turn_callback=lambda: None)
     for index, location in enumerate(locations):
         self.game_state.add_avatar(index, "", location)
     return self.turn_manager
Пример #7
0
class TestTurnManager(unittest.TestCase):
    def construct_default_avatar_appearance(self):
        return AvatarAppearance("#000", "#ddd", "#777", "#fff")

    def construct_turn_manager(self, avatars, locations):
        self.avatar_manager = DummyAvatarManager(avatars)
        self.game_state = GameState(InfiniteMap(), self.avatar_manager)
        self.turn_manager = ConcurrentTurnManager(
            game_state=self.game_state, end_turn_callback=lambda: None)
        for index, location in enumerate(locations):
            self.game_state.add_avatar(index, "", location)
        return self.turn_manager

    def assert_at(self, avatar, location):
        self.assertEqual(avatar.location, location)
        cell = self.game_state.world_map.get_cell(location)
        self.assertEqual(cell.avatar, avatar)

    def get_avatar(self, player_id):
        return self.avatar_manager.get_avatar(player_id)

    def run_turn(self):
        self.turn_manager.run_turn()

    def test_run_turn(self):
        '''
        Given:  > _
        (1)
        Expect: _ o
        '''
        self.construct_turn_manager([MoveEastDummy], [ORIGIN])
        avatar = self.get_avatar(0)

        self.assert_at(avatar, ORIGIN)
        self.run_turn()
        self.assert_at(avatar, RIGHT_OF_ORIGIN)

    def test_run_several_turns(self):
        '''
        Given:  > _ _ _ _ _
        (5)
        Expect: _ _ _ _ _ o
        '''
        self.construct_turn_manager([MoveEastDummy], [ORIGIN])
        avatar = self.get_avatar(0)

        self.assertEqual(avatar.location, ORIGIN)
        [self.run_turn() for _ in range(5)]
        self.assertEqual(avatar.location, FIVE_RIGHT_OF_ORIGIN)

    def test_run_several_turns_and_avatars(self):
        '''
        Given:  > _ _ _ _ _
                > _ _ _ _ _
        (5)
        Expect: _ _ _ _ _ o
                _ _ _ _ _ o
        '''
        self.construct_turn_manager([MoveEastDummy, MoveEastDummy],
                                    [ORIGIN, ABOVE_ORIGIN])
        avatar0 = self.get_avatar(0)
        avatar1 = self.get_avatar(1)

        self.assert_at(avatar0, ORIGIN)
        self.assert_at(avatar1, ABOVE_ORIGIN)
        [self.run_turn() for _ in range(5)]
        self.assert_at(avatar0, FIVE_RIGHT_OF_ORIGIN)
        self.assert_at(avatar1, FIVE_RIGHT_OF_ORIGIN_AND_ONE_ABOVE)

    def test_move_chain_succeeds(self):
        '''
        Given:  > > > > > _

        Expect: _ o o o o o
        '''
        self.construct_turn_manager([MoveEastDummy for _ in range(5)],
                                    [Location(x, 0) for x in range(5)])
        avatars = [self.get_avatar(i) for i in range(5)]

        [self.assert_at(avatars[x], Location(x, 0)) for x in range(5)]
        self.run_turn()
        [self.assert_at(avatars[x], Location(x + 1, 0)) for x in range(5)]

    def test_move_chain_fails_occupied(self):
        '''
        Given:  > > x _

        Expect: x x x _
        '''
        self.construct_turn_manager([MoveEastDummy, MoveEastDummy, WaitDummy],
                                    [Location(x, 0) for x in range(3)])
        avatars = [self.get_avatar(i) for i in range(3)]

        [self.assert_at(avatars[x], Location(x, 0)) for x in range(3)]
        self.run_turn()
        [self.assert_at(avatars[x], Location(x, 0)) for x in range(3)]

    def test_move_chain_fails_collision(self):
        '''
        Given:  > > > _ <
        (1)
        Expect: x x x _ x
        '''
        locations = [
            Location(0, 0),
            Location(1, 0),
            Location(2, 0),
            Location(4, 0)
        ]
        self.construct_turn_manager(
            [MoveEastDummy, MoveEastDummy, MoveEastDummy, MoveWestDummy],
            locations)
        avatars = [self.get_avatar(i) for i in range(4)]

        [self.assert_at(avatars[i], locations[i]) for i in range(4)]
        self.run_turn()
        [self.assert_at(avatars[i], locations[i]) for i in range(4)]

    def test_move_chain_fails_cycle(self):
        '''
        Given:  > v
                ^ <
        (1)
        Expect: x x
                x x
        '''
        locations = [
            Location(0, 1),
            Location(1, 1),
            Location(1, 0),
            Location(0, 0)
        ]
        self.construct_turn_manager(
            [MoveEastDummy, MoveSouthDummy, MoveWestDummy, MoveNorthDummy],
            locations)
        avatars = [self.get_avatar(i) for i in range(4)]

        [self.assert_at(avatars[i], locations[i]) for i in range(4)]
        self.run_turn()
        [self.assert_at(avatars[i], locations[i]) for i in range(4)]

    def test_move_chain_fails_spiral(self):
        '''
        Given:  > > v
                  ^ <
        (1)
        Expect: x x x
                  x x
        '''
        locations = [
            Location(0, 1),
            Location(1, 1),
            Location(2, 1),
            Location(2, 0),
            Location(1, 0)
        ]
        self.construct_turn_manager([
            MoveEastDummy, MoveEastDummy, MoveSouthDummy, MoveWestDummy,
            MoveNorthDummy
        ], locations)
        avatars = [self.get_avatar(i) for i in range(5)]

        [self.assert_at(avatars[i], locations[i]) for i in range(5)]
        self.run_turn()
        [self.assert_at(avatars[i], locations[i]) for i in range(5)]
Пример #8
0
class TestTurnManager(unittest.TestCase):
    def construct_default_avatar_appearance(self):
        return AvatarAppearance("#000", "#ddd", "#777", "#fff")

    def construct_turn_manager(self, avatars, locations):
        self.avatar_manager = DummyAvatarManager(avatars)
        self.game_state = MockGameState(InfiniteMap(), self.avatar_manager)
        self.turn_manager = ConcurrentTurnManager(game_state=self.game_state,
                                                  end_turn_callback=lambda: None)
        for index, location in enumerate(locations):
            self.game_state.add_avatar(index, "", location)
        return self.turn_manager

    def assert_at(self, avatar, location):
        self.assertEqual(avatar.location, location)
        cell = self.game_state.world_map.get_cell(location)
        self.assertEqual(cell.avatar, avatar)

    def get_avatar(self, player_id):
        return self.avatar_manager.get_avatar(player_id)

    def run_turn(self):
        self.turn_manager.run_turn()

    def test_run_turn(self):
        '''
        Given:  > _
        (1)
        Expect: _ o
        '''
        self.construct_turn_manager([MoveEastDummy], [ORIGIN])
        avatar = self.get_avatar(0)

        self.assert_at(avatar, ORIGIN)
        self.run_turn()
        self.assert_at(avatar, RIGHT_OF_ORIGIN)

    def test_run_several_turns(self):
        '''
        Given:  > _ _ _ _ _
        (5)
        Expect: _ _ _ _ _ o
        '''
        self.construct_turn_manager([MoveEastDummy], [ORIGIN])
        avatar = self.get_avatar(0)

        self.assertEqual(avatar.location, ORIGIN)
        [self.run_turn() for _ in range(5)]
        self.assertEqual(avatar.location, FIVE_RIGHT_OF_ORIGIN)

    def test_run_several_turns_and_avatars(self):
        '''
        Given:  > _ _ _ _ _
                > _ _ _ _ _
        (5)
        Expect: _ _ _ _ _ o
                _ _ _ _ _ o
        '''
        self.construct_turn_manager([MoveEastDummy, MoveEastDummy],
                                    [ORIGIN,        ABOVE_ORIGIN])
        avatar0 = self.get_avatar(0)
        avatar1 = self.get_avatar(1)

        self.assert_at(avatar0, ORIGIN)
        self.assert_at(avatar1, ABOVE_ORIGIN)
        [self.run_turn() for _ in range(5)]
        self.assert_at(avatar0, FIVE_RIGHT_OF_ORIGIN)
        self.assert_at(avatar1, FIVE_RIGHT_OF_ORIGIN_AND_ONE_ABOVE)

    def test_move_chain_succeeds(self):
        '''
        Given:  > > > > > _

        Expect: _ o o o o o
        '''
        self.construct_turn_manager([MoveEastDummy for _ in range(5)],
                                    [Location(x, 0) for x in range(5)])
        avatars = [self.get_avatar(i) for i in range(5)]

        [self.assert_at(avatars[x], Location(x, 0)) for x in range(5)]
        self.run_turn()
        [self.assert_at(avatars[x], Location(x + 1, 0)) for x in range(5)]

    def test_move_chain_fails_occupied(self):
        '''
        Given:  > > x _

        Expect: x x x _
        '''
        self.construct_turn_manager([MoveEastDummy, MoveEastDummy, WaitDummy],
                                    [Location(x, 0) for x in range(3)])
        avatars = [self.get_avatar(i) for i in range(3)]

        [self.assert_at(avatars[x], Location(x, 0)) for x in range(3)]
        self.run_turn()
        [self.assert_at(avatars[x], Location(x, 0)) for x in range(3)]

    def test_move_chain_fails_collision(self):
        '''
        Given:  > > > _ <
        (1)
        Expect: x x x _ x
        '''
        locations = [Location(0, 0), Location(1, 0), Location(2, 0), Location(4, 0)]
        self.construct_turn_manager(
            [MoveEastDummy, MoveEastDummy, MoveEastDummy, MoveWestDummy],
            locations)
        avatars = [self.get_avatar(i) for i in range(4)]

        [self.assert_at(avatars[i], locations[i]) for i in range(4)]
        self.run_turn()
        [self.assert_at(avatars[i], locations[i]) for i in range(4)]

    def test_move_chain_fails_cycle(self):
        '''
        Given:  > v
                ^ <
        (1)
        Expect: x x
                x x
        '''
        locations = [Location(0, 1), Location(1, 1), Location(1, 0), Location(0, 0)]
        self.construct_turn_manager(
            [MoveEastDummy, MoveSouthDummy, MoveWestDummy, MoveNorthDummy],
            locations)
        avatars = [self.get_avatar(i) for i in range(4)]

        [self.assert_at(avatars[i], locations[i]) for i in range(4)]
        self.run_turn()
        [self.assert_at(avatars[i], locations[i]) for i in range(4)]

    def test_move_chain_fails_spiral(self):
        '''
        Given:  > > v
                  ^ <
        (1)
        Expect: x x x
                  x x
        '''
        locations = [Location(0, 1),
                     Location(1, 1),
                     Location(2, 1),
                     Location(2, 0),
                     Location(1, 0)]
        self.construct_turn_manager(
            [MoveEastDummy, MoveEastDummy, MoveSouthDummy, MoveWestDummy, MoveNorthDummy],
            locations)
        avatars = [self.get_avatar(i) for i in range(5)]

        [self.assert_at(avatars[i], locations[i]) for i in range(5)]
        self.run_turn()
        [self.assert_at(avatars[i], locations[i]) for i in range(5)]