def starter_device(data: dict, user: str) -> dict: """ Creates a device for starters :param data: The given data :param user: The user uuid. :return: the response """ count: int = wrapper.session.query(func.count( Device.uuid)).filter_by(owner=user).scalar() if count > 0: return already_own_a_device performance: tuple = calculate_power(hardware["start_pc"]) device: Device = Device.create_starter_device(user, True) Workload.create(device.uuid, performance) create_hardware(hardware["start_pc"], device.uuid) m.contact_microservice("service", ["device_init"], { "device_uuid": device.uuid, "user": device.owner }) return device.serialize
def update_miner(miner: Miner): mined_coins: int = miner.update_miner() if mined_coins > 0: m.contact_microservice( "currency", ["put"], { "destination_uuid": miner.wallet, "amount": mined_coins, "create_transaction": False })
def delete_items(user: str, elements: dict): for element_type in ("mainboard", "powerPack", "case"): m.contact_microservice("inventory", ["inventory", "delete_by_name"], { "owner": user, "item_name": elements[element_type] }) for element_type in ("cpu", "gpu", "processorCooler", "disk", "ram"): for element in elements[element_type]: m.contact_microservice("inventory", ["inventory", "delete_by_name"], { "owner": user, "item_name": element })
def scale_resources(s: List[Service], scale: Tuple[float, float, float, float, float]): for service in s: send: dict = { "service_uuid": service.service_uuid, "cpu": scale[0] * service.allocated_cpu, "ram": scale[1] * service.allocated_ram, "gpu": scale[2] * service.allocated_gpu, "disk": scale[3] * service.allocated_disk, "network": scale[4] * service.allocated_network, } m.contact_microservice("service", ["hardware", "scale"], send)
def change_miner_power(power: float, service_uuid: str, device_uuid: str, user: str) -> float: stop_service(device_uuid, service_uuid, user) microservice_response = m.contact_microservice( "device", ["hardware", "register"], { "user": user, "service_uuid": service_uuid, "device_uuid": device_uuid, "cpu": config["services"]["miner"]["needs"]["cpu"] * power, "ram": config["services"]["miner"]["needs"]["ram"] * power, "gpu": config["services"]["miner"]["needs"]["gpu"] * power, "disk": config["services"]["miner"]["needs"]["disk"] * power, "network": config["services"]["miner"]["needs"]["network"] * power, }, ) if "error" in microservice_response: return -1 given_per: Tuple[float, float, float, float, float] = game_content.dict2tuple(microservice_response) expected_per: Tuple[float, float, float, float, float] = game_content.dict2tuple( config["services"]["miner"]["needs"]) return config["services"]["miner"]["speedm"](expected_per, given_per)
def check_access(self, user: str) -> bool: """ Check if the uuid has access to this device :param user: :return: The permission """ if user == self.owner: return True return m.contact_microservice("service", ["check_part_owner"], {"user_uuid": user, "device_uuid": self.uuid})[ "ok" ]
def create_device(data: dict, user: str) -> dict: """ Create a device. :param data: The given data. :param user: The user uuid. :return: The response """ count: int = wrapper.session.query(func.count( Device.uuid)).filter_by(owner=user).scalar() if count >= 3: return maximum_devices_reached comp, message = check_compatible(data) if not comp: return message comp, message = check_exists(user, data) if not comp: return message performance: tuple = calculate_power(data) device: Device = Device.create(user, True) Workload.create(device.uuid, performance) create_hardware(data, device.uuid) delete_items(user, data) m.contact_microservice("service", ["device_init"], { "device_uuid": device.uuid, "user": device.owner }) return device.serialize
def delete_device(data: dict, user: str, device: Device) -> dict: """ Delete a device. :param data: The given data. :param user: The user uuid. :param device: The device. :return: Success or not """ if device.owner != user: return permission_denied if device.starter_device: return device_is_starter_device stop_all_service(device.uuid, delete=True) delete_services(device.uuid) # Removes all Services in MS_Service delete_files(device.uuid) # remove all files for hw in wrapper.session.query(Hardware).filter_by( device_uuid=device.uuid): m.contact_microservice( "inventory", ["inventory", "create"], { "item_name": hw.hardware_element, "owner": device.owner, "related_ms": "device" }, ) wrapper.session.delete(hw) device: Device = wrapper.session.query(Device).get(device.uuid) wrapper.session.delete(device) wrapper.session.commit() return success
def power(data: dict, user: str, device: Device) -> dict: """ Turn a device on/off. :param data: The given data. :param user: The user uuid. :param device: The device. :return: The response """ device_uuid: str = data["device_uuid"] device.powered_on = not device.powered_on wrapper.session.commit() if not device.powered_on: stop_all_service(device_uuid) stop_services(device_uuid) else: m.contact_microservice("service", ["device_restart"], { "device_uuid": device_uuid, "user": device.owner }) return device.serialize
def pay_shop(wallet: str, key: str, amount: int, products: Dict[str, int]) -> dict: products: str = ", ".join(f"{quantity}x {product}" for product, quantity in products.items()) return m.contact_microservice( "currency", ["dump"], { "source_uuid": wallet, "key": key, "amount": amount, "create_transaction": True, "destination_uuid": "00000000-0000-0000-0000-000000000000", "usage": f"Payment for {products}", "origin": 1, }, )
def check_exists(user: str, elements: dict) -> Tuple[bool, dict]: inventory: dict = m.contact_microservice("inventory", ["inventory", "summary"], {"owner": user})["elements"] def check(name: str) -> bool: if inventory.get(name, 0) > 0: inventory[name] -= 1 return True return False for element_type in ("mainboard", "powerPack", "case"): if not check(elements[element_type]): return False, {"error": f"{element_type}_not_in_inventory"} for element_type in ("cpu", "gpu", "processorCooler", "disk", "ram"): for element in elements[element_type]: if not check(element): return False, {"error": f"{element_type}_not_in_inventory"} return True, {}
def register_service(device_uuid: str, service_uuid: str, name: str, user: str) -> float: r_data: dict = {"device_uuid": device_uuid, "service_uuid": service_uuid} microservice_response: dict = m.contact_microservice( "device", ["hardware", "register"], { **r_data, **config["services"][name]["needs"], "user": user }) if "error" in microservice_response: return -1 given_per: Tuple[float, float, float, float, float] = game_content.dict2tuple(microservice_response) expected_per: Tuple[float, float, float, float, float] = game_content.dict2tuple( config["services"][name]["needs"]) return config["services"][name]["speedm"](expected_per, given_per)
def get_wallet_owner(wallet_uuid: str) -> Optional[str]: return m.contact_microservice("currency", ["owner"], { "source_uuid": wallet_uuid }).get("owner")
def get_device_owner(device: str) -> str: return m.contact_microservice("device", ["owner"], { "device_uuid": device }).get("owner")
def check_device_online(device: str) -> bool: return m.contact_microservice("device", ["ping"], { "device_uuid": device }).get("online", False)
def stop_service(device_uuid: str, service_uuid: str, user: str): m.contact_microservice("device", ["hardware", "stop"], { "device_uuid": device_uuid, "service_uuid": service_uuid, "user": user })
def delete_services(device_uuid: str) -> None: m.contact_microservice("service", ["hardware", "delete"], {"device_uuid": device_uuid})
def stop_services(device_uuid: str) -> None: m.contact_microservice("service", ["hardware", "stop"], {"device_uuid": device_uuid})
def exists_wallet(wallet: str) -> bool: return m.contact_microservice("currency", ["exists"], {"source_uuid": wallet})["exists"]
def exists_device(device: str) -> bool: return m.contact_microservice("device", ["exist"], {"device_uuid": device})["exist"]