yyl
12 小时以前 5d3366f2e0f687995eb7ad2107c4379fe7acd4e8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
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 = "TODO"//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;
            }
 
        }
 
    }
 
}