Beispiel #1
0
    def validate(self):
        if not super().validate():
            return False

        try:
            MapFactory.create(self.as_configuration)
        except ConfigurationError as e:
            self.bots.errors.append(str(e))
            return False

        return True
Beispiel #2
0
    def test_create_basic(self):
        factory = MapFactory()
        conf = DefaultConfiguration()
        game_map = factory.create(conf)

        self.assertIsInMap(game_map, TreasureField)
        self.assertIsInMap(game_map, BotField, conf.bots)
        self.assertIsInMap(
            game_map,
            EmptyField,
            game_map.width * game_map.height - conf.bots - conf.treasures - conf.blocks
        )
Beispiel #3
0
    def test_export(self):
        game_map = MapFactory().create(DefaultConfiguration())
        game = Game(game_map)

        self.assertCountEqual(
            game.export(0),
            dict(
                map=game_map.export(),
                bots=game._export_bots(0),
                map_width=game_map.width,
                map_height=game_map.height,
                map_resolutions=(game_map.width, game_map.height)
            )
        )
Beispiel #4
0
    def test_create_small_map(self):
        factory = MapFactory()
        conf = CustomConfiguration(map_width=5, map_height=1, bots=2, treasures=2, blocks=1)
        with patch('random.randint', side_effect=(0, 0,
                                                  0, 0,
                                                  1, 0,
                                                  0, 0,
                                                  2, 0,
                                                  3, 0,
                                                  3, 0,
                                                  4, 0)):
            game_map = factory.create(conf)

        self.assertIsInMap(game_map, TreasureField, conf.treasures)
        self.assertIsInMap(game_map, BotField, conf.bots)
        self.assertIsInMap(game_map, BlockField, conf.blocks)
        self.assertIsInMap(
            game_map,
            EmptyField,
            game_map.width * game_map.height - conf.bots - conf.treasures - conf.blocks
        )
class GameController(object):
    _map_factory = None

    def __init__(self, conf_provider=configuration_provider):
        self._map_factory = MapFactory()
        self._conf_provider = conf_provider
        self.games = {}

    def get(self, bot_id):
        assert isinstance(bot_id, int)
        game = self.games.get(bot_id, None)
        if game:
            return game

        # try to find game with free bot
        game = self.open_game
        if game:
            game.add_bot(bot_id)
            self.games.update({
                bot_id: game
            })
            return game

        game = Game(self._map_factory.create(self._conf_provider.actual))
        game.add_bot(bot_id)
        self.games.update({bot_id: game})
        return game

    @property
    def open_game(self):
        for game in self.sorted_games(reverse=False).values():
            if game.is_filled:
                continue
            return game
        return None

    def action(self, bot_id, action):
        assert action in Action
        game = self.get(bot_id)
        try:
            return game.action(bot_id, action)
        except GameFinished:
            self.remove_game(game)
            raise

    def sorted_games(self, key='last_modified_at', reverse=True):
        return OrderedDict(sorted(self.games.items(), key=lambda x: attrgetter(key)(x[1]), reverse=reverse))

    def remove_game(self, game):
        self.games = {bot_id: g for bot_id, g in self.games.items() if g is not game}
Beispiel #6
0
    def test_invalid_action_in_laser_game(self):
        class Conf(BaseConfiguration, RandomFieldPlacerMixin):
            map_width = 1
            map_height = 1
            bots = 1
            treasures = 0
            blocks = 0
            battery_game = True
            laser_game = False

        conf = Conf()
        game = Game(MapFactory.create(conf), configuration=conf)
        bot_id = 1
        game.add_bot(bot_id)
        with self.assertRaises(ActionError):
            game.action(bot_id, Action.LASER_BEAM)
Beispiel #7
0
    def test_get_bot_position(self):
        class Conf(BaseConfiguration, RandomFieldPlacerMixin):
            map_width = 1
            map_height = 1
            bots = 1
            treasures = 0
            blocks = 0

        conf = Conf()
        game = Game(MapFactory.create(conf), configuration=conf)
        bot_id = 1
        game.add_bot(bot_id)
        self.assertEqual(
            game.get_bot_position(bot_id),
            (0, 0)
        )
Beispiel #8
0
    def get(self, bot_id):
        assert isinstance(bot_id, int)
        game = self.games.get(bot_id, None)
        if game:
            return game

        # try to find game with free bot
        game = self.open_game
        if game:
            game.add_bot(bot_id)
            self.games.update({bot_id: game})
            return game

        game = Game(MapFactory.create(self._conf_provider.actual))
        game.add_bot(bot_id)
        self.games.update({bot_id: game})
        return game
    def get(self, bot_id):
        assert isinstance(bot_id, int)
        game = self.games.get(bot_id, None)
        if game:
            return game

        # try to find game with free bot
        game = self.open_game
        if game:
            game.add_bot(bot_id)
            self.games.update({
                bot_id: game
            })
            return game

        game = Game(MapFactory.create(self._conf_provider.actual))
        game.add_bot(bot_id)
        self.games.update({bot_id: game})
        return game
Beispiel #10
0
    def test_export(self):
        game_map = MapFactory.create(DefaultConfiguration())
        game = Game(game_map)
        bot_id = 0
        game.add_bot(bot_id)

        self.assertCountEqual(
            game.export(bot_id),
            dict(
                map=game_map.export(),
                game_info=dict(
                    map_resolutions=dict(
                        width=game_map.width,
                        height=game_map.height
                    ),
                    rounded_game=DefaultConfiguration.rounded_game,
                    battery_game=DefaultConfiguration.battery_game,
                    laser_game=DefaultConfiguration.laser_game
                ),
            )
        )
Beispiel #11
0
    def test_rounded_game(self):
        class Conf(BaseConfiguration, RandomFieldPlacerMixin):
            map_width = 3
            map_height = 1
            bots = 3
            treasures = 0
            blocks = 0
            rounded_game = True

        conf = Conf()
        game = Game(MapFactory.create(conf), configuration=conf)

        my_bot_1 = 1
        my_bot_2 = 2
        my_bot_3 = 3

        game.add_bot(my_bot_1)
        game.add_bot(my_bot_2)

        game.action(my_bot_1, Action.TURN_LEFT)
        game.action(my_bot_2, Action.TURN_LEFT)

        with self.assertRaises(BotNotOnTurn):
            game.action(my_bot_2, Action.TURN_LEFT)

        game.action(my_bot_1, Action.TURN_LEFT)

        with self.assertRaises(BotNotOnTurn):
            game.action(my_bot_1, Action.TURN_LEFT)

        game.add_bot(my_bot_3)
        with self.assertRaises(BotNotOnTurn):
            game.action(my_bot_3, Action.TURN_LEFT)

        game.action(my_bot_2, Action.TURN_LEFT)
        game.action(my_bot_1, Action.TURN_LEFT)
        game.action(my_bot_3, Action.TURN_LEFT)
Beispiel #12
0
 def test_invalid_map(self):
     factory = MapFactory()
     with self.assertRaises(InvalidMapError):
         factory.create(CustomConfiguration(bots=2, treasures=2, map_width=3, map_height=1, blocks=0))
Beispiel #13
0
 def test_not_free_bots(self):
     game_map = MapFactory.create(CustomConfiguration(map_width=2, map_height=1, bots=0, treasures=0, blocks=0))
     game = Game(game_map)
     with self.assertRaises(NoFreeBots):
         game.action('bot_id', Action.STEP)
     self.assertTrue(game.is_filled, 'Game is filled.')
 def __init__(self, conf_provider=configuration_provider):
     self._map_factory = MapFactory()
     self._conf_provider = conf_provider
     self.games = {}