From 672621735495c1c234993b09329ed66d3929c660 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Sat, 24 Aug 2019 10:02:53 -0500 Subject: [PATCH] Make stats update after 10 kills so we don't wait quite as long Gracefully disconnect clients on shutting down again --- Application/ApplicationManager.cs | 11 ++++++-- Application/IW4MServer.cs | 42 ++++++++++++++++++++++------ Plugins/AutomessageFeed/Plugin.cs | 2 +- Plugins/Stats/Helpers/StatManager.cs | 15 ++++++++-- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/Application/ApplicationManager.cs b/Application/ApplicationManager.cs index fad4c3f14..d37d6e1c3 100644 --- a/Application/ApplicationManager.cs +++ b/Application/ApplicationManager.cs @@ -220,8 +220,15 @@ namespace IW4MAdmin.Application { await Task.Delay(ConfigHandler.Configuration().RConPollRate, _tokenSource.Token); } - // if a cancellation is received, we want to return immediately - catch { break; } + // if a cancellation is received, we want to return immediately after shutting down + catch + { + foreach (var server in Servers.Where(s => serverIds.Contains(s.EndPoint))) + { + await server.ProcessUpdatesAsync(_tokenSource.Token); + } + break; + } } } diff --git a/Application/IW4MServer.cs b/Application/IW4MServer.cs index 7d5e5937a..2d5389525 100644 --- a/Application/IW4MServer.cs +++ b/Application/IW4MServer.cs @@ -572,6 +572,30 @@ namespace IW4MAdmin }; } + private async Task ShutdownInternal() + { + foreach (var client in GetClientsAsList()) + { + await client.OnDisconnect(); + + var e = new GameEvent() + { + Type = GameEvent.EventType.Disconnect, + Owner = this, + Origin = client + }; + + Manager.GetEventHandler().AddEvent(e); + + await e.WaitAsync(Utilities.DefaultCommandTimeout, new CancellationTokenRegistration().Token); + } + + foreach (var plugin in SharedLibraryCore.Plugins.PluginImporter.ActivePlugins) + { + await plugin.OnUnloadAsync(); + } + } + DateTime start = DateTime.Now; DateTime playerCountStart = DateTime.Now; DateTime lastCount = DateTime.Now; @@ -579,18 +603,12 @@ namespace IW4MAdmin override public async Task ProcessUpdatesAsync(CancellationToken cts) { try - { - #region SHUTDOWN - if (Manager.CancellationToken.IsCancellationRequested) + { + if (cts.IsCancellationRequested) { - foreach (var plugin in SharedLibraryCore.Plugins.PluginImporter.ActivePlugins) - { - await plugin.OnUnloadAsync(); - } - + await ShutdownInternal(); return true; } - #endregion try { @@ -738,6 +756,12 @@ namespace IW4MAdmin return true; } + catch (TaskCanceledException) + { + await ShutdownInternal(); + return true; + } + // this one is ok catch (ServerException e) { diff --git a/Plugins/AutomessageFeed/Plugin.cs b/Plugins/AutomessageFeed/Plugin.cs index 56abd3d22..b738ad39f 100644 --- a/Plugins/AutomessageFeed/Plugin.cs +++ b/Plugins/AutomessageFeed/Plugin.cs @@ -79,7 +79,7 @@ namespace AutomessageFeed public Task OnUnloadAsync() { - throw new NotImplementedException(); + return Task.CompletedTask; } } } diff --git a/Plugins/Stats/Helpers/StatManager.cs b/Plugins/Stats/Helpers/StatManager.cs index e367a89eb..bba171c1b 100644 --- a/Plugins/Stats/Helpers/StatManager.cs +++ b/Plugins/Stats/Helpers/StatManager.cs @@ -436,15 +436,19 @@ namespace IW4MAdmin.Plugins.Stats.Helpers // sync their stats before they leave clientStats = UpdateStats(clientStats); + await SaveClientStats(clientStats); + // increment the total play time + serverStats.TotalPlayTime += pl.ConnectionLength; + } + + private static async Task SaveClientStats(EFClientStatistics clientStats) + { using (var ctx = new DatabaseContext(disableTracking: true)) { ctx.Update(clientStats); await ctx.SaveChangesAsync(); } - - // increment the total play time - serverStats.TotalPlayTime += pl.ConnectionLength; } public void AddDamageEvent(string eventLine, int attackerClientId, int victimClientId, long serverId) @@ -551,6 +555,11 @@ namespace IW4MAdmin.Plugins.Stats.Helpers clientStats.HitLocations.Single(hl => hl.Location == hit.HitLoc).HitCount += 1; } + if (clientStats.SessionKills % Detection.MAX_TRACKED_HIT_COUNT == 0) + { + await SaveClientStats(clientStats); + } + try { if (Plugin.Config.Configuration().StoreClientKills)