diff --git a/Libraries/SPTarkov.Server.Core/Callbacks/DialogueCallbacks.cs b/Libraries/SPTarkov.Server.Core/Callbacks/DialogueCallbacks.cs index 533db79d..f1269daa 100644 --- a/Libraries/SPTarkov.Server.Core/Callbacks/DialogueCallbacks.cs +++ b/Libraries/SPTarkov.Server.Core/Callbacks/DialogueCallbacks.cs @@ -17,10 +17,10 @@ public class DialogueCallbacks( ) : IOnUpdate { - public bool OnUpdate(long timeSinceLastRun) + public Task OnUpdate(long timeSinceLastRun) { _dialogueController.Update(); - return true; + return Task.CompletedTask; } /// diff --git a/Libraries/SPTarkov.Server.Core/Callbacks/HideoutCallbacks.cs b/Libraries/SPTarkov.Server.Core/Callbacks/HideoutCallbacks.cs index ab85b8de..995d49f9 100644 --- a/Libraries/SPTarkov.Server.Core/Callbacks/HideoutCallbacks.cs +++ b/Libraries/SPTarkov.Server.Core/Callbacks/HideoutCallbacks.cs @@ -17,15 +17,14 @@ public class HideoutCallbacks( { private readonly HideoutConfig _hideoutConfig = _configServer.GetConfig(); - public bool OnUpdate(long timeSinceLastRun) + public Task OnUpdate(long timeSinceLastRun) { if (timeSinceLastRun > _hideoutConfig.RunIntervalSeconds) { _hideoutController.Update(); - return true; } - return false; + return Task.CompletedTask; } /// diff --git a/Libraries/SPTarkov.Server.Core/Callbacks/InsuranceCallbacks.cs b/Libraries/SPTarkov.Server.Core/Callbacks/InsuranceCallbacks.cs index 54637522..b2e1fcb1 100644 --- a/Libraries/SPTarkov.Server.Core/Callbacks/InsuranceCallbacks.cs +++ b/Libraries/SPTarkov.Server.Core/Callbacks/InsuranceCallbacks.cs @@ -22,15 +22,14 @@ public class InsuranceCallbacks( { private readonly InsuranceConfig _insuranceConfig = _configServer.GetConfig(); - public bool OnUpdate(long timeSinceLastRun) + public Task OnUpdate(long timeSinceLastRun) { if (timeSinceLastRun > Math.Max(_insuranceConfig.RunIntervalSeconds, 1)) { _insuranceController.ProcessReturn(); - return true; } - return false; + return Task.CompletedTask; } /// diff --git a/Libraries/SPTarkov.Server.Core/Callbacks/RagfairCallbacks.cs b/Libraries/SPTarkov.Server.Core/Callbacks/RagfairCallbacks.cs index 4620afaf..5c9511f2 100644 --- a/Libraries/SPTarkov.Server.Core/Callbacks/RagfairCallbacks.cs +++ b/Libraries/SPTarkov.Server.Core/Callbacks/RagfairCallbacks.cs @@ -30,7 +30,7 @@ public class RagfairCallbacks( return Task.CompletedTask; } - public bool OnUpdate(long timeSinceLastRun) + public Task OnUpdate(long timeSinceLastRun) { if (timeSinceLastRun > _ragfairConfig.RunIntervalSeconds) { @@ -42,11 +42,9 @@ public class RagfairCallbacks( // Process all offers / expire offers _ragfairServer.Update(); - - return true; } - return false; + return Task.CompletedTask; } /// diff --git a/Libraries/SPTarkov.Server.Core/Callbacks/SaveCallbacks.cs b/Libraries/SPTarkov.Server.Core/Callbacks/SaveCallbacks.cs index c5024340..627a984b 100644 --- a/Libraries/SPTarkov.Server.Core/Callbacks/SaveCallbacks.cs +++ b/Libraries/SPTarkov.Server.Core/Callbacks/SaveCallbacks.cs @@ -22,14 +22,13 @@ public class SaveCallbacks( _saveServer.Load(); } - public bool OnUpdate(long timeSinceLastRun) + public Task OnUpdate(long timeSinceLastRun) { if (timeSinceLastRun > _coreConfig.ProfileSaveIntervalInSeconds) { _saveServer.Save(); - return true; } - return false; + return Task.CompletedTask; } } diff --git a/Libraries/SPTarkov.Server.Core/Callbacks/TraderCallbacks.cs b/Libraries/SPTarkov.Server.Core/Callbacks/TraderCallbacks.cs index 3e0df22d..bda3367b 100644 --- a/Libraries/SPTarkov.Server.Core/Callbacks/TraderCallbacks.cs +++ b/Libraries/SPTarkov.Server.Core/Callbacks/TraderCallbacks.cs @@ -23,9 +23,10 @@ public class TraderCallbacks( return Task.CompletedTask; } - public bool OnUpdate(long _) + public Task OnUpdate(long _) { - return _traderController.Update(); + _traderController.Update(); + return Task.CompletedTask; } /// diff --git a/Libraries/SPTarkov.Server.Core/DI/IOnUpdate.cs b/Libraries/SPTarkov.Server.Core/DI/IOnUpdate.cs index a02df08e..e6377db5 100644 --- a/Libraries/SPTarkov.Server.Core/DI/IOnUpdate.cs +++ b/Libraries/SPTarkov.Server.Core/DI/IOnUpdate.cs @@ -2,5 +2,5 @@ namespace SPTarkov.Server.Core.DI; public interface IOnUpdate { - bool OnUpdate(long timeSinceLastRun); + Task OnUpdate(long timeSinceLastRun); } diff --git a/Libraries/SPTarkov.Server.Core/Helpers/BotGeneratorHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/BotGeneratorHelper.cs index 587f1aa4..1cdf74e1 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/BotGeneratorHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/BotGeneratorHelper.cs @@ -15,7 +15,7 @@ using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; namespace SPTarkov.Server.Core.Helpers; -[Injectable(InjectionType.Singleton)] +[Injectable] public class BotGeneratorHelper( ISptLogger _logger, RandomUtil _randomUtil, @@ -26,7 +26,7 @@ public class BotGeneratorHelper( ProfileActivityService _profileActivityService, LocalisationService _localisationService, ConfigServer _configServer - ) : IOnLoad + ) { // Equipment slot ids that do not conflict with other slots private static readonly FrozenSet _slotsWithNoCompatIssues = [ @@ -37,16 +37,7 @@ public class BotGeneratorHelper( EquipmentSlots.ArmBand.ToString() ]; - private BotConfig _botConfig; - private string[] _pmcTypes; - - public Task OnLoad() - { - _botConfig = _configServer.GetConfig(); - var pmcConfig = _configServer.GetConfig(); - _pmcTypes = [pmcConfig.UsecType.ToLower(), pmcConfig.BearType.ToLower()]; - return Task.CompletedTask; - } + private readonly BotConfig _botConfig = _configServer.GetConfig(); /// /// Adds properties to an item @@ -529,7 +520,11 @@ public class BotGeneratorHelper( /// Equipment role (e.g. pmc / assault / bossTagilla) public string GetBotEquipmentRole(string botRole) { - return _pmcTypes.Contains(botRole, StringComparer.OrdinalIgnoreCase) + PmcConfig pmcConfig = _configServer.GetConfig(); + + string[] pmcTypes = [ pmcConfig.UsecType.ToLower(), pmcConfig.BearType.ToLower() ]; + + return pmcTypes.Contains(botRole, StringComparer.OrdinalIgnoreCase) ? Sides.PmcEquipmentRole : botRole; } diff --git a/Libraries/SPTarkov.Server.Core/Utils/App.cs b/Libraries/SPTarkov.Server.Core/Utils/App.cs index b7c22e3f..0f0da353 100644 --- a/Libraries/SPTarkov.Server.Core/Utils/App.cs +++ b/Libraries/SPTarkov.Server.Core/Utils/App.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.DependencyInjection; using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.DI; using SPTarkov.Server.Core.Helpers; @@ -20,6 +21,7 @@ public class App( EncodingUtil _encodingUtil, HttpServer _httpServer, DatabaseService _databaseService, + IHostApplicationLifetime _appLifeTime, IEnumerable _onLoadComponents, IEnumerable _onUpdateComponents, HttpServerHelper _httpServerHelper @@ -27,7 +29,6 @@ public class App( { protected CoreConfig _coreConfig = _configServer.GetConfig(); protected Dictionary _onUpdateLastRun = new(); - protected Timer _timer; public async Task InitializeAsync() { @@ -69,7 +70,8 @@ public class App( await onLoad.OnLoad(); } - _timer = new Timer(_ => Update(_onUpdateComponents), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(5000)); + // Discard here, as this task will run indefinitely + _ = Task.Run(Update); } public async Task StartAsync() @@ -95,17 +97,20 @@ public class App( return _localisationService.GetText("server_start_success"); } - protected void Update(IEnumerable onUpdateComponents) + protected async Task Update() { - try + while (!_appLifeTime.ApplicationStopping.IsCancellationRequested) { // If the server has failed to start, skip any update calls if (!_httpServer.IsStarted() || !_databaseService.IsDatabaseValid()) { - return; + await Task.Delay(5000, _appLifeTime.ApplicationStopping); + + // Skip forward to the next loop + continue; } - foreach (var updateable in onUpdateComponents) + foreach (var updateable in _onUpdateComponents) { var updateableName = updateable.GetType().FullName; if (string.IsNullOrEmpty(updateableName)) @@ -113,7 +118,6 @@ public class App( updateableName = $"{updateable.GetType().Namespace}.{updateable.GetType().Name}"; } - var success = false; if (!_onUpdateLastRun.TryGetValue(updateableName, out var lastRunTimeTimestamp)) { lastRunTimeTimestamp = 0; @@ -123,36 +127,18 @@ public class App( try { - success = updateable.OnUpdate(secondsSinceLastRun); + await updateable.OnUpdate(secondsSinceLastRun); } catch (Exception err) { LogUpdateException(err, updateable); } - if (success) - { - _onUpdateLastRun[updateableName] = _timeUtil.GetTimeStamp(); - } - else - { - /* temporary for debug */ - const int warnTime = 20 * 60; - - if (secondsSinceLastRun % warnTime == 0) - { - if (_logger.IsLogEnabled(LogLevel.Debug)) - { - _logger.Debug(_localisationService.GetText("route_onupdate_no_response", updateableName)); - } - } - } + // Set last run after try catch, so if an exception is caused the task is seen as failed. + _onUpdateLastRun[updateableName] = _timeUtil.GetTimeStamp(); } - } - catch (Exception e) - { - Console.WriteLine(e); - throw; + + await Task.Delay(5000, _appLifeTime.ApplicationStopping); } }