BuffComponent.cs 2.7 KB

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