using System; namespace ET { [FriendClass(typeof(Item))] public static class StoreHelper { public static async ETTask GetStore(Unit unit) { try { M2C_GetStore resp = await unit.ZoneScene().GetComponent().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(); component.Dispose(); component = unit.GetOrAddComponent(); 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; } /// /// 获取指定合成表内指定ID道具的目前最大可制作数量 /// /// /// /// public static int GetMaximumSythesisNum(Unit unit, int synthesisId) { if (synthesisId <= 0) return 0; var sc = unit.GetComponent(); 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 UseItem(Unit unit, int configId, int amount, long id = 0) { try { Log.Debug("UseItem:" + configId); M2C_UseItem resp = await unit.ZoneScene().GetComponent().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 DropItem(Unit unit, int configId, int amount) { try { M2C_DropItem resp = await unit.ZoneScene().GetComponent().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 UseSaplingItem(Unit unit, int itemId, float posX, float posY) { try { M2C_UseSaplingItem resp = await unit.ZoneScene().GetComponent().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; } } }