245 lines
6.4 KiB
C#
245 lines
6.4 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace BrewMonster
|
|
{
|
|
public class CECFriendMan
|
|
{
|
|
// =========================
|
|
// Nested Types
|
|
// =========================
|
|
|
|
public struct Friend
|
|
{
|
|
public int Id;
|
|
public int Profession;
|
|
public int GroupId;
|
|
public byte Status;
|
|
public int Level;
|
|
public int AreaId;
|
|
public string Name;
|
|
|
|
public bool IsGameOnline() => IsGameOnline(Status);
|
|
public bool IsGTOnline() => IsGTOnline(Status);
|
|
|
|
public static bool IsGameOnline(byte s)
|
|
{
|
|
// TODO: implement đúng theo logic C++
|
|
return (s & 0x01) != 0;
|
|
}
|
|
|
|
public static bool IsGTOnline(byte s)
|
|
{
|
|
// TODO: implement đúng theo logic C++
|
|
return (s & 0x02) != 0;
|
|
}
|
|
}
|
|
|
|
public class FRIEND_EX
|
|
{
|
|
public bool NewFriendPlaceHolder;
|
|
public int Uid;
|
|
public int Rid;
|
|
public int Level;
|
|
public long LastLoginTime;
|
|
public long UpdateTime;
|
|
public int ReincarnationCount;
|
|
public string Remarks = string.Empty;
|
|
}
|
|
|
|
public class SEND_INFO
|
|
{
|
|
public int Rid;
|
|
public long SendMailTime;
|
|
}
|
|
|
|
public class GROUP
|
|
{
|
|
public string Name = string.Empty;
|
|
public int GroupId;
|
|
public uint Color; // nếu dùng Unity thì có thể đổi sang Color
|
|
|
|
public List<Friend> Friends = new();
|
|
}
|
|
|
|
public class MESSAGE
|
|
{
|
|
public string SenderName = string.Empty;
|
|
public int SenderId;
|
|
public string MessageText = string.Empty;
|
|
public byte Flag;
|
|
}
|
|
|
|
// =========================
|
|
// Fields
|
|
// =========================
|
|
|
|
private List<GROUP> m_Groups = new();
|
|
private Dictionary<int, Friend> m_FriendTable = new();
|
|
private List<MESSAGE> m_OfflineMsgs = new();
|
|
private List<FRIEND_EX> m_FriendEx = new();
|
|
private List<SEND_INFO> m_SendInfo = new();
|
|
|
|
// =========================
|
|
// Operations
|
|
// =========================
|
|
|
|
public bool CheckInit() => m_Groups.Count > 0;
|
|
|
|
public Friend AddFriend(int id, int profession, int groupId, byte status, string name)
|
|
{
|
|
var friend = new Friend
|
|
{
|
|
Id = id,
|
|
Profession = profession,
|
|
GroupId = groupId,
|
|
Status = status,
|
|
Name = name
|
|
};
|
|
|
|
m_FriendTable[id] = friend;
|
|
|
|
var group = GetGroupByID(groupId);
|
|
group?.Friends.Add(friend);
|
|
|
|
return friend;
|
|
}
|
|
|
|
public void RemoveFriend(int idFriend)
|
|
{
|
|
if (!m_FriendTable.TryGetValue(idFriend, out var friend))
|
|
return;
|
|
|
|
m_FriendTable.Remove(idFriend);
|
|
|
|
var group = GetGroupByID(friend.GroupId);
|
|
group?.Friends.Remove(friend);
|
|
}
|
|
|
|
public void RemoveAllFriends()
|
|
{
|
|
m_FriendTable.Clear();
|
|
foreach (var group in m_Groups)
|
|
group.Friends.Clear();
|
|
}
|
|
|
|
public void ChangeFriendStatus(int idFriend, byte status)
|
|
{
|
|
if (m_FriendTable.TryGetValue(idFriend, out var friend))
|
|
friend.Status = status;
|
|
}
|
|
|
|
public void ChangeFriendGroup(int idFriend, int newGroupId)
|
|
{
|
|
if (!m_FriendTable.TryGetValue(idFriend, out var friend))
|
|
return;
|
|
|
|
var oldGroup = GetGroupByID(friend.GroupId);
|
|
oldGroup?.Friends.Remove(friend);
|
|
|
|
friend.GroupId = newGroupId;
|
|
|
|
var newGroup = GetGroupByID(newGroupId);
|
|
newGroup?.Friends.Add(friend);
|
|
}
|
|
|
|
public Friend? GetFriendByID(int idFriend)
|
|
{
|
|
m_FriendTable.TryGetValue(idFriend, out var friend);
|
|
return friend;
|
|
}
|
|
|
|
public Friend? GetFriendByName(string name)
|
|
{
|
|
foreach (var friend in m_FriendTable.Values)
|
|
{
|
|
if (friend.Name == name)
|
|
return friend;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public void SetFriendLevel(int idFriend, int level)
|
|
{
|
|
if (m_FriendTable.TryGetValue(idFriend, out var friend))
|
|
friend.Level = level;
|
|
}
|
|
|
|
public void SetFriendArea(int idFriend, int areaId)
|
|
{
|
|
if (m_FriendTable.TryGetValue(idFriend, out var friend))
|
|
friend.AreaId = areaId;
|
|
}
|
|
|
|
// =========================
|
|
// Group Operations
|
|
// =========================
|
|
|
|
public bool AddGroup(int idGroup, string name)
|
|
{
|
|
if (GetGroupByID(idGroup) != null)
|
|
return false;
|
|
|
|
m_Groups.Add(new GROUP
|
|
{
|
|
GroupId = idGroup,
|
|
Name = name
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
public void RemoveGroup(int idGroup)
|
|
{
|
|
m_Groups.RemoveAll(g => g.GroupId == idGroup);
|
|
}
|
|
|
|
public void ChangeGroupName(int idGroup, string name)
|
|
{
|
|
var group = GetGroupByID(idGroup);
|
|
if (group != null)
|
|
group.Name = name;
|
|
}
|
|
|
|
public void SetGroupColor(int idGroup, uint color)
|
|
{
|
|
var group = GetGroupByID(idGroup);
|
|
if (group != null)
|
|
group.Color = color;
|
|
}
|
|
|
|
public int GetGroupNum() => m_Groups.Count;
|
|
|
|
public GROUP? GetGroupByIndex(int index)
|
|
{
|
|
if (index < 0 || index >= m_Groups.Count)
|
|
return null;
|
|
|
|
return m_Groups[index];
|
|
}
|
|
|
|
public GROUP? GetGroupByID(int id)
|
|
{
|
|
return m_Groups.Find(g => g.GroupId == id);
|
|
}
|
|
|
|
// =========================
|
|
// Offline Messages
|
|
// =========================
|
|
|
|
public int GetOfflineMsgNum() => m_OfflineMsgs.Count;
|
|
|
|
public MESSAGE? GetOfflineMsg(int index)
|
|
{
|
|
if (index < 0 || index >= m_OfflineMsgs.Count)
|
|
return null;
|
|
|
|
return m_OfflineMsgs[index];
|
|
}
|
|
|
|
public void RemoveAllOfflineMsgs()
|
|
{
|
|
m_OfflineMsgs.Clear();
|
|
}
|
|
}
|
|
} |