private IEnumerator GetPlanetsTransportImpl(Action <List <GeneratorData> > onSuccess, Action <string> onError) { UnityWebRequest request = UnityWebRequest.Get(FullUrl(kPlanetsTransportUrl)); request.SetRequestHeader(authKey, AuthHeader); UnityWebRequestAsyncOperation operation = request.SendWebRequest(); yield return(operation); if (operation.isDone) { if (!operation.webRequest.isHttpError) { try { string result = operation.webRequest.downloadHandler.text; string json = JsonWebToken.Decode(result, secretKey); JObject parent = JObject.Parse(json); JToken arr = parent["response"]["data"]; List <GeneratorData> generators = new List <GeneratorData>(); int generatorId = 10; foreach (JToken token in arr) { GeneratorJsonData jsonData = new GeneratorJsonData { id = generatorId, baseCost = token.Value <double>(1), incrementFactor = token.Value <double>(2), baseGeneration = token.Value <double>(3), timeToGenerate = token.Value <float>(4), //coinPrice = token.Value<int>(5), enhancePrice = token.Value <int>(6), profitIncrementFactor = token.Value <double>(7) }; generators.Add(new GeneratorData(jsonData, GeneratorType.Planet)); generatorId++; } onSuccess?.Invoke(generators); } catch (Exception exception) { UDebug.LogError(exception.Message.Bold()); UDebug.LogError(exception.StackTrace.Bold()); onError?.Invoke($"{exception.Message}{Environment.NewLine}{exception.StackTrace}"); } } else { UDebug.LogError(operation.webRequest.error); onError?.Invoke(operation.webRequest.error); } } else { UDebug.LogError($"operation {nameof(GetPlanetsTransportImpl)} is not done".Bold()); onError?.Invoke($"operation {nameof(GetPlanetsTransportImpl)} is not done"); } }
private void UpdateBalance(string json) { //Debug.Log(json); Services.GetService <IConsoleService>().AddOutput(json, ConsoleTextColor.yellow, true); var obj = JObject.Parse(json); int[] ids = Services.ResourceService.Generators.NormalIds; foreach (int id in ids) { try { if (obj["response"]["data"][id] != null) { var m = obj["response"]["data"][id]; GeneratorJsonData jsonData = new GeneratorJsonData { id = id, baseCost = m[1].ToString().ToDouble(), incrementFactor = m[2].ToString().ToDouble(), baseGeneration = m[3].ToString().ToDouble(), timeToGenerate = m[4].ToString().ToFloat(), //coinPrice = m[5].ToString().ToInt(), enhancePrice = m[6].ToString().ToInt(), profitIncrementFactor = m[7].ToString().ToDouble() }; Services.ResourceService.Generators.ReplaceValues(jsonData); } }catch (System.Exception exception) { Debug.LogError($"error of parsing generator => {id}"); } } /* * foreach (var generator in GameData.instance.generators) * { * if (obj["response"]["data"][generator.Id] != null) * { * var m = obj["response"]["data"][generator.Id]; * generator.BaseCost = m[1].ToString().ToDouble(); * generator.IncrementFactor = m[2].ToString().ToDouble(); * generator.BaseGeneration = m[3].ToString().ToDouble(); * generator.TimeToGenerate = m[4].ToString().ToFloat(); * generator.CoinPrice = m[5].ToString().ToInt(); * generator.EnhancePrice = m[6].ToString().ToInt(); * generator.ProfitIncrementFactor = m[7].ToString().ToDouble(); * * } * }*/ GameEvents.OnGeneratorBalanceLoadedFromNet(); Services.ResourceService.SetGeneratorsLoaded(); }
private static void DownloadPlanetTransport() { WebClient webClient = new WebClient(); webClient.Headers.Add(HttpRequestHeader.Authorization, NetService.AuthHeader); string downloadedString = webClient.DownloadString(NetService.FullUrl(NetService.kPlanetsTransportUrl)); string json = JsonWebToken.Decode(downloadedString, NetService.secretKey); JObject parent = JObject.Parse(json); JToken arr = parent["response"]["data"]; List <GeneratorJsonData> generators = new List <GeneratorJsonData>(); int generatorId = 10; foreach (JToken token in arr) { GeneratorJsonData jsonData = new GeneratorJsonData { id = generatorId, baseCost = token.Value <double>(1), incrementFactor = token.Value <double>(2), baseGeneration = token.Value <double>(3), timeToGenerate = token.Value <float>(4), //coinPrice = token.Value<int>(5), enhancePrice = token.Value <int>(6), profitIncrementFactor = token.Value <double>(7), name = token.Value <string>(0).Trim(), managerIcon = string.Empty }; generatorId++; generators.Add(jsonData); } string serializePath = Path.Combine(Application.dataPath, "Resources/Data/planet_generator.json"); JsonSerializer serializer = new JsonSerializer(); serializer.Formatting = Formatting.Indented; Serialize(serializePath, generators); Debug.Log("planet generators saved..."); EditorUtility.DisplayDialog("Planet generators loaded", $"data saved to {"Resources/Data/planet_generator.json"}", "Ok"); }