HaoYueTunnel/ClientCore/Manager/UserMgr.cs

130 lines
4.0 KiB
C#
Raw Normal View History

2023-05-25 18:30:22 +08:00
using AxibugProtobuf;
using ClientCore.Common;
2023-06-15 15:45:58 +08:00
using ClientCore.Data;
2023-05-25 18:30:22 +08:00
using ClientCore.Event;
using ClientCore.Network;
using System;
using System.Security.Cryptography;
namespace ClientCore.Manager
{
public class UserMgr
{
2023-06-15 15:45:58 +08:00
public UserInfo MainPlayer { get; private set; }
public bool bLogin => MainPlayer != null;
Dictionary<long, UserInfo> DictUID2User = new Dictionary<long, UserInfo>();
2023-05-25 18:30:22 +08:00
public UserMgr()
{
2023-06-15 15:45:58 +08:00
//网络事件注册
2023-05-25 18:30:22 +08:00
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserOnlinelist, RecvUserOnlinelist);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserJoin, RecvCmdUserJoin);
NetMsg.Instance.RegNetMsgEvent((int)CommandID.CmdUserLeave, RecvGetUserLeave);
}
2023-06-15 15:45:58 +08:00
public void SetMainPlayer(long uid,string account,int state)
{
MainPlayer = new UserInfo()
{
State = state,
NickName = account,
UID = uid
};
}
public void Send_GetUserList()
{
Protobuf_UserList msg = new Protobuf_UserList()
{
};
App.networkMain.SendToServer((int)CommandID.CmdUserOnlinelist, ProtoBufHelper.Serizlize(msg));
}
2023-05-25 18:30:22 +08:00
public void RecvUserOnlinelist(byte[] reqData)
{
Protobuf_UserList_RESP msg = ProtoBufHelper.DeSerizlize<Protobuf_UserList_RESP>(reqData);
for(int i = 0;i < msg.UserList.Count;i++)
{
UserMiniInfo mi = msg.UserList[i];
UpdateOrAddUser(mi);
}
}
public void RecvCmdUserJoin(byte[] reqData)
{
Protobuf_UserJoin_RESP msg = ProtoBufHelper.DeSerizlize<Protobuf_UserJoin_RESP>(reqData);
UpdateOrAddUser(msg.UserInfo);
}
public void RecvGetUserLeave(byte[] reqData)
{
Protobuf_UserLeave_RESP msg = ProtoBufHelper.DeSerizlize<Protobuf_UserLeave_RESP>(reqData);
RemoveUser(msg.UID);
}
public void UpdateOrAddUser(UserMiniInfo minfo)
{
lock (DictUID2User)
{
if (!DictUID2User.ContainsKey(minfo.UID))
{
DictUID2User[minfo.UID] = new UserInfo()
{
UID = minfo.UID,
State = minfo.State,
NickName = minfo.NickName,
};
2024-04-26 17:51:55 +08:00
//抛出用户加入事件
EventSystem.Instance.PostEvent(EEvent.UserJoin, minfo.UID, minfo.NickName);
2023-05-25 18:30:22 +08:00
}
else
{
DictUID2User[minfo.UID].State = minfo.State;
DictUID2User[minfo.UID].NickName= minfo.NickName;
}
}
}
public void RemoveUser(long UID)
{
bool bflag = false;
2024-04-26 17:51:55 +08:00
string UName = "";
2023-05-25 18:30:22 +08:00
lock (DictUID2User)
{
if (DictUID2User.ContainsKey(UID))
{
2024-04-26 17:51:55 +08:00
UName = DictUID2User[UID].NickName;
2023-05-25 18:30:22 +08:00
DictUID2User.Remove(UID);
bflag = true;
}
}
if (bflag)
{
//抛出用户离开事件
2024-04-26 17:51:55 +08:00
EventSystem.Instance.PostEvent(EEvent.UserLeave, UID, UName);
}
}
public UserInfo GetUserByUid(long UID)
{
lock (DictUID2User)
{
if (DictUID2User.ContainsKey(UID))
{
return DictUID2User[UID];
}
return null;
2023-05-25 18:30:22 +08:00
}
}
2023-06-15 15:45:58 +08:00
public UserInfo[] GetUserInfo()
{
UserInfo[] ulist = new UserInfo[DictUID2User.Count];
long[] UIDs = DictUID2User.Keys.ToArray();
for (int i = 0; i < UIDs.Length; i++)
{
ulist[i] = DictUID2User[UIDs[i]];
}
return ulist;
}
2023-05-25 18:30:22 +08:00
}
}