From 67be4f8e7f7447108bbd00053f91e8efcf00db51 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Sun, 26 Dec 2021 17:14:06 -0600 Subject: [PATCH] reduce some potential errors --- Plugins/Stats/Helpers/StatManager.cs | 26 +++++++++++++++++++++++++- Plugins/Stats/Plugin.cs | 5 +---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Plugins/Stats/Helpers/StatManager.cs b/Plugins/Stats/Helpers/StatManager.cs index 01026c57e..33a6d2363 100644 --- a/Plugins/Stats/Helpers/StatManager.cs +++ b/Plugins/Stats/Helpers/StatManager.cs @@ -19,7 +19,10 @@ using Data.Models.Client; using Data.Models.Client.Stats; using Data.Models.Server; using Humanizer.Localisation; +using Microsoft.Data.Sqlite; using Microsoft.Extensions.Logging; +using MySql.Data.MySqlClient; +using Npgsql; using Stats.Client.Abstractions; using Stats.Config; using Stats.Helpers; @@ -432,6 +435,12 @@ namespace IW4MAdmin.Plugins.Stats.Helpers return null; } + if (pl.ClientId <= 0) + { + _log.LogWarning("Stats for {Client} are not yet initialized", pl.ToString()); + return null; + } + // get the client's stats from the database if it exists, otherwise create and attach a new one // if this fails we want to throw an exception @@ -511,6 +520,15 @@ namespace IW4MAdmin.Plugins.Stats.Helpers return clientStats; } + catch (DbUpdateException updateException) when ( + updateException.InnerException is PostgresException {SqlState: "23503"} + || updateException.InnerException is SqliteException {SqliteErrorCode: 787} + || updateException.InnerException is MySqlException {SqlState: "23503"}) + { + _log.LogWarning("Trying to add {Client} to stats before they have been added to the database", + pl.ToString()); + } + catch (Exception ex) { _log.LogError(ex, "Could not add client to stats {@client}", pl.ToString()); @@ -653,6 +671,12 @@ namespace IW4MAdmin.Plugins.Stats.Helpers var clientDetection = attacker.GetAdditionalProperty(CLIENT_DETECTIONS_KEY); var clientStats = attacker.GetAdditionalProperty(CLIENT_STATS_KEY); + if (clientDetection == null || clientStats?.ClientId == null) + { + _log.LogWarning("Client stats state for {Client} is not yet initialized", attacker.ToString()); + return; + } + waiter = clientStats.ProcessingHit; await waiter.WaitAsync(Utilities.DefaultCommandTimeout, Plugin.ServerManager.CancellationToken); @@ -1529,4 +1553,4 @@ namespace IW4MAdmin.Plugins.Stats.Helpers return serverId.Value; } } -} \ No newline at end of file +} diff --git a/Plugins/Stats/Plugin.cs b/Plugins/Stats/Plugin.cs index 7f4147271..fa226571d 100644 --- a/Plugins/Stats/Plugin.cs +++ b/Plugins/Stats/Plugin.cs @@ -1,5 +1,4 @@ -using IW4MAdmin.Plugins.Stats.Config; -using IW4MAdmin.Plugins.Stats.Helpers; +using IW4MAdmin.Plugins.Stats.Helpers; using Microsoft.EntityFrameworkCore; using SharedLibraryCore; using SharedLibraryCore.Dtos.Meta.Responses; @@ -15,9 +14,7 @@ using Data.Abstractions; using Data.Models.Client; using Data.Models.Client.Stats; using Data.Models.Server; -using Humanizer; using Microsoft.Extensions.Logging; -using SharedLibraryCore.Commands; using IW4MAdmin.Plugins.Stats.Client.Abstractions; using Stats.Client.Abstractions; using Stats.Config;