GroupSet.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. namespace NTMiner.Core.Impl {
  4. public class GroupSet : SetBase, IGroupSet {
  5. private readonly Dictionary<Guid, GroupData> _dicById = new Dictionary<Guid, GroupData>();
  6. private readonly IServerContext _context;
  7. public GroupSet(IServerContext context) {
  8. _context = context;
  9. context.AddCmdPath<AddGroupCommand>("添加组", LogEnum.DevConsole,
  10. action: (message) => {
  11. InitOnece();
  12. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  13. throw new ArgumentNullException();
  14. }
  15. if (_dicById.ContainsKey(message.Input.GetId())) {
  16. return;
  17. }
  18. if (string.IsNullOrEmpty(message.Input.Name)) {
  19. throw new ValidationException("Group name can't be null or empty");
  20. }
  21. GroupData entity = new GroupData().Update(message.Input);
  22. _dicById.Add(entity.Id, entity);
  23. var repository = context.CreateServerRepository<GroupData>();
  24. repository.Add(entity);
  25. VirtualRoot.RaiseEvent(new GroupAddedEvent(message.MessageId, entity));
  26. }, location: this.GetType());
  27. context.AddCmdPath<UpdateGroupCommand>("更新组", LogEnum.DevConsole,
  28. action: (message) => {
  29. InitOnece();
  30. if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty) {
  31. throw new ArgumentNullException();
  32. }
  33. if (string.IsNullOrEmpty(message.Input.Name)) {
  34. throw new ValidationException("Group name can't be null or empty");
  35. }
  36. if (!_dicById.TryGetValue(message.Input.GetId(), out GroupData entity)) {
  37. return;
  38. }
  39. if (ReferenceEquals(entity, message.Input)) {
  40. return;
  41. }
  42. entity.Update(message.Input);
  43. var repository = context.CreateServerRepository<GroupData>();
  44. repository.Update(entity);
  45. VirtualRoot.RaiseEvent(new GroupUpdatedEvent(message.MessageId, entity));
  46. }, location: this.GetType());
  47. context.AddCmdPath<RemoveGroupCommand>("移除组", LogEnum.DevConsole,
  48. action: (message) => {
  49. InitOnece();
  50. if (message == null || message.EntityId == Guid.Empty) {
  51. throw new ArgumentNullException();
  52. }
  53. if (!_dicById.ContainsKey(message.EntityId)) {
  54. return;
  55. }
  56. GroupData entity = _dicById[message.EntityId];
  57. Guid[] toRemoves = context.CoinGroupSet.GetGroupCoinIds(entity.Id).ToArray();
  58. foreach (var id in toRemoves) {
  59. VirtualRoot.Execute(new RemoveCoinGroupCommand(id));
  60. }
  61. _dicById.Remove(entity.GetId());
  62. var repository = context.CreateServerRepository<GroupData>();
  63. repository.Remove(message.EntityId);
  64. VirtualRoot.RaiseEvent(new GroupRemovedEvent(message.MessageId, entity));
  65. }, location: this.GetType());
  66. }
  67. protected override void Init() {
  68. var repository = _context.CreateServerRepository<GroupData>();
  69. foreach (var item in repository.GetAll()) {
  70. if (!_dicById.ContainsKey(item.GetId())) {
  71. _dicById.Add(item.GetId(), item);
  72. }
  73. }
  74. }
  75. public int Count {
  76. get {
  77. InitOnece();
  78. return _dicById.Count;
  79. }
  80. }
  81. public bool Contains(Guid groupId) {
  82. InitOnece();
  83. return _dicById.ContainsKey(groupId);
  84. }
  85. public bool TryGetGroup(Guid groupId, out IGroup group) {
  86. InitOnece();
  87. bool r = _dicById.TryGetValue(groupId, out GroupData g);
  88. group = g;
  89. return r;
  90. }
  91. public IEnumerable<IGroup> AsEnumerable() {
  92. InitOnece();
  93. return _dicById.Values;
  94. }
  95. }
  96. }