def __init__(self): self.systems = {} self.lanes = [] self.galaxies = {} self.game = Game(start_date=datetime.now(), last_move_date=None) self.game.save() user = User.objects.all()[0] self.player = Player(game=self.game, user=user, turn_index=0, color='red', resources=213, attack_tech=1.3, range_tech=3.5) self.player.save() try: other_user = User.objects.all()[1] except: print "Warning: if you don't have two users, the game will be very weird" other_user = User.objects.all()[0] self.opponent = Player(game=self.game, user=other_user, turn_index=1, color='blue', resources=383, attack_tech=1.3, range_tech=3.5) self.opponent.save()
class GameBuilder(object): def __init__(self): self.systems = {} self.lanes = [] self.galaxies = {} self.game = Game(start_date=datetime.now(), last_move_date=None) self.game.save() user = User.objects.all()[0] self.player = Player(game=self.game, user=user, turn_index=0, color='red', resources=213, attack_tech=1.3, range_tech=3.5) self.player.save() try: other_user = User.objects.all()[1] except: print "Warning: if you don't have two users, the game will be very weird" other_user = User.objects.all()[0] self.opponent = Player(game=self.game, user=other_user, turn_index=1, color='blue', resources=383, attack_tech=1.3, range_tech=3.5) self.opponent.save() def setup_system(self, system_str, visible=False, owned=False, ships=[]): galaxy_name, name, pop = parse_system(system_str) galaxy = None if galaxy_name: galaxy = self.get_galaxy(galaxy_name) system = System(name=name, population=pop, game=self.game, galaxy=galaxy) if owned: system.owner = self.player self.systems[name] = system system.save() if visible: system.visible_to.add(self.player) for ship in ships: type_, attack, range_, is_owned_by_player = ship s = Ship(game=self.game, owner=is_owned_by_player and self.player or self.opponent, ship_type=type_, attack_tech=attack, range_tech=range_, moves=range_, system=system) s.save() 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 link_systems(self, a, b, passable=False, visible=False): _, a_name, _ = parse_system(a) _, b_name, _ = parse_system(b) a_sys = self.systems[a_name] b_sys = self.systems[b_name] a_sys.save() b_sys.save() lane = StarLane(passable=passable, game=self.game) lane.save() a_link = LaneAndSystem(game=self.game, lane=lane, system=b_sys) b_link = LaneAndSystem(game=self.game, lane=lane, system=a_sys) a_link.save() b_link.save() a_sys.neighbors.add(a_link) b_sys.neighbors.add(b_link) lane.systems.add(a_sys) lane.systems.add(b_sys) self.lanes.append(lane) lane.save() if visible: lane.visible_to.add(self.player) def save(self): for system in self.systems.values(): system.save() for lane in self.lanes: lane.save()