def begin_pod_research(args, pod_id): """Begin a new pod research.""" from server.data.data import research_data pod = db.session.query(Pod).get(pod_id) if pod.user_id != g.current_user.id: return bad_request(f"Pod doesn't belong to current user.") # Check for valid research type research_type = args['type'] if research_type not in ResearchTypes.__members__: return bad_request('Unknown research type "{research_type}"') research = db.session.query(Research) \ .filter(Research.pod_id == g.current_user.pod.id) \ .filter(Research.type == research_type) \ .first() next_level = 0 if research: next_level = research.level + 1 highest_queue_entry = db.session.query(QueueEntry) \ .filter(QueueEntry.research == research) \ .join(Queue) \ .filter(Queue.pod == pod) \ .order_by(QueueEntry.level.desc()) \ .first() if highest_queue_entry: next_level = highest_queue_entry.level + 1 # Ensure we didn't reach max level. if next_level >= len(research_data[research_type]['levels']): return bad_request("Max level reached.") research_level = research_data[research_type]['levels'][next_level] # Check if we have enough resources requirements = research_level['resources'] enough, missing = pod.enough_resources(requirements) if not enough: return bad_request(f'Not enough resources: {missing}') # Subtract the resources from the pod and create a queue entry. pod.subtract_resources(requirements) # Create a new research if we don't have it yet. if not research: research = Research(research_type, pod, 0) # Create a new queue entry. queue_entry = QueueEntry(pod.queue, next_level, research_level['duration'], research=research) pod.queue.next_entry() db.session.add(queue_entry) db.session.add(research) db.session.commit() return ok()
def pod_resources(pod_id): """Get the resources of a specified pod.""" pod = db.session.query(Pod).get(pod_id) if pod.user_id != g.current_user.id: return bad_request(f"Pod doesn't belong to current user.") schema = ResourceSchema() return ok(schema.dump(pod.resources, many=True).data)
def get_pod_research(pod_id): """Send the pod meta data combined with your researches.""" pod = db.session.query(Pod).get(pod_id) if pod.user_id != g.current_user.id: return bad_request(f"Pod doesn't belong to current user.") researches = db.session.query(Research) \ .filter(Research.pod == pod) \ .filter(Research.researched.is_(True)) \ .all() schema = ResearchSchema() return ok(schema.dump(researches, many=True).data)
def upgrade_pod_module(pod_id, module_id): """Update a module on the pod grid.""" from server.data.data import module_data pod = db.session.query(Pod).get(pod_id) if pod.user_id != g.current_user.id: return bad_request(f"Pod doesn't belong to current user.") # Check if we already have a module with this type # at the specified position. module = db.session.query(Module).get(module_id) if not module: return bad_request('No module with this id') next_level = module.level + 1 highest_queue_entry = db.session.query(QueueEntry) \ .filter(QueueEntry.module == module) \ .join(Queue) \ .filter(Queue.pod == pod) \ .order_by(QueueEntry.level.desc()) \ .first() if highest_queue_entry: next_level = highest_queue_entry.level + 1 # Ensure we didn't reach max level. if next_level >= len(module_data[module.type]['levels']): return bad_request("Max level reached.") module_level = module_data[module.type]['levels'][next_level] # Ensure we have enough resources requirements = module_level['resources'] enough, missing = pod.enough_resources(requirements) if not enough: return bad_request(f'Not enough resources: {missing}') # Subtract the resources from the pod and create a queue entry. pod.subtract_resources(requirements) queue_entry = QueueEntry( pod.queue, next_level, module_level['duration'], module=module, ) pod.queue.next_entry() db.session.add(queue_entry) db.session.add(module) db.session.commit() return ok()
def remove_queue_entry(pod_id, entry_id): """Remove a specific queue entry.""" pod = db.session.query(Pod).get(pod_id) if pod.user_id != g.current_user.id: return bad_request("Pod doesn't belong to current user.") """Get the queue of the specified pod.""" queue_entry = db.session.query(QueueEntry) \ .join(Queue) \ .filter(Queue.pod == pod) \ .filter(QueueEntry.id == entry_id) \ .one_or_none() if queue_entry is None: return bad_request("Queue entry doesn't exist") db.session.delete(queue_entry) pod.queue.next_entry() db.session.commit() return ok()
def get_research_meta(): """Get the research meta data.""" from server.data.data import research_data return ok(research_data)
def get_pod_modules(pod_id): """Get all modules and their information from a specific pod.""" pod = db.session.query(Pod).get(pod_id) schema = ModuleSchema() return ok(schema.dump(pod.modules, many=True).data)
def get_module_meta(): """Get the meta information about all modules.""" from server.data.data import module_data return ok(module_data)
def pod_queue(pod_id): """Get the queue of the specified pod.""" pod = db.session.query(Pod).get(pod_id) schema = QueueSchema() return ok(schema.dump(pod.queue).data)