77 lines
1.6 KiB
C#
77 lines
1.6 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace ET
|
|
{
|
|
/// <summary>
|
|
/// 用于Map服务器
|
|
/// </summary>
|
|
|
|
public class UserComponent : Entity
|
|
{
|
|
public static UserComponent Instance { get; private set; }
|
|
|
|
private readonly Dictionary<long, User> idPlayers = new Dictionary<long, User>();
|
|
public void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
public void Add(User user)
|
|
{
|
|
if(!this.idPlayers.TryAdd(user.Id, user))
|
|
{
|
|
Log.Error($"userDic has user where id = {user.Id}");
|
|
idPlayers[user.Id] = user;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从缓存获取
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public User Get(long id)
|
|
{
|
|
if(!this.idPlayers.TryGetValue(id, out User user))
|
|
{
|
|
return null;
|
|
}
|
|
return user;
|
|
}
|
|
|
|
public bool Remove(long id)
|
|
{
|
|
return this.idPlayers.Remove(id);
|
|
}
|
|
|
|
public int Count
|
|
{
|
|
get
|
|
{
|
|
return this.idPlayers.Count;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<User> GetAll()
|
|
{
|
|
return this.idPlayers.Values;
|
|
}
|
|
|
|
public override void Dispose()
|
|
{
|
|
if (this.IsDisposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
base.Dispose();
|
|
|
|
foreach (User player in this.idPlayers.Values)
|
|
{
|
|
player.Dispose();
|
|
}
|
|
|
|
Instance = null;
|
|
}
|
|
}
|
|
} |