def test_serializable(a):
    coordinate = Coordinate(1, 2, -3)

    data = coordinate.json

    assert data == "1,2,-3", "Json was not the expected output"

    assert coordinate == Coordinate.load(
        data), "Reserialization did not return an equal object"
Beispiel #2
0
def load_map(session, map_file, sector):
    """ Load a map of tiles into a sector. """

    map_data = load(map_file)

    rows = len(map_data)
    cols = len(map_data[0])

    r_start = -(rows // 2) + 1

    for x, row in enumerate(map_data, r_start):  # rows

        c_start = -(cols // 2) + 1
        for y, tile_type in enumerate(row, c_start):
            tile_type = TileType(tile_type)
            z = -(x + y)

            coordinate = Coordinate(x, y, z)

            tile = get_tile(session, sector, coordinate)
            if tile is None:
                tile = Tile(type=tile_type)
                tile.location = get_location(session, sector, coordinate)
                session.add(tile)
            elif tile.type != tile_type:
                print(f"Updating {tile} to {tile_type.name}")
                tile.type = tile_type
Beispiel #3
0
    def load(cls, data):
        obj = cls()

        obj.uuid = data["uuid"]
        obj.position = Coordinate.load(data["position"])

        return obj
Beispiel #4
0
def get_city_data(message):
    """ Send the data for a city to the requesting player. """
    position = message["position"]

    city = CITIES.get(Coordinate.load(position))
    LOGGER.debug(f"{current_user} loading city {city}")
    if city:
        emit("update_city", city)
def test_neighbors(origin):
    neighbors = origin.neighbors

    assert origin not in neighbors, "A position cannot be its own neighbor"

    assert len(neighbors) == 6, "Each tile should have 6 neighbors"

    for permutation in permutations((-1, 0, 1), 3):
        expected = Coordinate(*permutation)

        assert expected in neighbors, "Neighbors is missing a value!"
Beispiel #6
0
def generate_sectors(session, sectors_data):
    """ Get or create sectors using static config data. """
    print(sectors_data)
    for sector_name, sector_data in sectors_data.items():
        try:
            sector = get_sector(session, sector_name=sector_name)
        except NoResultFound:
            print(f"{sector_name} not in db, creating it")
            sector = Sector(name=sector_name)
            session.add(sector)

        cities_data = sector_data.pop("cities")
        for city_name, city_data in cities_data.items():
            try:
                get_city(session, city_name=city_name)
            except NoResultFound:
                print(f"{city_name} not found, creating it")
                coordinate = Coordinate.load(city_data["location"])
                location = get_location(session, sector, coordinate)
                create_city(session, city_name, location)

        load_map(session, f"data/maps/{sector.name}.map", sector)
Beispiel #7
0
def _origin():
    return Coordinate(0, 0, 0)
Beispiel #8
0
def _b():
    return Coordinate(-4, 1, 3)
Beispiel #9
0
def _a():
    return Coordinate(1, 2, -3)
Beispiel #10
0
    def sell(self, resource, volume):
        """ Add resources to the city and return the total price of those resources. """
        value = resource.cost(self.demand(resource)) * volume
        LOGGER.info(
            f"{volume} units of {resource} sold to {self} for ${value}")
        self.resources[resource] += volume

        LOGGER.debug(f"{self.resources[resource]} units in stock at {self}")
        return value

    def __str__(self):
        return self.name


# TODO: move initialization of cities to a proper loading function
c = City()
c.name = "Demoville"
c.population = 10

c.position = Coordinate(-4, 2, 2)

CITIES[c.position] = c

c = City()
c.name = "Otherville"
c.population = 42

c.position = Coordinate(-2, 2, 0)

CITIES[c.position] = c
Beispiel #11
0
""" Module for providing global resource data. """

from data import Resource

from world.coordinates import Coordinate

# TODO: Implement ResourceNode to handle variability in the quality of nodes
# TODO: track resources on a world layer instead
RESOURCE_NODES = {
    Coordinate(0, 2, -2): Resource.WATER,
    Coordinate(1, 0, -1): Resource.WATER,
    Coordinate(-1, 0, 1): Resource.FOOD,
    Coordinate(-3, 0, 3): Resource.FUEL,
}
Beispiel #12
0
    def __init__(self):
        self.uuid = str(uuid4())
        self.position = Coordinate(0, 0, 0)

        GameObject.objects[self.uuid] = self
def test_add(origin, a, b):
    assert origin + a == a
    assert origin + b == b

    assert a + b == Coordinate(-3, 3, 0)