BuffComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 BuffComponent: Component, ISupportInitialize
  10. {
  11. [BsonElement]
  12. public HashSet<Buff> Buffs { get; set; }
  13. private Dictionary<ObjectId, Buff> buffGuidDict { get; set; }
  14. private MultiMap<BuffType, Buff> buffTypeMMap { get; set; }
  15. public BuffComponent()
  16. {
  17. this.Buffs = new HashSet<Buff>();
  18. this.buffGuidDict = new Dictionary<ObjectId, Buff>();
  19. this.buffTypeMMap = new MultiMap<BuffType, Buff>();
  20. }
  21. void ISupportInitialize.BeginInit()
  22. {
  23. }
  24. void ISupportInitialize.EndInit()
  25. {
  26. foreach (var buff in this.Buffs)
  27. {
  28. this.buffGuidDict.Add(buff.Guid, buff);
  29. }
  30. foreach (var buff in this.Buffs)
  31. {
  32. this.buffTypeMMap.Add(buff.Type, buff);
  33. }
  34. }
  35. public bool Add(Buff buff)
  36. {
  37. if (this.Buffs.Contains(buff))
  38. {
  39. return false;
  40. }
  41. if (this.buffGuidDict.ContainsKey(buff.Guid))
  42. {
  43. return false;
  44. }
  45. if (this.buffTypeMMap.GetOne(buff.Type) != null)
  46. {
  47. return false;
  48. }
  49. this.Buffs.Add(buff);
  50. this.buffGuidDict.Add(buff.Guid, buff);
  51. this.buffTypeMMap.Add(buff.Type, buff);
  52. return true;
  53. }
  54. public Buff GetByGuid(ObjectId guid)
  55. {
  56. if (!this.buffGuidDict.ContainsKey(guid))
  57. {
  58. return null;
  59. }
  60. return this.buffGuidDict[guid];
  61. }
  62. public Buff GetOneByType(BuffType type)
  63. {
  64. return this.buffTypeMMap.GetOne(type);
  65. }
  66. public Buff[] GetByType(BuffType type)
  67. {
  68. return this.buffTypeMMap.GetByKey(type);
  69. }
  70. private bool Remove(Buff buff)
  71. {
  72. if (buff == null)
  73. {
  74. return false;
  75. }
  76. this.Buffs.Remove(buff);
  77. this.buffGuidDict.Remove(buff.Guid);
  78. this.buffTypeMMap.Remove(buff.Type, buff);
  79. return true;
  80. }
  81. public bool RemoveByGuid(ObjectId guid)
  82. {
  83. Buff buff = this.GetByGuid(guid);
  84. return this.Remove(buff);
  85. }
  86. public void RemoveByType(BuffType type)
  87. {
  88. Buff[] buffs = this.GetByType(type);
  89. foreach (Buff buff in buffs)
  90. {
  91. this.Remove(buff);
  92. }
  93. }
  94. }
  95. }