-
Notifications
You must be signed in to change notification settings - Fork 391
Expand file tree
/
Copy pathInstanceProcessor.cs
More file actions
93 lines (77 loc) · 2.82 KB
/
Copy pathInstanceProcessor.cs
File metadata and controls
93 lines (77 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using Intersect.Core;
using Intersect.Server.Core.MapInstancing.Controllers;
using Intersect.Server.Entities;
using Intersect.Server.Maps;
using Microsoft.Extensions.Logging;
namespace Intersect.Server.Core.MapInstancing;
public static class InstanceProcessor
{
private static readonly Dictionary<Guid, InstanceController> InstanceControllers = new();
private static readonly HashSet<Guid> ActiveIdScratch = [];
public static bool TryGetInstanceController(Guid instanceId, out InstanceController controller) => InstanceControllers.TryGetValue(instanceId, out controller);
private static void CleanupOrphanedControllers(MapInstance[] activeMaps)
{
ActiveIdScratch.Clear();
for (var i = 0; i < activeMaps.Length; i++)
{
ActiveIdScratch.Add(activeMaps[i].MapInstanceId);
}
List<Guid>? toRemove = null;
foreach (var id in InstanceControllers.Keys)
{
if (id == default)
{
continue; // never clean overworld
}
if (!ActiveIdScratch.Contains(id))
{
(toRemove ??= new List<Guid>()).Add(id);
}
}
if (toRemove != null)
{
foreach (var id in toRemove)
{
InstanceControllers.Remove(id);
ApplicationContext.Context.Value?.Logger.LogDebug($"Removing instance controller {id}");
}
}
}
public static bool TryAddInstanceController(Guid mapInstanceId, Player creator)
{
if (InstanceControllers.ContainsKey(mapInstanceId))
{
return false;
}
InstanceControllers[mapInstanceId] = new InstanceController(mapInstanceId, creator);
return true;
}
public static void UpdateInstanceControllers(MapInstance[] activeMaps)
{
if (activeMaps == null || activeMaps.Length == 0)
{
return;
}
CleanupOrphanedControllers(activeMaps);
// Manual grouping + ToDictionary allocations
var mapsAndInstances = new Dictionary<Guid, List<MapInstance>>();
for (var i = 0; i < activeMaps.Length; i++)
{
var map = activeMaps[i];
if (!mapsAndInstances.TryGetValue(map.MapInstanceId, out var list))
{
mapsAndInstances[map.MapInstanceId] = list = new List<MapInstance>();
}
list.Add(map);
}
foreach (var (instanceId, mapsInInstance) in mapsAndInstances)
{
// Fetch our instance controller...
if (!InstanceControllers.TryGetValue(instanceId, out var instanceController))
{
continue;
}
// TODO do update-y things in here, i.e processing permadead NPCs. Keeping empty for initial code review
}
}
}