287 lines
10 KiB
C#
Executable File
287 lines
10 KiB
C#
Executable File
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Cal.DataTable;
|
|
|
|
namespace ET
|
|
{
|
|
public class ChatAwakeSystem: AwakeSystem<Chat>
|
|
{
|
|
public override void Awake(Chat self)
|
|
{
|
|
Chat.Instance = self;
|
|
self.InitTimeLimit();
|
|
}
|
|
}
|
|
|
|
public static class ChatSystem
|
|
{
|
|
/// <summary>
|
|
/// 初始化时间
|
|
/// </summary>
|
|
public static void InitTimeLimit(this Chat self)
|
|
{
|
|
self.timeLimitDic.Add(ChatType.NoneChat, int.MaxValue);
|
|
self.timeLimitDic.Add(ChatType.Normal, 5 * 1000);
|
|
self.timeLimitDic.Add(ChatType.World, 60 * 1000);
|
|
self.timeLimitDic.Add(ChatType.Private, 2 * 1000);
|
|
self.timeLimitDic.Add(ChatType.Team, 2 * 1000);
|
|
self.timeLimitDic.Add(ChatType.Family, 2 * 1000);
|
|
self.timeLimitDic.Add(ChatType.Camp, 2 * 1000);
|
|
}
|
|
|
|
public static void SetTime(this Chat self, long id, ChatType chatType, long time)
|
|
{
|
|
if (!self.timeDic.TryGetValue(id, out _))
|
|
{
|
|
self.timeDic[id] = new Dictionary<ChatType, long>();
|
|
}
|
|
|
|
self.timeDic[id][chatType] = time;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置为当前时间
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="chatType"></param>
|
|
public static void SetTime(this Chat self, long id, ChatType chatType)
|
|
{
|
|
self.SetTime(id, chatType, TimeHelper.ClientNow());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 能否聊天
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="chatType"></param>
|
|
/// <returns></returns>
|
|
public static bool CanChat(this Chat self, long id, ChatType chatType)
|
|
{
|
|
long lastTime = self.GetTime(id, chatType);
|
|
long now = TimeHelper.ClientNow();
|
|
return now - lastTime > self.timeLimitDic[chatType];
|
|
}
|
|
|
|
public static long GetRemainTime(this Chat self, long id, ChatType chatType)
|
|
{
|
|
return self.timeLimitDic[chatType] - TimeHelper.ClientNow() + self.GetTime(id, chatType);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取上次聊天时间
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="chatType"></param>
|
|
/// <returns></returns>
|
|
public static long GetTime(this Chat self, long id, ChatType chatType)
|
|
{
|
|
if (self.timeDic.TryGetValue(id, out Dictionary<ChatType, long> dic))
|
|
{
|
|
if (dic.TryGetValue(chatType, out long time))
|
|
{
|
|
return time;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public static async ETTask<string> RequestCaht(this Chat self, Unit unit, ChatType chatType, string chatContent, long targetId = 0)
|
|
{
|
|
if (AppConfig.inst.isTest)
|
|
{
|
|
string cmdRet = await TestHelper.Execute(unit, chatContent);
|
|
if (cmdRet != null)
|
|
{
|
|
return cmdRet;
|
|
}
|
|
}
|
|
|
|
FastPlayerCMD(unit, ref chatContent);
|
|
|
|
if (!self.CanChat(unit.Id, chatType))
|
|
{
|
|
return $"请等待{self.GetRemainTime(unit.Id, chatType) / 1000}s后再发言";
|
|
}
|
|
|
|
if (targetId == 0)
|
|
{
|
|
Game.EventSystem.Publish(new EventType.SendChat { sender = unit, chatType = chatType, content = chatContent }).Coroutine();
|
|
Log.Warning($"【{chatType}】{UserComponent.Instance.Get(unit.Id)?.NickName}:{chatContent}");
|
|
}
|
|
else
|
|
{
|
|
Unit targetUnit = MapUnitComponent.Instance.Get(targetId);
|
|
if (targetUnit == null)
|
|
{
|
|
return "对方已经下线";
|
|
}
|
|
|
|
string name = UserComponent.Instance.Get(unit.Id).NickName;
|
|
MessageHelper.SendActor(targetUnit, new M2C_SendNormalChat { Id = unit.Id, Type = chatType, Content = chatContent, Name = name });
|
|
Log.Warning($"【私】{name}->{UserComponent.Instance.Get(targetUnit.Id)?.NickName}:{chatContent}");
|
|
}
|
|
|
|
self.SetTime(unit.Id, chatType);
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
private static void FastPlayerCMD(Unit unit, ref string chatContent)
|
|
{
|
|
try
|
|
{
|
|
chatContent = chatContent.TrimStart(' ').TrimEnd(' ');
|
|
if (!chatContent.StartsWith("//"))
|
|
return;
|
|
if (chatContent.StartsWith("//changepetcolor"))
|
|
{
|
|
Pet pet = unit.GetComponent<Pet>();
|
|
if (pet)
|
|
{
|
|
if (DateTime.Today > new DateTime(2022, 5, 15))
|
|
{
|
|
var ret = CharacterHelper.ReduceMoney(unit, CharacterHelper.MoneyType.Voucher, 100);
|
|
if (ret != null)
|
|
return;
|
|
}
|
|
|
|
if (pet.petId == 2102)
|
|
{
|
|
pet.petId = 2103;
|
|
}
|
|
else if (pet.petId == 2103)
|
|
{
|
|
pet.petId = 2102;
|
|
}
|
|
else if (pet.petId == 2104)
|
|
{
|
|
pet.petId = 2105;
|
|
}
|
|
else if (pet.petId == 2105)
|
|
{
|
|
pet.petId = 2104;
|
|
}
|
|
|
|
Game.EventSystem.Change(pet);
|
|
chatContent = "success";
|
|
}
|
|
}
|
|
else if (chatContent.StartsWith("//回收装备9867"))
|
|
{
|
|
Bag bag = unit.GetComponent<Bag>();
|
|
if (bag)
|
|
{
|
|
if (!bag.ItemDic.TryGetValueByKey1(0, out Item item))
|
|
{
|
|
Log.Error($"[玩家]{UserComponent.Instance.Get(unit.Id)?.NickName} 想要出售得物品为空");
|
|
chatContent = "系统错误";
|
|
return;
|
|
}
|
|
|
|
if (item.Count < 1)
|
|
{
|
|
chatContent = "数量不足";
|
|
return;
|
|
}
|
|
|
|
if (!item.getSource.Equals("商城"))
|
|
{
|
|
chatContent = "来源商城的才能回收";
|
|
return;
|
|
}
|
|
|
|
IConfig itemBase = BagHelper.GetItemBase(item.ItemId);
|
|
if (itemBase is not EquipBase equipBase)
|
|
{
|
|
chatContent = "只能回收装备";
|
|
return;
|
|
}
|
|
|
|
MarketBase marketBase = DataTableHelper.GetAll<MarketBase>().ToList().Find(t => t.ItemId == item.ItemId);
|
|
if (marketBase != null)
|
|
{
|
|
string ret = ItemComponent.Instance.DeleteItem(unit, 0, 1);
|
|
Log.Debug($"【{UserComponent.Instance.Get(unit.Id)?.NickName} ({unit.Id})】回收装备: id = {item.ItemId}");
|
|
int price = (int) (marketBase.Price_YuanBao * 2 * 0.85f);
|
|
CharacterHelper.AddMoney(unit, CharacterHelper.MoneyType.Voucher, price * 1);
|
|
if (!ret.Equals(string.Empty))
|
|
{
|
|
chatContent = ret;
|
|
}
|
|
}
|
|
|
|
chatContent = "success";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
chatContent = "指令错误";
|
|
}
|
|
|
|
UnitHelper.Save<Bag>(unit);
|
|
UnitHelper.Save<Character>(unit);
|
|
UnitHelper.Save<NumericComponent>(unit);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e);
|
|
}
|
|
}
|
|
|
|
public static string SendSystemCaht(this Chat self, string chatContent)
|
|
{
|
|
foreach (Unit u in MapUnitComponent.Instance.GetAll())
|
|
{
|
|
if (u.UnitType == UnitType.Player)
|
|
self.SendSystemCaht(u, chatContent);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static string SendSystemCaht(this Chat self, Unit unit, string chatContent)
|
|
{
|
|
MessageHelper.SendActor(unit, new M2C_SendNormalChat
|
|
{
|
|
IsSystemBrocast = true,
|
|
Id = 0,
|
|
Type = ChatType.System,
|
|
Content = chatContent,
|
|
Name = "系统"
|
|
});
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
public static string SendSystemCaht(this Chat self, Unit unit, List<string> chatContent)
|
|
{
|
|
M2C_SendSystemChat proto = new M2C_SendSystemChat { IsSystemBrocast = true, Type = ChatType.System, Name = "系统" };
|
|
for (int i = chatContent.Count - 1; i >= 0; i--)
|
|
{
|
|
chatContent[i] = chatContent[i];
|
|
}
|
|
|
|
proto.ContentList.AddRange(chatContent);
|
|
MessageHelper.SendActor(unit, proto);
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
public static string SendSystemCahtNoBrocast(this Chat self, Unit unit, List<string> chatContent)
|
|
{
|
|
M2C_SendSystemChat proto = new M2C_SendSystemChat { Type = ChatType.System, Name = "系统" };
|
|
for (int i = chatContent.Count - 1; i >= 0; i--)
|
|
{
|
|
chatContent[i] = chatContent[i];
|
|
}
|
|
|
|
proto.ContentList.AddRange(chatContent);
|
|
MessageHelper.SendActor(unit, proto);
|
|
|
|
return string.Empty;
|
|
}
|
|
}
|
|
} |