def info_towers_type_id_get(type_id): """ info_towers_type_id_get Gets type details for a specific tower with `type_id`. :param type_id: Type of the item that needs fetched. :type type_id: int :rtype: TowerInfo """ session = info_map.Session() q = session.query(info_map.Tower).filter(info_map.Tower.type == type_id) q_tower = q.one_or_none() if q_tower is None: error = Error('Type {} Not Found'.format(type_id)) return error, 404 else: tower = TowerInfo(type=q_tower.type, fuel_bay=q_tower.fuel_bay, stront_bay=q_tower.stront_bay, name=q_tower.name, storage_mult=q_tower.storage_mult, cpu=q_tower.cpu, powergrid=q_tower.powergrid, fuel_usage=q_tower.fuel_usage, stront_usage=q_tower.stront_usage, fuel_type=q_tower.fuel_type) return tower, 200
def info_equipment_type_id_get(type_id): """ ... Gets information about a specific silo or reactor of type `type_id`. :param type_id: Type of the item that needs fetched. :type type_id: float :rtype: EquipmentInfo """ session = info_map.Session() q = session.query( info_map.Equipment).filter(info_map.Equipment.type == type_id) q_equipment = q.one_or_none() if q_equipment is not None: equipment = EquipmentInfo( type=q_equipment.type, name=q_equipment.name, group=q_equipment.group_id, capacity=q_equipment.capacity, fitting=EquipmentInfoFitting(cpu=q_equipment.cpu, powergrid=q_equipment.powergrid), allowed_groups=[g.group_id for g in q_equipment.groups]) return equipment else: error = Error('Type {} Not Found'.format(type_id)) return error, 404
def info_towers_get(): """ info_towers_get Gets **array** of all tower information. :rtype: List[TowerInfo] """ session = info_map.Session() q = session.query(info_map.Tower) q_towers = q.all() towers = [] for q_tower in q_towers: tower = TowerInfo(type=q_tower.type, fuel_bay=q_tower.fuel_bay, stront_bay=q_tower.stront_bay, name=q_tower.name, storage_mult=q_tower.storage_mult, cpu=q_tower.cpu, powergrid=q_tower.powergrid, fuel_usage=q_tower.fuel_usage, stront_usage=q_tower.stront_usage, fuel_type=q_tower.fuel_type) towers.append(tower) return towers, 200
def fromId(cls, process_id): qEquipmentInfo = info_map.Equipment info_session = info_map.Session() storage_types = [ e.type for e in info_session.query(qEquipmentInfo).filter( qEquipmentInfo.group_id == 404).all() ] nodes = {} qProcess = application_map.Process session = application_map.Session() q_process = session.query(qProcess).filter( qProcess.id == process_id).one() for q_equipment in q_process.equipment: if q_equipment.type in storage_types: Node = StorageNode else: Node = ProductionNode nodes[q_equipment.id] = Node( id=q_equipment.id, children=[l.source for l in q_equipment.inputs], parents=[l.target for l in q_equipment.outputs]) # properly link nodes for node_id in nodes: node = nodes[node_id] node.children = [nodes[id] for id in node.children] node.parents = [nodes[id] for id in node.parents] return cls(equipment=[nodes[id] for id in nodes])
def resource_allowed(equipment_type, resource_type): qEquipment = info_map.Equipment qGroup = info_map.Group qMaterial = info_map.Material qReaction = info_map.Reaction session = info_map.Session() try: q_equipment = session.query(qEquipment).filter( qEquipment.type == equipment_type).one() except NoResultFound: raise EquipmentNotFound( 'Equipment `{}` not valid.'.format(equipment_type)) q_material = session.query(qMaterial).filter( qMaterial.type == resource_type).one_or_none() q_reaction = session.query(qReaction).filter( qReaction.type == resource_type).one_or_none() if q_material is None: if q_reaction is None: raise ResourceNotFound( 'Resource `{}` not valid.'.format(resource_type)) else: q_resource = q_reaction else: q_resource = q_material if q_equipment.groups.filter( qGroup.group_id == q_resource.group_id).one_or_none() is None: return False else: return True
def _reaction_by_group(*group_ids): """ _reaction_by_group NOTE: Helper Function for other controllers, not independent. Gets list of `Reaction` objects from the reaction and reaction_io tables by matching group_ids. Search multiple groups by including them as extra parameters: >>> _reaction_by_group(662,484) If no group is supplied, all reactions are returned. :param group_ids: Group Ids of the item that needs fetched. :type group_ids: integer :rtype: List[Reaction] """ session = info_map.Session() reactions = [] if len(group_ids) == 0: q = session.query(info_map.Reaction) else: q = session.query(info_map.Reaction).filter( info_map.Reaction.group_id.in_(group_ids)) q_reactions = q.all() if len(q_reactions) == 0: return None for q_reaction in q_reactions: outputs = [] inputs = [] for row in q_reaction.materials: reaction_mat = ReactionMaterial(type=row.material_id, name=row.material.name, amount=row.quantity) if row.is_input: inputs.append(reaction_mat) else: outputs.append(reaction_mat) reaction = Reaction(type=q_reaction.type, name=q_reaction.name, inputs=inputs, outputs=outputs) reactions.append(reaction) return reactions
def info_materials_groups_get(): """ info_materials_groups_get Get **array** of information for all materials, or if an array of `type_ids` is included, information on only those materials. :rtype: List[Group] """ session = info_map.Session() mat = aliased(info_map.Material) grp = aliased(info_map.Group) q = session.query(mat.group_id, grp.name).join(grp).distinct() groups = [Group(group=row.group_id, name=row.name) for row in q.all()] return groups, 200
def _equipment_by_group(*group_ids): """ _reaction_by_group NOTE: Helper Function for other controllers, not independent. Gets list of `Equipment` objects from the reaction and reaction_io tables by matching group_ids. Search multiple groups by including them as extra parameters: >>> _equipment_by_group(662,484) If no group is supplied, all equipment are returned. :param group_ids: Group Ids of the item that needs fetched. :type group_ids: integer :rtype: List[Equipment] """ session = info_map.Session() equipment_list = [] if len(group_ids) == 0: q = session.query(info_map.Equipment) else: q = session.query(info_map.Equipment).filter( info_map.Equipment.group_id.in_(group_ids)) q_equipment_list = q.all() if len(q_equipment_list) == 0: return None for q_equipment in q_equipment_list: equipment = EquipmentInfo( type=q_equipment.type, name=q_equipment.name, group=q_equipment.group_id, capacity=q_equipment.capacity, fitting=EquipmentInfoFitting(cpu=q_equipment.cpu, powergrid=q_equipment.powergrid), allowed_groups=[g.group_id for g in q_equipment.groups]) equipment_list.append(equipment) return equipment_list
def _material_by_group(*group_ids): """ _material_by_group NOTE: Helper Function for other controllers, not independent. Gets list of `MaterialInfo` objects from the material table by matching group_ids. Search multiple groups by including them as extra parameters: >>> _material_by_group(428, 18) If no group is supplied, all materials are returned. :param group_ids: Group Ids of the item that needs fetched. :type group_ids: integer :rtype: List[MaterialInfo] """ session = info_map.Session() if len(group_ids) > 0: q = session.query(info_map.Material).filter( info_map.Material.group_id.in_(group_ids)) else: q = session.query(info_map.Material) mats = q.all() # return all rows from result if len(mats) == 0: return None materials = [] for mat in mats: materials.append( MaterialInfo(type=mat.type, group=mat.group_id, name=mat.name, volume=mat.volume)) return materials
def _reaction_by_type(type_id): """ _reaction_by_type NOTE: Helper Function for other controllers, not independent. Gets a `Reaction` object from the material table by matching type_id. :param group_ids: Group Ids of the item that needs fetched. :type group_ids: integer :rtype: List[Reaction] """ session = info_map.Session() q = session.query( info_map.Reaction).filter(info_map.Reaction.type == type_id) q_reaction = q.one_or_none() if q_reaction is None: return None outputs = [] inputs = [] for row in q_reaction.materials: reaction_mat = ReactionMaterial(type=row.material_id, name=row.material.name, amount=row.quantity) if row.is_input: inputs.append(reaction_mat) else: outputs.append(reaction_mat) reaction = Reaction(type=q_reaction.type, name=q_reaction.name, inputs=inputs, outputs=outputs) return reaction
def info_materials_type_id_get(type_id): """ info_materials_type_id_get Gets type details for a specific item with `type_id`. :param type_id: Type of the item that needs fetched. :type type_id: int :rtype: MaterialInfo """ session = info_map.Session() q = session.query( info_map.Material).filter(info_map.Material.type == type_id) mat = q.one_or_none() # return the only result or `None` if mat is not None: material_info = MaterialInfo(type=mat.type, group=mat.group_id, name=mat.name, volume=mat.volume) return material_info, 200 else: error = Error('Type {} Not Found'.format(type_id)) return error, 404
def available_links(equipment_id, outputs=True): qEquipment = application_map.Equipment qEquipmentInfo = info_map.Equipment qMaterial = info_map.Material qReaction = info_map.Reaction session = application_map.Session() info_session = info_map.Session() q_equipment = session.query(qEquipment).filter( qEquipment.id == equipment_id).one() # special handle for mining arrays (unlike silos they have no inputs) mining_equipment_type = info_session.query(qEquipmentInfo).filter( qEquipmentInfo.name == 'Moon Harvesting Array') if q_equipment.type == mining_equipment_type: if outputs: return [q_equipment.resource] else: return [] resource = q_equipment.resource if resource is None: return [] else: q_reaction = info_session.query(qReaction).filter( qReaction.type == resource).one_or_none() if q_reaction is None: q_material = info_session.query(qMaterial).filter( qMaterial.type == resource).one() return [resource] else: if outputs: return [m.material_id for m in q_reaction.outputs] else: return [m.material_id for m in q_reaction.inputs]
def towers_tower_id_processes_process_id_equipment_post( tower_id, process_id, equipment, api_key): """ towers_tower_id_processes_process_id_equipment_post :param tower_id: Tower Id number to look under. :type tower_id: int :param process_id: Process Id number to look under. :type process_id: int :param equipment: :type equipment: dict | bytes :param api_key: Operation Access Key :type api_key: str :rtype: Equipment """ if connexion.request.is_json: equipment = Equipment.from_dict(connexion.request.get_json()) try: # check if equipment type is valid and set the default name info_session = info_map.Session() qEquipmentInfo = info_map.Equipment default_name = info_session.query(qEquipmentInfo).filter( qEquipmentInfo.type == equipment.type).one().name except NoResultFound: error = Error('Equipment of type `{}` not valid.'.format( equipment.type)) return error, 400 qEquipment = application_map.Equipment qTower = application_map.Tower qProcess = application_map.Process session = application_map.Session() # find the tower q_tower = session.query(qTower).filter(qTower.id == tower_id).one_or_none() if q_tower is None or q_tower.op_id != api_key.operation_id: error = Error('Tower `{}` not Found'.format(tower_id)) return error, 404 q_process = q_tower.processes.filter( qProcess.id == process_id).one_or_none() if q_process is None or q_process.id != process_id: error = Error('Process `{}` not Found'.format(process_id)) return error, 404 # validate resource if equipment.resource is not None: try: if not resource_allowed(equipment.type, equipment.resource): error = Error( 'Resource `{}` not allowed in equipment of type `{}`.'. format(equipment.resource, equipment.type)) return error, 400 except EquipmentNotFound as e: error = Error(message=str(e)) return error, 400 except ResourceNotFound as e: error = Error(message=str(e)) return error, 400 # set defaults if values not included equipment.name = equipment.name or default_name equipment.contains = equipment.contains or 0 equipment.online = equipment.online or False equipment.last_updated = int(time.time()) equipment.inputs = [] equipment.outputs = [] q_equipment = qEquipment(last_updated=equipment.last_updated, resource=equipment.resource, contains=equipment.contains, type=equipment.type, name=equipment.name, online=equipment.online) q_process.equipment.append(q_equipment) session.commit() q_equipment.id += random.randint( 0, 49) # add some uncertainty so that id is not directly count equipment.id = q_equipment.id session.commit() return equipment, 200