using Cal.DataTable; using System; using System.Collections.Generic; using System.Text; namespace ET { public class ConsignmentComponentAwakeSystem : AwakeSystem { public override void Awake(ConsignmentComponent self) { ConsignmentComponent.Instance = self; } } public class ConsignmentComponentDestroySystem : DestroySystem { public override void Destroy(ConsignmentComponent self) { self.ConsignmentDic.Clear(); } } public class UpdatePer1Day_ConsignmentComponentEvent : AEvent { public override async ETTask Run(EventType.UpdatePer1DayOfMonth args) { long now = args.now; List list = await DBComponent.Instance.QueryJson($"{{'RemainTime':{{ $lt:{now}}}}}"); if (list == null || list.Count == 0) { return; } for (int i = 0; i < list.Count; i++) { Consignment consignment = list[i]; try { if (consignment == null || consignment.Item == null || consignment.Item.IsEmpty) continue; Mail mail = new() { Id = Game.IdGenerater.GenerateId(), Title = "寄售回收", Content = $"您在寄售处售卖的[color=#895674]{BagHelper.GetName(consignment.Item.ItemId)}[/color]超时无人购买,系统返还给您,请注意查收!", SenderName = "寄售商人", State = MailState.UnReceive, RemainTime = now + (long)TimeSpan.FromDays(7).TotalMilliseconds, }; Item item = consignment.Item; mail.RewordArr.Add(new MailItem { ItemId = item.ItemId, Count = item.Count, IsLock = item.IsLock, IsHasItem = true, Item = consignment.Item, }); //删除寄售 await DBComponent.Instance.Remove(consignment.Id); Dictionary dic = ConsignmentComponent.Instance.ConsignmentDic; if (dic.TryGetValue(consignment.Id, out Consignment _consignment)) _consignment.Dispose(); dic.Remove(consignment.Id); await ConsignmentComponent.Instance.ReduceItemCount(consignment.UnitId); //添加邮件 await MailComponent.Instance.AddMail(consignment.UnitId, mail); } catch (Exception e) { Log.Error(e); } } } } public static class ConsignmentComponentSystem { private const int ONCE_GET_Count = 10; private const int Consignment_Count_Per_Unit = 20; public static async ETTask QueryById(this ConsignmentComponent self, long consignmentId) { if (!self.ConsignmentDic.TryGetValue(consignmentId, out Consignment consignMent)) { consignMent = await DBComponent.Instance.Query(consignmentId); if (consignMent == null) { consignMent = EntityFactory.Create(self); } self.ConsignmentDic[consignmentId] = consignMent; } return consignMent; } public static async ETTask> QueryAll(this ConsignmentComponent self) { long now = TimeHelper.ClientNow(); List list = await DBComponent.Instance.QueryJson($"{{'RemainTime':{{ $gte:{now}}}}}"); return list; } /// /// 数据保存到数据库 /// /// /// public static async ETVoid Save(this ConsignmentComponent self, Consignment consignment) { await DBComponent.Instance.Save(consignment); } /// /// // by 左倾月 on 2020/7/27 at 15:46 /// 从0 - Count 取数据 /// /// /// /// public static async ETTask GetConsignmet(this ConsignmentComponent self, Unit unit, System.Collections.Generic.List consignMapList) { List list = await self.QueryAll(); if (list == null || list.Count == 0) { return 0; } int index = 0; foreach (Consignment item in list) { if (index++ < ONCE_GET_Count) { ConsignMap consignMap = new ConsignMap { ConsignItemId = item.Id, UnitId = item.UnitId, Item = new NetItem(item.Item), Name = item.NickName, RemainTime = item.RemainTime, Price = item.Price, NeedPwd = !string.IsNullOrEmpty(item.Pwd) }; if (item.Item.ItemType == ItemType.EquipItem) { consignMap.EquipTransMessage = new EquipTransMessage(item.Item.data.As()); } consignMapList.Add(consignMap); } } return list.Count / ONCE_GET_Count + 1; } /// /// // by 左倾月 on 2020/7/27 at 15:48 /// 从第page*Count ~ (page+1)*Count取数据 /// /// /// /// /// /// /// /// public static async ETTask GetConsignmet(this ConsignmentComponent self, Unit unit, int page, ItemType itemType, JobType jobType, System.Collections.Generic.List consignMapList) { List list = await self.QueryAll(); if (list == null || list.Count == 0) { return 0; } int index = -1, count = ONCE_GET_Count; foreach (Consignment item in list) { if (item.RemainTime < TimeHelper.ClientNow()) continue; if (count <= 0) return list.Count / ONCE_GET_Count + 1; try { if (!item.Item.IsEmpty) { //!判断物品类型 if (item.Item.ItemType != itemType) continue; //!装备才有职业 if (itemType == ItemType.EquipItem) { EquipBase equipBase = DataTableHelper.Get(item.Item.ItemId); if (equipBase == null) { Log.Error($"玩家Id=【{item.Id}】 Name=【{item.NickName}】 equipBase ==null where ItemId = {item.Item.ItemId}"); continue; } if (jobType != JobType.UnKnown) //!判断职业类型 if (equipBase.JobId != (int)jobType) continue; } } index++; if (index >= page * ONCE_GET_Count && index < (page + 1) * ONCE_GET_Count) { ConsignMap consignMap = new() { ConsignItemId = item.Id, UnitId = item.UnitId, Item = new NetItem(item.Item), Name = item.NickName, RemainTime = item.RemainTime, Price = item.Price, NeedPwd = !string.IsNullOrEmpty(item.Pwd) }; if (item.Item.ItemType == ItemType.EquipItem) { consignMap.EquipTransMessage = new EquipTransMessage(item.Item.data.As()); } count--; consignMapList.Add(consignMap); } } catch (Exception e) { Log.Error(e); } } return list.Count / ONCE_GET_Count + 1; } private static async ETTask AddItemCount(this ConsignmentComponent self, Unit unit) { if (!self.CountDic.TryGetValue(unit.Id, out int _count)) { long now = TimeHelper.ClientNow(); List list = await DBComponent.Instance.QueryJson($"{{'RemainTime':{{ $gte:{now}}},'UnitId':{unit.Id}}}"); if (list == null) { self.CountDic[unit.Id] = 0; } self.CountDic[unit.Id] = _count = list.Count; } if (_count >= Consignment_Count_Per_Unit) return $"寄售数量达到上限{Consignment_Count_Per_Unit}"; self.CountDic[unit.Id]++; return string.Empty; } public static async ETTask ReduceItemCount(this ConsignmentComponent self, long unitId) { if (!self.CountDic.TryGetValue(unitId, out int _count)) { long now = TimeHelper.ClientNow(); List list = await DBComponent.Instance.QueryJson($"{{'RemainTime':{{ $gte:{now}}},'UnitId':{unitId}}}"); if (list == null) { self.CountDic[unitId] = 0; return; } self.CountDic[unitId] = _count = list.Count; } self.CountDic[unitId]--; return; } public static async ETTask PutInConsignment(this ConsignmentComponent self, Unit unit, int index, int count, long price, string pwd) { try { string checkRet = await self.AddItemCount(unit); if (!checkRet.Equals(string.Empty)) { return checkRet; } Bag bag = unit.GetComponent(); if (!bag.ItemDic.TryGetValueByKey1(index, out Item item)) { Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要寄售Index = {index}的背包物品,背包中没有"); return "系统错误"; } if (item.IsLock) { return "物品已经绑定,不能交易!"; } if (count > item.Count) { return "您的物品数量不足!"; } if (item.ItemType == ItemType.EquipItem && count != 1) { Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】 想要寄售多个装备,突破了客户端的限制"); return "只能寄售一个装备!"; } if (price < 1000) { Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】 寄售价格少于1000星币,突破了客户端的限制"); return "单价至少1000星币!"; } //!背包删除 bag.DeleteItem(index, count); //!放入寄售 Consignment newConsignment = EntityFactory.CreateWithParent(self); newConsignment.UnitId = unit.Id; newConsignment.NickName = UserComponent.Instance.Get(unit.Id).NickName; newConsignment.RemainTime = (long)(TimeHelper.ClientNow() + TimeSpan.FromDays(2).TotalMilliseconds); newConsignment.Item = new Item(index, item.data, count); newConsignment.Price = price * count; newConsignment.Pwd = pwd; self.ConsignmentDic[newConsignment.Id] = newConsignment; self.Save(newConsignment).Coroutine(); UnitHelper.SaveComponenet(bag).Coroutine(); } catch (Exception e) { Log.Error(e); } return string.Empty; } const float tax = 0.1f; const float pwdTax = 0.1f; //元宝礼盒 const int yuanBaoId = 110825; public static async ETTask BuyInConsignment(this ConsignmentComponent self, Unit unit, long consignmentId, string pwd) { try { Consignment consignment = await self.QueryById(consignmentId); if (consignment == null) { Log.Error($"【{ UserComponent.Instance.Get(unit.Id)?.NickName} ({ unit.Id})】想要购买consignmentId={consignmentId} 的不存在物品"); return "系统错误"; } if (consignment.UnitId == unit.Id) { return "您不能买自己的商品!"; } if (consignment.Item == null || consignment.Item.IsEmpty) { Log.Error($"consignmentItem is Empty where id = {consignmentId}"); return "购买的物品被其他人买走!无法购买"; } if (!ItemComponent.Instance.CanAddItem(unit, consignment.Item.ItemId, consignment.Item.Count)) { return "背包已满,请整理您的背包!"; } if (!consignment.Pwd.Equals(pwd)) { return "密码错误"; } if (!BagHelper.GetName(yuanBaoId).Contains("元宝礼盒")) { return "元宝礼盒出错"; } //!付钱 string payRet = CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Gem, consignment.Price); if (payRet != null) return payRet; //!商品下架 await DBComponent.Instance.Remove(consignment.Id); self.ConsignmentDic.Remove(consignment.Id); //!卖家收钱(可能已经离线) Mail mail = new() { Id = Game.IdGenerater.GenerateId(), Title = "寄售获利", Content = $"您在寄售处售卖的[color=#895674]{BagHelper.GetName(consignment.Item.ItemId)}[/color]已被购买,扣除手续费后,奖励如下,请注意查收!", SenderName = "寄售商人", State = MailState.UnReceive, RemainTime = (TimeHelper.ClientNow() + (long)TimeSpan.FromDays(7).TotalMilliseconds), }; float _tax = tax; if (!string.IsNullOrEmpty(consignment.Pwd)) { _tax += pwdTax; } if (consignment.Item.ItemId == yuanBaoId) { _tax = 0; } mail.RewordArr.Add(new MailItem { ItemId = BagHelper.GemId, Count = MathHelper.RoundToLong(consignment.Price * (1 - _tax)) }); await MailComponent.Instance.AddMail(consignment.UnitId, mail); await self.ReduceItemCount(consignment.UnitId); //!买家到货 ItemComponent.Instance.AddItem(unit, consignment.Item.ItemId, consignment.Item.Count, false, consignment.Item, getSource: consignment.Item.getSource); consignment.Dispose(); UnitHelper.Save(unit).Coroutine(); UnitHelper.Save(unit).Coroutine(); } catch (Exception e) { Log.Error(e); } return string.Empty; } } }