Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using MiniJSON;
- using UnityEngine;
- namespace Assets.Scripts.Boss
- {
- public sealed class ConnectionManager : Singleton<ConnectionManager>
- {
- public static event GameEvent LoadDataCompleted;
- //internal Dictionary<string, Dictionary<string, object>> Tops = new Dictionary<string, Dictionary<string, object>>();
- //internal Dictionary<string, Dictionary<string, object>> Rewards = new Dictionary<string, Dictionary<string, object>>();
- //internal Dictionary<string, Dictionary<string, object>> Damages = new Dictionary<string, Dictionary<string, object>>();
- //internal Dictionary<string, int> Times = new Dictionary<string, int>();
- internal MyTime DateTime;
- internal Reward MyReward;
- internal List<Reward> SeasonRewards = new List<Reward>();
- internal List<int> BonusMutiplier;
- public List<Person> PersonsTopList = new List<Person>();
- public Person MySelf;
- public static bool IsConnected
- {
- get;
- set;
- }
- public float MaxTimePending = 5f;
- private ConnectionManager()
- {
- BonusMutiplier = new List<int>();
- PersonsTopList = new List<Person>();
- }
- public void CheckConnection(string url, CallBackFunction callback)
- {
- StartCoroutine(Connect(url, callback));
- }
- public void OnCheckConnection(string message)
- {
- if (IsConnected)
- {
- OnConnectSuccessful("Connected ");
- }
- else
- {
- OnConnectFailed("Not Connected");
- }
- }
- public void OnConnectSuccessful(string message)
- {
- Debug.Log("OnConnectSuccessful");
- }
- public void OnConnectFailed(string message)
- {
- Debug.Log("OnConnectFailded");
- }
- public void SynsDataToServer(string url, CallBackFunction callBack)
- {
- StartCoroutine(Connect(url, callBack));
- }
- public void SynsDataFromServer(string url, CallBackFunction callBack)
- {
- Debug.Log("SynsDataFromServer");
- StartCoroutine(Connect(url, callBack));
- }
- public void OnSynsDataFromServer(string message)
- {
- Debug.Log(message);
- ParseData(message);
- }
- public void OnSynsDataToServer(string message)
- {
- Debug.Log("SynsDataToServer");
- }
- public IEnumerator Connect(string url, CallBackFunction callBack)
- {
- WWW www = new WWW(url);
- float timer = Time.timeSinceLevelLoad;
- bool failed = false;
- while (!www.isDone)
- {
- if (timer > MaxTimePending)
- {
- failed = true;
- break;
- }
- timer += Time.deltaTime;
- yield return null;
- }
- if (failed)
- {
- www.Dispose();
- IsConnected = false;
- }
- else
- {
- if (string.IsNullOrEmpty(www.error))
- {
- IsConnected = true;
- callBack(www.text);
- }
- else
- {
- // fireEventConnectFail();
- }
- }
- }
- void ParseData(string data)
- {
- Debug.Log("Parse Data");
- var dict = Json.Deserialize(data) as Dictionary<string, object>;
- if (dict == null) return;
- var listTops = dict["top"] as List<object>;
- if (listTops == null) return;
- foreach (var dictTop in listTops.Select(top => top as Dictionary<string, object>))
- {
- //if (!Tops.ContainsKey(listTops.IndexOf(dictTop).ToString()))
- //{
- // Tops.Add(listTops.IndexOf(dictTop).ToString(), dictTop);
- //}
- //else
- //{
- // Tops[listTops.IndexOf(dictTop).ToString()] = dictTop;
- //}
- Person p = new Person();
- p.Id = dictTop["id"].ToString();
- p.Name = dictTop["name"].ToString();
- p.Damage = int.Parse(dictTop["damage"].ToString());
- PersonsTopList.Add(p);
- }
- var dictSelf = dict["self"] as Dictionary<string, object>;
- if (dictSelf != null)
- {
- MySelf = new Person();
- MySelf.Id = dictSelf["id"].ToString();
- MySelf.Damage = int.Parse(dictSelf["damage"].ToString());
- MySelf.Name = dictSelf["name"].ToString();
- MySelf.Rank = int.Parse(dictSelf["stt"].ToString());
- MySelf.Turn = int.Parse(dictSelf["turn"].ToString());
- Debug.Log(MySelf.Turn);
- }
- var dictTour = dict["timetournament"] as Dictionary<string, object>;
- if (dictTour != null)
- {
- DateTime = new MyTime(int.Parse(dictTour["day"].ToString()), int.Parse(dictTour["hour"].ToString()),
- int.Parse(dictTour["minute"].ToString()), int.Parse(dictTour["second"].ToString()));
- //if (!Times.ContainsKey("day"))
- //{
- // Times.Add("day", int.Parse(dictTour["day"].ToString()));
- //}
- //else
- //{
- // Times["day"] = int.Parse(dictTour["day"].ToString());
- //}
- //if (!Times.ContainsKey("hour"))
- //{
- // Times.Add("hour", int.Parse(dictTour["hour"].ToString()));
- //}
- //else
- //{
- // Times["hour"] = int.Parse(dictTour["hour"].ToString());
- //}
- //if (!Times.ContainsKey("minute"))
- //{
- // Times.Add("minute", int.Parse(dictTour["minute"].ToString()));
- //}
- //else
- //{
- // Times["minute"] = int.Parse(dictTour["minute"].ToString());
- //}
- //if (!Times.ContainsKey("second"))
- //{
- // Times.Add("second", int.Parse(dictTour["second"].ToString()));
- //}
- //else
- //{
- // Times["second"] = int.Parse(dictTour["second"].ToString());
- //}
- }
- var listReward = dict["reward"] as List<object>;
- if (listReward == null) return;
- Boss b = null;
- Diamond d = null;
- Food f = null;
- Soul s = null;
- foreach (var dictTop in listReward.Select(reward => reward as Dictionary<string, object>))
- {
- // Debug.Log(dictTop["id"].ToString() + long.Parse(dictTop["damage"].ToString()) + dictTop["name"].ToString());
- if (listReward.IndexOf(dictTop) == 0)
- {
- b = new Boss(dictTop["id"].ToString(), dictTop["name"].ToString(), int.Parse(dictTop["amount"].ToString()));
- }
- if (listReward.IndexOf(dictTop) == 1)
- {
- d = new Diamond(dictTop["id"].ToString(), dictTop["name"].ToString(), int.Parse(dictTop["amount"].ToString()));
- }
- if (listReward.IndexOf(dictTop) == 2)
- {
- f = new Food(dictTop["id"].ToString(), dictTop["name"].ToString(), int.Parse(dictTop["amount"].ToString()));
- }
- if (listReward.IndexOf(dictTop) == 3)
- {
- s = new Soul(dictTop["id"].ToString(), dictTop["name"].ToString(), int.Parse(dictTop["amount"].ToString()));
- }
- //if (!Rewards.ContainsKey(listReward.IndexOf(dictTop).ToString()))
- //{
- // Rewards.Add(listReward.IndexOf(dictTop).ToString(), dictTop);
- //}
- //else
- //{
- // Rewards[listReward.IndexOf(dictTop).ToString()] = dictTop;
- //}
- }
- MyReward = new Reward(b, d, s, f);
- BonusMutiplier = new List<int>();
- var listMutiplier = dict["bonusmultiplier"] as List<object>;
- for (var i = 0; i < listMutiplier.Count; i++)
- {
- BonusMutiplier.Add(Int16.Parse((i.ToString(CultureInfo.InvariantCulture))));
- }
- var seasonReward = dict["seasonreward"] as List<object>;
- // seven level reward
- if (seasonReward == null) return;
- foreach (var season in seasonReward)
- {
- var dictSeason = season as List<object>;
- if (dictSeason.Count == 4)
- {
- for (int i = 0; i < dictSeason.Count; i++)
- {
- var dicts = dictSeason[i] as Dictionary<string, object>;
- if (i == 0)
- {
- b = new Boss(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- if (i == 1)
- {
- d = new Diamond(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- if (i == 2)
- {
- f = new Food(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- if (i == 3)
- {
- s = new Soul(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- }
- SeasonRewards.Add(new Reward(b, d, s, f));
- }
- if (dictSeason.Count == 3)
- {
- for (int i = 0; i < dictSeason.Count; i++)
- {
- var dicts = dictSeason[i] as Dictionary<string, object>;
- b = null;
- if (i == 0)
- {
- d = new Diamond(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- if (i == 1)
- {
- f = new Food(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- if (i == 2)
- {
- s = new Soul(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- }
- SeasonRewards.Add(new Reward(b, d, s, f));
- }
- if (dictSeason.Count == 2)
- {
- for (int i = 0; i < dictSeason.Count; i++)
- {
- var dicts = dictSeason[i] as Dictionary<string, object>;
- b = null;
- d = null;
- if (i == 0)
- {
- f = new Food(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- if (i == 1)
- {
- s = new Soul(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
- }
- }
- SeasonRewards.Add(new Reward(b, d, s, f));
- }
- }
- Debug.Log(SeasonRewards.Count);
- if (LoadDataCompleted != null) LoadDataCompleted.Invoke();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment