少年修仙传客户端代码仓库
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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Snxxz.UI;
 
namespace Snxxz.UI
{
    public class SinglePackModel
    {
        public readonly PackType type;
        public int openGridCount { get; private set; }
        private Dictionary<int, ItemModel> items = new Dictionary<int, ItemModel>(); //key 物品位置索引
 
        PackModelInterface modelInterface { get { return ModelCenter.Instance.GetModel<PackModelInterface>(); } }
        PlayerPackModel playerPack { get { return ModelCenter.Instance.GetModel<PlayerPackModel>(); } }
 
        public SinglePackModel(PackType type)
        {
            this.type = type;
        }
 
        /// <summary>
        /// 增加和刷新数据
        /// </summary>
        /// <param name="itemInfo"></param>
        /// <param name="type"></param>
        public void UpdateItem(ItemInfo itemInfo)
        {
            var index = itemInfo.ItemPlace;
            if (!items.ContainsKey(index))
            {
                var itemModel = new ItemModel(type);
                itemModel.SetItemInfo(itemInfo);
                items.Add(index, itemModel);
            }
            else
            {
                items[index].SetItemInfo(itemInfo);
            }
 
            playerPack.SetItemGUIDDict(items[index]);
        }
 
        public void SetOpenGridCount(int count)
        {
            openGridCount = count;
        }
 
        public ItemModel GetItemByIndex(int index)
        {
            switch (type)
            {
                case PackType.JadeDynastyEquip:
                    if (index >= 121)
                    {
                        index = index - 121;
                    }
                    break;
            }
 
            ItemModel item = null;
            items.TryGetValue(index, out item);
            return item;
        }
 
        public Dictionary<int, ItemModel> GetAllItems()
        {
            return items;
        }
 
        /// <summary>
        /// 得到此ID物品的所有数量
        /// </summary>
        /// <returns></returns>
        public int GetItemCountByID(int itemId, out List<ItemModel> list)
        {
            int count = 0;
            list = new List<ItemModel>();
            foreach (var model in items.Values)
            {
                if (model.itemId == itemId)
                {
                    count += model.count;
                    list.Add(model);
                }
            }
 
            return count;
        }
 
        /// <summary>
        /// 得到此ID物品的所有数量
        /// </summary>
        /// <returns></returns>
        public int GetItemCountByID(int itemId)
        {
            int count = 0;
            foreach (ItemModel model in items.Values)
            {
                if (model.itemId == itemId)
                {
                    count += model.count;
                }
            }
 
            return count;
        }
 
        /// <summary>
        /// 得到此类型物品的所有数量
        /// </summary>
        /// <param name="itemType"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public int GetItemCountByType(int itemType, out List<ItemModel> list)
        {
            int count = 0;
            list = new List<ItemModel>();
            foreach (ItemModel model in items.Values)
            {
                if (model.config.Type == itemType)
                {
                    count += model.count;
                    list.Add(model);
                }
            }
            return count;
        }
 
        /// <summary>
        /// 得到相同品质物品
        /// </summary>
        /// <param name="_quality"></param>
        /// <returns></returns>
        public List<int> GetItemsByQuality(int _quality)
        {
            var itemList = new List<int>();
            foreach (var value in items.Values)
            {
                if (value.config.ItemColor == _quality)
                {
                    itemList.Add(value.itemId);
                }
            }
 
            return itemList;
        }
 
        /// <summary>
        /// 得到指定数量物品绑定和未绑定的索引
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="needCount"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public List<int> ItemIndexlist(int itemId, int needCount)
        {
            List<int> itemIndexlist = new List<int>();
            List<ItemModel> itemModellist = new List<ItemModel>();
            foreach (ItemModel model in items.Values)
            {
                if (model.itemId == itemId)
                {
                    itemModellist.Add(model);
                }
            }
 
            itemModellist.Sort(CompareIsBind);
 
            int i = 0;
            int count = 0;
            for (i = 0; i < itemModellist.Count; i++)
            {
                if (count < needCount)
                {
                    itemIndexlist.Add(itemModellist[i].itemPlace);
                    count += itemModellist[i].count;
                }
                else
                {
                    break;
                }
 
            }
 
            return itemIndexlist;
        }
 
        /// <summary>
        /// 得到相同Id所有绑定 或不绑定的物品
        /// </summary>
        /// <param name="itemId"></param>
        /// <param name="isBind"></param>
        /// <returns></returns>
        public List<int> ItemIndexlistByIsBind(int itemId, int needCnt, int isBind)
        {
            List<int> itemIndexlist = new List<int>();
            List<ItemModel> itemModellist = new List<ItemModel>();
            foreach (ItemModel model in items.Values)
            {
                if (model.itemId == itemId)
                {
                    itemModellist.Add(model);
                }
            }
 
            int count = 0;
            for (int i = 0; i < itemModellist.Count; i++)
            {
                if (count < needCnt)
                {
                    if (itemModellist[i].isBind == isBind)
                    {
                        itemIndexlist.Add(itemModellist[i].itemPlace);
                        count += itemModellist[i].count;
                    }
                }
                else
                {
                    break;
                }
            }
 
            for (int i = 0; i < itemModellist.Count; i++)
            {
                if (count < needCnt)
                {
                    if (itemModellist[i].isBind != isBind)
                    {
                        itemIndexlist.Add(itemModellist[i].itemPlace);
                        count += itemModellist[i].count;
                    }
                }
                else
                {
                    break;
                }
            }
            return itemIndexlist;
        }
 
        private int CompareIsBind(ItemModel start, ItemModel end)
        {
            int startIsBind = start.isBind;
            int endIsBind = end.isBind;
            if (startIsBind.CompareTo(endIsBind) != 0)
                return -startIsBind.CompareTo(endIsBind);
 
            return 0;
        }
 
        /// <summary>
        /// 得到第一个空格位置索引
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public int GetNullGridIndex()
        {
            int i = -1;
            for (i = 0; i < openGridCount; i++)
            {
                if (!items.ContainsKey(i))
                {
                    return i;
                }
            }
            return i;
        }
 
        /// <summary>
        /// 得到剩余背包格子数
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public int GetRemainGridCount()
        {
            return openGridCount - items.Count;
        }
        public void ClearItemModelByIndex(int index)
        {
            if (items.ContainsKey(index))
            {
                items.Remove(index);
            }
        }
 
        public void ClearPackModel()
        {
            items.Clear();
        }
    }
}