def get_galaxy(self, galaxy_name): galaxy = self.galaxies.get(galaxy_name.lower(), None) if not galaxy: galaxy = Galaxy(game=self.game, name=galaxy_name, guid=uuid4().hex) galaxy.save() self.galaxies[galaxy_name.lower()] = galaxy return galaxy
def new_game_map(game, players): """Generate and save a fresh new universe""" universe_spec = get_default_universe_spec(players) generator = UniverseGenerator() universe = generator.generate_universe(universe_spec) system_to_db_system = {} for galaxy in universe.galaxies: if galaxy.name: db_galaxy = Galaxy(game=game, name=galaxy.name, guid=uuid4().hex) db_galaxy.save() else: db_galaxy = None for system in galaxy.systems: db_system = System(game=game, galaxy=db_galaxy, name=system.name, population=system.population, owner=None) db_system.save() system_to_db_system[system] = db_system # link systems for lane in universe.lanes: db_lane = StarLane(game=game, passable=False) db_lane.save() sys_a, sys_b = [system_to_db_system[s] for s in lane.systems] a_to_b = LaneAndSystem(game=game, lane=db_lane, system=sys_b) a_to_b.save() sys_a.neighbors.add(a_to_b) b_to_a = LaneAndSystem(game=game, lane=db_lane, system=sys_a) b_to_a.save() sys_b.neighbors.add(b_to_a) db_lane.systems.add(sys_a) db_lane.systems.add(sys_b) for system in lane.systems: db_system = system_to_db_system[system] link = LaneAndSystem(game=game, lane=db_lane, system=db_system) link.save() db_system.neighbors.add(link) db_lane.systems.add(db_system) for player, home in universe.homes.items(): home_system = system_to_db_system[home] home_system.owner = player home_system.save() home_system.visible_to.add(player) player.home = home_system player.save() Ship(game=game, owner=player, ship_type=game_constants.SCOUT, attack_tech=player.attack_tech, range_tech=game_constants.ship_range(player, game_constants.SCOUT), moves=game_constants.ship_range(player, game_constants.SCOUT), system=home_system).save() for lane in StarLane.objects.filter(systems__id=home_system.id): lane.visible_to.add(player)