|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace Visibility.Utils; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Manages containers for different unit types and their relationships |
| 9 | +/// </summary> |
| 10 | +public class ContainerManager |
| 11 | +{ |
| 12 | + private readonly Dictionary<UnitType, Dictionary<ContainerType, Dictionary<uint, long>>> containers; |
| 13 | + private readonly HashSet<uint> idsToDelete = new(capacity: 200); |
| 14 | + |
| 15 | + public ContainerManager() |
| 16 | + { |
| 17 | + this.containers = new Dictionary<UnitType, Dictionary<ContainerType, Dictionary<uint, long>>> |
| 18 | + { |
| 19 | + { |
| 20 | + UnitType.Players, new Dictionary<ContainerType, Dictionary<uint, long>> |
| 21 | + { |
| 22 | + { ContainerType.All, new Dictionary<uint, long>() }, |
| 23 | + { ContainerType.Friend, new Dictionary<uint, long>() }, |
| 24 | + { ContainerType.Party, new Dictionary<uint, long>() }, |
| 25 | + { ContainerType.Company, new Dictionary<uint, long>() }, |
| 26 | + } |
| 27 | + }, |
| 28 | + { |
| 29 | + UnitType.Pets, new Dictionary<ContainerType, Dictionary<uint, long>> |
| 30 | + { |
| 31 | + { ContainerType.All, new Dictionary<uint, long>() }, |
| 32 | + { ContainerType.Friend, new Dictionary<uint, long>() }, |
| 33 | + { ContainerType.Party, new Dictionary<uint, long>() }, |
| 34 | + { ContainerType.Company, new Dictionary<uint, long>() }, |
| 35 | + } |
| 36 | + }, |
| 37 | + { |
| 38 | + UnitType.Chocobos, new Dictionary<ContainerType, Dictionary<uint, long>> |
| 39 | + { |
| 40 | + { ContainerType.All, new Dictionary<uint, long>() }, |
| 41 | + { ContainerType.Friend, new Dictionary<uint, long>() }, |
| 42 | + { ContainerType.Party, new Dictionary<uint, long>() }, |
| 43 | + { ContainerType.Company, new Dictionary<uint, long>() }, |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + UnitType.Minions, new Dictionary<ContainerType, Dictionary<uint, long>> |
| 48 | + { |
| 49 | + { ContainerType.All, new Dictionary<uint, long>() }, |
| 50 | + { ContainerType.Friend, new Dictionary<uint, long>() }, |
| 51 | + { ContainerType.Party, new Dictionary<uint, long>() }, |
| 52 | + { ContainerType.Company, new Dictionary<uint, long>() }, |
| 53 | + } |
| 54 | + }, |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Add an entity to a specific container |
| 60 | + /// </summary> |
| 61 | + public void AddToContainer(UnitType unitType, ContainerType containerType, uint entityId) |
| 62 | + { |
| 63 | + this.containers[unitType][containerType][entityId] = Environment.TickCount64; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Remove an entity from a specific container |
| 68 | + /// </summary> |
| 69 | + public void RemoveFromContainer(UnitType unitType, ContainerType containerType, uint entityId) |
| 70 | + { |
| 71 | + this.containers[unitType][containerType].Remove(entityId); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Check if an entity exists in a specific container |
| 76 | + /// </summary> |
| 77 | + public bool IsInContainer(UnitType unitType, ContainerType containerType, uint entityId) |
| 78 | + { |
| 79 | + return this.containers[unitType][containerType].ContainsKey(entityId); |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Cleanup expired entries from all containers |
| 84 | + /// </summary> |
| 85 | + public void CleanupContainers() |
| 86 | + { |
| 87 | + foreach ((UnitType _, Dictionary<ContainerType, Dictionary<uint, long>>? unitContainer) in this.containers) |
| 88 | + { |
| 89 | + foreach ((ContainerType _, Dictionary<uint, long>? container) in unitContainer) |
| 90 | + { |
| 91 | + foreach ((uint id, long ticks) in container) |
| 92 | + if (ticks > Environment.TickCount64 + 5000) |
| 93 | + this.idsToDelete.Add(id); |
| 94 | + |
| 95 | + foreach (uint id in this.idsToDelete) container.Remove(id); |
| 96 | + |
| 97 | + this.idsToDelete.Clear(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Get all entities in a specific container |
| 104 | + /// </summary> |
| 105 | + public IEnumerable<KeyValuePair<uint, long>> GetContainerEntities(UnitType unitType, ContainerType containerType) |
| 106 | + { |
| 107 | + return this.containers[unitType][containerType].ToList(); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Clear a specific container |
| 112 | + /// </summary> |
| 113 | + public void ClearContainer(UnitType unitType, ContainerType containerType) |
| 114 | + { |
| 115 | + this.containers[unitType][containerType].Clear(); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Clear all containers |
| 120 | + /// </summary> |
| 121 | + public void ClearAllContainers() |
| 122 | + { |
| 123 | + foreach ((UnitType _, Dictionary<ContainerType, Dictionary<uint, long>>? unitContainer) in this.containers) |
| 124 | + foreach ((ContainerType _, Dictionary<uint, long>? container) in unitContainer) |
| 125 | + container.Clear(); |
| 126 | + } |
| 127 | +} |
0 commit comments