using System.Collections.Generic; namespace ET { /// /// 用于Map服务器 /// public class UserComponent : Entity { public static UserComponent Instance { get; private set; } private readonly Dictionary idPlayers = new Dictionary(); 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; } } /// /// 从缓存获取 /// /// /// 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 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; } } }