BuffManager.cs 2.7 KB

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