CTT/Server/Hotfix/Game/System/UI/ConsignmentComponentSystem.cs

397 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Cal.DataTable;
using System;
using System.Collections.Generic;
using System.Text;
namespace ET
{
public class ConsignmentComponentAwakeSystem : AwakeSystem<ConsignmentComponent>
{
public override void Awake(ConsignmentComponent self)
{
ConsignmentComponent.Instance = self;
}
}
public class ConsignmentComponentDestroySystem : DestroySystem<ConsignmentComponent>
{
public override void Destroy(ConsignmentComponent self)
{
self.ConsignmentDic.Clear();
}
}
public class UpdatePer1Day_ConsignmentComponentEvent : AEvent<EventType.UpdatePer1DayOfMonth>
{
public override async ETTask Run(EventType.UpdatePer1DayOfMonth args)
{
var now = args.now;
var list = await DBComponent.Instance.QueryJson<Consignment>($"{{'RemainTime':{{ $lt:{now}}}}}");
if (list == null || list.Count == 0)
{
return;
}
for (int i = 0; i < list.Count; i++)
{
var 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,
};
var 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>(consignment.Id);
var dic = ConsignmentComponent.Instance.ConsignmentDic;
if (dic.TryGetValue(consignment.Id, out var _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<Consignment> QueryById(this ConsignmentComponent self, long consignmentId)
{
if (!self.ConsignmentDic.TryGetValue(consignmentId, out var consignMent))
{
consignMent = await DBComponent.Instance.Query<Consignment>(consignmentId);
if (consignMent == null)
{
consignMent = EntityFactory.Create<Consignment>(self);
}
self.ConsignmentDic[consignmentId] = consignMent;
}
return consignMent;
}
public static async ETTask<List<Consignment>> QueryAll(this ConsignmentComponent self)
{
var now = TimeHelper.ClientNow();
var list = await DBComponent.Instance.QueryJson<Consignment>($"{{'RemainTime':{{ $gte:{now}}}}}");
return list;
}
/// <summary>
/// 数据保存到数据库
/// </summary>
/// <param name="self"></param>
/// <returns></returns>
public static async ETVoid Save(this ConsignmentComponent self, Consignment consignment)
{
await DBComponent.Instance.Save(consignment);
}
/// <summary>
/// // by 左倾月 on 2020/7/27 at 15:46
/// 从0 - Count 取数据
/// </summary>
/// <param name="self"></param>
/// <param name="consignMapList"></param>
/// <returns></returns>
public static async ETTask<int> GetConsignmet(this ConsignmentComponent self, Unit unit, System.Collections.Generic.List<ConsignMap> consignMapList)
{
var list = await self.QueryAll();
if (list == null || list.Count == 0)
{
return 0;
}
int index = 0;
foreach (var 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<EquipItem>());
}
consignMapList.Add(consignMap);
}
}
return list.Count / ONCE_GET_Count + 1;
}
/// <summary>
/// // by 左倾月 on 2020/7/27 at 15:48
/// 从第page*Count ~ (page+1)*Count取数据
/// </summary>
/// <param name="self"></param>
/// <param name="unit"></param>
/// <param name="page"></param>
/// <param name="itemType"></param>
/// <param name="jobType"></param>
/// <param name="consignMapList"></param>
/// <returns></returns>
public static async ETTask<int> GetConsignmet(this ConsignmentComponent self, Unit unit, int page, ItemType itemType, JobType jobType, System.Collections.Generic.List<ConsignMap> consignMapList)
{
var list = await self.QueryAll();
if (list == null || list.Count == 0)
{
return 0;
}
int index = -1, count = ONCE_GET_Count;
foreach (var 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<EquipBase>(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<EquipItem>());
}
count--;
consignMapList.Add(consignMap);
}
}
catch (Exception e)
{
Log.Error(e);
}
}
return list.Count / ONCE_GET_Count + 1;
}
private static async ETTask<string> AddItemCount(this ConsignmentComponent self, Unit unit)
{
if (!self.CountDic.TryGetValue(unit.Id, out int _count))
{
var now = TimeHelper.ClientNow();
var list = await DBComponent.Instance.QueryJson<Consignment>($"{{'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))
{
var now = TimeHelper.ClientNow();
var list = await DBComponent.Instance.QueryJson<Consignment>($"{{'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<string> PutInConsignment(this ConsignmentComponent self, Unit unit, int index, int count, long price, string pwd)
{
try
{
var checkRet = await self.AddItemCount(unit);
if (!checkRet.Equals(string.Empty))
{
return checkRet;
}
Bag bag = unit.GetComponent<Bag>();
if (!bag.ItemDic.TryGetValueByKey1(index, out var 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);
//!放入寄售
var newConsignment = EntityFactory.CreateWithParent<Consignment>(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<string> BuyInConsignment(this ConsignmentComponent self, Unit unit, long consignmentId, string pwd)
{
try
{
var 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 "元宝礼盒出错";
}
//!付钱
var payRet = CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Gem, consignment.Price);
if (payRet != null) return payRet;
//!商品下架
await DBComponent.Instance.Remove<Consignment>(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<NumericComponent>(unit).Coroutine();
UnitHelper.Save<Bag>(unit).Coroutine();
}
catch (Exception e)
{
Log.Error(e);
}
return string.Empty;
}
}
}