少年修仙传客户端代码仓库
client_Wu Xijin
2019-04-16 fc714208ff3a320c3bb52bddd5ea3d91f647a206
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Snxxz.UI
{
    public class HazyRegionModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk
    {
        Dictionary<int, Incident> m_Incidents = new Dictionary<int, Incident>();
        Dictionary<int, AdventureInfo> m_AdventureInfos = new Dictionary<int, AdventureInfo>();
 
        public int limitPoint { get; private set; }
        public int point { get; private set; }
        public int processingIncidentId { get; private set; }
        public int adventureDialogueId { get; private set; }
        public bool playing { get; private set; }
        public bool satisfyFakeOpen { get; private set; }
        public int hazyRegionOpenTimes { get; private set; }
        public int fakeOpenTimes { get; private set; }
        public bool isServerPrepare { get; private set; }
        public bool requireIncidentAnimation { get; set; }
 
        int m_SelectIncident;
        public int selectIncident
        {
            get { return m_SelectIncident; }
            set
            {
                if (m_SelectIncident != value)
                {
                    m_SelectIncident = value;
                    if (selectIncidentRefresh != null)
                    {
                        selectIncidentRefresh();
                    }
                }
            }
        }
 
        public bool InFakeHazyRegion
        {
            get { return hazyRegionOpenTimes <= fakeOpenTimes; }
        }
 
        int cacheMapId = 0;
 
        public event Action selectIncidentRefresh;
        public event Action<int> onHazyRegionStateRefresh;  //0-结束拜访 1-开始拜访 2-强制刷新
        public event Action onHazyRegionIncidentRefresh;
 
        DungeonModel dungeonModel { get { return ModelCenter.Instance.GetModel<DungeonModel>(); } }
        DailyQuestModel dailyQuestModel { get { return ModelCenter.Instance.GetModel<DailyQuestModel>(); } }
        HazyDemonKingModel hazyDemonKingModel { get { return ModelCenter.Instance.GetModel<HazyDemonKingModel>(); } }
        HazyGrassModel hazyGrassModel { get { return ModelCenter.Instance.GetModel<HazyGrassModel>(); } }
 
        public override void Init()
        {
            ParseConfig();
 
            StageLoad.Instance.onStageLoadFinish += OnStageLoadFinish;
            AdventureStage.Instance.onLoadAdventureStage += OnLoadAdventureStage;
            AdventureStage.Instance.onExitAdventureStage += OnExitAdventureStage;
        }
 
        public void OnBeforePlayerDataInitialize()
        {
            isServerPrepare = false;
            point = 0;
            playing = false;
            hazyRegionOpenTimes = 0;
            m_Incidents.Clear();
            m_AdventureInfos.Clear();
        }
 
        public void OnPlayerLoginOk()
        {
            isServerPrepare = true;
        }
 
        public override void UnInit()
        {
            StageLoad.Instance.onStageLoadFinish -= OnStageLoadFinish;
            AdventureStage.Instance.onExitAdventureStage -= OnExitAdventureStage;
            AdventureStage.Instance.onLoadAdventureStage -= OnLoadAdventureStage;
        }
 
        private void OnLoadAdventureStage()
        {
            WindowCenter.Instance.Open<HazyAdventureHintWin>();
        }
 
        private void OnExitAdventureStage()
        {
            SnxxzGame.Instance.StartCoroutine(Co_TryOpenHazyRegionWin());
            WindowCenter.Instance.Close<HazyAdventureHintWin>();
        }
 
        private void OnStageLoadFinish()
        {
            if (!(StageLoad.Instance.currentStage is DungeonStage))
            {
                cacheMapId = 0;
            }
            else
            {
                var mapId = PlayerDatas.Instance.baseData.MapID;
                if (!MapUtility.IsDungeon(mapId))
                {
                    if (IsIncidentDungeon(cacheMapId))
                    {
                        SnxxzGame.Instance.StartCoroutine(Co_TryOpenHazyRegionWin());
                    }
                }
                cacheMapId = mapId;
            }
        }
 
        IEnumerator Co_TryOpenHazyRegionWin()
        {
            yield return WaitingForSecondConst.WaitMS1000;
            if (NewBieCenter.Instance.inGuiding ||
                ModelCenter.Instance.GetModel<TreasureModel>().newGotShowing)
            {
                yield break;
            }
            if (WindowCenter.Instance.IsOpen<MainInterfaceWin>())
            {
                WindowCenter.Instance.Open<CrossServerWin>(false, 2);
            }
        }
 
        void ParseConfig()
        {
            var funcConfig = FuncConfigConfig.Get("ImmortalDomainStrength");
            limitPoint = int.Parse(funcConfig.Numerical1);
 
            funcConfig = FuncConfigConfig.Get("FakeImmortalCount");
            fakeOpenTimes = int.Parse(funcConfig.Numerical1);
        }
 
        public bool TryGetIncident(int id, out Incident incident)
        {
            return m_Incidents.TryGetValue(id, out incident);
        }
 
        public bool TryGotoIncident(int id, out int error)
        {
            error = 0;
            var config = HazyRegionConfig.Get(id);
            if (config == null)
            {
                return false;
            }
            if (!TryGotoDungeon(id, out error))
            {
                return false;
            }
            Incident incident;
            if (TryGetIncident(id, out incident))
            {
                if (incident.state != IncidentState.Processing)
                {
                    switch (incident.state)
                    {
                        case IncidentState.UnStart:
                            if (!InFakeHazyRegion && point < config.point)
                            {
                                error = 3;
                                return false;
                            }
                            break;
                        case IncidentState.Complete:
                            error = 4;
                            return false;
                    }
                }
            }
            return true;
        }
 
        public bool TryGotoDungeon(int id, out int error)
        {
            error = 0;
            if (CrossServerUtility.IsCrossServer())
            {
                error = 1;
                return false;
            }
            var mapId = PlayerDatas.Instance.baseData.MapID;
            if (MapUtility.IsDungeon(mapId))
            {
                error = 2;
                return false;
            }
            if (AdventureStage.Instance.IsInAdventureStage)
            {
                error = 2;
                return false;
            }
            return true;
        }
 
        public bool TryAddTimes()
        {
            var config = DailyQuestConfig.Get((int)DailyQuestType.HazyRegion);
            var dailyQuestOpenTime = DailyQuestOpenTimeConfig.Get(config.RelatedID);
            var limitTimes = dailyQuestOpenTime.DayTimes;
 
            var totalTimes = dailyQuestModel.GetDailyQuestTotalTimes((int)DailyQuestType.HazyRegion);
            var completedTimes = dailyQuestModel.GetDailyQuestCompletedTimes((int)DailyQuestType.HazyRegion);
            var times = Mathf.Clamp(totalTimes - completedTimes, 0, limitTimes);
 
            if (times >= limitTimes)
            {
                return false;
            }
 
            return true;
        }
 
        public bool TryGetAdventureInfo(int id, out AdventureInfo adventureInfo)
        {
            return m_AdventureInfos.TryGetValue(id, out adventureInfo);
        }
 
        public ICollection<int> GetAllIncidents()
        {
            return m_Incidents.Keys;
        }
 
        public int GetIncidentId(int mapId, int lineId)
        {
            var configs = HazyRegionConfig.GetValues();
            foreach (var config in configs)
            {
                if (config.dungeonId == mapId && config.lineId == lineId)
                {
                    return config.id;
                }
            }
            return 0;
        }
 
        public int GetAdventureNpcId()
        {
            var dialogueConfig = AdventureDialogueConfig.Get(adventureDialogueId);
            if (dialogueConfig != null)
            {
                return dialogueConfig.npcId;
            }
            return 0;
        }
 
        public bool IsIncidentDungeon(int mapId)
        {
            if (mapId == 0)
            {
                return false;
            }
            if (mapId == HazyDemonKingModel.Client_MapID
                || mapId == HazyGrassModel.Client_FairyGrassMapID
                || mapId == HazyGrassModel.Client_ReikiGrassMapID)
            {
                return true;
            }
            var configs = HazyRegionConfig.GetValues();
            foreach (var config in configs)
            {
                if (config.dungeonId == mapId)
                {
                    return true;
                }
            }
            return false;
        }
 
        public bool IsIncidentDungeon()
        {
            if (AdventureStage.Instance.IsInAdventureStage)
            {
                return true;
            }
            if (IsIncidentDungeon(PlayerDatas.Instance.baseData.MapID))
            {
                return true;
            }
            return false;
        }
 
        public void DisplayErrorRemind(int error)
        {
            switch (error)
            {
                default:
                    break;
            }
        }
 
        public void StartAdventureDialogue()
        {
            WindowCenter.Instance.Close<MainInterfaceWin>();
            HazyRegionDialogueWin.adventureDialogueId = adventureDialogueId;
            WindowCenter.Instance.Open<HazyRegionDialogueWin>();
        }
 
        void InitializeAdventure()
        {
            var config = HazyRegionConfig.Get(processingIncidentId);
            if (config != null)
            {
                var gear = 0;
                AdventureInfo adventureInfo;
                if (TryGetAdventureInfo(processingIncidentId, out adventureInfo))
                {
                    switch (config.lineId)
                    {
                        case 1:
                            gear = PlayerDatas.Instance.baseData.LV >= adventureInfo.condition ? adventureInfo.gear : 0;
                            break;
                        case 2:
                            gear = PlayerDatas.Instance.baseData.realmLevel >= adventureInfo.condition ? adventureInfo.gear : 0;
                            break;
                        case 3:
                            gear = PlayerDatas.Instance.baseData.FightPoint >= adventureInfo.condition ? adventureInfo.gear : 0;
                            break;
                        case 4:
                            gear = PlayerDatas.Instance.extersion.luckValue >= adventureInfo.condition ? adventureInfo.gear : 0;
                            break;
                    }
                }
                var dialogueConfig = AdventureDialogueConfig.Get(config.lineId, gear);
                if (dialogueConfig != null)
                {
                    adventureDialogueId = dialogueConfig.id;
                }
            }
        }
 
        public void SendGotoIncident(int id)
        {
            processingIncidentId = id;
 
            var config = HazyRegionConfig.Get(id);
            switch ((HazyRegionIncidentType)config.incidentType)
            {
                case HazyRegionIncidentType.Adventure:
                    InitializeAdventure();
 
                    WindowCenter.Instance.CloseAll(WindowCenter.CloseAllIgnoreType.BaseAndCustom);
 
                    AdventureStage.Instance.Enter();
 
                    Incident incident;
                    if(TryGetIncident(id,out incident))
                    {
                        if (incident.state == IncidentState.UnStart)
                        {
                            SendSwitchAdventureState(id, 2);
                        }
                    }
                    break;
                case HazyRegionIncidentType.DemonKing:
                    if (InFakeHazyRegion)
                    {
                        hazyDemonKingModel.RequestEnterClientDungeon();
                    }
                    else
                    {
                        SendGotoIncidentDungeon(config);
                    }
                    break;
                case HazyRegionIncidentType.FairyGrass:
                case HazyRegionIncidentType.ReikiGrass:
                    if (InFakeHazyRegion)
                    {
                        hazyGrassModel.RequestEnterClientDungeon();
                    }
                    else
                    {
                        SendGotoIncidentDungeon(config);
                    }
                    break;
                case HazyRegionIncidentType.Precious:
                    SendGotoIncidentDungeon(config);
                    break;
            }
        }
 
        void SendGotoIncidentDungeon(HazyRegionConfig config)
        {
            if (config.crossServer == 1)
            {
                var pak = new CC105_tagCMEnterCrossServer();
                pak.DataMapID = (uint)config.dungeonId;
                pak.LineID = (ushort)config.lineId;
                GameNetSystem.Instance.SendInfo(pak);
            }
            else
            {
                dungeonModel.SingleChallenge(config.dungeonId, config.lineId);
            }
        }
 
        public void SendBackHazyRegion()
        {
            var pak = new CA526_tagCMVisitFairyDomain();
            pak.Type = 1;
            GameNetSystem.Instance.SendInfo(pak);
        }
 
        public void SendOpenHazyRegion()
        {
            var pak = new CA526_tagCMVisitFairyDomain();
            pak.Type = 0;
            GameNetSystem.Instance.SendInfo(pak);
        }
 
        public void SendSwitchAdventureState(int id, int state)
        {
            var pak = new CA504_tagCMPlayerGetReward();
            pak.RewardType = (byte)GotServerRewardType.Def_RewardType_FairyAdventuresAward;
            pak.DataEx = (uint)id;
            pak.DataExStr = state.ToString();
            pak.DataExStrLen = (byte)Encoding.UTF8.GetBytes(pak.DataExStr).Length;
            GameNetSystem.Instance.SendInfo(pak);
        }
 
        public void ReceivePackage(HA306_tagMCFairyDomainInfo package)
        {
            var preplaying = playing;
 
            playing = package.State == 1;
 
            satisfyFakeOpen = package.State == 2;
 
            hazyRegionOpenTimes = (int)package.VisitCnt;
 
            point = package.Energy;
 
            if (package.IsAll == 1)
            {
                m_Incidents.Clear();
            }
 
            for (int i = 0; i < package.Count; i++)
            {
                var data = package.InfoList[i];
                m_Incidents[data.EventID] = new Incident()
                {
                    id = data.EventID,
                    state = (IncidentState)data.EventState,
                };
            }
 
            if (onHazyRegionIncidentRefresh != null)
            {
                onHazyRegionIncidentRefresh();
            }
 
            if (isServerPrepare)
            {
                if (preplaying != playing)
                {
                    if (onHazyRegionStateRefresh != null)
                    {
                        onHazyRegionStateRefresh(playing ? 1 : 0);
                    }
                }
            }
            else
            {
                if (onHazyRegionStateRefresh != null)
                {
                    onHazyRegionStateRefresh(2);
                }
            }
        }
 
        public void ReceivePackage(HA307_tagMCFairyAdventuresInfo package)
        {
            m_AdventureInfos.Clear();
            for (int i = 0; i < package.Cnt; i++)
            {
                var data = package.InfoList[i];
 
                m_AdventureInfos[data.EventID] = new AdventureInfo()
                {
                    gear = data.Gear,
                    condition = (int)data.Condition,
                };
            }
        }
 
        public enum IncidentState
        {
            UnStart = 1,
            Processing = 2,
            Complete = 3,
        }
 
        public struct Incident
        {
            public int id;
            public IncidentState state;
        }
 
        public struct AdventureInfo
        {
            public int gear;
            public int condition;
        }
    }
 
    public enum HazyRegionIncidentType
    {
        Adventure,
        Precious,
        FairyGrass,
        DemonKing,
        ReikiGrass,
    }
}