CTT/Server/Hotfix/Game/System/Chat/ChatSystem.cs

183 lines
6.2 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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)
{
2021-04-11 19:50:39 +08:00
long lastTime = self. GetTime(id, chatType);
2021-04-08 20:09:59 +08:00
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)
{
2021-04-11 19:50:39 +08:00
if ( self.timeDic.TryGetValue(id, out Dictionary<ChatType, long> dic))
2021-04-08 20:09:59 +08:00
{
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;
}
}
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
{
2021-04-11 19:50:39 +08:00
Unit targetUnit = MapUnitComponent.Instance.Get(targetId);
2021-04-08 20:09:59 +08:00
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)
{
2021-04-11 19:50:39 +08:00
foreach (Unit u in MapUnitComponent.Instance.GetAll())
2021-04-08 20:09:59 +08:00
{
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)
{
2021-04-11 19:50:39 +08:00
M2C_SendSystemChat proto = new M2C_SendSystemChat
2021-04-08 20:09:59 +08:00
{
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;
}
2021-04-15 00:12:07 +08:00
public static string SendSystemCahtNoBrocast(this Chat self, Unit unit, List<string> chatContent)
2021-04-08 20:09:59 +08:00
{
2021-04-11 19:50:39 +08:00
M2C_SendSystemChat proto = new M2C_SendSystemChat
2021-04-08 20:09:59 +08:00
{
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;
}
}
}