mirror of
https://github.com/Empostor/Empostor.git
synced 2026-10-19 15:51:39 +00:00
Fix playerlog
This commit is contained in:
1 parent
07f81c5d75
commit
43f8281486
4 files changed
+58
-2
No files matched your search
@@ -10,9 +10,10 @@ using Empostor.Api.Config;
|
||||
using Empostor.Api.Events.Managers;
|
||||
using Empostor.Api.Net;
|
||||
using Empostor.Api.Net.Manager;
|
||||
using Empostor.Api.Service;
|
||||
using Empostor.Server.Events.Client;
|
||||
using Empostor.Server.Net.Factories;
|
||||
using Empostor.Api.Service;
|
||||
using Empostor.Server.Service;
|
||||
using Empostor.Server.Service.Admin.Reactor;
|
||||
using Empostor.Server.Service.Auth;
|
||||
using Empostor.Server.Utils;
|
||||
@@ -36,6 +37,7 @@ namespace Empostor.Server.Net.Manager
|
||||
private readonly AuthApiConfig _authApiConfig;
|
||||
private readonly PortPoolService _portPool;
|
||||
private readonly IpGeolocationService _ipGeo;
|
||||
private readonly ClientIdStore _clientIdStore;
|
||||
private int _idLast;
|
||||
|
||||
public ClientManager(
|
||||
@@ -49,7 +51,8 @@ namespace Empostor.Server.Net.Manager
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IOptions<AuthApiConfig> authApiConfig,
|
||||
PortPoolService portPool,
|
||||
IpGeolocationService ipGeo)
|
||||
IpGeolocationService ipGeo,
|
||||
ClientIdStore clientIdStore)
|
||||
{
|
||||
_logger = logger;
|
||||
_eventManager = eventManager;
|
||||
@@ -63,6 +66,8 @@ namespace Empostor.Server.Net.Manager
|
||||
_authApiConfig = authApiConfig.Value;
|
||||
_portPool = portPool;
|
||||
_ipGeo = ipGeo;
|
||||
_clientIdStore = clientIdStore;
|
||||
_idLast = checked((int)_clientIdStore.GetLastId());
|
||||
|
||||
if (_compatibilityConfig.AllowFutureGameVersions)
|
||||
{
|
||||
@@ -91,6 +96,7 @@ namespace Empostor.Server.Net.Manager
|
||||
{
|
||||
var clientId = Interlocked.Increment(ref _idLast);
|
||||
if (clientId < 1) { _idLast = 0; clientId = Interlocked.Increment(ref _idLast); }
|
||||
_clientIdStore.SetLastId(clientId);
|
||||
return clientId;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ using Next.Hazel.Extensions;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using Serilog.Settings.Configuration;
|
||||
using Empostor.Server.Service;
|
||||
|
||||
namespace Empostor.Server
|
||||
{
|
||||
@@ -164,6 +165,7 @@ namespace Empostor.Server
|
||||
services.AddSingleton<IpGeolocationService>();
|
||||
services.AddSingleton<IModuleTagRegistry>(ModuleTagRegistry.Instance);
|
||||
services.AddSingleton<ICompatibilityManager, CompatibilityManager>();
|
||||
services.AddSingleton<ClientIdStore>();
|
||||
services.AddSingleton<ClientManager>();
|
||||
services.AddSingleton<IClientManager>(p => p.GetRequiredService<ClientManager>());
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
using Empostor.Api.Service;
|
||||
using Empostor.Server.Service.Stat;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Empostor.Server.Service;
|
||||
|
||||
/// <summary>
|
||||
/// Persists the last-assigned client ID so that client IDs are never
|
||||
/// reused across server restarts. Client IDs are used as the stable
|
||||
/// identity for persisted per-player data (e.g. the player log). Without
|
||||
/// persistence the ID counter resets to 1 on every restart, which makes
|
||||
/// old and new log entries that share the same ID belong to different
|
||||
/// players.
|
||||
/// </summary>
|
||||
public sealed class ClientIdStore : JsonDataStore<long>
|
||||
{
|
||||
private long _lastId;
|
||||
|
||||
public ClientIdStore(ILogger<ClientIdStore> logger, PlayerLogStore playerLogs)
|
||||
: base(logger)
|
||||
{
|
||||
Load();
|
||||
|
||||
_lastId = System.Math.Max(_lastId, playerLogs.GetMaxClientId());
|
||||
if (_lastId > 0)
|
||||
{
|
||||
SaveFireAndForget();
|
||||
}
|
||||
}
|
||||
|
||||
public long GetLastId() => _lastId;
|
||||
|
||||
public void SetLastId(long id)
|
||||
{
|
||||
_lastId = id;
|
||||
SaveFireAndForget();
|
||||
}
|
||||
|
||||
protected override long GetSnapshot() => _lastId;
|
||||
|
||||
protected override void ApplySnapshot(long data) => _lastId = data;
|
||||
}
|
||||
@@ -71,6 +71,12 @@ public sealed class PlayerLogStore : JsonDataStore<List<PlayerLogEntry>>
|
||||
public List<int> GetLoggedClientIds() =>
|
||||
_entries.Where(e => e.ClientId.HasValue).Select(e => e.ClientId!.Value).Distinct().ToList();
|
||||
|
||||
public int GetMaxClientId() =>
|
||||
_entries.Where(e => e.ClientId.HasValue)
|
||||
.Select(e => e.ClientId!.Value)
|
||||
.DefaultIfEmpty(0)
|
||||
.Max();
|
||||
|
||||
/// <summary>
|
||||
/// Returns the most recent non-empty player name recorded for a client.
|
||||
/// Used by the admin panel so disconnected players still show their
|
||||
|
||||
Reference in new issue
Block a user