You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
938 lines
31 KiB
938 lines
31 KiB
4 years ago
|
|
||
|
using Assets.Scripts.Common.ChatSystem.NetWork;
|
||
|
using AX.MessageSystem;
|
||
|
using AX.NetworkSystem;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using System.Reflection;
|
||
|
using System.Text;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class ChatManager : Singleton<ChatManager>
|
||
|
{
|
||
|
#region 属性字段
|
||
|
/// <summary>
|
||
|
/// 房间内所有的私聊聊天消息
|
||
|
/// 频道id
|
||
|
/// 消息集合
|
||
|
/// </summary>
|
||
|
public Dictionary<long, List<ChatMessage>> Msgs = new Dictionary<long, List<ChatMessage>>();
|
||
|
/// <summary>
|
||
|
/// 房间内未读私聊聊天消息
|
||
|
/// 频道id
|
||
|
/// 消息集合
|
||
|
/// </summary>
|
||
|
public Dictionary<long, List<ChatMessage>> UnReadMsgs = new Dictionary<long, List<ChatMessage>>();
|
||
|
//私聊指令
|
||
|
|
||
|
////文本聊天类型
|
||
|
//public ChatMsgType CurrentMsgType = ChatMsgType.Str;
|
||
|
[HideInInspector]
|
||
|
/// <summary>
|
||
|
/// 用户消息存储路径
|
||
|
/// </summary>
|
||
|
public string UserPath;
|
||
|
//公聊当前频道
|
||
|
public ChatChannel CurrentPublicChannel;
|
||
|
//想要进入的公聊频道id
|
||
|
public long joinChannelID = 1;
|
||
|
//公聊消息数据
|
||
|
public Dictionary<long, List<ChatMessage>> PublicChatMsgs = new Dictionary<long, List<ChatMessage>>();
|
||
|
//任务指令数据
|
||
|
public Dictionary<int, TaskModel> TaskData = new Dictionary<int, TaskModel>();
|
||
|
//选中的指令索引
|
||
|
public int SelectTaskIndex = -1;
|
||
|
//建议消息数据
|
||
|
public Dictionary<long, List<ChatMessage>> SuggestChatMsg = new Dictionary<long, List<ChatMessage>>();
|
||
|
//当前建议消息频道
|
||
|
public ChatChannel CurrentSuggestChannel;
|
||
|
////播放列表
|
||
|
//public List<string> UnReadAudioPublic = new List<string>();
|
||
|
#endregion
|
||
|
|
||
|
#region 方法
|
||
|
private void Awake()
|
||
|
{
|
||
|
//私聊消息
|
||
|
gameObject.AddComponent<CREATE_PRIVATE_CHAT_CHANNEL_REPLY>();
|
||
|
gameObject.AddComponent<PRIVATE_TEXT_MESSAGE_SYNC>();
|
||
|
gameObject.AddComponent<PRIVATE_CHAT_VOICE_SYNC>();
|
||
|
gameObject.AddComponent<PRIVATE_TASK_MESSAGE_SYNC>();
|
||
|
//作战建议消息
|
||
|
gameObject.AddComponent<ENTER_SUGGEST_CHAT_CHANNEL_SYNC>();
|
||
|
gameObject.AddComponent<SUGGEST_TEXT_MESSAGE_SYNC>();
|
||
|
gameObject.AddComponent<SUGGEST_AUDIO_MESSAGE_SYNC>();
|
||
|
}
|
||
|
private void Start()
|
||
|
{
|
||
|
UserPath = Path.Combine(Application.dataPath, "ChatData");
|
||
|
UserPath = Path.Combine(UserPath, CurrentUserInfo.mySelf.Id.ToString());
|
||
|
string csvTaskModel = Path.Combine(Application.streamingAssetsPath, "TaskModel.csv");
|
||
|
TaskData = LoadCsvData<TaskModel>(csvTaskModel);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 获取当前建议频道消息
|
||
|
/// </summary>
|
||
|
/// <param name="index"></param>
|
||
|
/// <returns></returns>
|
||
|
public ChatMessage GetSuggestMsgByIndex(int index)
|
||
|
{
|
||
|
ChatMessage msg = SuggestChatMsg[CurrentSuggestChannel.ID][index];
|
||
|
return msg;
|
||
|
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取建议消息Count
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
public int GetSuggestMsgCount()
|
||
|
{
|
||
|
if (CurrentSuggestChannel != null)
|
||
|
{
|
||
|
if (SuggestChatMsg.ContainsKey(CurrentSuggestChannel.ID))
|
||
|
{
|
||
|
return SuggestChatMsg[CurrentSuggestChannel.ID].Count;
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
//向当前建议频道添加消息
|
||
|
public void AddSuggestMsg(ChatMessage msg)
|
||
|
{
|
||
|
if (SuggestChatMsg.ContainsKey(CurrentSuggestChannel.ID))
|
||
|
{
|
||
|
SuggestChatMsg[CurrentSuggestChannel.ID].Add(msg);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
SuggestChatMsg.Add(CurrentSuggestChannel.ID, new List<ChatMessage>() { msg });
|
||
|
}
|
||
|
//刷新UI
|
||
|
MessageDispatcher.SendMessage(MessageName.RefreshSuggestChatMsgs.ToString());
|
||
|
}
|
||
|
public void AddSuggestMsg(ChatAudioMessage msg, string tempFilePath)
|
||
|
{
|
||
|
string filePaht = Path.Combine(UserPath, msg.FileName);
|
||
|
if (msg.SenderId != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
if (!Directory.Exists(UserPath))
|
||
|
{
|
||
|
Directory.CreateDirectory(UserPath);
|
||
|
}
|
||
|
File.Move(tempFilePath, filePaht);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
msg.IsPlayed = true;
|
||
|
}
|
||
|
if (SuggestChatMsg.ContainsKey(CurrentSuggestChannel.ID))
|
||
|
{
|
||
|
SuggestChatMsg[CurrentSuggestChannel.ID].Add(msg);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
SuggestChatMsg.Add(CurrentSuggestChannel.ID, new List<ChatMessage>() { msg });
|
||
|
}
|
||
|
//刷新UI
|
||
|
MessageDispatcher.SendMessage(MessageName.RefreshSuggestChatMsgs.ToString());
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 查询当前聊天频道是否已经建立
|
||
|
/// </summary>
|
||
|
/// <param name="user"></param>
|
||
|
/// <returns></returns>
|
||
|
public bool CheckChannel(long channelId)
|
||
|
{
|
||
|
return Msgs.Keys.Contains(channelId);
|
||
|
}
|
||
|
//TODO 查找上级用户,下级用户
|
||
|
public List<UserInfoItemViewModel> FindAllSupUser()
|
||
|
{
|
||
|
//查找要显示的上级用户数据
|
||
|
List<UserInfoItemViewModel> tempData = new List<UserInfoItemViewModel>();
|
||
|
//练习模式组织机构可以变化
|
||
|
SupsId.Clear();
|
||
|
FindAllSupID(CurrentUserInfo.organization.ParentId);
|
||
|
foreach (var item in CurrentUserInfo.room.UserList)
|
||
|
{
|
||
|
if (item.UserInfo.Id != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
foreach (var id in SupsId)
|
||
|
{
|
||
|
// 数据中不存在战斗班的数据
|
||
|
if (item.Org.Id.Equals(id)
|
||
|
&& item.Org.Level != 5
|
||
|
&& item.Role != Role.总队指挥中心
|
||
|
&& item.Role != Role.支队指挥中心
|
||
|
&& item.Role != Role.导调组
|
||
|
&& item.Role != Role.观察团)
|
||
|
{
|
||
|
UserInfoItemViewModel data = new UserInfoItemViewModel()
|
||
|
{
|
||
|
UserInfo = item,
|
||
|
IsSelected = false,
|
||
|
//UnReadMsgCount = 0,
|
||
|
Index = -1,
|
||
|
ChannelId = item.UserInfo.Id + CurrentUserInfo.mySelf.Id
|
||
|
};
|
||
|
tempData.Add(data);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
// 在房间中查找战斗班数据-战斗班目前不存在下级
|
||
|
}
|
||
|
}
|
||
|
return tempData;
|
||
|
}
|
||
|
public List<UserInfoItemViewModel> FindAllSubUser()
|
||
|
{
|
||
|
//查找要显示的下级用户数据
|
||
|
List<UserInfoItemViewModel> tempData = new List<UserInfoItemViewModel>();
|
||
|
// 战斗班是虚拟数据没有下级,不用查找,后面需要查找的话需要更改,
|
||
|
// 因为现在战斗班的数据中,ID是总队级别的ID
|
||
|
if (CurrentUserInfo.organization.Level != 5)
|
||
|
{
|
||
|
//练习模式组织机构可以变化
|
||
|
SubsId.Clear();
|
||
|
FindAllSubID(CurrentUserInfo.organization.Id);
|
||
|
foreach (var item in CurrentUserInfo.room.UserList)
|
||
|
{
|
||
|
// 不是自己的人
|
||
|
if (item.UserInfo.Id != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
foreach (var id in SubsId)
|
||
|
{
|
||
|
// 数据中不存在战斗班的数据
|
||
|
if (item.Org.ParentId.Equals(id))
|
||
|
{
|
||
|
UserInfoItemViewModel data = new UserInfoItemViewModel()
|
||
|
{
|
||
|
UserInfo = item,
|
||
|
IsSelected = false,
|
||
|
//UnReadMsgCount = 0,
|
||
|
Index = -1,
|
||
|
ChannelId = item.UserInfo.Id + CurrentUserInfo.mySelf.Id
|
||
|
};
|
||
|
tempData.Add(data);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
// 在房间中查找战斗班数据-战斗班是下级
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return tempData;
|
||
|
}
|
||
|
public List<long> FindAllSubUserId()
|
||
|
{
|
||
|
//查找要显示的下级用户数据
|
||
|
List<long> tempData = new List<long>();
|
||
|
// 战斗班是虚拟数据没有下级,不用查找,后面需要查找的话需要更改,
|
||
|
// 因为现在战斗班的数据中,ID是总队级别的ID
|
||
|
if (CurrentUserInfo.organization.Level != 5)
|
||
|
{
|
||
|
//练习模式组织机构可以变化
|
||
|
SubsId.Clear();
|
||
|
FindAllSubID(CurrentUserInfo.organization.Id);
|
||
|
foreach (var item in CurrentUserInfo.room.UserList)
|
||
|
{
|
||
|
// 不是自己的人
|
||
|
if (item.UserInfo.Id != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
foreach (var id in SubsId)
|
||
|
{
|
||
|
// 数据中不存在战斗班的数据
|
||
|
if (item.Org.ParentId.Equals(id))
|
||
|
{
|
||
|
tempData.Add(item.UserInfo.Id);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
// 在房间中查找战斗班数据-战斗班是下级
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return tempData;
|
||
|
}
|
||
|
|
||
|
public List<long> FindDirectionSubUserDataID()
|
||
|
{
|
||
|
//查找要显示的上级用户数据
|
||
|
List<long> usersDatalist = new List<long>();
|
||
|
//练习模式组织机构可以变化
|
||
|
DirectionSubsId.Clear();
|
||
|
FindDirectionSupId(CurrentUserInfo.organization.ParentId);
|
||
|
|
||
|
foreach (var item in CurrentUserInfo.room.UserList)
|
||
|
{
|
||
|
if (item.UserInfo.Id != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
foreach (var id in DirectionSubsId)
|
||
|
{
|
||
|
// 数据中不存在战斗班的数据
|
||
|
if (item.Org.Id.Equals(id)
|
||
|
&& item.Org.Level != 5
|
||
|
&& item.Role != Role.总队指挥中心
|
||
|
&& item.Role != Role.支队指挥中心
|
||
|
&& item.Role != Role.导调组
|
||
|
&& item.Role != Role.观察团)
|
||
|
{
|
||
|
usersDatalist.Add(item.UserInfo.Id);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
// 在房间中查找战斗班数据-战斗班目前不存在下级
|
||
|
}
|
||
|
}
|
||
|
return usersDatalist;
|
||
|
}
|
||
|
public long FindDirectionSupUserDataID()
|
||
|
{
|
||
|
//查找要显示的上级用户数据
|
||
|
long tempData = 0;
|
||
|
//练习模式组织机构可以变化
|
||
|
DirectionSupId = 0;
|
||
|
FindDirectionSupId(CurrentUserInfo.organization.ParentId);
|
||
|
|
||
|
foreach (var item in CurrentUserInfo.room.UserList)
|
||
|
{
|
||
|
if (item.UserInfo.Id != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
// 数据中不存在战斗班的数据
|
||
|
if (item.Org.Id.Equals(DirectionSupId)
|
||
|
&& item.Org.Level != 5
|
||
|
&& item.Role != Role.总队指挥中心
|
||
|
&& item.Role != Role.支队指挥中心
|
||
|
&& item.Role != Role.导调组
|
||
|
&& item.Role != Role.观察团)
|
||
|
{
|
||
|
tempData = item.UserInfo.Id;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return tempData;
|
||
|
}
|
||
|
|
||
|
private List<int> SupsId = new List<int>();
|
||
|
private List<int> SubsId = new List<int>();
|
||
|
private int DirectionSupId;
|
||
|
private List<int> DirectionSubsId = new List<int>();
|
||
|
private List<Organization> orgs = new List<Organization>();
|
||
|
public void SetOrgs(List<Organization> data)
|
||
|
{
|
||
|
orgs = data;
|
||
|
}
|
||
|
public void GetIds()
|
||
|
{
|
||
|
FindAllSupID(CurrentUserInfo.organization.ParentId);
|
||
|
FindAllSubID(CurrentUserInfo.organization.Id);
|
||
|
}
|
||
|
// 查找某个ID的所有上级ID
|
||
|
public void FindAllSupID(int supId)
|
||
|
{
|
||
|
//查看是否还有上级
|
||
|
foreach (var item in orgs)
|
||
|
{
|
||
|
//查看我是否存在上级
|
||
|
if (item.Id == supId && item.Level != 5)
|
||
|
{
|
||
|
//保存上级的ID
|
||
|
SupsId.Add(item.Id);
|
||
|
//继续查找item的上级是否存在上级
|
||
|
FindAllSupID(item.ParentId);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public void FindDirectionSupId(int supId)
|
||
|
{
|
||
|
foreach (var item in orgs)
|
||
|
{
|
||
|
//查看我是否存在上级
|
||
|
if (item.Id == supId && item.Level != 5)
|
||
|
{
|
||
|
//保存上级的ID
|
||
|
DirectionSupId = item.Id;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public void FindDirectionSubId(int id)
|
||
|
{
|
||
|
foreach (var item in orgs)
|
||
|
{
|
||
|
//查看item是否是我的下级
|
||
|
if (item.ParentId == id)
|
||
|
{
|
||
|
//保存下级ID
|
||
|
DirectionSubsId.Add(item.Id);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// 查找某个ID的所有下级ID
|
||
|
public void FindAllSubID(int id)
|
||
|
{
|
||
|
SubsId.Add(id);
|
||
|
foreach (var item in orgs)
|
||
|
{
|
||
|
//查看item是否是我的下级
|
||
|
if (item.ParentId == id)
|
||
|
{
|
||
|
//保存下级ID
|
||
|
SubsId.Add(item.Id);
|
||
|
//继续查找item是否存在下级
|
||
|
FindAllSubID(item.Id);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 创建一个2人的房间
|
||
|
/// </summary>
|
||
|
/// <param name="channel"></param>
|
||
|
public void CreatePrivateChatChannel(UserData other)
|
||
|
{
|
||
|
//向服务器发送创建私聊频道消息
|
||
|
ChatChannel channel = new ChatChannel()
|
||
|
{
|
||
|
ID = -1,
|
||
|
EnterUserId = CurrentUserInfo.mySelf.Id,
|
||
|
RoomID = CurrentUserInfo.room.Id
|
||
|
};
|
||
|
UserData my = CurrentUserInfo.room.FindUserById(CurrentUserInfo.mySelf.Id);
|
||
|
channel.Users.Add(my);
|
||
|
channel.Users.Add(other);
|
||
|
NetworkManager.Default.SendAsync("CREATE_PRIVATE_CHAT_CHANNEL_REQUEST", channel);
|
||
|
}
|
||
|
///// <summary>
|
||
|
///// 当前频道添加消息
|
||
|
///// </summary>
|
||
|
///// <param name="msg"></param>
|
||
|
//internal void AddCurrentMsg(ChatMsg msg)
|
||
|
//{
|
||
|
// CurrentMsgs.Add(msg);
|
||
|
// MessageDispatcher.SendMessage(MessageName.RefreshChatContentLoopList.ToString());
|
||
|
//}
|
||
|
#region 私聊频道未读消息
|
||
|
public void AddUnReadMsg(ChatMessage msg)
|
||
|
{
|
||
|
long id = msg.Channel.ID;
|
||
|
|
||
|
if (UnReadMsgs.ContainsKey(id))
|
||
|
{
|
||
|
UnReadMsgs[id].Add(msg);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
List<ChatMessage> msgs = new List<ChatMessage>();
|
||
|
msgs.Add(msg);
|
||
|
UnReadMsgs.Add(id, msgs);
|
||
|
}
|
||
|
//刷新未读消息
|
||
|
MessageDispatcher.SendMessage("REFRESH_UNREAD_MSG");
|
||
|
}
|
||
|
public void ClearUnReadMsgByChannelId(long channelId)
|
||
|
{
|
||
|
if (UnReadMsgs.ContainsKey(channelId))
|
||
|
{
|
||
|
if (UnReadMsgs[channelId].Count > 0)
|
||
|
{
|
||
|
UnReadMsgs[channelId].Clear();
|
||
|
//刷新未读消息
|
||
|
MessageDispatcher.SendMessage("REFRESH_UNREAD_MSG");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
public int GetUnReadMsgsCount(long channelId)
|
||
|
{
|
||
|
if (UnReadMsgs.ContainsKey(channelId))
|
||
|
{
|
||
|
return UnReadMsgs[channelId].Count;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
//创建频道
|
||
|
public void CretePrivateChatChannel(ChatChannel channel)
|
||
|
{
|
||
|
if (!Msgs.ContainsKey(channel.ID))
|
||
|
{
|
||
|
Msgs.Add(channel.ID, new List<ChatMessage>());
|
||
|
}
|
||
|
}
|
||
|
// 添加私聊消息
|
||
|
public void AddPrivateChatMessage(ChatMessage message)
|
||
|
{
|
||
|
//添加频道消息
|
||
|
Msgs[message.Channel.ID].Add(message);
|
||
|
//刷新频道消息
|
||
|
MessageDispatcher.SendMessage(MessageName.RefreshChatContentLoopList.ToString());
|
||
|
if (message.SenderId != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
//添加未读消息
|
||
|
AddUnReadMsg(message);
|
||
|
}
|
||
|
}
|
||
|
internal void AddPrivateChatMessage(ChatAudioMessage msg, string tempFilePath)
|
||
|
{
|
||
|
// 语音消息是自己发送的标记为已读
|
||
|
if (msg.SenderId == CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
msg.IsPlayed = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
string filePath = Path.Combine(UserPath, msg.FileName);
|
||
|
// 保存音频文件
|
||
|
if (!Directory.Exists(UserPath))
|
||
|
{
|
||
|
Directory.CreateDirectory(UserPath);
|
||
|
}
|
||
|
File.Move(tempFilePath, filePath);
|
||
|
}
|
||
|
// 添加私聊消息
|
||
|
AddPrivateChatMessage(msg);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 发送私聊文本信息
|
||
|
/// </summary>
|
||
|
/// <param name="msg"></param>
|
||
|
public void SendPrivateChatStrMsg(string str, ChatChannel channel)
|
||
|
{
|
||
|
ChatTextMessage textMsg = new ChatTextMessage();
|
||
|
textMsg.SenderId = CurrentUserInfo.mySelf.Id;
|
||
|
textMsg.Channel = channel;
|
||
|
textMsg.Data = str;
|
||
|
textMsg.ReplayFrameCount = InputManager.frameCount;
|
||
|
textMsg.ChatGroup = ChatType.PrivateChat;
|
||
|
NetworkManager.Default.SendAsync("PRIVATE_TEXT_MESSAGE_SYNC", textMsg);
|
||
|
}
|
||
|
public void SendPrivateChatTaskMsg(string taskContent, ChatChannel channel,UserData userdata)
|
||
|
{
|
||
|
ChatTaskMessage msg = new ChatTaskMessage();
|
||
|
msg.SenderId = CurrentUserInfo.mySelf.Id;
|
||
|
msg.Channel = channel;
|
||
|
msg.TaskId = SelectTaskIndex;
|
||
|
msg.TaskContent = taskContent;
|
||
|
msg.ReplayFrameCount = InputManager.frameCount;
|
||
|
msg.ChatGroup = ChatType.PrivateChat;
|
||
|
msg.ReceiverId = userdata.UserInfo.Id;
|
||
|
NetworkManager.Default.SendAsync("PRIVATE_TASK_MESSAGE_SYNC", msg);
|
||
|
// 记录发送过指令
|
||
|
ReportDataMgr.SetSendCommand(msg);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 获取选中的指令名称
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
public string GetSelectedTaskName()
|
||
|
{
|
||
|
return TaskData[SelectTaskIndex].Name;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 根据Id获取TaskModel
|
||
|
/// </summary>
|
||
|
/// <param name="id"></param>
|
||
|
/// <returns></returns>
|
||
|
public TaskModel GetTaskById(int id)
|
||
|
{
|
||
|
return TaskData[id];
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 发送私聊语音信息
|
||
|
/// </summary>
|
||
|
/// <param name="msg"></param>
|
||
|
public void SendPrivateChatAudioMsg(ChatAudioMessage msg, string filePath)
|
||
|
{
|
||
|
//ChatAudioMessage textMsg = new ChatAudioMessage();
|
||
|
//textMsg.Channel = msg.Channel;
|
||
|
//textMsg.FileName = msg.FileName;
|
||
|
//textMsg.SenderId = msg.SenderId;
|
||
|
//textMsg.ReplayFrameCount = msg.ReplayFrameCount;
|
||
|
//textMsg.Channel = msg.Channel;
|
||
|
//textMsg.BattleId = msg.BattleId;
|
||
|
//textMsg.ChatGroup = ChatType.PrivateChat;
|
||
|
NetworkManager.Default.SendFileAsync("PRIVATE_CHAT_VOICE_SYNC", msg, filePath);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 初始化公聊数据
|
||
|
/// </summary>
|
||
|
public void InitPublicChatMsgs()
|
||
|
{
|
||
|
|
||
|
if (GameSettings.othersSettings.mode != Mode.DisasterManagement)
|
||
|
{
|
||
|
PublicChatMsgs.Clear();
|
||
|
for (int i = 1; i <= 8; i++)
|
||
|
{
|
||
|
PublicChatMsgs.Add(i, new List<ChatMessage>());
|
||
|
}
|
||
|
CurrentPublicChannel = new ChatChannel()
|
||
|
{
|
||
|
ID = 1,
|
||
|
RoomID = CurrentUserInfo.room.Id,
|
||
|
Users = CurrentUserInfo.room.UserList,
|
||
|
EnterUserId = CurrentUserInfo.mySelf.Id
|
||
|
};
|
||
|
}
|
||
|
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 清空公聊数据
|
||
|
/// </summary>
|
||
|
public void ClearPublicChatMsgs()
|
||
|
{
|
||
|
for (int i = 1; i <= 8; i++)
|
||
|
{
|
||
|
PublicChatMsgs[i].Clear();
|
||
|
}
|
||
|
}
|
||
|
//返回当前公聊频道消息数量
|
||
|
public int GetMsgsCountFromCurrentPublicChannel()
|
||
|
{
|
||
|
if (CurrentPublicChannel != null)
|
||
|
{
|
||
|
return PublicChatMsgs[CurrentPublicChannel.ID].Count;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
//返回当前频道消息
|
||
|
public ChatMessage GetCurrentPublicChatMsgByIndex(int index)
|
||
|
{
|
||
|
return PublicChatMsgs[CurrentPublicChannel.ID][index];
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 添加消息到当前公聊频道并刷新UI
|
||
|
/// </summary>
|
||
|
/// <param name="msg">聊天消息</param>
|
||
|
public void AddMsgToPublicChat(ChatMessage msg)
|
||
|
{
|
||
|
if (CurrentPublicChannel != null)
|
||
|
{
|
||
|
PublicChatMsgs[CurrentPublicChannel.ID].Add(msg);
|
||
|
}
|
||
|
//刷新UI
|
||
|
MessageDispatcher.SendMessage(MessageName.RefreshPublicChatMsgs.ToString());
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 添加音频消息到公聊信息
|
||
|
/// </summary>
|
||
|
/// <param name="msg"></param>
|
||
|
/// <param name="tempFilePath"></param>
|
||
|
internal void AddAudioMsgToPublicChat(ChatAudioMessage msg, string tempFilePath)
|
||
|
{
|
||
|
// 语音消息是自己发送的标记为已读
|
||
|
if (msg.SenderId == CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
msg.IsPlayed = true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
string filePath = Path.Combine(UserPath, msg.FileName);
|
||
|
//// 保存未读消息地址
|
||
|
//UnReadAudioPublic.Add(filePath);
|
||
|
// 保存音频文件
|
||
|
if (!Directory.Exists(UserPath))
|
||
|
{
|
||
|
Directory.CreateDirectory(UserPath);
|
||
|
}
|
||
|
File.Move(tempFilePath, filePath);
|
||
|
}
|
||
|
if (CurrentPublicChannel != null)
|
||
|
{
|
||
|
PublicChatMsgs[CurrentPublicChannel.ID].Add(msg);
|
||
|
}
|
||
|
//刷新UI
|
||
|
MessageDispatcher.SendMessage(MessageName.RefreshPublicChatMsgs.ToString());
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 添加公聊消息
|
||
|
/// </summary>
|
||
|
/// <param name="channel">公聊频道</param>
|
||
|
/// <param name="msg">聊天消息</param>
|
||
|
public void AddMsgToPublicChat(ChatChannel channel, ChatMessage msg)
|
||
|
{
|
||
|
PublicChatMsgs[channel.ID].Add(msg);
|
||
|
}
|
||
|
|
||
|
public void AddOSMsg(string str)
|
||
|
{
|
||
|
ChatMessage msg = new ChatTextMessage()
|
||
|
{
|
||
|
SenderId = -1,
|
||
|
Data = str
|
||
|
};
|
||
|
AddMsgToPublicChat(msg);
|
||
|
}
|
||
|
public void JoinPublicChannel(ChatChannel channel)
|
||
|
{
|
||
|
if (channel.EnterUserId != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
string userName = CurrentUserInfo.room.FindUserById(channel.EnterUserId).UserInfo.RealName;
|
||
|
string str = string.Format("{0}进入了频道{1}", userName, channel.ID);
|
||
|
AddOSMsg(str);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
CurrentPublicChannel = channel;
|
||
|
MessageDispatcher.SendMessage(MessageName.RefreshPublicChatMsgs.ToString());
|
||
|
}
|
||
|
}
|
||
|
public void LeavePublicChanel(ChatChannel channel)
|
||
|
{
|
||
|
if (channel.EnterUserId != CurrentUserInfo.mySelf.Id)
|
||
|
{
|
||
|
//添加显示消息
|
||
|
string userName = CurrentUserInfo.room.FindUserById(channel.EnterUserId).UserInfo.RealName;
|
||
|
string str = string.Format("{0}离开了频道{1}", userName, channel.ID);
|
||
|
AddOSMsg(str);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
CurrentPublicChannel = null;
|
||
|
MessageDispatcher.SendMessage(MessageName.RefreshPublicChatMsgs.ToString());
|
||
|
//申请进入频道
|
||
|
SendJoinPublicChatChannel();
|
||
|
}
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 发送公共文本聊天消息
|
||
|
/// </summary>
|
||
|
/// <param name="msg"></param>
|
||
|
public void SendPublicTextChatMessage(string str)
|
||
|
{
|
||
|
//要发送的文本消息
|
||
|
ChatTextMessage msg = new ChatTextMessage()
|
||
|
{
|
||
|
Channel = CurrentPublicChannel,
|
||
|
Data = str,
|
||
|
ID = -1,
|
||
|
ReplayFrameCount = InputManager.frameCount,
|
||
|
SenderId = CurrentUserInfo.mySelf.Id,
|
||
|
BattleId = null,
|
||
|
CreateTime = DateTime.Now
|
||
|
};
|
||
|
NetworkManager.Default.SendAsync("PUBLIC_TEXT_MESSAGE_SYNC", msg);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 发送公共语音聊天消息
|
||
|
/// </summary>
|
||
|
/// <param name="msg"></param>
|
||
|
public void SendPublicAudioChatMessage(string fileName, TimeSpan time)
|
||
|
{
|
||
|
int timeSpan = Mathf.RoundToInt((float)(time.TotalSeconds));
|
||
|
timeSpan = Mathf.Clamp(timeSpan, 0, 60);
|
||
|
//要发送的语音消息内容
|
||
|
var msg = new ChatAudioMessage()
|
||
|
{
|
||
|
Channel = CurrentPublicChannel,
|
||
|
FileName = fileName,
|
||
|
ID = -1,
|
||
|
IsPlayed = false,
|
||
|
ReplayFrameCount = InputManager.frameCount,
|
||
|
SenderId = CurrentUserInfo.mySelf.Id,
|
||
|
ChatGroup = ChatType.PublicChat,
|
||
|
TimeSpanSeconds = timeSpan
|
||
|
};
|
||
|
string filePath = Path.Combine(UserPath, fileName);
|
||
|
NetworkManager.Default.SendFileAsync("PUBLIC_CHAT_VOICE_SYNC", msg, filePath);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 申请加入公共聊天频道
|
||
|
/// </summary>
|
||
|
/// <param name="channel"></param>
|
||
|
public void SendJoinPublicChatChannel()
|
||
|
{
|
||
|
//向服务器发送进入频道请求
|
||
|
ChatChannel channel = new ChatChannel()
|
||
|
{
|
||
|
ID = joinChannelID,
|
||
|
RoomID = CurrentUserInfo.room.Id,
|
||
|
EnterUserId = CurrentUserInfo.mySelf.Id,
|
||
|
Users = new List<UserData>()
|
||
|
};
|
||
|
NetworkManager.Default.SendAsync("ENTER_PUBLIC_CHAT_CHANNEL_SYNC", channel);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 离开公共聊天频道
|
||
|
/// </summary>
|
||
|
/// <param name="channel"></param>
|
||
|
public void SendLeavePublicChatChannel()
|
||
|
{
|
||
|
NetworkManager.Default.SendAsync("LEAVE_PUBLIC_CHAT_CHANNEL_SYNC", CurrentPublicChannel);
|
||
|
}
|
||
|
|
||
|
#region 参谋建议
|
||
|
/// <summary>
|
||
|
/// 发送建议文本
|
||
|
/// </summary>
|
||
|
/// <param name="str"></param>
|
||
|
public void SendSuggestTextMessage(string str)
|
||
|
{
|
||
|
ChatTextMessage msg = new ChatTextMessage()
|
||
|
{
|
||
|
BattleId = 0,
|
||
|
Channel = CurrentSuggestChannel,
|
||
|
ChatGroup = ChatType.SuggestChat,
|
||
|
CreateTime = DateTime.Now,
|
||
|
Data = str,
|
||
|
ID = 0,
|
||
|
ReceiverId = FindSuggestReceiverId(),
|
||
|
SenderId = CurrentUserInfo.mySelf.Id,
|
||
|
ReplayFrameCount = InputManager.frameCount
|
||
|
};
|
||
|
NetworkManager.Default.SendAsync("SUGGEST_TEXT_MESSAGE_SYNC", msg);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 发送建议语音
|
||
|
/// </summary>
|
||
|
/// <param name="fileName"></param>
|
||
|
public void SendSuggestAudioMessage(string fileName, TimeSpan time)
|
||
|
{
|
||
|
int timeSpan = Mathf.RoundToInt((float)(time.TotalSeconds));
|
||
|
timeSpan = Mathf.Clamp(timeSpan, 0, 60);
|
||
|
ChatAudioMessage msg = new ChatAudioMessage()
|
||
|
{
|
||
|
BattleId = 0,
|
||
|
Channel = CurrentSuggestChannel,
|
||
|
ChatGroup = ChatType.SuggestChat,
|
||
|
CreateTime = DateTime.Now,
|
||
|
FileName = fileName,
|
||
|
IsPlayed = false,
|
||
|
TimeSpanSeconds = timeSpan,
|
||
|
ID = 0,
|
||
|
ReceiverId = FindSuggestReceiverId(),
|
||
|
SenderId = CurrentUserInfo.mySelf.Id,
|
||
|
ReplayFrameCount = InputManager.frameCount
|
||
|
};
|
||
|
string filePath = Path.Combine(UserPath, fileName);
|
||
|
NetworkManager.Default.SendFileAsync("SUGGEST_AUDIO_MESSAGE_SYNC", msg, filePath);
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 进入建议频道
|
||
|
/// </summary>
|
||
|
public void SendEnterSuggestChatChannelMessage()
|
||
|
{
|
||
|
long id = 0;
|
||
|
switch (CurrentUserInfo.organization.Level)
|
||
|
{
|
||
|
//总队
|
||
|
case 1:
|
||
|
id = 1001;
|
||
|
break;
|
||
|
//支队
|
||
|
case 2:
|
||
|
id = 1002;
|
||
|
break;
|
||
|
//大队
|
||
|
case 3:
|
||
|
id = 1003;
|
||
|
break;
|
||
|
}
|
||
|
ChatChannel channel = new ChatChannel()
|
||
|
{
|
||
|
EnterUserId = CurrentUserInfo.mySelf.Id,
|
||
|
ID = id,
|
||
|
RoomID = CurrentUserInfo.room.Id,
|
||
|
Users = new List<UserData>()
|
||
|
};
|
||
|
NetworkManager.Default.SendAsync("ENTER_SUGGEST_CHAT_CHANNEL_SYNC", channel);
|
||
|
//(int)ErrorCode.ChannelIsNotExist);错误码
|
||
|
}
|
||
|
public void JoinSuggestChatChannel(ChatChannel channel)
|
||
|
{
|
||
|
CurrentSuggestChannel = channel;
|
||
|
}
|
||
|
/// <summary>
|
||
|
/// 找到接收者ID
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
public long FindSuggestReceiverId()
|
||
|
{
|
||
|
long receiverId = -1;
|
||
|
int level = CurrentUserInfo.organization.Level;
|
||
|
if (CurrentUserInfo.role == Role.参谋)
|
||
|
{
|
||
|
foreach (var item in CurrentUserInfo.room.UserList)
|
||
|
{
|
||
|
if (item.Org.Level == level)
|
||
|
{
|
||
|
if (item.Role == Role.支队指挥
|
||
|
|| item.Role == Role.大队指挥
|
||
|
|| item.Role == Role.总队指挥)
|
||
|
{
|
||
|
receiverId = item.UserInfo.Id;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return receiverId;
|
||
|
}
|
||
|
#endregion
|
||
|
#region CSV文件读取
|
||
|
private Dictionary<int, T_CsvData> LoadCsvData<T_CsvData>(string csvFilePath)
|
||
|
{
|
||
|
Dictionary<int, T_CsvData> dic = new Dictionary<int, T_CsvData>();
|
||
|
//从CSV文件读取数据
|
||
|
Dictionary<string, Dictionary<string, string>> result = LoadCsvFile(csvFilePath);
|
||
|
//遍历每一行数据
|
||
|
foreach (string ID in result.Keys)
|
||
|
{
|
||
|
//CSV的一行数据
|
||
|
Dictionary<string, string> datas = result[ID];
|
||
|
//读取Csv数据对象的属性
|
||
|
PropertyInfo[] props = typeof(T_CsvData).GetProperties();
|
||
|
//使用反射,将CSV文件的数据赋值给CSV数据对象的相应字段,要求CSV文件的字段名和CSV数据对象的字段名完全相同
|
||
|
T_CsvData obj = Activator.CreateInstance<T_CsvData>();
|
||
|
foreach (PropertyInfo pi in props)
|
||
|
{
|
||
|
pi.SetValue(obj, Convert.ChangeType(datas[pi.Name], pi.PropertyType), null);
|
||
|
}
|
||
|
//按ID-数据的形式存储
|
||
|
dic[Convert.ToInt32(ID)] = obj;
|
||
|
}
|
||
|
return dic;
|
||
|
}
|
||
|
public Dictionary<string, Dictionary<string, string>> LoadCsvFile(string filePath)
|
||
|
{
|
||
|
Dictionary<string, Dictionary<string, string>> result = new Dictionary<string, Dictionary<string, string>>();
|
||
|
string[] fileData = File.ReadAllLines(filePath);
|
||
|
// CSV文件的第一行为Key字段,第二行开始是数据。第一个字段一定是ID。
|
||
|
string[] keys = fileData[1].Split(',');
|
||
|
for (int i = 2; i < fileData.Length; i++)
|
||
|
{
|
||
|
string[] line = fileData[i].Split(',');
|
||
|
// 以ID为key值,创建一个新的集合,用于保存当前行的数据
|
||
|
string ID = line[0];
|
||
|
result[ID] = new Dictionary<string, string>();
|
||
|
for (int j = 0; j < line.Length; j++)
|
||
|
{
|
||
|
// 每一行的数据存储规则:Key字段-Value值
|
||
|
result[ID][keys[j]] = line[j];
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
#endregion
|
||
|
#endregion
|
||
|
public void ResertChatManager()
|
||
|
{
|
||
|
Msgs.Clear();
|
||
|
UnReadMsgs.Clear();
|
||
|
PublicChatMsgs.Clear();
|
||
|
//TaskData.Clear();
|
||
|
SuggestChatMsg.Clear();
|
||
|
}
|
||
|
}
|
||
|
|