BuffManager.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using MongoDB.Bson;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. namespace Component
  6. {
  7. public class BuffManager: ISupportInitialize
  8. {
  9. public HashSet<Buff> Buffs { get; private set; }
  10. [BsonIgnore]
  11. public Dictionary<ObjectId, Buff> BuffGuidDict { get; private set; }
  12. [BsonIgnore]
  13. public Dictionary<int, Buff> BuffTypeDict { get; private set; }
  14. public BuffManager()
  15. {
  16. this.Buffs = new HashSet<Buff>();
  17. this.BuffGuidDict = new Dictionary<ObjectId, Buff>();
  18. this.BuffTypeDict = new Dictionary<int, Buff>();
  19. }
  20. void ISupportInitialize.BeginInit()
  21. {
  22. }
  23. void ISupportInitialize.EndInit()
  24. {
  25. foreach (var buff in this.Buffs)
  26. {
  27. this.BuffGuidDict.Add(buff.Id, buff);
  28. }
  29. foreach (var buff in this.Buffs)
  30. {
  31. this.BuffTypeDict.Add(buff.Type, buff);
  32. }
  33. }
  34. public bool Add(Buff buff)
  35. {
  36. if (this.Buffs.Contains(buff))
  37. {
  38. return false;
  39. }
  40. if (this.BuffGuidDict.ContainsKey(buff.Id))
  41. {
  42. return false;
  43. }
  44. if (this.BuffTypeDict.ContainsKey(buff.Type))
  45. {
  46. return false;
  47. }
  48. this.Buffs.Add(buff);
  49. this.BuffGuidDict.Add(buff.Id, buff);
  50. this.BuffTypeDict.Add(buff.Type, buff);
  51. return true;
  52. }
  53. public Buff GetById(ObjectId id)
  54. {
  55. if (!this.BuffGuidDict.ContainsKey(id))
  56. {
  57. return null;
  58. }
  59. return this.BuffGuidDict[id];
  60. }
  61. public Buff GetByType(int type)
  62. {
  63. if (!this.BuffTypeDict.ContainsKey(type))
  64. {
  65. return null;
  66. }
  67. return this.BuffTypeDict[type];
  68. }
  69. private bool Remove(Buff buff)
  70. {
  71. if (buff == null)
  72. {
  73. return false;
  74. }
  75. this.Buffs.Remove(buff);
  76. this.BuffGuidDict.Remove(buff.Id);
  77. this.BuffTypeDict.Remove(buff.Type);
  78. return true;
  79. }
  80. public bool RemoveById(ObjectId id)
  81. {
  82. var buff = this.GetById(id);
  83. return this.Remove(buff);
  84. }
  85. public bool RemoveByType(int type)
  86. {
  87. var buff = this.GetByType(type);
  88. return this.Remove(buff);
  89. }
  90. }
  91. }