323 lines
10 KiB
C#
Executable File
323 lines
10 KiB
C#
Executable File
using Cal.DataTable;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.Linq;
|
||
|
||
namespace ET
|
||
{
|
||
public class StoreAwakeSystem: AwakeSystem<Store>
|
||
{
|
||
public override void Awake(Store self)
|
||
{
|
||
self.isOpenRemote = false;
|
||
}
|
||
}
|
||
|
||
public class StoreDestroySystem: DestroySystem<Store>
|
||
{
|
||
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));
|
||
}
|
||
}
|
||
|
||
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));
|
||
}
|
||
}
|
||
|
||
public static void CheckSlot(this Store self)
|
||
{
|
||
if (self.StoreDic.Count == self.MaxItemCount)
|
||
{
|
||
return;
|
||
}
|
||
|
||
self.ItemCount = 0;
|
||
for (int i = 0; i < self.MaxItemCount; i++)
|
||
{
|
||
if (!self.StoreDic.TryGetValueByKey1(i, out Item item))
|
||
self.StoreDic.Add(i, 0, Item.Empty(i));
|
||
else
|
||
{
|
||
if (item.ItemId != 0)
|
||
self.ItemCount++;
|
||
}
|
||
}
|
||
}
|
||
|
||
public static int AddItem(this Store self, Item item)
|
||
{
|
||
if (item.ItemType != ItemType.EquipItem)
|
||
{
|
||
//!判断是否可以堆叠
|
||
return CalucateStacking(self, item);
|
||
}
|
||
|
||
if (!self.CanAddNewItem)
|
||
{
|
||
return 0;
|
||
}
|
||
{
|
||
if (!self.StoreDic.TryGetValueByKey2(0, out List<Item> nullList))
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
return 0;
|
||
|
||
//!可堆叠物品逻辑
|
||
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}"),
|
||
};
|
||
if (self.StoreDic.TryGetValueByKey2(itemId, out List<Item> list))
|
||
{
|
||
CalucateCount();
|
||
}
|
||
|
||
while (count > 0)
|
||
{
|
||
if (self.ItemCount + 1 > self.MaxItemCount)
|
||
{
|
||
return item.Count - count;
|
||
}
|
||
|
||
if (self.StoreDic.TryGetValueByKey2(0, out List<Item> nullList))
|
||
{
|
||
Item nullItem = nullList[0];
|
||
int nullItemIndex = nullItem.index;
|
||
const int nullItemId = 0;
|
||
self.StoreDic.Remove(nullItemIndex, nullItemId, nullItem);
|
||
self.StoreDic.Add(nullItemIndex, itemId, new Item(nullItemIndex, item.data, 0));
|
||
self.ItemCount++;
|
||
}
|
||
|
||
if (self.StoreDic.TryGetValueByKey2(itemId, out list))
|
||
{
|
||
CalucateCount();
|
||
}
|
||
}
|
||
|
||
return item.Count;
|
||
|
||
//!计算堆叠后剩余数量
|
||
void CalucateCount()
|
||
{
|
||
foreach (Item bagItem in list)
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
public static bool CanAddItem(this Store self, int itemId, int count)
|
||
{
|
||
if (!self.StoreDic.TryGetValueByKey2(itemId, out List<Item> list))
|
||
{
|
||
return self.CanAddNewItem;
|
||
}
|
||
|
||
int maxCount = BagHelper.GetItemBase(itemId) switch
|
||
{
|
||
GoodsBase goodsBase => goodsBase.MaxAmount,
|
||
MaterialBase materialBase => materialBase.MaxAmount,
|
||
_ => throw new InvalidCastException($"物品类型错误,id= {itemId}"),
|
||
};
|
||
foreach (Item item in list)
|
||
{
|
||
count = count + item.Count - maxCount;
|
||
if (count <= 0) return true;
|
||
}
|
||
|
||
if (count <= 0) return true;
|
||
if (!self.StoreDic.TryGetValueByKey2(0, out List<Item> nullItemList))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
foreach (Item item in nullItemList)
|
||
{
|
||
count -= maxCount;
|
||
if (count <= 0) return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
public static Item GetItem(this Store self, int index, int count)
|
||
{
|
||
if (!self.StoreDic.TryGetValueByKey1(index, out Item item))
|
||
{
|
||
return null;
|
||
}
|
||
if (item.IsEmpty)
|
||
{
|
||
return null;
|
||
}
|
||
if (item.Count < count)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
return item;
|
||
|
||
}
|
||
public static Item DeleteItem(this Store self, int index, int count)
|
||
{
|
||
if (!self.StoreDic.TryGetValueByKey1(index, out Item item))
|
||
{
|
||
return null;
|
||
}
|
||
if (item.IsEmpty)
|
||
{
|
||
return null;
|
||
}
|
||
if (item.Count > count)
|
||
{
|
||
item.Count -= count;
|
||
}
|
||
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;
|
||
|
||
}
|
||
|
||
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;
|
||
|
||
int index = -1;
|
||
foreach (KeyValuePair<KeyValuePair<int, int>, Item> kv in self.StoreDic)
|
||
{
|
||
index++;
|
||
if (index < page * Store.Slot_PerPage)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
if (index < (page + 1) * Store.Slot_PerPage)
|
||
sourceList.Add((kv.Key.Key, kv.Key.Value, kv.Value));
|
||
else
|
||
break;
|
||
}
|
||
|
||
foreach ((int _index, int id, Item item) in sourceList)
|
||
{
|
||
self.StoreDic.Remove(_index, id, item);
|
||
if (id == 0 || item.IsEmpty)
|
||
{
|
||
continue;
|
||
}
|
||
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();
|
||
index = page * Store.Slot_PerPage - 1;
|
||
for (int i = 0; i < listComponent2.List.Count; i++)
|
||
{
|
||
(int _index, int id, Item item) = listComponent2.List[i];
|
||
++index;
|
||
if (id == 0 || item.IsEmpty)
|
||
{
|
||
continue;
|
||
}
|
||
|
||
sourceList.Add((index, id, item));
|
||
}
|
||
|
||
sourceList.Sort((a, b) => a.Item2.CompareTo(b.Item2));
|
||
for (int i = 0; i < sourceListCount; i++)
|
||
{
|
||
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));
|
||
}
|
||
}
|
||
|
||
return string.Empty;
|
||
}
|
||
}
|
||
} |