public static IEnumerator RemoveItemsFromClusterio(string bodyName, Dictionary <string, int> clusterioInventory, string itemName, int count, Action <bool> onFinished = null) { if (!BodyHasServer(bodyName)) { Debug.Log(String.Format("XenoIndustrySignpost: Sending a RemoveItemsFromClusterio request to a celestial {0} which has no assigned server!", bodyName)); yield break; } XenoIndustrySignpostBodyAddress address = bodyAddresses[bodyName]; // Connect to the server if we aren't connected yet if (!ClusterioConnector.IsConnected(address.masterIP, address.masterPort)) { ClusterioConnector.ConntectToMaster(address.masterIP, address.masterPort, address.masterAuthToken); } // First, we need to see what items are stored in the Clusterio server, and wait for that request to be finished yield return(XenoIndustryCore.instance.StartCoroutine(XenoIndustrySignpost.GetClusterioInventory(bodyName, clusterioInventory))); // Don't do a remove request if there's not enough items, or if the item isn't there at all if (!clusterioInventory.ContainsKey(itemName) || clusterioInventory[itemName] < count) { yield break; } Dictionary <string, string> sendValues = new Dictionary <string, string>(); sendValues["name"] = itemName; sendValues["count"] = count.ToString(); Debug.Log(String.Format("ClusterioUtil: removing {0} of {1} from item repository", count, itemName)); string apiCommand = "/api/remove"; ClusterioMessage resultMessage = new ClusterioMessage(); yield return(ClusterioConnector.SendPostRequest(address.masterIP, address.masterPort, apiCommand, resultMessage, sendValues)); if (resultMessage.result == ClusterioMessageResult.SUCCESS) { if (onFinished != null) { onFinished(true); } } else { if (onFinished != null) { onFinished(false); } } }
public static IEnumerator GetClusterioInventory(string bodyName, Dictionary <string, int> clusterioInventory) { if (!BodyHasServer(bodyName)) { Debug.Log(String.Format("XenoIndustrySignpost: Sending a GetClusterioInventory request to a celestial {0} which has no assigned server!", bodyName)); yield break; } XenoIndustrySignpostBodyAddress address = bodyAddresses[bodyName]; // Connect to the server if we aren't connected yet if (!ClusterioConnector.IsConnected(address.masterIP, address.masterPort)) { ClusterioConnector.ConntectToMaster(address.masterIP, address.masterPort, address.masterAuthToken); } string apiRequest = "/api/inventory"; ClusterioMessage resultMessage = new ClusterioMessage(); yield return(ClusterioConnector.SendGetRequest(address.masterIP, address.masterPort, apiRequest, resultMessage)); if (resultMessage.result == ClusterioMessageResult.SUCCESS) { // Inventory request successful, refresh the inventory clusterioInventory.Clear(); JSONNode rootNode = JSON.Parse(resultMessage.text); foreach (JSONNode childNode in rootNode.Children) { if (childNode["name"] == null) { Debug.Log("GetClusterioInventory: an item is missing its name!"); continue; } if (childNode["count"] == null) { Debug.Log(String.Format("GetClusterioInventory: item {0} is missing its count number!", childNode["name"])); continue; } clusterioInventory[childNode["name"]] = childNode["count"].AsInt; } } }
public static void RefreshConnection(string bodyName) { if (!BodyHasServer(bodyName)) { return; } XenoIndustrySignpostBodyAddress address = bodyAddresses[bodyName]; if (!ClusterioConnector.IsConnected(address.masterIP, address.masterPort)) { ClusterioConnector.ConntectToMaster(address.masterIP, address.masterPort, address.masterAuthToken); } else { ClusterioConnector.RefreshConnectionFromAddress(address.masterIP, address.masterPort); } }
public static IEnumerator AddItemsToClusterio(string bodyName, string itemName, int count, Action <bool> onFinished = null) { if (!BodyHasServer(bodyName)) { Debug.Log(String.Format("XenoIndustrySignpost: Sending a AddItemsToClusterio request to a celestial {0} which has no assigned server!", bodyName)); yield break; } XenoIndustrySignpostBodyAddress address = bodyAddresses[bodyName]; // Connect to the server if we aren't connected yet if (!ClusterioConnector.IsConnected(address.masterIP, address.masterPort)) { ClusterioConnector.ConntectToMaster(address.masterIP, address.masterPort, address.masterAuthToken); } Dictionary <string, string> sendValues = new Dictionary <string, string>(); sendValues["name"] = itemName; sendValues["count"] = count.ToString(); Debug.Log(String.Format("ClusterioUtil: adding {0} of {1} to item repository", count, itemName)); string apiCommand = "/api/place"; ClusterioMessage resultMessage = new ClusterioMessage(); yield return(ClusterioConnector.SendPostRequest(address.masterIP, address.masterPort, apiCommand, resultMessage, sendValues)); if (resultMessage.result == ClusterioMessageResult.SUCCESS) { if (onFinished != null) { onFinished(true); } } else { if (onFinished != null) { onFinished(false); } } }