Exemple #1
0
    def test_ui_cells_serialization(self):
        generator = WorldInfoPrototype.get_by_id(
            map_info_storage.item.world_id).generator

        cells = UICells(generator)

        self.assertEqual(cells.serialize(),
                         UICells.deserialize(cells.serialize()).serialize())
Exemple #2
0
    def create(cls, turn_number, width, height, terrain, world):  # pylint: disable=R0914
        from the_tale.game.map.generator.descriptors import UICells

        terrain_percents = {}

        terrain_squares = collections.defaultdict(int)

        for y in range(0, height):
            for x in range(0, width):
                cell = world.generator.cell_info(x, y)

                if cell.height < -0.2:
                    terrain_squares[MAP_STATISTICS.LOWLANDS] += 1
                elif cell.height < 0.3:
                    terrain_squares[MAP_STATISTICS.PLAINS] += 1
                else:
                    terrain_squares[MAP_STATISTICS.UPLANDS] += 1

                if cell.vegetation == VEGETATION_TYPE.DESERT:
                    terrain_squares[MAP_STATISTICS.DESERTS] += 1
                elif cell.vegetation == VEGETATION_TYPE.GRASS:
                    terrain_squares[MAP_STATISTICS.GRASS] += 1
                else:
                    terrain_squares[MAP_STATISTICS.FORESTS] += 1

        total_cells = width * height

        terrain_percents = dict((id_.value, float(square) / total_cells)
                                for id_, square in terrain_squares.items())

        person_race_percents = get_person_race_percents(
            persons_storage.persons.all())
        race_percents = get_race_percents(places_storage.places.all())

        #race to cities percents
        race_cities = dict((race.value, 0) for race in RACE.records)
        for place_model in Place.objects.all():
            place = places_logic.load_place(place_model=place_model)
            race_cities[place.race.value] += 1

        statistics = {
            'terrain_percents': terrain_percents,
            'person_race_percents': person_race_percents,
            'race_percents': race_percents,
            'race_cities': race_cities
        }

        model = MapInfo.objects.create(
            turn_number=turn_number,
            width=width,
            height=height,
            terrain=s11n.to_json([[cell.value for cell in row]
                                  for row in terrain]),
            cells=s11n.to_json(UICells.create(world.generator).serialize()),
            world=world._model,
            statistics=s11n.to_json(statistics))
        return cls(model)
Exemple #3
0
    def create(cls, turn_number, width, height, terrain, world): # pylint: disable=R0914
        from the_tale.game.map.generator.descriptors import UICells

        terrain_percents = {}

        terrain_squares = collections.defaultdict(int)

        for y in xrange(0, height):
            for x in xrange(0, width):
                cell = world.generator.cell_info(x, y)

                if cell.height < -0.2:
                    terrain_squares[MAP_STATISTICS.LOWLANDS] += 1
                elif cell.height < 0.3:
                    terrain_squares[MAP_STATISTICS.PLAINS] += 1
                else:
                    terrain_squares[MAP_STATISTICS.UPLANDS] += 1

                if cell.vegetation == VEGETATION_TYPE.DESERT:
                    terrain_squares[MAP_STATISTICS.DESERTS] += 1
                elif cell.vegetation == VEGETATION_TYPE.GRASS:
                    terrain_squares[MAP_STATISTICS.GRASS] += 1
                else:
                    terrain_squares[MAP_STATISTICS.FORESTS] += 1

        total_cells = width * height

        terrain_percents = dict( (id_.value, float(square) / total_cells) for id_, square in terrain_squares.items())

        person_race_percents = get_person_race_percents(persons_storage.filter(state=PERSON_STATE.IN_GAME))
        race_percents = get_race_percents(places_storage.all())

        #race to cities percents
        race_cities = dict( (race.value, 0) for race in RACE.records)
        for place_model in Place.objects.all():
            place = PlacePrototype(place_model)
            race_cities[place.race.value] += 1

        statistics = {'terrain_percents': terrain_percents,
                      'person_race_percents': person_race_percents,
                      'race_percents': race_percents,
                      'race_cities': race_cities}

        model = MapInfo.objects.create(turn_number=turn_number,
                                       width=width,
                                       height=height,
                                       terrain=s11n.to_json([ [cell.value for cell in row] for row in terrain]),
                                       cells=s11n.to_json(UICells.create(world.generator).serialize()),
                                       world=world._model,
                                       statistics=s11n.to_json(statistics))
        return cls(model)
Exemple #4
0
    def test_ui_cells_serialization(self):
        generator = WorldInfoPrototype.get_by_id(map_info_storage.item.world_id).generator

        cells = UICells(generator)

        self.assertEqual(cells.serialize(), UICells.deserialize(cells.serialize()).serialize())
Exemple #5
0
 def cells(self):
     from the_tale.game.map.generator.descriptors import UICells
     return UICells.deserialize(s11n.from_json(self._model.cells))