BuffComponent.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Generic;
  2. using Common.Base;
  3. using MongoDB.Bson;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. using Component = Common.Base.Component;
  6. namespace Model
  7. {
  8. public class BuffComponent: Component
  9. {
  10. [BsonElement]
  11. private HashSet<Buff> Buffs { get; set; }
  12. private Dictionary<ObjectId, Buff> buffGuidDict { get; set; }
  13. private MultiMap<BuffType, Buff> buffTypeMMap { get; set; }
  14. public BuffComponent()
  15. {
  16. this.Buffs = new HashSet<Buff>();
  17. this.buffGuidDict = new Dictionary<ObjectId, Buff>();
  18. this.buffTypeMMap = new MultiMap<BuffType, Buff>();
  19. }
  20. public override void EndInit()
  21. {
  22. foreach (var buff in this.Buffs)
  23. {
  24. this.buffGuidDict.Add(buff.Guid, buff);
  25. this.buffTypeMMap.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.Guid))
  35. {
  36. return false;
  37. }
  38. if (this.buffTypeMMap.GetOne(buff.Type) != null)
  39. {
  40. return false;
  41. }
  42. this.Buffs.Add(buff);
  43. this.buffGuidDict.Add(buff.Guid, buff);
  44. this.buffTypeMMap.Add(buff.Type, buff);
  45. return true;
  46. }
  47. public Buff GetByGuid(ObjectId guid)
  48. {
  49. if (!this.buffGuidDict.ContainsKey(guid))
  50. {
  51. return null;
  52. }
  53. return this.buffGuidDict[guid];
  54. }
  55. public Buff GetOneByType(BuffType type)
  56. {
  57. return this.buffTypeMMap.GetOne(type);
  58. }
  59. public Buff[] GetByType(BuffType type)
  60. {
  61. return this.buffTypeMMap.GetByKey(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.Guid);
  71. this.buffTypeMMap.Remove(buff.Type, buff);
  72. return true;
  73. }
  74. public bool RemoveByGuid(ObjectId guid)
  75. {
  76. Buff buff = this.GetByGuid(guid);
  77. return this.Remove(buff);
  78. }
  79. public void RemoveByType(BuffType type)
  80. {
  81. Buff[] buffs = this.GetByType(type);
  82. foreach (Buff buff in buffs)
  83. {
  84. this.Remove(buff);
  85. }
  86. }
  87. }
  88. }