少年修仙传客户端代码仓库
client_Wu Xijin
2019-02-20 a87120c155c48fa45b20a97c1a58bdbeb77318b7
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
 
using UnityEngine;
namespace Snxxz.UI
{
    [XLua.LuaCallCSharp]
    [XLua.Hotfix]
    public class VirtualPackModel : Model, IBeforePlayerDataInitialize, IPlayerLoginOk
    {
 
        Dictionary<PackType, List<VirtualPackItem>> virtualPackItems
            = new Dictionary<PackType, List<VirtualPackItem>>();
        Dictionary<PackType, int> virtualPackCapacityDict = new Dictionary<PackType, int>();
 
        public event Action<PackType> virtualPackRefresh;
 
        public override void Init()
        {
            ParseConfig();
        }
 
        public void OnBeforePlayerDataInitialize()
        {
            virtualPackItems.Clear();
        }
 
        public void OnPlayerLoginOk()
        {
        }
 
        public override void UnInit()
        {
        }
 
        void ParseConfig()
        {
            var funcConfig = FuncConfigConfig.Get("GatherSoulPackCount");
            if (funcConfig != null)
            {
                virtualPackCapacityDict.Add(PackType.GatherSoul, int.Parse(funcConfig.Numerical1));
            }
        }
 
        public int GetPackCapacity(PackType packType)
        {
            if (virtualPackCapacityDict.ContainsKey(packType))
            {
                return virtualPackCapacityDict[packType];
            }
            return 0;
        }
 
        public int GetPackRemainCount(PackType packType)
        {
            var capacity = GetPackCapacity(packType);
            if (virtualPackItems.ContainsKey(packType))
            {
                return capacity - virtualPackItems[packType].Count;
            }
            return capacity;
        }
 
        public int GetItemCountById(PackType packType, int id)
        {
            var count = 0;
            List<int> list;
            if (TryGetItems(packType, out list))
            {
                for (int i = 0; i < list.Count; i++)
                {
                    VirtualPackItem item;
                    if (TryGetItem(packType, list[i], out item)
                        && item.id == id)
                    {
                        count++;
                    }
                }
            }
            return count;
        }
 
        public bool TryGetItems(PackType packType, out List<int> list)
        {
            list = new List<int>();
            List<VirtualPackItem> _list;
            if (virtualPackItems.TryGetValue(packType, out _list))
            {
                for (int i = 0; i < _list.Count; i++)
                {
                    list.Add(_list[i].index);
                }
                return true;
            }
            return false;
        }
 
        public bool TryGetItem<T>(PackType packType, int packIndex, out T item) where T : VirtualPackItem
        {
            item = default(T);
            if (virtualPackItems.ContainsKey(packType))
            {
                var _index = virtualPackItems[packType].FindIndex((x) =>
                 {
                     return x.index == packIndex;
                 });
                if (_index != -1)
                {
                    item = virtualPackItems[packType][_index] as T;
                }
                return _index != -1;
            }
            return false;
        }
 
        public void OnReceiveServerPack(HA204_tagMCVPackRefresh package)
        {
            var packType = (PackType)package.PackType;
            switch (packType)
            {
                case PackType.GatherSoul:
                    break;
                default:
                    return;
            }
            List<VirtualPackItem> list;
            if (!virtualPackItems.TryGetValue(packType, out list))
            {
                list = new List<VirtualPackItem>();
                virtualPackItems.Add(packType, list);
            }
            SetVirtualItem(packType, ref list, package.VPacklItemList);
            if (virtualPackRefresh != null)
            {
                virtualPackRefresh((PackType)package.PackType);
            }
        }
 
        public void OnReceiveServerPack(HA205_tagMCVPackClear package)
        {
            List<VirtualPackItem> list;
            if (virtualPackItems.TryGetValue((PackType)package.PackType, out list))
            {
                for (int i = 0; i < package.Count; i++)
                {
                    var index = package.ItemPlaceList[i];
                    list.RemoveAll((x) =>
                    {
                        return x.index == index;
                    });
                }
                if (virtualPackRefresh != null)
                {
                    virtualPackRefresh((PackType)package.PackType);
                }
            }
        }
 
        void SetVirtualItem(PackType packType, ref List<VirtualPackItem> list, HA204_tagMCVPackRefresh.tagMCVPackItem[] items)
        {
            for (int i = 0; i < items.Length; i++)
            {
                var item = list.Find((x) =>
                {
                    return x.index == items[i].ItemPlace;
                });
                if (item != null)
                {
                    list.Remove(item);
                    item = null;
                }
                item = VirtualPackItem.Get(packType);
                list.Add(item);
                item.ParsePackItem(items[i].ItemPlace, items[i].ItemData);
            }
        }
    }
 
    public abstract class VirtualPackItem
    {
        public int index { get; private set; }
 
        public int id { get; private set; }
 
        public int level { get; private set; }
 
        protected string dataString { get; private set; }
 
        protected static StringBuilder sb = new StringBuilder();
 
        public virtual void ParsePackItem(int index, uint data)
        {
            this.index = index;
 
            dataString = data.ToString();
            int delta = 10 - dataString.Length;
            sb.Length = 0;
            sb.Append('0', delta);
            dataString = dataString.Insert(0, sb.ToString());
 
            id = int.Parse(dataString.Substring(5, 5));
            level = int.Parse(dataString.Substring(2, 3)) + 1;
        }
 
        public static VirtualPackItem Get(PackType packType)
        {
            switch (packType)
            {
                case PackType.GatherSoul:
                    return new GatherSoulItem();
            }
            return null;
        }
    }
 
    public struct VirtualItem
    {
        public PackType packType;
        public int itemId;
        public int index;
        public int level;
    }
}