90 lines
1.9 KiB
C#
90 lines
1.9 KiB
C#
using LiteNetLib;
|
|
|
|
namespace MMOserver.Game.Channel;
|
|
|
|
public class ChannelManager
|
|
{
|
|
// 일단은 채널은 서버 켤때 고정으로간다 1개
|
|
public static ChannelManager Instance
|
|
{
|
|
get;
|
|
} = new ChannelManager();
|
|
|
|
// 채널 관리
|
|
private List<Channel> channels = new List<Channel>();
|
|
|
|
// 채널별 유저 관리 (유저 key, 채널 val)
|
|
private Dictionary<long, int> connectUsers = new Dictionary<long, int>();
|
|
|
|
public ChannelManager()
|
|
{
|
|
Initializer();
|
|
}
|
|
|
|
public void Initializer(int channelSize = 1)
|
|
{
|
|
for (int i = 0; i <= channelSize; i++)
|
|
{
|
|
channels.Add(new Channel(i));
|
|
}
|
|
}
|
|
|
|
public Channel GetChannel(int channelId)
|
|
{
|
|
return channels[channelId];
|
|
}
|
|
|
|
public List<Channel> GetChannels()
|
|
{
|
|
return channels;
|
|
}
|
|
|
|
public void AddUser(int channelId, long userId, Player player, NetPeer peer)
|
|
{
|
|
// 유저 추가
|
|
connectUsers.Add(userId, channelId);
|
|
// 채널에 유저 + peer 추가
|
|
channels[channelId].AddUser(userId, player, peer);
|
|
}
|
|
|
|
public bool RemoveUser(long userId)
|
|
{
|
|
// 채널에 없는 유저면 스킵
|
|
if (!connectUsers.TryGetValue(userId, out int channelId))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// 날린다.
|
|
if (channelId >= 0)
|
|
{
|
|
channels[channelId].RemoveUser(userId);
|
|
connectUsers.Remove(userId);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public int HasUser(long userId)
|
|
{
|
|
int channelId = -1;
|
|
if (connectUsers.ContainsKey(userId))
|
|
{
|
|
channelId = connectUsers[userId];
|
|
}
|
|
|
|
if (channelId != -1)
|
|
{
|
|
return channels[channelId].HasUser(userId);
|
|
}
|
|
|
|
return channelId;
|
|
}
|
|
|
|
public Dictionary<long, int> GetConnectUsers()
|
|
{
|
|
return connectUsers;
|
|
}
|
|
}
|