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.

130 lines
4.7 KiB

using System;
namespace ET
{
[FriendClass(typeof(Item))]
public static class StoreHelper
{
public static async ETTask<int> GetStore(Unit unit)
{
try
{
M2C_GetStore resp = await unit.ZoneScene().GetComponent<SessionComponent>().Session.Call(new C2M_GetStore{}) as M2C_GetStore;
if (resp.Error != ErrorCode.ERR_Success)
{
Log.Error(resp.Error.ToString());
return resp.Error;
}
StoreComponent component = unit.GetOrAddComponent<StoreComponent>();
component.Dispose();
component = unit.GetOrAddComponent<StoreComponent>();
foreach (var v in resp.Store)
{
var item = new Item();
item.FromMessage(v);
component.Add(item.ConfigId,item.Amount,item.Id);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
await ETTask.CompletedTask;
return ErrorCode.ERR_Success;
}
/// <summary>
/// 获取指定合成表内指定ID道具的目前最大可制作数量
/// </summary>
/// <param name="unit"></param>
/// <param name="synthesisId"></param>
/// <returns></returns>
public static int GetMaximumSythesisNum(Unit unit, int synthesisId)
{
if (synthesisId <= 0) return 0;
var sc = unit.GetComponent<StoreComponent>();
SynthesisConfig config = SynthesisConfigCategory.Instance.Get(synthesisId);
int result = 0;
for(int i = 0; i < config.ItemId.Length; i++)
{
int subMatId = config.ItemId[i];
int subRequire = config.ItemNum[i];
long curStoreRemain = sc.GetItemNum(subMatId);
int makableNum = (int)(curStoreRemain / subRequire);
result = i == 0 ? makableNum : Math.Min(result, makableNum);
}
return result;
}
public static async ETTask<int> UseItem(Unit unit, int configId, int amount, long id = 0)
{
try
{
Log.Debug("UseItem:" + configId);
M2C_UseItem resp = await unit.ZoneScene().GetComponent<SessionComponent>().Session
.Call(new C2M_UseItem() { ConfigId = configId, Amount = amount ,Id = id}) as M2C_UseItem;
if (resp.Error != ErrorCode.ERR_Success)
{
Log.Error(resp.Error.ToString());
return resp.Error;
}
StoreOperate.UseItem(unit, configId, amount, id);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
await ETTask.CompletedTask;
return ErrorCode.ERR_Success;
}
public static async ETTask<int> DropItem(Unit unit, int configId, int amount)
{
try
{
M2C_DropItem resp = await unit.ZoneScene().GetComponent<SessionComponent>().Session.Call(new C2M_DropItem(){ ConfigId = configId, Amount = amount}) as M2C_DropItem;
if (resp.Error != ErrorCode.ERR_Success)
{
Log.Error(resp.Error.ToString());
return resp.Error;
}
StoreOperate.DropItem(unit, configId, amount);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
await ETTask.CompletedTask;
return ErrorCode.ERR_Success;
}
public static async ETTask<int> UseSaplingItem(Unit unit, int itemId, float posX, float posY)
{
try
{
M2C_UseSaplingItem resp = await unit.ZoneScene().GetComponent<SessionComponent>().Session.Call(new C2M_UseSaplingItem(){ ItemId = itemId, X = posX, Y = posY}) as M2C_UseSaplingItem;
if (resp.Error != ErrorCode.ERR_Success)
{
Log.Error(resp.Error.ToString());
return resp.Error;
}
var res = StoreOperate.UseSaplingItem(unit, itemId, posX, posY);
Game.EventSystem.Publish(new EventType.AfterCreateResource(){Unit = unit, Resource = res});
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return ErrorCode.ERR_Success;
}
}
}