using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ET { public class ChatAwakeSystem : AwakeSystem { public override void Awake(Chat self) { Chat.Instance = self; self.InitTimeLimit(); } } public static class ChatSystem { /// /// 初始化时间 /// 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(); } self. timeDic[id][chatType] = time; } /// /// 设置为当前时间 /// /// /// public static void SetTime(this Chat self,long id, ChatType chatType) { self.SetTime(id, chatType, TimeHelper.ClientNow()); } /// /// 能否聊天 /// /// /// /// 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); } /// /// 获取上次聊天时间 /// /// /// /// public static long GetTime(this Chat self,long id, ChatType chatType) { if ( self.timeDic.TryGetValue(id, out Dictionary dic)) { if (dic.TryGetValue(chatType, out long time)) { return time; } } return 0; } public static async ETTask 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; } } 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; } 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 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 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; } } }