LSUpdater.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. namespace ET
  5. {
  6. public class LSUpdater
  7. {
  8. [BsonIgnore]
  9. public LSWorld Parent { get; set; }
  10. private SortedSet<long> updateIds = new();
  11. private Dictionary<long, EntityRef<LSEntity>> lsEntities = new();
  12. private Queue<long> addUpdateIds = new();
  13. private Queue<long> removeUpdateIds = new();
  14. public void Update()
  15. {
  16. while (this.addUpdateIds.Count > 0)
  17. {
  18. this.updateIds.Add(this.addUpdateIds.Dequeue());
  19. }
  20. foreach (long id in this.updateIds)
  21. {
  22. LSEntity entity = lsEntities[id];
  23. if (entity == null)
  24. {
  25. this.removeUpdateIds.Enqueue(id);
  26. continue;
  27. }
  28. LSSington.Instance.LSUpdate(entity);
  29. }
  30. while (this.removeUpdateIds.Count > 0)
  31. {
  32. long id = this.removeUpdateIds.Dequeue();
  33. this.updateIds.Remove(id);
  34. this.lsEntities.Remove(id);
  35. }
  36. }
  37. public void Add(LSEntity entity)
  38. {
  39. this.addUpdateIds.Enqueue(entity.Id);
  40. this.lsEntities.Add(entity.Id, entity);
  41. }
  42. }
  43. }