少年修仙传客户端代码仓库
client_linchunjie
2019-04-17 f1f2599ba0e6b64358ffef7ae5c9f9af3ed8b8b4
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
 
namespace Snxxz.UI
{
    [XLua.LuaCallCSharp]
    public class EquipStarModel : Model, IPlayerLoginOk, IBeforePlayerDataInitialize
    {
        public readonly LogicInt selectedLevel = new LogicInt();
        public readonly LogicInt selectedPlace = new LogicInt();
        public readonly LogicInt equipStarLevel = new LogicInt();
        public readonly LogicInt equipMaxStarLevel = new LogicInt();
        public readonly LogicInt operateMaterialIndex = new LogicInt();
        public readonly LogicInt starUpgradeProbability = new LogicInt();
        public readonly Dictionary<int, LogicString> materials = new Dictionary<int, LogicString>() {
            {1,new LogicString () },
            {2,new LogicString () },
            {3,new LogicString () },
            {4,new LogicString () },
            {5,new LogicString () },
        };
 
        public readonly LogicList<Star> stars = new LogicList<Star>();
 
        public readonly LogicInt specialMaterial = new LogicInt();
        public readonly LogicInt starResultEffect = new LogicInt();
 
        Dictionary<int, EquipSetStar> equipStars = new Dictionary<int, EquipSetStar>();
        Dictionary<int, EquipStarUpgradeCandidate> candidatePlaces = new Dictionary<int, EquipStarUpgradeCandidate>();
 
        PackModel packModel { get { return ModelCenter.Instance.GetModel<PackModel>(); } }
        EquipModel equipModel { get { return ModelCenter.Instance.GetModel<EquipModel>(); } }
 
        public override void Init()
        {
            ParseConfig();
            DTC0721_tagMakeItemAnswer.MakeItemAnswerEvent += OnGetUpgradeStarResult;
        }
 
        public override void UnInit()
        {
            DTC0721_tagMakeItemAnswer.MakeItemAnswerEvent += OnGetUpgradeStarResult;
        }
 
        public void OnBeforePlayerDataInitialize()
        {
        }
 
        public void OnPlayerLoginOk()
        {
        }
 
        public void UpdateStarLevels(HA3B1_tagMCEquipPartStarInfo info)
        {
            var selectedEquipOldStar = GetEquipStarLevel(new Int2(selectedLevel.value, selectedPlace.value));
            for (var i = 0; i < info.InfoList.Length; i++)
            {
                var equipPosition = EquipSet.ServerPlaceToClientPlace(info.InfoList[i].EquipPackIndex);
                var level = equipPosition.x;
                if (equipStars.ContainsKey(level))
                {
                    equipStars[level].UpdateEquipStarLevel(equipPosition.y, info.InfoList[i].Star);
                }
            }
 
            var starLevel = GetEquipStarLevel(new Int2(selectedLevel.value, selectedPlace.value));
            equipStarLevel.value = starLevel;
            selectedPlace.dirty = true;
 
            var maxStarLevel = GetMaxStarLevel(selectedLevel.value);
            UpdateSelelctedEquipStars(selectedEquipOldStar, starLevel, maxStarLevel);
 
            foreach (var candidate in candidatePlaces.Values)
            {
                candidate.starLevel.value = GetEquipStarLevel(candidate.equipPosition);
            }
        }
 
        void OnGetUpgradeStarResult(H0721_tagMakeItemAnswer info)
        {
            if (info.MakeType == (byte)MakeType.EquipStarUpgrade)
            {
                var equipPosition = new Int2(selectedLevel.value, selectedPlace.value);
                AutoAddMaterials(equipPosition);
                CalculateStarUpgradeProbability(equipPosition);
 
                if (info.Result == 1)
                {
                    starResultEffect.value = 1;
                }
                else
                {
                    starResultEffect.value = -1;
                }
            }
        }
 
        public void DoStarUpgrade(Int2 equipPosition, int starLevel)
        {
            if (!equipStars.ContainsKey(equipPosition.x))
            {
                return;
            }
 
            var equip = packModel.GetItemByGuid(equipModel.GetEquip(equipPosition));
            if (equip == null)
            {
                return;
            }
 
            if (equip.config.ItemColor < 4)
            {
                SysNotifyMgr.Instance.ShowTip("StarLevelUp4");
                return;
            }
 
            var maxLevel = GetMaxStarLevel(equipPosition.x);
            if (starLevel > maxLevel)
            {
                SysNotifyMgr.Instance.ShowTip("StarLevelUp1");
                return;
            }
 
            var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, starLevel);
            if (config == null)
            {
                return;
            }
 
            if (config.CostItemDict.x > 0 && specialMaterial.value == 0)
            {
                SysNotifyMgr.Instance.ShowTip("StarLevelUp2", config.CostItemDict.x);
                return;
            }
 
            if (config.CostItemDict.x > 0 && packModel.GetItemCountByID(PackType.Item, config.CostItemDict.x) < config.CostItemDict.y)
            {
                SysNotifyMgr.Instance.ShowTip("StarLevelUp2", config.CostItemDict.x);
                return;
            }
 
            var materialIndexs = new List<ushort>();
            var materialItemIds = new List<uint>();
            foreach (var material in materials.Values)
            {
                if (!string.IsNullOrEmpty(material.value))
                {
                    var item = packModel.GetItemByGuid(material.value);
                    materialIndexs.Add((ushort)item.gridIndex);
                    materialItemIds.Add((uint)item.itemId);
                }
            }
 
            if (materialIndexs.Count < config.CostEquipCnt)
            {
                SysNotifyMgr.Instance.ShowTip("StarLevelUp3");
                return;
            }
 
            var info = new CA5C5_tagCMEquipPartStarUp();
            info.EquipPackIndex = (ushort)EquipSet.ClientPlaceToServerPlace(equipPosition);
            info.CostEquipIndex = materialIndexs.ToArray();
            info.CostEquipID = materialItemIds.ToArray();
            info.CostEquipCnt = (byte)materialIndexs.Count;
 
            GameNetSystem.Instance.SendInfo(info);
        }
 
        public void ResetOperateParams()
        {
            selectedLevel.value = 0;
            selectedPlace.value = 0;
            equipStarLevel.value = 0;
            equipMaxStarLevel.value = 0;
            operateMaterialIndex.value = 0;
            starUpgradeProbability.value = -1;
            starResultEffect.value = 0;
            foreach (var item in materials.Values)
            {
                item.value = string.Empty;
            }
 
            stars.Clear();
        }
 
        public void SelectLevel(int level)
        {
            selectedLevel.value = level;
            candidatePlaces.Clear();
 
            var places = GetStarUpgradablePlaces(level);
            if (places.IsNullOrEmpty())
            {
                SelectPlace(new Int2(level, 0));
            }
            else
            {
                for (var i = 0; i < places.Count; i++)
                {
                    var place = places[i];
                    var equipPosition = new Int2(level, place);
                    var candidate = new EquipStarUpgradeCandidate(equipPosition);
                    candidate.starLevel.value = GetEquipStarLevel(equipPosition);
                    candidatePlaces[place] = candidate;
                    if (i == 0)
                    {
                        SelectPlace(equipPosition);
                    }
                }
            }
        }
 
        public void SelectPlace(Int2 equipPosition)
        {
            selectedPlace.value = equipPosition.y;
            selectedPlace.dirty = true;
            foreach (var candidate in candidatePlaces.Values)
            {
                candidate.selected.value = candidate.equipPosition == equipPosition;
            }
 
            var starLevel = GetEquipStarLevel(equipPosition);
            var maxStarLevel = GetMaxStarLevel(equipPosition.x);
            equipStarLevel.value = starLevel;
            equipMaxStarLevel.value = maxStarLevel;
 
            UpdateSelelctedEquipStars(starLevel, starLevel, maxStarLevel);
            AutoAddMaterials(equipPosition);
            CalculateStarUpgradeProbability(equipPosition);
        }
 
        private List<int> GetStarUpgradablePlaces(int level)
        {
            if (level <= 0)
            {
                return null;
            }
 
            var equipSet = equipModel.GetEquipSet(level);
            if (equipSet == null)
            {
                return null;
            }
 
            var places = new List<int>();
            for (var i = 1; i <= 12; i++)
            {
                var unLocked = equipSet.IsSlotUnLocked(i);
                if (unLocked)
                {
                    places.Add(i);
                }
            }
 
            return places;
        }
 
        public List<int> GetCandidatePlaces()
        {
            return new List<int>(candidatePlaces.Keys);
        }
 
        public EquipStarUpgradeCandidate GetCandidatePlace(int index)
        {
            if (candidatePlaces.ContainsKey(index))
            {
                return candidatePlaces[index];
            }
            else
            {
                return null;
            }
        }
 
        public bool IsEquipPlaceUpgradable(string equipGuid)
        {
            if (string.IsNullOrEmpty(equipGuid))
            {
                return false;
            }
 
            var equip = packModel.GetItemByGuid(equipGuid);
            if (equip == null)
            {
                return false;
            }
 
            var maxStarLevel = GetMaxStarLevel(equip.config.ItemColor, equip.config.LV);
            var currentStarLevel = GetEquipStarLevel(new Int2(equip.config.LV, equip.config.EquipPlace));
            return currentStarLevel < maxStarLevel;
        }
 
        public static int GetMaxStarLevel(int quality, int level)
        {
            var max = 0;
            if (GeneralDefine.equipStarLimit.ContainsKey(quality))
            {
                var itemLevelStars = GeneralDefine.equipStarLimit[quality];
                if (itemLevelStars.ContainsKey(level))
                {
                    max = itemLevelStars[level];
                }
            }
 
            return max;
        }
 
        public static int GetMaxStarLevel(int level)
        {
            var max = 0;
            foreach (var itemLevelStars in GeneralDefine.equipStarLimit.Values)
            {
                if (itemLevelStars.ContainsKey(level))
                {
                    if (itemLevelStars[level] > max)
                    {
                        max = itemLevelStars[level];
                    }
                }
            }
 
            return max;
        }
 
        public int GetEquipPositionMaxStarLevel(Int2 equipPosition)
        {
            var equipGuid = equipModel.GetEquip(equipPosition);
            if (equipGuid == null)
            {
                return 0;
            }
 
            var equip = packModel.GetItemByGuid(equipGuid);
            if (equip == null)
            {
                return 0;
            }
 
            return GetMaxStarLevel(equip.config.ItemColor, equip.config.LV);
        }
 
        public int GetTotalStarLevel(int level)
        {
            if (!equipStars.ContainsKey(level))
            {
                return 0;
            }
 
            return equipStars[level].GetTotalStarLevel();
        }
 
        /// <summary>
        /// 这是指已经装备位置已经升级到的星级,不考虑当前穿戴的装备
        /// </summary>
        /// <param name="equipPosition"></param>
        /// <returns></returns>
        public int GetStarLevel(Int2 equipPosition)
        {
            if (!equipStars.ContainsKey(equipPosition.x))
            {
                return 0;
            }
 
            return equipStars[equipPosition.x].GetEquipStarLevel(equipPosition.y);
        }
 
        /// <summary>
        /// 这是指受当前装备限制的星级,比如当前装备位已经升级到了9星,但是穿戴的装备最高可升级到6星,那么返回的值是6
        /// </summary>
        /// <param name="equipPosition"></param>
        /// <returns></returns>
        public int GetEquipStarLevel(Int2 equipPosition)
        {
            var starLevel = GetStarLevel(equipPosition);
            var maxLevel = GetEquipPositionMaxStarLevel(equipPosition);
 
            return Mathf.Min(starLevel, maxLevel);
        }
 
        public List<int> GetMaterialGetWays(Int2 equipPosition, int starLevel)
        {
            var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, starLevel);
            if (config == null)
            {
                return null;
            }
 
            var getWays = new List<int>();
            foreach (int place in config.CostEquipPlace)
            {
                var equipControlConfig = EquipControlConfig.Get(equipPosition.x, place);
                foreach (int getWay in equipControlConfig.getWays)
                {
                    if (!getWays.Contains(getWay))
                    {
                        getWays.Add(getWay);
                    }
                }
            }
 
            return getWays;
        }
 
        public List<string> GetMaterials(Int2 equipPosition, int starLevel)
        {
            var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, starLevel);
            if (config == null)
            {
                return null;
            }
 
            var equipGuid = equipModel.GetEquip(equipPosition);
            var equip = packModel.GetItemByGuid(equipGuid);
            if (equip == null)
            {
                return null;
            }
 
            var expcetMaterials = packModel.GetItems(PackType.Item, new SinglePack.FilterParams() { });
            for (var i = expcetMaterials.Count - 1; i >= 0; i--)
            {
                var item = expcetMaterials[i];
                if (item.config.RealmLimit != equip.config.RealmLimit)
                {
                    expcetMaterials.RemoveAt(i);
                }
                else if (!config.CostEquipPlace.Contains(item.config.EquipPlace))
                {
                    expcetMaterials.RemoveAt(i);
                }
                else if (!config.CostEquipColor.Contains(item.config.ItemColor))
                {
                    expcetMaterials.RemoveAt(i);
                }
            }
 
            for (var i = 1; i <= 5; i++)
            {
                var placedMaterialGuid = GetMaterialLogicStringByIndex(i).value;
                var materialItem = packModel.GetItemByGuid(placedMaterialGuid);
                if (materialItem != null)
                {
                    expcetMaterials.Remove(materialItem);
                }
            }
 
            var materialGuids = new List<string>();
            foreach (var item in expcetMaterials)
            {
                materialGuids.Add(item.guid);
            }
 
            return materialGuids;
        }
 
        public void AddMaterial(int index, string itemGuid)
        {
            GetMaterialLogicStringByIndex(index).value = itemGuid;
            CalculateStarUpgradeProbability(new Int2(selectedLevel.value, selectedPlace.value));
        }
 
        public void RemoveMaterial(int index)
        {
            GetMaterialLogicStringByIndex(index).value = string.Empty;
            CalculateStarUpgradeProbability(new Int2(selectedLevel.value, selectedPlace.value));
        }
 
        private void AutoAddMaterials(Int2 equipPosition)
        {
            var equipGuid = equipModel.GetEquip(equipPosition);
            var upgradable = IsEquipPlaceUpgradable(equipGuid);
 
            if (upgradable)
            {
                var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, GetEquipStarLevel(equipPosition) + 1);
                if (config.CostEquipPlace.Length == 1 && config.CostEquipColor.Length == 1)
                {
                    var items = packModel.GetItems(PackType.Item, new SinglePack.FilterParams()
                    {
                        levels = new List<int>() { equipPosition.x },
                        qualitys = new List<int>() { config.CostEquipColor[0] },
                        equipTypes = new List<int>() { config.CostEquipPlace[0] }
                    });
 
                    if (!items.IsNullOrEmpty())
                    {
                        var minCount = Mathf.Min(config.CostEquipCnt, items.Count);
                        for (int i = 1; i <= 5; i++)
                        {
                            materials[i].value = i <= minCount ? items[i].guid : string.Empty;
                        }
                    }
                }
                {
                    for (int i = 1; i <= 5; i++)
                    {
                        materials[i].value = string.Empty;
                    }
                }
 
                if (config.CostItemDict.x > 0)
                {
                    var itemId = config.CostItemDict.x;
                    specialMaterial.value = itemId;
                }
                else
                {
                    specialMaterial.value = 0;
                }
            }
            else
            {
                foreach (var material in materials.Values)
                {
                    material.value = string.Empty;
                }
            }
 
        }
 
        private void CalculateStarUpgradeProbability(Int2 equipPosition)
        {
            var probability = 0f;
            var equipGuid = equipModel.GetEquip(equipPosition);
            var upgradable = IsEquipPlaceUpgradable(equipGuid);
            if (upgradable)
            {
                for (var i = 1; i <= 5; i++)
                {
                    var itemGuid = GetMaterialLogicStringByIndex(i).value;
                    probability += GetMaterialSuccessRate(equipPosition, itemGuid);
                }
 
                starUpgradeProbability.value = Mathf.RoundToInt(probability * 100);
            }
            else
            {
                starUpgradeProbability.value = -1;
            }
        }
 
        public float GetMaterialSuccessRate(Int2 equipPosition, string guid)
        {
            if (string.IsNullOrEmpty(guid))
            {
                return 0f;
            }
 
            var item = packModel.GetItemByGuid(guid);
            if (item == null)
            {
                return 0f;
            }
 
            var currentStarLevel = GetEquipStarLevel(equipPosition);
            var config = EquipStarConfig.Get(equipPosition.x, equipPosition.y, currentStarLevel + 1);
            if (config == null)
            {
                return 0f;
            }
 
            var singleItemRate = (float)config.SuitTotalRate * 0.01f / config.CostEquipCnt;
            return item.config.SuiteiD > 0 ? singleItemRate : singleItemRate * 0.5f;
        }
 
        public LogicString GetMaterialLogicStringByIndex(int index)
        {
            return materials.ContainsKey(index) ? materials[index] : null;
        }
 
        private void UpdateSelelctedEquipStars(int oldStarLevel, int newStarLevel, int maxStarLevel)
        {
            stars.Clear();
            for (var i = 1; i <= maxStarLevel; i++)
            {
                stars.Add(new Star()
                {
                    actived = i <= newStarLevel,
                    newGet = i <= newStarLevel && i > oldStarLevel,
                });
            }
        }
 
        private void ParseConfig()
        {
            var configs = EquipStarConfig.GetValues();
            foreach (var config in configs)
            {
                if (!equipStars.ContainsKey(config.Level))
                {
                    equipStars[config.Level] = new EquipSetStar(config.Level);
                }
            }
        }
 
        public struct Star
        {
            public bool actived;
            public bool newGet;
        }
 
    }
}