using Cal.DataTable; using System; using System.Collections.Generic; using System.Text; namespace ET { public class StoreComponentAwakeSystem: AwakeSystem { public override void Awake(StoreComponent self) { StoreComponent.Instance = self; } } public static class StoreComponentSystem { public static readonly int[] extandStorePrice = new[] { 100 * 10000, 200 * 10000, 400 * 10000, 800 * 10000, 1600 * 10000, 3200 * 10000, 6400 * 10000, 12800 * 10000, 25600 * 10000, 51200 * 10000, 200, 300, 400, 600, 800, 1000, 1500, 3000, 3000, 3000 }; public const int RemoteStorePrice = 150 * 10000; public static async ETTask Query(this StoreComponent self, long id) { if (!self.StoreDic.TryGetValue(id, out Store store)) { store = await DBComponent.Instance.Query(id); if (store == null) { store = EntityFactory.CreateWithId(self, id); } self.StoreDic[id] = store; } return store; } public static async ETTask Save(this StoreComponent self, Store store) { await DBComponent.Instance.Save(store); } public static async ETTask Init(this StoreComponent self, long id) { Store store = await self.Query(id); store.Init(); await DBComponent.Instance.Save(store); } public static async ETTask<(string, long, int)> GetStore(this StoreComponent self, Unit unit, int page, System.Collections.Generic.List storeList) { Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit?.Id}"); return ("系统错误", 0, 0); } if (page >= store.Max_Page) { Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName} ({unit.Id})】想要获取page = {page}的仓库界面,超过界限"); return ("系统错误", 0, 0); } try { if (store.StoreDic.Count == 0) { await self.Init(unit.Id); } GetUpdatedList(store, unit.Id, page, storeList); } catch (Exception e) { Log.Error(e); } return (string.Empty, store.CoinCount, store.Max_Page); } public static async ETTask SortStore(this StoreComponent self, Unit unit, int page, System.Collections.Generic.List storeList) { await ETTask.CompletedTask; // return "修复中"; Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit?.Id}"); return "系统错误"; } if (page >= store.Max_Page) { Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName} ({unit.Id})】想要获取page = {page}的仓库界面"); return "系统错误"; } var now = TimeHelper.ClientNow(); if (!AppConfig.inst.isTest && now - store.lastSortTime <= store.SortTimeInterval) { return $"请等待{(store.lastSortTime + store.SortTimeInterval - now) / 1000}s之后再整理"; } store.lastSortTime = now; store.SortStore(page); await self.Save(store); GetUpdatedList(store, unit.Id, page, storeList); return string.Empty; } public static bool CanAddItem(this StoreComponent self, Unit unit, Store store, int itemId, int count) { IConfig itemBase = BagHelper.GetItemBase(itemId); switch (itemBase) { case EquipBase equipBase: return store.CanAddNewItem; case GoodsBase goodsBase: case MaterialBase materialBase: return store.CanAddItem(itemId, count); } return false; } public static async ETTask<(string, int, int)> QueryExtand(this StoreComponent self, Unit unit) { Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit?.Id}"); return ("系统错误", 0, 0); } //!money int index = store.Max_Page - Store.InitMax_Page; if (index >= extandStorePrice.Length) { return ("背包已经无法扩展", 0, 0); } int price = extandStorePrice[index]; int coin = 0, voucher = 0; if (index >= 10) voucher = price; else coin = price; return (null, coin, voucher); } public static async ETTask ExtandStore(this StoreComponent self, Unit unit) { Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit?.Id}"); return "系统错误"; } //!money int index = store.Max_Page - Store.InitMax_Page; if (index >= extandStorePrice.Length) { return "背包已经无法扩展"; } int price = extandStorePrice[index]; if (index >= 10) { //!代金券 string ret = CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Voucher, price); if (ret != null) return ret; } else { //!金币 string ret = CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Coin, price); if (ret != null) return ret; } store.ExtandStore(); await self.Save(store); return null; } public static async ETTask PutInStore(this StoreComponent self, Unit unit, int bagIndex, int count, int page, System.Collections.Generic.List storeList) { Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit?.Id}"); return "系统错误"; } Bag bag = unit.GetComponent(); if (!bag.ItemDic.TryGetValueByKey1(bagIndex, out Item item)) { Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName} ({unit.Id})】想把背包中Index= {bagIndex}不存在的物品存入仓库"); return "系统错误"; } if (item.Count < count) { return "物品数量不足"; } if (!CanAddItem(self, unit, store, item.ItemId, count)) { return "仓库已满,无法存放!"; } //!从背包中移除 bag.DeleteItem(bagIndex, count); store.AddItem(new Item(0, item.data, count)); UnitHelper.SaveComponenet(bag); await self.Save(store); //!获取更新后的列表 GetUpdatedList(store, unit.Id, page, storeList); return string.Empty; } public static async ETTask TakeOutStore(this StoreComponent self, Unit unit, int storeIndex, int count, int page, List storeList) { Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit.Id.GetPlayerFormatName()}"); return "系统错误"; } storeIndex += page * Store.Slot_PerPage; if (!store.StoreDic.TryGetValueByKey1(storeIndex, out Item item)) { Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName} ({unit.Id})】想要获取 storeIndex = {storeIndex}的仓库物品,但是不存在"); return "系统错误"; } if (item.Count < count) { return "物品数量不足"; } Item removeItem = store.GetItem(storeIndex, count); if (removeItem == null) { return "仓库中不存在此物品或者数量不足"; } if (!BagHelper.CanAddItem(unit, removeItem.ItemId, count)) { return "背包容量不足"; } store.DeleteItem(storeIndex, count); var addStr=BagHelper.AddItem(unit, removeItem.ItemId, count, removeItem.IsLock, new Item(removeItem.data.Clone())); if (addStr != null) { Log.Error($"{unit.Id.GetPlayerFormatName()} 添加物品失败 itemId = {removeItem.ItemId} count = {count}"); return addStr; } UnitHelper.Save(unit); await self.Save(store); //!获取更新后的列表 GetUpdatedList(store, unit.Id, page, storeList); return string.Empty; } /// /// 获取更新后的列表 /// /// /// /// /// private static void GetUpdatedList(Store store, long id, int page, System.Collections.Generic.List storeList) { int index = -1, listcount = Store.Slot_PerPage; foreach (Item _item in store.StoreDic.Values) { if (listcount <= 0) break; index++; if (index >= page * Store.Slot_PerPage && index < (page + 1) * Store.Slot_PerPage) { listcount--; if (_item.IsEmpty) { continue; } BagMap bagMap = new BagMap { Index = _item.index, NetItem = new NetItem(_item), }; if (_item.ItemType == ItemType.EquipItem) { bagMap.EquipTransMessage = new EquipTransMessage(_item.data.As()); } storeList.Add(bagMap); } } } public static async ETTask<(string, long)> PutCoinInStore(this StoreComponent self, Unit unit, long coin) { Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit?.Id}"); return ("系统错误", 0); } if (store.CoinCount + coin > store.MaxCoinCount) { return ("已达上限,无法继续储存", 0); } string retStr = CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Coin, coin); if (retStr != null) { //Log.Error($"玩家Id = {unit.Id}存入仓库铜钱数{coin}超过了持有数{unit.GetAsLong(NumericType.Coin)},客户端已经限制过"); return ("金币不足", 0); } store.CoinCount += coin; UnitHelper.Save(unit); await self.Save(store); return (string.Empty, store.CoinCount); } public static async ETTask<(string, long)> TakeCoinOutStore(this StoreComponent self, Unit unit, long coin) { Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit?.Id}"); return ("系统错误", 0); } if (store.CoinCount < coin) { //Log.Error($"玩家Id = {unit.Id}取出仓库铜钱数{coin}超过了存储数{store.CoinCount},客户端已经限制过"); return ("取出数量大于存储数量", 0); } store.CoinCount -= coin; CharacterHelper.AddMoney(unit, CharacterHelper.MoneyType.Coin, coin); UnitHelper.Save(unit); await self.Save(store); return (string.Empty, store.CoinCount); } public static async ETTask SetOpenRemoteStore(this StoreComponent self, Unit unit, bool isOpen) { Store store = await self.Query(unit.Id); if (store == null) { Log.Error($"store == null where id = {unit?.Id}"); return; } store.isOpenRemote = isOpen; return; } } }