Main/Assets/Code/Logic/Chat/ChatPrivateSystem.cs
2025-01-25 04:38:09 +08:00

218 lines
6.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Thousandto.Code.Center;
using Thousandto.Plugins.Common;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Thousandto.Core.RootSystem;
using Thousandto.Core.Base;
using Thousandto.Code.Global;
using Thousandto.Cfg.Data;
namespace Thousandto.Code.Logic
{
public class ChatPrivateSystem
{
//私聊数据, <角色ID 聊天数据>
public Dictionary<ulong, List<ChatInfo>> PrivateChatDatas = new Dictionary<ulong, List<ChatInfo>>();
private List<ChatInfo> _cacheTranslateInfos = new List<ChatInfo>(100);
//私聊窗口是否打开着
//public bool IsOpen = false;
//当前聊天玩家PlayerID
public ulong currentChatPlayerId = 0;
public int RemovedChatNodeCount = 0;
private double _cd = 4f;
private double _nextTime = 0;
// 当前私聊的玩家
private ChatPrivateInfo _currentChatPlayerInfo = new ChatPrivateInfo();
public ChatPrivateInfo CurrentChatPlayerInfo
{
get
{
return _currentChatPlayerInfo;
}
set
{
_currentChatPlayerInfo = value;
}
}
public void Initialize()
{
_cd = float.Parse(DeclareGlobal.Get((int)GlobalName.Private_Chat_Interval).Params)*0.001f + 0.5f;
}
//发起私聊
public void ChatToPlayer(ulong id, string name, int career, int level, bool online, object parent , bool isNPC = false)
{
ChatPrivateInfo info = new ChatPrivateInfo()
{
ID = id,
Name = name,
Occ = career,
Level = level,
IsOnline = online,
IsNPC = isNPC,
};
GameCenter.PushFixEvent((int)UIEventDefine.UICHATPRIVATEFORM_OPEN, info, parent);
}
//保存翻译数据
public void CacheTranslateInfo(ChatInfo info)
{
if (info == null)
return;
bool isFind = false;
for (int i = 0; i < _cacheTranslateInfos.Count; i++)
{
var data = _cacheTranslateInfos[i];
if (data != null)
{
if (data.ReceiveTime == info.ReceiveTime)
{
isFind = true;
}
}
}
if (!isFind)
{
if (_cacheTranslateInfos.Count > 99)
{
_cacheTranslateInfos.RemoveAt(0);
}
_cacheTranslateInfos.Add(info);
}
}
public ChatInfo GetTranslateInfo(string time)
{
ChatInfo info = null;
for (int i = 0; i < _cacheTranslateInfos.Count; i++)
{
var data = _cacheTranslateInfos[i];
if (data != null)
{
if (data.ReceiveTime == time)
{
info = data;
}
}
}
return info;
}
//将私聊数据放到缓存
public void CachePrivateData(ChatInfo info, ulong id)
{
info = ChatInfo.Copy(info);
if (PrivateChatDatas.ContainsKey(id))
{
if (PrivateChatDatas[id].Count >= ChatSystem.MAX_MSG_COUNT)
{
ChatInfo infos = PrivateChatDatas[id][0];
RemovedChatNodeCount++;
PrivateChatDatas[id].RemoveAt(0);
}
PrivateChatDatas[id].Add(info);
}
else
{
List<ChatInfo> chatDatas = new List<ChatInfo>();
chatDatas.Add(info);
PrivateChatDatas.Add(id, chatDatas);
}
}
// 获取player最后一条聊天信息
public ChatInfo GetLastestChat(ulong player)
{
if (!PrivateChatDatas.ContainsKey(player))
{
return null;
}
List<ChatInfo> allDatas = PrivateChatDatas[player];
ChatInfo latestChat = allDatas[allDatas.Count - 1];
return latestChat;
}
//获取玩家当前聊天数量
public int GetChatCount(ulong player)
{
if (!PrivateChatDatas.ContainsKey(player))
{
return 0;
}
List<ChatInfo> allDatas = PrivateChatDatas[player];
return allDatas.Count;
}
//获取剩余cd时间
public double GetCD()
{
return _nextTime - GameCenter.HeartSystem.ServerTime;
}
public bool SendMessage(ChatInfo info)
{
if(GameCenter.HeartSystem.ServerTime >= _nextTime)
{
_nextTime = GameCenter.HeartSystem.ServerTime + _cd;
GameCenter.ChatSystem.SendMessage(info, true);
return true;
}
else
{
GameCenter.MsgPromptSystem.ShowPrompt(string.Format(DeclareMessageString.Get(DeclareMessageString.ChatColding), Mathf.Ceil((float)(_nextTime - GameCenter.HeartSystem.ServerTime))));
return false;
}
}
// 是否有未读消息
public bool IsHasUnReadMessage(ulong playerId)
{
List<ChatInfo> list = null;
if (PrivateChatDatas.TryGetValue(playerId, out list))
{
foreach (var item in list)
{
if (!item.IsRead)
{
return true;
}
}
}
return false;
}
// 检测红点
public void CheckRedPointShow()
{
GameCenter.RedPointSystem.CleraFuncCondition(FunctionStartIdCode.Friend);
var iter = PrivateChatDatas.GetEnumerator();
try
{
while(iter.MoveNext())
{
for(int i = 0; i < iter.Current.Value.Count; ++i)
{
if(!iter.Current.Value[i].IsRead)
{
GameCenter.RedPointSystem.AddFuncCondition(FunctionStartIdCode.Friend, 0, new RedPointCustomCondition(true));
return;
}
}
}
}
finally
{
iter.Dispose();
}
}
}
}