Skip to content

Commit

Permalink
- added onlinePlayerCache in PlayerService for slightly faster proces…
Browse files Browse the repository at this point in the history
…sing in EclipseService
  • Loading branch information
mfoltz committed Aug 30, 2024
1 parent 73791f1 commit 016ab26
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 1 addition & 3 deletions Services/EclipseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,11 @@ static IEnumerator ClientUpdateLoop()
continue;
}

Dictionary<string, PlayerInfo> players = new(PlayerCache); // Shallow copy of the player cache to make sure updates to that don't interfere with loop
Dictionary<string, PlayerInfo> players = new(OnlinePlayerCache); // Shallow copy of the player cache to make sure updates to that don't interfere with loop
HashSet<ulong> users = new(RegisteredUsers);

foreach (PlayerInfo playerInfo in players.Values)
{
if (!playerInfo.User.IsConnected) continue;

if (users.Contains(playerInfo.User.PlatformId))
{
try
Expand Down
13 changes: 12 additions & 1 deletion Services/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public struct PlayerInfo(Entity userEntity = default, Entity charEntity = defaul
static EntityQuery UserQuery;

public static readonly Dictionary<string, PlayerInfo> PlayerCache = [];

public static readonly Dictionary<string, PlayerInfo> OnlinePlayerCache = [];
public PlayerService()
{
UserQuery = EntityManager.CreateEntityQuery(UserComponent);
Expand All @@ -36,6 +38,7 @@ static IEnumerator PlayerUpdateLoop()
while (true)
{
PlayerCache.Clear();
OnlinePlayerCache.Clear();

var players = GetEntitiesEnumerable(UserQuery);
players
Expand All @@ -57,7 +60,15 @@ static IEnumerator PlayerUpdateLoop()
.SelectMany(entry => new[] { entry.PlayerNameEntry, entry.SteamIdEntry })
.GroupBy(entry => entry.Key)
.ToDictionary(group => group.Key, group => group.First().Value)
.ForEach(kvp => PlayerCache[kvp.Key] = kvp.Value);
.ForEach(kvp =>
{
PlayerCache[kvp.Key] = kvp.Value; // Add to PlayerCache
if (kvp.Value.User.IsConnected) // Add to OnlinePlayerCache if connected
{
OnlinePlayerCache[kvp.Key] = kvp.Value;
}
});

yield return Delay;
}
Expand Down

0 comments on commit 016ab26

Please sign in to comment.