PlayerComponent.cs 819 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace Model
  4. {
  5. public class PlayerComponent : Component
  6. {
  7. private readonly Dictionary<long, Player> idPlayers = new Dictionary<long, Player>();
  8. public void Add(Player player)
  9. {
  10. this.idPlayers.Add(player.Id, player);
  11. }
  12. public Player Get(long id)
  13. {
  14. this.idPlayers.TryGetValue(id, out Player gamer);
  15. return gamer;
  16. }
  17. public void Remove(long id)
  18. {
  19. this.idPlayers.Remove(id);
  20. }
  21. public int Count
  22. {
  23. get
  24. {
  25. return this.idPlayers.Count;
  26. }
  27. }
  28. public Player[] GetAll()
  29. {
  30. return this.idPlayers.Values.ToArray();
  31. }
  32. public override void Dispose()
  33. {
  34. if (this.Id == 0)
  35. {
  36. return;
  37. }
  38. base.Dispose();
  39. foreach (Player player in this.idPlayers.Values)
  40. {
  41. player.Dispose();
  42. }
  43. }
  44. }
  45. }