259 lines
8.9 KiB
C#
259 lines
8.9 KiB
C#
|
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 var 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)
|
|||
|
{
|
|||
|
if (self.StoreDic.TryGetValueByKey2(0, out var nullList))
|
|||
|
{
|
|||
|
var 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 var list))
|
|||
|
{
|
|||
|
CalucateCount();
|
|||
|
}
|
|||
|
while (count > 0)
|
|||
|
{
|
|||
|
if (self.ItemCount + 1 > self.MaxItemCount)
|
|||
|
{
|
|||
|
return item.Count - count;
|
|||
|
}
|
|||
|
if (self.StoreDic.TryGetValueByKey2(0, out var nullList))
|
|||
|
{
|
|||
|
var nullItem = nullList[0];
|
|||
|
int nullItemIndex = nullItem.index;
|
|||
|
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 (var 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 var list))
|
|||
|
{
|
|||
|
return self.CanAddNewItem;
|
|||
|
}
|
|||
|
int maxCount = BagHelper.GetItemBase(itemId) switch
|
|||
|
{
|
|||
|
GoodsBase goodsBase => goodsBase.MaxAmount,
|
|||
|
MaterialBase materialBase => materialBase.MaxAmount,
|
|||
|
_ => throw new InvalidCastException($"物品类型错误,id= {itemId}"),
|
|||
|
};
|
|||
|
foreach (var item in list)
|
|||
|
{
|
|||
|
count = count + item.Count - maxCount;
|
|||
|
if (count <= 0) return true;
|
|||
|
}
|
|||
|
if (count <= 0) return true;
|
|||
|
if (!self.StoreDic.TryGetValueByKey2(0, out var nullItemList))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
foreach (var item in nullItemList)
|
|||
|
{
|
|||
|
count -= maxCount;
|
|||
|
if (count <= 0) return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
public static Item DeleteItem(this Store self, int index, int count)
|
|||
|
{
|
|||
|
if (self.StoreDic.TryGetValueByKey1(index, out var item))
|
|||
|
{
|
|||
|
if (!item.IsEmpty)
|
|||
|
{
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
public static string SortStore(this Store self, int page)
|
|||
|
{
|
|||
|
List<(int, int, Item)> sourceList = new List<(int, int, Item)>(Store.Slot_PerPage);
|
|||
|
|
|||
|
int index = -1;
|
|||
|
foreach (var kv in self.StoreDic)
|
|||
|
{
|
|||
|
index++;
|
|||
|
if (index >= page * Store.Slot_PerPage)
|
|||
|
{
|
|||
|
if (index < (page + 1) * Store.Slot_PerPage)
|
|||
|
sourceList.Add((kv.Key.Key, kv.Key.Value, kv.Value));
|
|||
|
else
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
foreach (var (_index, id, item) in sourceList)
|
|||
|
{
|
|||
|
self.StoreDic.Remove(_index, id, item);
|
|||
|
self.ItemCount--;
|
|||
|
}
|
|||
|
index = page * Store.Slot_PerPage - 1;
|
|||
|
for (int i = 0; i < sourceList.Count; i++)
|
|||
|
{
|
|||
|
var (_index, id, item) = sourceList[i];
|
|||
|
++index;
|
|||
|
if (id == 0 || item.IsEmpty)
|
|||
|
{
|
|||
|
sourceList[i] = (index, 0, Item.Empty(index));
|
|||
|
continue;
|
|||
|
}
|
|||
|
item.index = index;
|
|||
|
sourceList[i] = (index, id, item);
|
|||
|
}
|
|||
|
sourceList.Sort((a, b) => a.Item2.CompareTo(b.Item2));
|
|||
|
for (int i = 0; i < sourceList.Count; i++)
|
|||
|
{
|
|||
|
var (_, id, item) = sourceList[i];
|
|||
|
int _index = i + page * Store.Slot_PerPage;
|
|||
|
item.index = _index;
|
|||
|
self.StoreDic.Add(_index, id, item);
|
|||
|
self.ItemCount++;
|
|||
|
}
|
|||
|
return string.Empty;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|