| using System.Collections; | 
| using System.Collections.Generic; | 
| using UnityEngine; | 
| using LitJson; | 
| using System; | 
|   | 
| public class ServerListCenter : Singleton<ServerListCenter> | 
|   | 
| { | 
|     public static readonly string[] JUMP_URL = new string[] { "http://gamecenter.secondworld.net.cn:11000/center/server_list.php/?" }; | 
|   | 
|     public static string SERVERLIST_URL_COMMON = ""; | 
|     public static string SERVERLIST_URL_PLAYER = ""; | 
|   | 
|     public ServerInfoPlayer serverInfoPlayer { get; private set; } | 
|     public ServerInfoCommon serverInfoCommon { get; private set; } | 
|   | 
|     ServerData m_CurrentServer; | 
|     public ServerData currentServer | 
|     { | 
|         get { return m_CurrentServer; } | 
|         set | 
|         { | 
|             m_CurrentServer = value; | 
|             m_SelectedServer = true; | 
|   | 
|             localSaveServerId = m_CurrentServer.region_flag; | 
|   | 
|             if (serverSelectEvent != null) | 
|             { | 
|                 serverSelectEvent(); | 
|             } | 
|         } | 
|     } | 
|   | 
|     string m_CurrentServerGroup = string.Empty; | 
|     public string currentServerGroup | 
|     { | 
|         get | 
|         { | 
|             return m_CurrentServerGroup; | 
|         } | 
|         set | 
|         { | 
|             if (m_CurrentServerGroup != value) | 
|             { | 
|                 m_CurrentServerGroup = value; | 
|                 if (serverGroupSelectEvent != null) | 
|                 { | 
|                     serverGroupSelectEvent(); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     bool m_SelectedServer = false; | 
|   | 
|     bool serverListPlayerPartGot = false; | 
|     bool serverListCommonPartGot = false; | 
|   | 
|     public bool serverListGot | 
|     { | 
|         get { return serverListCommonPartGot; } | 
|     } | 
|   | 
|     string accountNameBuf; | 
|     private string serverUrl; | 
|     public event Action onServerListRefreshEvent; | 
|     public event Action serverGroupSelectEvent; | 
|     public event Action serverSelectEvent; | 
|   | 
|     const string LOGIN_SERVER = "LoginServer"; | 
|     public int localSaveServerId | 
|     { | 
|         get | 
|         { | 
|             return LocalSave.GetInt(LOGIN_SERVER); | 
|         } | 
|         set | 
|         { | 
|             LocalSave.SetInt(LOGIN_SERVER, value); | 
|         } | 
|     } | 
|   | 
|     int jumpUrlIndex = 0; | 
|   | 
|     //从有数据的服务器列表 或者 总列表获取 | 
|     public ServerData GetServerData(int _id) | 
|     { | 
|         if (serverInfoPlayer != null && serverInfoPlayer.player != null && serverInfoPlayer.player.group_list != null) | 
|         { | 
|             var serverGroup = serverInfoPlayer.player; | 
|             for (int i = 0; i < serverGroup.group_list.Length; i++) | 
|             { | 
|                 var serverData = serverGroup.group_list[i]; | 
|                 if (serverData.region_flag == _id) | 
|                 { | 
|                     return serverData; | 
|                 } | 
|             } | 
|         } | 
|   | 
|         if (serverInfoPlayer != null && serverInfoPlayer.gametest != null && serverInfoPlayer.gametest.group_list != null) | 
|         { | 
|             var serverGroup = serverInfoPlayer.gametest; | 
|             for (int i = 0; i < serverGroup.group_list.Length; i++) | 
|             { | 
|                 var serverData = serverGroup.group_list[i]; | 
|                 if (serverData.region_flag == _id) | 
|                 { | 
|                     return serverData; | 
|                 } | 
|             } | 
|         } | 
|   | 
|         if (serverInfoCommon == null) | 
|         { | 
|             return default(ServerData); | 
|         } | 
|         else | 
|         { | 
|             if (serverInfoCommon.common != null) | 
|             { | 
|                 for (int i = 0; i < serverInfoCommon.common.Length; i++) | 
|                 { | 
|                     var group = serverInfoCommon.common[i]; | 
|                     for (int j = 0; j < group.group_list.Length; j++) | 
|                     { | 
|                         var serverData = group.group_list[j]; | 
|                         if (serverData.region_flag == _id) | 
|                         { | 
|                             return serverData; | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|   | 
|         return default(ServerData); | 
|     } | 
|   | 
|     //从总服务器列表获取 | 
|     public ServerData GetServerDataEx(int _id) | 
|     { | 
|         if (serverInfoCommon == null) | 
|         { | 
|             return default(ServerData); | 
|         } | 
|         else | 
|         { | 
|             if (serverInfoCommon.common != null) | 
|             { | 
|                 for (int i = 0; i < serverInfoCommon.common.Length; i++) | 
|                 { | 
|                     var group = serverInfoCommon.common[i]; | 
|                     for (int j = 0; j < group.group_list.Length; j++) | 
|                     { | 
|                         var serverData = group.group_list[j]; | 
|                         if (serverData.region_flag == _id) | 
|                         { | 
|                             return serverData; | 
|                         } | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|   | 
|         return default(ServerData); | 
|     } | 
|   | 
|     public string GetServerName(int id, bool defaultName = true) | 
|     { | 
|         var data = GetServerDataEx(id); | 
|         if (string.IsNullOrEmpty(data.name)) | 
|         { | 
|             if (defaultName) | 
|                 return "S" + id; | 
|             return ""; | 
|         } | 
|         return data.name.Replace("@gm", ""); | 
|     } | 
|   | 
|     public void RequestJumpUrl() | 
|     { | 
|         if (VersionUtility.Instance.InIosAuditTime()) | 
|         { | 
|             return; | 
|         } | 
|   | 
|         var url = StringUtility.Contact(JUMP_URL[0], "game=xbqy&flag=", VersionConfig.Get().appId, "_", VersionConfig.Get().branch, "_", VersionConfig.Get().version); | 
|         jumpUrlIndex++; | 
|         serverUrl = url; | 
|         Debug.Log("http地址:serverlist  " + url); | 
|         HttpRequest.Instance.RequestHttpGet(url, HttpRequest.defaultHttpContentType, 1, OnRequestJumpUrl); | 
|     } | 
|   | 
|     private void OnRequestJumpUrl(bool _ok, string _result) | 
|     { | 
|         if (_ok) | 
|         { | 
|             var serverUrls = _result.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries); | 
|             SERVERLIST_URL_COMMON = serverUrls[0]; | 
|             SERVERLIST_URL_PLAYER = serverUrls[1]; | 
|   | 
|             RequestServerList(); | 
|         } | 
|         else | 
|         { | 
|             Debug.Log("http 数据通讯: ServerlistCenter:" + serverUrl + "  result:" + _result); | 
|             //BuglyAgent.ReportException(new System.Exception(), "http 数据通讯: ServerlistCenter:" + serverUrl + "  result:" + _result); | 
|             Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), RequestJumpUrl); | 
|         } | 
|     } | 
|   | 
|     public void RequestServerList() | 
|     { | 
|         serverListCommonPartGot = false; | 
|         serverListPlayerPartGot = false; | 
|         RequestServerCommonList(); | 
|   | 
|         if (VersionConfig.Get().versionAuthority == VersionAuthority.InterTest) | 
|         { | 
|             var localSaveAccountName = LocalSave.GetString(LoginManager.USER_ACCOUNT); | 
|             RequestServerListPlayer(localSaveAccountName); | 
|         } | 
|     } | 
|   | 
|     public void RequestServerCommonList() | 
|     { | 
|         HttpRequest.Instance.RequestHttpGet(SERVERLIST_URL_COMMON, HttpRequest.defaultHttpContentType, 1, OnGetServerList); | 
|     } | 
|   | 
|     private void OnGetServerList(bool _ok, string _result) | 
|     { | 
|         if (_ok) | 
|         { | 
|             ServerListParser.Instance.PushCommonServerListRawData(_result); | 
|             NetLinkWin.Hide(); | 
|         } | 
|         else | 
|         { | 
|             Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), RequestServerCommonList); | 
|         } | 
|     } | 
|   | 
|     public void SetServerlistCommon(ServerInfoCommon common) | 
|     { | 
|         serverInfoCommon = common; | 
|         serverInfoCommon.recommend = new ServerGroup(); | 
|         serverInfoCommon.recommend.group_title = Language.GetFromLocal(18); | 
|   | 
|         var recommendServers = new List<ServerData>(); | 
|         for (int i = 0; i < serverInfoCommon.common.Length; i++) | 
|         { | 
|             var serverList = serverInfoCommon.common[i].group_list; | 
|             foreach (var server in serverList) | 
|             { | 
|                 if (server.is_recommend == 1) | 
|                 { | 
|                     recommendServers.Add(server); | 
|                 } | 
|             } | 
|         } | 
|   | 
|         // List<string> prints = FieldPrint.PrintFieldsExpand(serverInfoCommon.common[0], true); | 
|   | 
|         // for (int i = 0; i < prints.Count; i++) | 
|         // { | 
|         //     Debug.Log($"index : {i} " + prints[i]); | 
|         // } | 
|   | 
|         serverInfoCommon.recommend.group_list = recommendServers.ToArray(); | 
|         FiltrateDefaultServerAndServerGroup(); | 
|   | 
|         serverListCommonPartGot = true; | 
|   | 
|         if (onServerListRefreshEvent != null) | 
|         { | 
|             onServerListRefreshEvent(); | 
|         } | 
|     } | 
|   | 
|     public void RequestServerListPlayer(string _accountName) | 
|     { | 
|         if (VersionUtility.Instance.InIosAuditTime()) | 
|         { | 
|             return; | 
|         } | 
|   | 
|         accountNameBuf = _accountName; | 
|         var url = string.Empty; | 
|         var tables = new Dictionary<string, string>(); | 
|   | 
|         if (string.IsNullOrEmpty(_accountName)) | 
|         { | 
|             url = SERVERLIST_URL_PLAYER; | 
|             return; | 
|         } | 
|         else | 
|         { | 
|             tables["player"] = _accountName; | 
|             tables["lang"] = "vi"; | 
|             url = StringUtility.Contact(SERVERLIST_URL_PLAYER, "&", HttpRequest.HashtablaToString(tables)); | 
|         } | 
|   | 
|         HttpRequest.Instance.RequestHttpGet(url, HttpRequest.defaultHttpContentType, 1, OnGetServerListPlayer); | 
|     } | 
|   | 
|     private void OnGetServerListPlayer(bool _ok, string _result) | 
|     { | 
|         if (_ok) | 
|         { | 
|             serverListPlayerPartGot = true; | 
|             ServerListParser.Instance.PushPlayerServerListRawData(_result); | 
|         } | 
|         else | 
|         { | 
|             // if (StageLoad.Instance.currentStage == null || StageLoad.Instance.currentStage is LoginStage) | 
|             // { | 
|                 Clock.AlarmAt(DateTime.Now + new TimeSpan(TimeSpan.TicksPerSecond), () => | 
|                 { | 
|                     RequestServerListPlayer(accountNameBuf); | 
|                 }); | 
|             // } | 
|         } | 
|     } | 
|   | 
|     public void SetServerListPlayer(ServerInfoPlayer serverInfoPlayer) | 
|     { | 
|         this.serverInfoPlayer = serverInfoPlayer; | 
|         ProcessRecentServerData(); | 
|         FiltrateDefaultServerAndServerGroup(); | 
|     } | 
|   | 
|     public bool TryGetServerGroup(string _key, out ServerGroup _group) | 
|     { | 
|         if (serverInfoPlayer != null && serverInfoPlayer.player != null && _key == serverInfoPlayer.player.group_title) | 
|         { | 
|             _group = serverInfoPlayer.player; | 
|             return true; | 
|         } | 
|         else if (serverInfoPlayer != null && serverInfoPlayer.gametest != null && _key == serverInfoPlayer.gametest.group_title) | 
|         { | 
|             _group = serverInfoPlayer.gametest; | 
|             return true; | 
|         } | 
|         else if (serverInfoCommon != null && serverInfoCommon.recommend != null && _key == serverInfoCommon.recommend.group_title) | 
|         { | 
|             _group = serverInfoCommon.recommend; | 
|             return true; | 
|         } | 
|         else | 
|         { | 
|             for (int i = 0; i < serverInfoCommon.common.Length; i++) | 
|             { | 
|                 if (_key == serverInfoCommon.common[i].group_title) | 
|                 { | 
|                     _group = serverInfoCommon.common[i]; | 
|                     return true; | 
|                 } | 
|             } | 
|         } | 
|   | 
|         _group = null; | 
|         return false; | 
|     } | 
|   | 
|     // public void ParseServerLocalServerList() | 
|     // { | 
|     //     serverListCommonPartGot = true; | 
|     //     var configs = LoginSeverListConfig.GetValues(); | 
|     //     var count = configs.Count; | 
|     //     serverInfoCommon = new ServerInfoCommon(); | 
|     //     serverInfoCommon.common = new ServerGroup[1]; | 
|     //     serverInfoCommon.common[0] = new ServerGroup(); | 
|     //     serverInfoCommon.common[0].group_title = Language.Get("IOSShenhe"); | 
|     //     serverInfoCommon.common[0].group_list = new ServerData[count]; | 
|     //     for (var i = 0; i < count; i++) | 
|     //     { | 
|     //         var config = configs[i]; | 
|     //         serverInfoCommon.common[0].group_list[i] = new ServerData(config); | 
|     //     } | 
|   | 
|     //     FiltrateDefaultServerAndServerGroup(); | 
|   | 
|     //     if (onServerListRefreshEvent != null) | 
|     //     { | 
|     //         onServerListRefreshEvent(); | 
|     //     } | 
|     // } | 
|   | 
|     public List<string> GetAllServerGroup() | 
|     { | 
|         var serverGroupTitles = new List<string>(); | 
|   | 
|         if (serverInfoPlayer != null && serverInfoPlayer.player != null) | 
|         { | 
|             serverGroupTitles.Add(serverInfoPlayer.player.group_title); | 
|         } | 
|   | 
|         if (serverInfoPlayer != null && serverInfoPlayer.gametest != null) | 
|         { | 
|             serverGroupTitles.Add(serverInfoPlayer.gametest.group_title); | 
|         } | 
|   | 
|         if (serverInfoCommon != null && serverInfoCommon.recommend != null && serverInfoCommon.recommend.group_list.Length > 0) | 
|         { | 
|             serverGroupTitles.Add(serverInfoCommon.recommend.group_title); | 
|         } | 
|   | 
|         for (int i = 0; i < serverInfoCommon.common.Length; i++) | 
|         { | 
|             serverGroupTitles.Add(serverInfoCommon.common[i].group_title); | 
|         } | 
|   | 
|         return serverGroupTitles; | 
|     } | 
|   | 
|     private void ProcessRecentServerData() | 
|     { | 
|         if (serverInfoPlayer != null && serverInfoPlayer.player != null && serverInfoPlayer.player.group_list != null) | 
|         { | 
|             var group = new List<ServerData>(serverInfoPlayer.player.group_list); | 
|             for (int i = group.Count - 1; i >= 0; i--) | 
|             { | 
|                 ServerData serverData; | 
|                 if (!serverInfoPlayer.player.FindServerData(group[i].region_flag, out serverData)) | 
|                 { | 
|                     group.RemoveAt(i); | 
|                 } | 
|                 else | 
|                 { | 
|                     var originalData = group[i]; | 
|                     group[i] = new ServerData() | 
|                     { | 
|                         region_flag = serverData.region_flag, | 
|                         name = serverData.name, | 
|                         running_status = serverData.running_status, | 
|                         statue = serverData.statue, | 
|                         is_recommend = serverData.is_recommend, | 
|                         region_domain = serverData.region_domain, | 
|                         login_port = serverData.login_port, | 
|                         game_port = serverData.game_port, | 
|                         start_date = serverData.start_date, | 
|                         job = originalData.job, | 
|                         roleid = originalData.roleid, | 
|                         level = originalData.level, | 
|                         last_login_time = originalData.last_login_time | 
|                     }; | 
|                 } | 
|             } | 
|   | 
|             group.Sort(ServerData.LastLoginTimeCompare); | 
|             serverInfoPlayer.player.group_list = group.ToArray(); | 
|         } | 
|     } | 
|   | 
|   | 
|     //如果已经登录过游戏返回的需要重新请求一次登录列表 | 
|     //这边请求用的是旧账号,sdk登录成功会用新账号请求一次,内网登录需要手动点击一次服务器列表刷新 | 
|     public void RequestPlayerServerList() | 
|     { | 
|         if (LoginManager.Instance.isLogined) | 
|         { | 
|             var account = LoginManager.Instance.localSaveAccountName; | 
|             if (string.IsNullOrEmpty(account)) | 
|                 return; | 
|             //要用新账号请求 | 
|             RequestServerListPlayer(LoginManager.Instance.localSaveAccountName); | 
|         } | 
|     } | 
|   | 
|     //请求先后顺序 | 
|     //1.公共列表 获取上一次登陆服,记录下来;当第二次获得 打开服务器列表界面的情况不刷新 | 
|     //2.玩家列表刷新 若有玩家信息,查找上一次玩家登陆服,没有默认选一个;若没有玩家信息 默认选公共服 | 
|   | 
|     //此处耦合度比较高 需谨慎修改 | 
|     //1.请求发公共列表和最近服 是先后顺序刷新 | 
|     //2.服务器列表界面刷新逻辑没做好,serverGroupSelectEvent的调用直接决定了界面显示 | 
|     public void FiltrateDefaultServerAndServerGroup() | 
|     { | 
|         if (LoginManager.Instance.isLogined) | 
|         { | 
|             return; | 
|         } | 
|   | 
|         if (serverInfoCommon != null) | 
|         { | 
|             var findTarget = false; | 
|             for (int i = 0; i < serverInfoCommon.common.Length; i++) | 
|             { | 
|                 var serverDatas = serverInfoCommon.common[i].group_list; | 
|                 for (int j = 0; j < serverDatas.Length; j++) | 
|                 { | 
|                     if (localSaveServerId == serverDatas[j].region_flag) | 
|                     { | 
|                         findTarget = true; | 
|                         currentServer = serverDatas[j]; | 
|                         if (string.IsNullOrEmpty(currentServerGroup)) | 
|                         { | 
|                             currentServerGroup = serverInfoCommon.common[i].group_title; | 
|                         } | 
|                         else | 
|                         { | 
|                             serverGroupSelectEvent?.Invoke(); | 
|                         } | 
|                         break; | 
|                     } | 
|                 } | 
|   | 
|                 if (findTarget) | 
|                 { | 
|                     break; | 
|                 } | 
|             } | 
|   | 
|             if (!findTarget) | 
|             { | 
|                 if (serverInfoPlayer != null && serverInfoPlayer.player != null && serverInfoPlayer.player.group_list != null && serverInfoPlayer.player.group_list.Length > 0) | 
|                 { | 
|                     if (string.IsNullOrEmpty(currentServer.name)) | 
|                     { | 
|                         var serverDatas = serverInfoPlayer.player.group_list; | 
|                         for (int j = 0; j < serverDatas.Length; j++) | 
|                         { | 
|                             if (localSaveServerId == serverDatas[j].region_flag) | 
|                             { | 
|                                 findTarget = true; | 
|                                 currentServer = serverDatas[j]; | 
|                                 if (string.IsNullOrEmpty(currentServerGroup)) | 
|                                 { | 
|                                     currentServerGroup = serverInfoPlayer.player.group_title; | 
|                                 } | 
|                                 else | 
|                                 { | 
|                                     serverGroupSelectEvent?.Invoke(); | 
|                                 } | 
|                                 break; | 
|                             } | 
|                         } | 
|                         if (!findTarget) | 
|                         { | 
|                             currentServer = serverInfoPlayer.player.group_list[0]; | 
|                             currentServerGroup = serverInfoPlayer.player.group_title; | 
|                         } | 
|                     } | 
|                     else | 
|                     { | 
|                         serverGroupSelectEvent?.Invoke(); | 
|                     } | 
|                     return; | 
|                 } | 
|   | 
|                 var serverGroup = serverInfoCommon.common[serverInfoCommon.common.Length - 1]; | 
|   | 
|                 var serverList = new List<ServerData>(serverGroup.group_list); | 
|                 serverList.Sort(ServerData.Compare); | 
|                 currentServer = serverList[0]; | 
|                 currentServerGroup = serverGroup.group_title; | 
|             } | 
|   | 
|         } | 
|   | 
|     } | 
|   | 
| } |