CTT/Server/Hotfix/Game/System/UI/StoreSystem.cs

306 lines
9.6 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using Cal.DataTable;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace ET
{
public class StoreAwakeSystem: AwakeSystem<Store>
2021-04-08 20:09:59 +08:00
{
public override void Awake(Store self)
{
self.isOpenRemote = false;
}
}
public class StoreDestroySystem: DestroySystem<Store>
2021-04-08 20:09:59 +08:00
{
public override void Destroy(Store self)
{
self.ItemCount = 0;
self.CoinCount = 0;
self.StoreDic.Clear();
self.MaxItemCount = self.InitMaxItemCount;
self.isOpenRemote = false;
self.Max_Page = Store.InitMax_Page;
}
}
public static class StoreSystem
{
public static void Init(this Store self)
{
self.StoreDic.Clear();
for (int i = 0; i < self.InitMaxItemCount; i++)
{
self.StoreDic.Add(i, 0, Item.Empty(i));
}
}
2021-04-08 20:09:59 +08:00
public static void ExtandStore(this Store self)
{
int oldMax = self.MaxItemCount;
self.MaxItemCount += Store.Slot_PerPage;
self.Max_Page++;
for (int i = oldMax; i < self.MaxItemCount; i++)
{
self.StoreDic.Add(i, 0, Item.Empty(i));
}
}
2021-04-08 20:09:59 +08:00
public static void CheckSlot(this Store self)
{
if (self.StoreDic.Count == self.MaxItemCount)
{
return;
}
2021-04-08 20:09:59 +08:00
self.ItemCount = 0;
for (int i = 0; i < self.MaxItemCount; i++)
{
2021-04-11 19:50:39 +08:00
if (!self.StoreDic.TryGetValueByKey1(i, out Item item))
2021-04-08 20:09:59 +08:00
self.StoreDic.Add(i, 0, Item.Empty(i));
else
{
if (item.ItemId != 0)
self.ItemCount++;
}
}
}
2021-04-08 20:09:59 +08:00
public static int AddItem(this Store self, Item item)
{
if (item.ItemType != ItemType.EquipItem)
{
//!判断是否可以堆叠
return CalucateStacking(self, item);
}
2021-04-15 00:12:07 +08:00
if (!self.CanAddNewItem)
2021-04-08 20:09:59 +08:00
{
2021-04-15 00:12:07 +08:00
return 0;
}
{
if (!self.StoreDic.TryGetValueByKey2(0, out List<Item> nullList))
2021-04-08 20:09:59 +08:00
{
2021-04-15 00:12:07 +08:00
return 0;
2021-04-08 20:09:59 +08:00
}
2021-04-15 00:12:07 +08:00
Item nullItem = nullList[0];
int nullItemindex = nullItem.index;
self.StoreDic.Remove(nullItemindex, 0, nullItem);
item.index = nullItemindex;
self.StoreDic.Add(nullItemindex, item.ItemId, item);
self.ItemCount++;
return 1;
2021-04-08 20:09:59 +08:00
}
2021-04-08 20:09:59 +08:00
return 0;
2021-04-08 20:09:59 +08:00
//!可堆叠物品逻辑
static int CalucateStacking(Store self, Item item)
{
int itemId = item.ItemId;
int count = item.Count;
int maxCount = item.ItemType switch
{
ItemType.GoodsItem => DataTableHelper.Get<GoodsBase>(itemId).MaxAmount,
ItemType.MaterialsItem => DataTableHelper.Get<MaterialBase>(itemId).MaxAmount,
_ => throw new InvalidCastException($"物品类型错误type= {item.ItemType}"),
};
2021-04-11 19:50:39 +08:00
if (self.StoreDic.TryGetValueByKey2(itemId, out List<Item> list))
2021-04-08 20:09:59 +08:00
{
CalucateCount();
}
2021-04-08 20:09:59 +08:00
while (count > 0)
{
if (self.ItemCount + 1 > self.MaxItemCount)
{
return item.Count - count;
}
2021-04-11 19:50:39 +08:00
if (self.StoreDic.TryGetValueByKey2(0, out List<Item> nullList))
2021-04-08 20:09:59 +08:00
{
2021-04-11 19:50:39 +08:00
Item nullItem = nullList[0];
2021-04-08 20:09:59 +08:00
int nullItemIndex = nullItem.index;
2021-04-15 00:12:07 +08:00
const int nullItemId = 0;
2021-04-08 20:09:59 +08:00
self.StoreDic.Remove(nullItemIndex, nullItemId, nullItem);
self.StoreDic.Add(nullItemIndex, itemId, new Item(nullItemIndex, item.data, 0));
self.ItemCount++;
}
2021-04-08 20:09:59 +08:00
if (self.StoreDic.TryGetValueByKey2(itemId, out list))
{
CalucateCount();
}
}
2021-04-08 20:09:59 +08:00
return item.Count;
2021-04-08 20:09:59 +08:00
//!计算堆叠后剩余数量
void CalucateCount()
{
2021-04-11 19:50:39 +08:00
foreach (Item bagItem in list)
2021-04-08 20:09:59 +08:00
{
if (bagItem.IsLock == item.IsLock &&
bagItem.Count < maxCount)
{
int oldCount = bagItem.Count;
bagItem.Count += count;
if (bagItem.Count > maxCount)
{
int dCount = bagItem.Count - maxCount;
bagItem.Count -= dCount;
count -= maxCount - oldCount;
}
else
{
count -= bagItem.Count - oldCount;
break;
}
}
else
{
continue;
}
}
}
}
}
2021-04-08 20:09:59 +08:00
public static bool CanAddItem(this Store self, int itemId, int count)
{
2021-04-11 19:50:39 +08:00
if (!self.StoreDic.TryGetValueByKey2(itemId, out List<Item> list))
2021-04-08 20:09:59 +08:00
{
return self.CanAddNewItem;
}
2021-04-08 20:09:59 +08:00
int maxCount = BagHelper.GetItemBase(itemId) switch
{
GoodsBase goodsBase => goodsBase.MaxAmount,
MaterialBase materialBase => materialBase.MaxAmount,
_ => throw new InvalidCastException($"物品类型错误id= {itemId}"),
};
2021-04-11 19:50:39 +08:00
foreach (Item item in list)
2021-04-08 20:09:59 +08:00
{
count = count + item.Count - maxCount;
if (count <= 0) return true;
}
2021-04-08 20:09:59 +08:00
if (count <= 0) return true;
2021-04-11 19:50:39 +08:00
if (!self.StoreDic.TryGetValueByKey2(0, out List<Item> nullItemList))
2021-04-08 20:09:59 +08:00
{
return false;
}
2021-04-11 19:50:39 +08:00
foreach (Item item in nullItemList)
2021-04-08 20:09:59 +08:00
{
count -= maxCount;
if (count <= 0) return true;
}
2021-04-08 20:09:59 +08:00
return false;
}
2021-04-08 20:09:59 +08:00
public static Item DeleteItem(this Store self, int index, int count)
{
2021-04-15 00:12:07 +08:00
if (!self.StoreDic.TryGetValueByKey1(index, out Item item))
2021-04-08 20:09:59 +08:00
{
2021-04-15 00:12:07 +08:00
return null;
}
if (item.IsEmpty)
{
return null;
}
if (item.Count > count)
{
item.Count -= count;
2021-04-08 20:09:59 +08:00
}
2021-04-15 00:12:07 +08:00
else if (item.Count == count)
{
self.StoreDic.Remove(index, item.ItemId, item);
self.StoreDic.Add(index, 0, Item.Empty(index));
self.ItemCount--;
}
else
{
return null;
}
return item;
2021-04-08 20:09:59 +08:00
}
2021-04-08 20:09:59 +08:00
public static string SortStore(this Store self, int page)
{
//index itemId item
using ListComponent<(int, int, Item)> listComponent1 = ListComponent<(int, int, Item)>.Create();
List<(int, int, Item)> sourceList = listComponent1.List;
2021-04-08 20:09:59 +08:00
int index = -1;
2021-04-11 19:50:39 +08:00
foreach (KeyValuePair<KeyValuePair<int, int>, Item> kv in self.StoreDic)
2021-04-08 20:09:59 +08:00
{
index++;
if (index < page * Store.Slot_PerPage)
2021-04-08 20:09:59 +08:00
{
continue;
2021-04-08 20:09:59 +08:00
}
if (index < (page + 1) * Store.Slot_PerPage)
sourceList.Add((kv.Key.Key, kv.Key.Value, kv.Value));
else
break;
2021-04-08 20:09:59 +08:00
}
2021-04-11 19:50:39 +08:00
foreach ((int _index, int id, Item item) in sourceList)
2021-04-08 20:09:59 +08:00
{
self.StoreDic.Remove(_index, id, item);
if (id == 0 || item.IsEmpty)
{
continue;
}
2021-04-08 20:09:59 +08:00
self.ItemCount--;
}
const int sourceListCount = Store.Slot_PerPage;
using ListComponent<(int, int, Item)> listComponent2 = ListComponent<(int, int, Item)>.Create();
listComponent2.List.AddRange(sourceList);
sourceList.Clear();
2021-04-08 20:09:59 +08:00
index = page * Store.Slot_PerPage - 1;
for (int i = 0; i < listComponent2.List.Count; i++)
2021-04-08 20:09:59 +08:00
{
(int _index, int id, Item item) = listComponent2.List[i];
2021-04-08 20:09:59 +08:00
++index;
if (id == 0 || item.IsEmpty)
{
continue;
}
sourceList.Add((index, id, item));
2021-04-08 20:09:59 +08:00
}
2021-04-08 20:09:59 +08:00
sourceList.Sort((a, b) => a.Item2.CompareTo(b.Item2));
for (int i = 0; i < sourceListCount; i++)
2021-04-08 20:09:59 +08:00
{
int _index = i + page * Store.Slot_PerPage;
if (i < sourceList.Count)
{
(_, int id, Item item) = sourceList[i];
item.index = _index;
self.StoreDic.Add(_index, id, item);
self.ItemCount++;
}
else
{
self.StoreDic.Add(_index, 0, Item.Empty(_index));
}
2021-04-08 20:09:59 +08:00
}
2021-04-08 20:09:59 +08:00
return string.Empty;
}
}
}