少年修仙传客户端代码仓库
client_Wu Xijin
2019-06-13 033958214c0b16d7e7b93cc821b018c295251867
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
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, ObjectPool<VirtualPackItem>> m_VirtualItemPool
            = new Dictionary<PackType, ObjectPool<VirtualPackItem>>();
        Dictionary<PackType, int> virtualPackCapacityDict = new Dictionary<PackType, int>();
 
        public event Action<PackType> virtualPackRefresh;
 
        public override void Init()
        {
            ParseConfig();
            m_VirtualItemPool.Add(PackType.GatherSoul, new ObjectPool<VirtualPackItem>(OnGetItem, OnReleaseItem));
            m_VirtualItemPool.Add(PackType.RunePack, new ObjectPool<VirtualPackItem>(OnGetItem, OnReleaseItem));
        }
 
        public void OnBeforePlayerDataInitialize()
        {
            foreach (var packType in virtualPackItems.Keys)
            {
                var pool = m_VirtualItemPool[packType];
                var items = virtualPackItems[packType];
                foreach (var item in items)
                {
                    pool.Release(item);
                }
            }
            virtualPackItems.Clear();
        }
 
        public void OnPlayerLoginOk()
        {
        }
 
        public override void UnInit()
        {
        }
 
        void ParseConfig()
        {
            var funcConfig = FuncConfigConfig.Get("GatherSoulPackCount");
            virtualPackCapacityDict.Add(PackType.GatherSoul, int.Parse(funcConfig.Numerical1));
            funcConfig = FuncConfigConfig.Get("RunePackageNum");
            virtualPackCapacityDict.Add(PackType.RunePack, int.Parse(funcConfig.Numerical1));
        }
 
        public bool IsVirtualPack(PackType packType)
        {
            switch (packType)
            {
                case PackType.GatherSoul:
                case PackType.RunePack:
                    return true;
            }
            return false;
        }
 
        public int GetPackCapacity(PackType packType)
        {
            if (virtualPackCapacityDict.ContainsKey(packType))
            {
                return virtualPackCapacityDict[packType];
            }
            return 0;
        }
 
        public int GetPackRemainCount(PackType packType)
        {
            var capacity = GetPackCapacity(packType);
            return capacity - GetPackItemCount(packType);
        }
 
        public int GetPackItemCount(PackType packType)
        {
            if (virtualPackItems.ContainsKey(packType))
            {
                return virtualPackItems[packType].Count;
            }
            return 0;
        }
 
        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 void GetItemIndexs(PackType packType, ref List<int> list)
        {
            list.Clear();
            List<VirtualPackItem> _list;
            if (virtualPackItems.TryGetValue(packType, out _list))
            {
                for (int i = 0; i < _list.Count; i++)
                {
                    list.Add(_list[i].index);
                }
            }
        }
 
        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;
            if (!IsVirtualPack(packType))
            {
                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)
        {
            var packType = (PackType)package.PackType;
            if (!IsVirtualPack(packType))
            {
                return;
            }
            List<VirtualPackItem> list;
            if (virtualPackItems.TryGetValue(packType, out list))
            {
                var pool = m_VirtualItemPool[packType];
                for (int i = 0; i < package.Count; i++)
                {
                    var index = package.ItemPlaceList[i];
                    var items = list.FindAll((x) =>
                    {
                        return x.index == index;
                    });
                    foreach (var item in items)
                    {
                        list.Remove(item);
                        pool.Release(item);
                    }
                }
                if (virtualPackRefresh != null)
                {
                    virtualPackRefresh(packType);
                }
            }
        }
 
        void SetVirtualItem(PackType packType, ref List<VirtualPackItem> list, HA204_tagMCVPackRefresh.tagMCVPackItem[] items)
        {
            var pool = m_VirtualItemPool[packType];
            for (int i = 0; i < items.Length; i++)
            {
                var item = list.Find((x) =>
                {
                    return x.index == items[i].ItemPlace;
                });
                if (item != null)
                {
                    item.Release();
                }
                else
                {
                    if (pool.inactivedCount > 0)
                    {
                        item = pool.Get();
                        list.Add(item);
                    }
                }
                if (item == null)
                {
                    item = VirtualPackItem.Get(packType);
                    list.Add(item);
                }
                item.ParsePackItem(items[i].ItemPlace, items[i].ItemData);
            }
        }
 
        static void OnGetItem(VirtualPackItem item)
        {
 
        }
 
        static void OnReleaseItem(VirtualPackItem item)
        {
            item.Release();
        }
    }
 
    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; 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 virtual void Release()
        {
            dataString = null;
        }
 
        public static VirtualPackItem Get(PackType packType)
        {
            switch (packType)
            {
                case PackType.GatherSoul:
                    return new GatherSoulItem();
                case PackType.RunePack:
                    return new RuneItem();
            }
            return null;
        }
    }
 
    public struct VirtualItem
    {
        public PackType packType;
        public int itemId;
        public int index;
        public int level;
    }
}