示例#1
0
    public void SetGameSateFromJson(string result)
    {
        //string json = @"{  CPU: 'Intel',  Drives: [    'DVD read/writer',    '500 gigabyte hard drive'  ]}";

        JObject jresult = JObject.Parse(result) as JObject;

        //Debug.Log(jresult["result"]["gamestate"]["channels"]);

        //================  Get channel Info  ==============//
        JObject jChannels = jresult["result"]["gamestate"]["channels"] as JObject;
        Dictionary <string, JObject> dictObj = jChannels.ToObject <Dictionary <string, JObject> >();

        List <ChannelInfo> oldChannelList = new List <ChannelInfo>();

        oldChannelList = GlobalData.ggameChannelList;
        if (GlobalData.ggameChannelList != null)
        {
            GlobalData.ggameChannelList.Clear();
        }
        if (GlobalData.ggameLobbyChannelList != null)
        {
            GlobalData.ggameLobbyChannelList.Clear();
        }

        foreach (KeyValuePair <string, JObject> item in dictObj)
        {
            ChannelInfo channelInfo = new ChannelInfo();
            channelInfo.id = item.Key;
            JArray a = item.Value["meta"]["participants"] as JArray;

            ChannelInfo oldChannel = null;
            foreach (ChannelInfo c in oldChannelList)
            {
                if (c.id == channelInfo.id)
                {
                    oldChannel = c;
                }
            }

            channelInfo.userNames  = new string[a.Count];
            channelInfo.statusText = item.Value["state"]["parsed"]["phase"].ToString();

            int index = 0;
            foreach (JObject j in a)
            {
                channelInfo.userNames[index++] = j["name"].ToString();
            }

            if (!GlobalData.bRunonce && a.Count > 1)
            {
                channelInfo.bignored = true;
                GlobalData.ggameIgnoredChannelIDs.Add(channelInfo.id);
                continue;
            }

            GlobalData.AddChannel(channelInfo);

            if (a.Count == 1)
            {
                GlobalData.ggameLobbyChannelList.Add(channelInfo);
            }

            if (!GlobalData.bLogin)
            {
                continue;
            }


            //====== case new channel, load channel service =============//
            if (channelInfo.userNames.Length > 1)
            {
                if (GlobalData.bOpenedChannel ||
                    (channelInfo.userNames[0] != GlobalData.gPlayerName.Substring(2) &&
                     channelInfo.userNames[1] != GlobalData.gPlayerName.Substring(2)))
                {
                    continue;
                }

                string opponentName = channelInfo.userNames[0];
                if (channelInfo.userNames[0] == GlobalData.gPlayerName.Substring(2))
                {
                    opponentName = channelInfo.userNames[1];
                }
                //=========================    ==============================//
                if (!GetComponent <GameUserManager>().IsExistName("p/" + opponentName))    //=== case  only other user====//
                {
                    Debug.Log("liveFlag:" + GlobalData.bLiveChannel);
                    if (!GlobalData.bLiveChannel && !GlobalData.IsIgnoreChannel(channelInfo.id))
                    //   if (!GlobalData.bLiveChannel)
                    {
                        GlobalData.bOpenedChannel = true;
                        //============= Create  Channe
                        GetComponent <GameUserManager>().StartGameByChannel(channelInfo.id);
                        //case menu is active, collapse  menu
                        GetComponent <GameUserManager>().InitMenu();
                        GlobalData.InitGameChannelData();

                        GlobalData.bLiveChannel = true;
                        GlobalData.bFinished    = false;
                        GlobalData.bPlaying     = false;
                    }
                    //Debug.Log(channelInfo.id);
                }
            }
        }
        //============================================================//
        if (!GlobalData.bRunonce)
        {
            GlobalData.bRunonce = true;
        }

        //============================= Get Leader Info ===============================//
        JObject jstatss = jresult["result"]["gamestate"]["gamestats"] as JObject;

        //Debug.Log(jstatss.ToString());
        if (jstatss == null)
        {
            return;
        }
        dictObj = jstatss.ToObject <Dictionary <string, JObject> >();

        if (GlobalData.ggameLeaderList != null)
        {
            GlobalData.ggameLeaderList.Clear();
        }

        foreach (KeyValuePair <string, JObject> item in dictObj)
        {
            LeaderInfo leaderInfo = new LeaderInfo();
            leaderInfo.playerName  = item.Key;
            leaderInfo.winCount    = int.Parse(item.Value["won"].ToString());
            leaderInfo.playedCount = leaderInfo.winCount + int.Parse(item.Value["lost"].ToString());
            GlobalData.ggameLeaderList.Add(leaderInfo);
        }
        //============================================================//
    }