def add_squad(): data = request.json['data'] points = request.json['points'] faction = request.json['faction'] tourney_id = request.args.get('tourney_id') tourney_list_id = request.args.get('tourney_list_id') pm = PersistenceManager(myapp.db_connector) tourney_list = pm.get_tourney_list(tourney_list_id) tourney_list.faction = Faction.from_string( faction ) tourney_list.points = points ships = [] for squad_member in data: ship_pilot = pm.get_ship_pilot( squad_member['ship'], squad_member['pilot'] ) ship = Ship( ship_pilot_id=ship_pilot.id, tlist_id=tourney_list.id) tourney_list.ships.append( ship ) for upgrade in squad_member['upgrades']: upgrade = pm.get_upgrade(upgrade['type'], upgrade['name']) ship_upgrade = ShipUpgrade( ship_id=ship.id, upgrade=upgrade ) ship.upgrades.append( ship_upgrade ) ships.append( ship ) tourney_list.generate_hash_key() pm.db_connector.get_session().add_all( ships ) pm.db_connector.get_session().commit() return jsonify(tourney_id=tourney_id, tourney_list_id=tourney_list.id)
def convert(self, pm, tourney_list): xws = self.xws name = None if xws.has_key( name ): name = xws['name'] faction = xws['faction'] pilots = xws['pilots'] tourney_list.name = name tourney_list.faction = Faction.from_string(faction) points = 0 for ship_pilot in pilots: ship = ship_pilot['ship'] pilot = ship_pilot['name'] sp = pm.get_canonical_ship_pilot( ship, pilot ) if sp is None: raise Exception("xws lookup failed for ship " + ship + ", pilot " + pilot ) points = points + sp.pilot.cost ship = Ship( ship_pilot_id=sp.id, tlist_id=tourney_list.id) tourney_list.ships.append( ship ) if ship_pilot.has_key('upgrades'): upgrades = ship_pilot['upgrades'] hastiex1 = False #this is a nasty hack but, what can you do for upgrade_type in upgrades.keys(): if upgrade_type == 'title': for title in upgrades[upgrade_type]: if title == 'tiex1': hastiex1 = True break for upgrade_type in upgrades.keys(): if upgrade_type is None or upgrade_type == 'undefined': raise Exception("got undefined upgrade type from xws source!" ) for upgrade_name in upgrades[upgrade_type]: upgrade = pm.get_upgrade_canonical(upgrade_type, upgrade_name) if upgrade is None: raise Exception("xws lookup failed for upgrade " + upgrade_name ) if hastiex1 and upgrade_type=='system': cost = upgrade.cost cost = cost - 4 if cost < 0: cost = 0 points = points + cost else: points = points + upgrade.cost ship_upgrade = ShipUpgrade( ship_id=ship.id, upgrade=upgrade ) ship.upgrades.append( ship_upgrade ) tourney_list.points = points tourney_list.generate_hash_key()
def convert(self, pm, tourney_list=None): xws = self.xws name = None if xws.has_key( name ): name = xws['name'] faction = xws['faction'] pilots = xws['pilots'] #TODO: temporary hack if faction == "rebels": faction = "rebel" if faction == "empire": faction = "imperial" points = 0 ships = [] for ship_pilot in pilots: ship = ship_pilot['ship'] pilot = ship_pilot['name'] sp = pm.get_canonical_ship_pilot( ship, pilot ) if sp is None: raise Exception("xws lookup failed for ship " + ship + ", pilot " + pilot ) points = points + sp.pilot.cost ship = Ship( ship_pilot=sp) ships.append(ship) if ship_pilot.has_key('upgrades'): upgrades = ship_pilot['upgrades'] hastiex1 = False #this is a nasty hack but, what can you do for upgrade_type in upgrades.keys(): if upgrade_type == 'title': for title in upgrades[upgrade_type]: if title == 'tiex1': hastiex1 = True break for upgrade_type in upgrades.keys(): if upgrade_type is None or upgrade_type == 'undefined': raise Exception("got undefined upgrade type from xws source!" ) for upgrade_name in upgrades[upgrade_type]: upgrade = pm.get_upgrade_canonical(upgrade_type, upgrade_name) if upgrade is None: raise Exception("xws lookup failed for upgrade " + upgrade_name ) if hastiex1 and upgrade_type=='system': cost = upgrade.cost cost = cost - 4 if cost < 0: cost = 0 points = points + cost else: points = points + upgrade.cost ship_upgrade = ShipUpgrade( ship=ship, upgrade=upgrade ) #ship.upgrades.append( ship_upgrade ) hashkey = ArchtypeList.generate_hash_key(ships) archtype = pm.get_archtype_by_hashkey(hashkey) first_time_archtype_seen = False if archtype is None: #ding ding! #we've never seen this list before! #note that this quote is a dupe of the add_squads method in xwlists.py #refactor it if we get a third way of adding archtypes first_time_archtype_seen = True archtype = ArchtypeList() archtype.ships = ships for ship in ships: ship.archtype = archtype pm.db_connector.get_session().add(ship) archtype.faction = Faction.from_string( faction ) archtype.points = points archtype.pretty = archtype.pretty_print_list() archtype.hashkey = hashkey pm.db_connector.get_session().add(archtype) pm.db_connector.get_session().commit() if tourney_list is not None: tourney_list.name = name tourney_list.faction = Faction.from_string(faction) tourney_list.archtype = archtype tourney_list.archtype_id = archtype.id pm.db_connector.get_session().add(tourney_list) pm.db_connector.get_session().commit() return archtype, first_time_archtype_seen
def convert(self, pm, tourney_list=None): xws = self.xws name = None if xws.has_key( 'name' ): name = xws['name'] faction = xws['faction'] pilots = xws['pilots'] #TODO: temporary hack if faction == "rebels": faction = "rebel" if faction == "empire": faction = "imperial" points = 0 ships = [] for ship_pilot in pilots: ship = ship_pilot['ship'] pilot = ship_pilot['name'] sp = pm.get_canonical_ship_pilot( ship, pilot ) if sp is None: raise Exception("xws lookup failed for ship " + ship + ", pilot " + pilot ) points = points + sp.pilot.cost ship = Ship( ship_pilot=sp) ships.append(ship) if ship_pilot.has_key('upgrades'): upgrades = ship_pilot['upgrades'] hastiex1 = False #this is a nasty hack but, what can you do for upgrade_type in upgrades.keys(): if upgrade_type == 'title': for title in upgrades[upgrade_type]: if title == 'tiex1': hastiex1 = True break for upgrade_type in upgrades.keys(): if upgrade_type is None or upgrade_type == 'undefined': raise Exception("got undefined upgrade type from xws source!" ) for upgrade_name in upgrades[upgrade_type]: upgrade = pm.get_upgrade_canonical(upgrade_type, upgrade_name) if upgrade is None: raise Exception("xws lookup failed for upgrade " + upgrade_name ) if hastiex1 and upgrade_type=='system': cost = upgrade.cost cost = cost - 4 if cost < 0: cost = 0 points = points + cost else: points = points + upgrade.cost ship_upgrade = ShipUpgrade( ship=ship, upgrade=upgrade ) hashkey = ArchtypeList.generate_hash_key(ships) archtype = pm.get_archtype_by_hashkey(hashkey) first_time_archtype_seen = False if archtype is None: #ding ding! #we've never seen this list before! #note that this quote is a dupe of the add_squads method in xwlists.py #refactor it if we get a third way of adding archtypes first_time_archtype_seen = True archtype = ArchtypeList() archtype.ships = ships for ship in ships: ship.archtype = archtype pm.db_connector.get_session().add(ship) archtype.faction = Faction.from_string( faction ) archtype.points = points archtype.pretty = archtype.pretty_print_list() archtype.hashkey = hashkey pm.db_connector.get_session().add(archtype) pm.db_connector.get_session().commit() if tourney_list is not None: tourney_list.name = name tourney_list.faction = Faction.from_string(faction) tourney_list.archtype = archtype tourney_list.archtype_id = archtype.id pm.db_connector.get_session().add(tourney_list) pm.db_connector.get_session().commit() return archtype, first_time_archtype_seen