You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
6.5 KiB

using System.Collections.Generic;
using System.Linq;
namespace ET
{
3 years ago
public class StoreComponentSystemDestroySystem: DestroySystem<StoreComponent>
{
public override void Destroy(StoreComponent self)
{
}
}
3 years ago
[FriendClass(typeof (Item))]
public static class StoreComponentSystem
{
3 years ago
public static void Add(this StoreComponent self, int configId, long amount, long id = 0, bool bUpdateTime = false)
{
var config = AllItemConfigCategory.Instance.Get(configId);
if (config.SuperpositionMax > 1)
{
foreach (var v in self.Children.Values)
{
var tempItem = (Item) v;
if (tempItem.ConfigId == configId)
{
3 years ago
tempItem.Amount += amount;
return;
}
}
Item item;
if (id == 0)
{
item = self.AddChild<Item>();
}
else
{
item = self.AddChildWithId<Item>(id);
}
3 years ago
item.Amount = amount;
item.ConfigId = configId;
if (bUpdateTime)
{
item.CreateTime = TimeHelper.ServerUnix();
}
}
else
{
for (int i = 0; i < amount; i++)
{
Item item;
if (id == 0)
{
item = self.AddChild<Item>();
}
else
{
item = self.AddChildWithId<Item>(id);
}
3 years ago
item.Amount = 1;
item.ConfigId = configId;
if (bUpdateTime)
{
item.CreateTime = TimeHelper.ServerUnix();
}
}
}
}
public static long GetItemNum(this StoreComponent self, int configId)
{
var config = AllItemConfigCategory.Instance.Get(configId);
if (config.SuperpositionMax > 1)
{
foreach (var v in self.Children.Values)
{
var item = (Item) v;
if (item.ConfigId == configId)
{
return item.Amount;
}
}
}
else
{
long num = 0;
foreach (var v in self.Children.Values)
{
3 years ago
if (((Item) v).Config == config)
{
num++;
}
return num;
}
}
return 0;
}
public static bool Remove(this StoreComponent self, int configId, int amount)
{
if (GetItemNum(self, configId) < amount)
{
return false;
}
3 years ago
var config = AllItemConfigCategory.Instance.Get(configId);
if (config.SuperpositionMax > 1)
{
foreach (var v in self.Children.Values)
{
3 years ago
var item = (Item) v;
if (item.ConfigId == configId)
{
item.Amount -= amount;
if (item.Amount <= 0)
{
item.Dispose();
}
3 years ago
return true;
}
}
}
else
{
List<Item> removeList = new List<Item>();
foreach (var v in self.Children)
{
3 years ago
var item = (Item) v.Value;
if (item.ConfigId == configId)
{
removeList.Add(item);
if (removeList.Count == amount)
{
break;
}
}
}
foreach (var v in removeList)
{
self.RemoveChildWithId(v.Id);
}
}
return true;
}
/// <summary>
/// 获得仓库内指定类型(可选子类型)的所有道具
/// </summary>
/// <param name="self"></param>
/// <param name="mainType">AllItem表内的关联表ID</param>
/// <param name="subType">关联表对应的子类型</param>
/// <returns></returns>
public static List<Item> GetItemsWithType(this StoreComponent self, int mainType, int subType = -1)
{
3 years ago
if (mainType == 0) return null;
var result = new List<Item>();
foreach (var v in self.Children.Values)
{
3 years ago
var item = (Item) v;
var config = AllItemConfigCategory.Instance.Get(item.ConfigId);
3 years ago
if (config.RelatedTable != mainType) continue;
if (subType == -1)
{
result.Add(item);
}
else
{
switch (mainType)
{
case 1:
3 years ago
{
MaterialConfig materialConfig = MaterialConfigCategory.Instance.Get(config.RelatedId);
if (materialConfig == null) continue;
var types = materialConfig.Type.ToList();
if (types.Contains(subType))
{
3 years ago
result.Add(item);
}
3 years ago
}
break;
}
}
}
3 years ago
return result;
}
public static bool IsEnoughItem(this StoreComponent self, Dictionary<int, int> itemDic)
{
foreach (var pair in itemDic)
{
if (self.GetItemNum(pair.Key) < pair.Value)
{
return false;
}
}
return true;
}
3 years ago
public static bool IsEnoughItem(this StoreComponent self, int configId, int amount)
{
if (self.GetItemNum(configId) < amount)
{
return false;
}
return true;
}
}
}