thieumao

ConnectionManager.cs

Oct 7th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.33 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using MiniJSON;
  7. using UnityEngine;
  8.  
  9. namespace Assets.Scripts.Boss
  10. {
  11.     public sealed class ConnectionManager : Singleton<ConnectionManager>
  12.     {
  13.  
  14.         public static event GameEvent LoadDataCompleted;
  15.         //internal Dictionary<string, Dictionary<string, object>> Tops = new Dictionary<string, Dictionary<string, object>>();
  16.         //internal Dictionary<string, Dictionary<string, object>> Rewards = new Dictionary<string, Dictionary<string, object>>();
  17.         //internal Dictionary<string, Dictionary<string, object>> Damages = new Dictionary<string, Dictionary<string, object>>();
  18.         //internal Dictionary<string, int> Times = new Dictionary<string, int>();
  19.  
  20.         internal MyTime DateTime;
  21.  
  22.         internal Reward MyReward;
  23.  
  24.         internal List<Reward> SeasonRewards = new List<Reward>();
  25.  
  26.         internal List<int> BonusMutiplier;
  27.  
  28.         public List<Person> PersonsTopList = new List<Person>();
  29.  
  30.         public Person MySelf;
  31.         public static bool IsConnected
  32.         {
  33.             get;
  34.             set;
  35.         }
  36.         public float MaxTimePending = 5f;
  37.  
  38.         private ConnectionManager()
  39.         {
  40.  
  41.             BonusMutiplier = new List<int>();
  42.             PersonsTopList = new List<Person>();
  43.  
  44.  
  45.         }
  46.         public void CheckConnection(string url, CallBackFunction callback)
  47.         {
  48.             StartCoroutine(Connect(url, callback));
  49.         }
  50.  
  51.         public void OnCheckConnection(string message)
  52.         {
  53.             if (IsConnected)
  54.             {
  55.                 OnConnectSuccessful("Connected ");
  56.             }
  57.             else
  58.             {
  59.                 OnConnectFailed("Not Connected");
  60.             }
  61.         }
  62.         public void OnConnectSuccessful(string message)
  63.         {
  64.             Debug.Log("OnConnectSuccessful");
  65.         }
  66.  
  67.         public void OnConnectFailed(string message)
  68.         {
  69.             Debug.Log("OnConnectFailded");
  70.         }
  71.  
  72.         public void SynsDataToServer(string url, CallBackFunction callBack)
  73.         {
  74.             StartCoroutine(Connect(url, callBack));
  75.         }
  76.  
  77.         public void SynsDataFromServer(string url, CallBackFunction callBack)
  78.         {
  79.             Debug.Log("SynsDataFromServer");
  80.             StartCoroutine(Connect(url, callBack));
  81.         }
  82.  
  83.         public void OnSynsDataFromServer(string message)
  84.         {
  85.             Debug.Log(message);
  86.             ParseData(message);
  87.         }
  88.         public void OnSynsDataToServer(string message)
  89.         {
  90.             Debug.Log("SynsDataToServer");
  91.         }
  92.  
  93.         public IEnumerator Connect(string url, CallBackFunction callBack)
  94.         {
  95.             WWW www = new WWW(url);
  96.             float timer = Time.timeSinceLevelLoad;
  97.             bool failed = false;
  98.             while (!www.isDone)
  99.             {
  100.                 if (timer > MaxTimePending)
  101.                 {
  102.                     failed = true;
  103.                     break;
  104.                 }
  105.                 timer += Time.deltaTime;
  106.                 yield return null;
  107.             }
  108.             if (failed)
  109.             {
  110.                 www.Dispose();
  111.                 IsConnected = false;
  112.             }
  113.             else
  114.             {
  115.                 if (string.IsNullOrEmpty(www.error))
  116.                 {
  117.                     IsConnected = true;
  118.                     callBack(www.text);
  119.  
  120.                 }
  121.                 else
  122.                 {
  123.                     // fireEventConnectFail();
  124.                 }
  125.             }
  126.         }
  127.  
  128.         void ParseData(string data)
  129.         {
  130.             Debug.Log("Parse Data");
  131.             var dict = Json.Deserialize(data) as Dictionary<string, object>;
  132.             if (dict == null) return;
  133.             var listTops = dict["top"] as List<object>;
  134.             if (listTops == null) return;
  135.             foreach (var dictTop in listTops.Select(top => top as Dictionary<string, object>))
  136.             {
  137.                 //if (!Tops.ContainsKey(listTops.IndexOf(dictTop).ToString()))
  138.                 //{
  139.                 //    Tops.Add(listTops.IndexOf(dictTop).ToString(), dictTop);
  140.                 //}
  141.                 //else
  142.                 //{
  143.                 //    Tops[listTops.IndexOf(dictTop).ToString()] = dictTop;
  144.                 //}
  145.                 Person p = new Person();
  146.                 p.Id = dictTop["id"].ToString();
  147.                 p.Name = dictTop["name"].ToString();
  148.                 p.Damage = int.Parse(dictTop["damage"].ToString());
  149.                 PersonsTopList.Add(p);
  150.  
  151.             }
  152.             var dictSelf = dict["self"] as Dictionary<string, object>;
  153.             if (dictSelf != null)
  154.             {
  155.                 MySelf = new Person();
  156.                 MySelf.Id = dictSelf["id"].ToString();
  157.                 MySelf.Damage = int.Parse(dictSelf["damage"].ToString());
  158.                 MySelf.Name = dictSelf["name"].ToString();
  159.                 MySelf.Rank = int.Parse(dictSelf["stt"].ToString());
  160.                 MySelf.Turn = int.Parse(dictSelf["turn"].ToString());
  161.                 Debug.Log(MySelf.Turn);
  162.  
  163.             }
  164.             var dictTour = dict["timetournament"] as Dictionary<string, object>;
  165.             if (dictTour != null)
  166.             {
  167.                 DateTime = new MyTime(int.Parse(dictTour["day"].ToString()), int.Parse(dictTour["hour"].ToString()),
  168.                     int.Parse(dictTour["minute"].ToString()), int.Parse(dictTour["second"].ToString()));
  169.                 //if (!Times.ContainsKey("day"))
  170.                 //{
  171.                 //    Times.Add("day", int.Parse(dictTour["day"].ToString()));
  172.                 //}
  173.                 //else
  174.                 //{
  175.                 //    Times["day"] = int.Parse(dictTour["day"].ToString());
  176.                 //}
  177.  
  178.                 //if (!Times.ContainsKey("hour"))
  179.                 //{
  180.                 //    Times.Add("hour", int.Parse(dictTour["hour"].ToString()));
  181.                 //}
  182.                 //else
  183.                 //{
  184.                 //    Times["hour"] = int.Parse(dictTour["hour"].ToString());
  185.                 //}
  186.                 //if (!Times.ContainsKey("minute"))
  187.                 //{
  188.                 //    Times.Add("minute", int.Parse(dictTour["minute"].ToString()));
  189.                 //}
  190.                 //else
  191.                 //{
  192.                 //    Times["minute"] = int.Parse(dictTour["minute"].ToString());
  193.                 //}
  194.                 //if (!Times.ContainsKey("second"))
  195.                 //{
  196.                 //    Times.Add("second", int.Parse(dictTour["second"].ToString()));
  197.                 //}
  198.                 //else
  199.                 //{
  200.                 //    Times["second"] = int.Parse(dictTour["second"].ToString());
  201.                 //}
  202.             }
  203.             var listReward = dict["reward"] as List<object>;
  204.             if (listReward == null) return;
  205.             Boss b = null;
  206.             Diamond d = null;
  207.             Food f = null;
  208.             Soul s = null;
  209.  
  210.             foreach (var dictTop in listReward.Select(reward => reward as Dictionary<string, object>))
  211.             {
  212.  
  213.                 //               Debug.Log(dictTop["id"].ToString() + long.Parse(dictTop["damage"].ToString()) + dictTop["name"].ToString());
  214.                 if (listReward.IndexOf(dictTop) == 0)
  215.                 {
  216.                     b = new Boss(dictTop["id"].ToString(), dictTop["name"].ToString(), int.Parse(dictTop["amount"].ToString()));
  217.  
  218.                 }
  219.                 if (listReward.IndexOf(dictTop) == 1)
  220.                 {
  221.                     d = new Diamond(dictTop["id"].ToString(), dictTop["name"].ToString(), int.Parse(dictTop["amount"].ToString()));
  222.                 }
  223.                 if (listReward.IndexOf(dictTop) == 2)
  224.                 {
  225.                     f = new Food(dictTop["id"].ToString(), dictTop["name"].ToString(), int.Parse(dictTop["amount"].ToString()));
  226.                 }
  227.                 if (listReward.IndexOf(dictTop) == 3)
  228.                 {
  229.                     s = new Soul(dictTop["id"].ToString(), dictTop["name"].ToString(), int.Parse(dictTop["amount"].ToString()));
  230.                 }
  231.  
  232.                 //if (!Rewards.ContainsKey(listReward.IndexOf(dictTop).ToString()))
  233.                 //{
  234.                 //    Rewards.Add(listReward.IndexOf(dictTop).ToString(), dictTop);
  235.                 //}
  236.                 //else
  237.                 //{
  238.                 //    Rewards[listReward.IndexOf(dictTop).ToString()] = dictTop;
  239.                 //}
  240.             }
  241.             MyReward = new Reward(b, d, s, f);
  242.             BonusMutiplier = new List<int>();
  243.             var listMutiplier = dict["bonusmultiplier"] as List<object>;
  244.             for (var i = 0; i < listMutiplier.Count; i++)
  245.             {
  246.                 BonusMutiplier.Add(Int16.Parse((i.ToString(CultureInfo.InvariantCulture))));
  247.             }
  248.             var seasonReward = dict["seasonreward"] as List<object>;
  249.             // seven level reward
  250.             if (seasonReward == null) return;
  251.             foreach (var season in seasonReward)
  252.             {
  253.                 var dictSeason = season as List<object>;
  254.                 if (dictSeason.Count == 4)
  255.                 {
  256.                     for (int i = 0; i < dictSeason.Count; i++)
  257.                     {
  258.                         var dicts = dictSeason[i] as Dictionary<string, object>;
  259.                         if (i == 0)
  260.                         {
  261.                             b = new Boss(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  262.  
  263.                         }
  264.                         if (i == 1)
  265.                         {
  266.                             d = new Diamond(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  267.                         }
  268.                         if (i == 2)
  269.                         {
  270.                             f = new Food(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  271.                         }
  272.                         if (i == 3)
  273.                         {
  274.                             s = new Soul(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  275.                         }
  276.                     }
  277.                     SeasonRewards.Add(new Reward(b, d, s, f));
  278.  
  279.                 }
  280.                 if (dictSeason.Count == 3)
  281.                 {
  282.                     for (int i = 0; i < dictSeason.Count; i++)
  283.                     {
  284.                         var dicts = dictSeason[i] as Dictionary<string, object>;
  285.                         b = null;
  286.                         if (i == 0)
  287.                         {
  288.                             d = new Diamond(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  289.                         }
  290.                         if (i == 1)
  291.                         {
  292.                             f = new Food(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  293.                         }
  294.                         if (i == 2)
  295.                         {
  296.                             s = new Soul(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  297.                         }
  298.                     }
  299.                     SeasonRewards.Add(new Reward(b, d, s, f));
  300.                 }
  301.                 if (dictSeason.Count == 2)
  302.                 {
  303.                     for (int i = 0; i < dictSeason.Count; i++)
  304.                     {
  305.                         var dicts = dictSeason[i] as Dictionary<string, object>;
  306.                         b = null;
  307.  
  308.                         d = null;
  309.  
  310.                         if (i == 0)
  311.                         {
  312.                             f = new Food(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  313.                         }
  314.                         if (i == 1)
  315.                         {
  316.                             s = new Soul(dicts["id"].ToString(), dicts["name"].ToString(), int.Parse(dicts["amount"].ToString()));
  317.                         }
  318.                     }
  319.                     SeasonRewards.Add(new Reward(b, d, s, f));
  320.                 }
  321.  
  322.             }
  323.             Debug.Log(SeasonRewards.Count);
  324.             if (LoadDataCompleted != null) LoadDataCompleted.Invoke();
  325.         }
  326.     }
  327.  
  328. }
Advertisement
Add Comment
Please, Sign In to add comment