Use dictionary and hashset for admin log entities and players respectively

Add test for duplicate player ids in an admin log not throwing
This commit is contained in:
DrSmugleaf
2021-12-26 00:50:10 +01:00
parent 82d9c38c96
commit 0ca8c705ab
5 changed files with 65 additions and 25 deletions

View File

@@ -336,4 +336,49 @@ public class AddTests : ContentIntegrationTest
Assert.Pass(); Assert.Pass();
} }
[Test]
public async Task DuplicatePlayerIdDoesNotThrowTest()
{
var (client, server) = await StartConnectedServerClientPair(serverOptions: new ServerContentIntegrationOption
{
CVarOverrides =
{
[CCVars.AdminLogsQueueSendDelay.Name] = "0"
},
});
await Task.WhenAll(client.WaitIdleAsync(), server.WaitIdleAsync());
var sPlayers = server.ResolveDependency<IPlayerManager>();
var sSystems = server.ResolveDependency<IEntitySystemManager>();
var sAdminLogSystem = sSystems.GetEntitySystem<AdminLogSystem>();
var guid = Guid.NewGuid();
await server.WaitPost(() =>
{
var player = sPlayers.ServerSessions.Single();
sAdminLogSystem.Add(LogType.Unknown, $"{player:first} {player:second} test log: {guid}");
});
await WaitUntil(server, async () =>
{
var logs = await sAdminLogSystem.CurrentRoundLogs(new LogFilter
{
Search = guid.ToString()
});
if (logs.Count == 0)
{
return false;
}
return true;
});
Assert.Pass();
}
} }

View File

@@ -35,11 +35,11 @@ public partial class AdminLogSystem
_sawmill.Debug($"Admin log converters found: {string.Join(" ", converterNames)}"); _sawmill.Debug($"Admin log converters found: {string.Join(" ", converterNames)}");
} }
private (JsonDocument json, List<Guid> players, List<(int id, string? name)> entities) ToJson( private (JsonDocument json, HashSet<Guid> players, Dictionary<int, string?> entities) ToJson(
Dictionary<string, object?> properties) Dictionary<string, object?> properties)
{ {
var entities = new List<(int id, string? name)>(); var entities = new Dictionary<int, string?>();
var players = new List<Guid>(); var players = new HashSet<Guid>();
var parsed = new Dictionary<string, object?>(); var parsed = new Dictionary<string, object?>();
foreach (var key in properties.Keys) foreach (var key in properties.Keys)
@@ -54,7 +54,7 @@ public partial class AdminLogSystem
var parsedKey = NamingPolicy.ConvertName(key); var parsedKey = NamingPolicy.ConvertName(key);
parsed.Add(parsedKey, value); parsed.Add(parsedKey, value);
EntityUid? entityId = properties[key] switch var entityId = properties[key] switch
{ {
EntityUid id => id, EntityUid id => id,
EntityStringRepresentation rep => rep.Uid, EntityStringRepresentation rep => rep.Uid,
@@ -72,9 +72,7 @@ public partial class AdminLogSystem
? metadata.EntityName ? metadata.EntityName
: null; : null;
if (entities.Any(e => e.id == (int) uid)) continue; entities.TryAdd((int) uid, entityName);
entities.Add(((int) uid, entityName));
if (_entityManager.TryGetComponent(uid, out ActorComponent? actor)) if (_entityManager.TryGetComponent(uid, out ActorComponent? actor))
{ {

View File

@@ -243,7 +243,7 @@ public partial class AdminLogSystem : SharedAdminLogSystem
} }
} }
private async void Add(LogType type, LogImpact impact, string message, JsonDocument json, List<Guid> players, List<(int id, string? name)> entities) private async void Add(LogType type, LogImpact impact, string message, JsonDocument json, HashSet<Guid> players, Dictionary<int, string?> entities)
{ {
var logId = NextLogId; var logId = NextLogId;
var date = DateTime.UtcNow; var date = DateTime.UtcNow;

View File

@@ -1,12 +1,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using Content.Server.Database; using Content.Server.Database;
using JetBrains.Annotations;
namespace Content.Server.Administration.Logs; namespace Content.Server.Administration.Logs;
public readonly struct QueuedLog public readonly struct QueuedLog
{ {
public QueuedLog(AdminLog log, List<(int id, string? name)> entities) public QueuedLog(AdminLog log, Dictionary<int, string?> entities)
{ {
Log = log; Log = log;
Entities = entities; Entities = entities;
@@ -14,9 +13,9 @@ public readonly struct QueuedLog
public AdminLog Log { get; } public AdminLog Log { get; }
public List<(int id, string? name)> Entities { get; } public Dictionary<int, string?> Entities { get; }
public void Deconstruct(out AdminLog log, out List<(int id, string? name)> entities) public void Deconstruct(out AdminLog log, out Dictionary<int, string?> entities)
{ {
log = Log; log = Log;
entities = Entities; entities = Entities;

View File

@@ -40,22 +40,20 @@ public ref struct LogStringHandler
format = argument[0] == '@' ? argument[1..] : argument; format = argument[0] == '@' ? argument[1..] : argument;
} }
if (!Values.TryAdd(format, value)) if (Values.TryAdd(format, value) ||
Values[format] == (object?) value)
{ {
if (Values[format] == (object?) value) return;
{ }
return;
}
var originalFormat = format; var originalFormat = format;
var i = 2; var i = 2;
format = $"{originalFormat}_{i}";
while (!Values.TryAdd(format, value))
{
format = $"{originalFormat}_{i}"; format = $"{originalFormat}_{i}";
i++;
while (!Values.TryAdd(format, value))
{
format = $"{originalFormat}_{i}";
i++;
}
} }
} }