BuffManager.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. void ISupportInitialize.BeginInit()
  15. {
  16. }
  17. void ISupportInitialize.EndInit()
  18. {
  19. foreach (var buff in Buffs)
  20. {
  21. this.BuffGuidDict.Add(buff.Id, buff);
  22. }
  23. foreach (var buff in Buffs)
  24. {
  25. this.BuffTypeDict.Add(buff.Type, buff);
  26. }
  27. }
  28. public bool Add(Buff buff)
  29. {
  30. if (!this.Buffs.Contains(buff))
  31. {
  32. return false;
  33. }
  34. if (!this.BuffGuidDict.ContainsKey(buff.Id))
  35. {
  36. return false;
  37. }
  38. if (!this.BuffTypeDict.ContainsKey(buff.Type))
  39. {
  40. return false;
  41. }
  42. this.Buffs.Add(buff);
  43. this.BuffGuidDict.Add(buff.Id, buff);
  44. this.BuffTypeDict.Add(buff.Type, buff);
  45. return true;
  46. }
  47. public Buff Get(ObjectId guid)
  48. {
  49. if (!this.BuffGuidDict.ContainsKey(guid))
  50. {
  51. return null;
  52. }
  53. return this.BuffGuidDict[guid];
  54. }
  55. public Buff Get(int type)
  56. {
  57. if (!this.BuffTypeDict.ContainsKey(type))
  58. {
  59. return null;
  60. }
  61. return this.BuffTypeDict[type];
  62. }
  63. private bool Remove(Buff buff)
  64. {
  65. if (buff == null)
  66. {
  67. return false;
  68. }
  69. this.Buffs.Remove(buff);
  70. this.BuffGuidDict.Remove(buff.Id);
  71. this.BuffTypeDict.Remove(buff.Type);
  72. return true;
  73. }
  74. public bool Remove(ObjectId guid)
  75. {
  76. var buff = this.Get(guid);
  77. return this.Remove(buff);
  78. }
  79. public bool Remove(int type)
  80. {
  81. var buff = this.Get(type);
  82. return this.Remove(buff);
  83. }
  84. }
  85. }