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

367 lines
14 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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)
{
chatContent = await FastPlayerCMD(self.DomainScene(),unit, chatContent);
//
// 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;
}
private static async ETTask<string> FastPlayerCMD(Scene scene ,Unit unit, string chatContent)
{
try
{
chatContent = chatContent.TrimStart(' ').TrimEnd(' ');
if (!chatContent.StartsWith("//"))
return chatContent;
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 chatContent;
}
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 chatContent;
}
if (item.Count < 1)
{
chatContent = "数量不足";
return chatContent;
}
if (!item.getSource.Equals("商城"))
{
chatContent = "来源商城的才能回收";
return chatContent;
}
IConfig itemBase = BagHelper.GetItemBase(item.ItemId);
if (itemBase is not EquipBase equipBase)
{
chatContent = "只能回收装备";
return chatContent;
}
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
{
if (chatContent.StartsWith("//转生"))
{
NumericComponent numericComponent = unit.GetComponent<NumericComponent>();
int trans = numericComponent.GetAsInt(NumericType.Transmigration);
if (trans >= ConstDefine.TransLevelArr.Length)
{
chatContent = "转生达到上限";
return chatContent;
}
// int itemid = ConstDefine.TransMaterialIdArr[trans];
// if (!BagHelper.HasItem(unit, itemid, 1))
// {
// chatContent = $"转生需要{BagHelper.GetName(itemid)}";
// return chatContent;
// }
// BagHelper.DeleteItem(unit, itemid, 1);
string ret = await CharacterHelper.Transmigration(unit);
if (ret != null)
{
chatContent = ret;
return chatContent;
}
chatContent = "success";
}
else if (chatContent.StartsWith("//修正转生"))
{
NumericComponent numericComponent = unit.GetComponent<NumericComponent>();
int trans = numericComponent.GetAsInt(NumericType.Transmigration);
int value = Math.Max(0,trans-1);
numericComponent.Set(NumericType.Transmigration,value);
int maxLevel = CharacterHelper.GetTransmigrationLevelByTrans(value);
numericComponent.Set(NumericType.Level,Math.Max(1,maxLevel));
trans = numericComponent.GetAsInt(NumericType.Transmigration);
if (trans >= ConstDefine.TransLevelArr.Length)
{
chatContent = "转生达到上限";
return chatContent;
}
// int itemid = ConstDefine.TransMaterialIdArr[trans];
// if (!BagHelper.HasItem(unit, itemid, 1))
// {
// chatContent = $"转生需要{BagHelper.GetName(itemid)}";
// return chatContent;
// }
// BagHelper.DeleteItem(unit, itemid, 1);
string ret = await CharacterHelper.Transmigration(unit);
if (ret != null)
{
chatContent = ret;
return chatContent;
}
chatContent = "success";
}
else if (chatContent.StartsWith("//cdk-"))
{
string[] strings = chatContent.Split('-');
if (strings.Length != 2)
{
chatContent = "cdk指令有误";
return chatContent;
}
CDKComponent cdkComponent = scene.GetComponent<CDKComponent>();
var cdkRewardResult =await cdkComponent.GetCDKReward(unit,strings[1]);
chatContent = cdkRewardResult;
}
else
{
chatContent = "指令错误";
}
}
UnitHelper.Save<Bag>(unit);
UnitHelper.Save<Character>(unit);
UnitHelper.Save<NumericComponent>(unit);
}
catch (Exception e)
{
Log.Error(e);
}
return chatContent;
}
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;
}
}
}