From 1da156ef4fd1dd3f3e3742c16afaf517eacafc42 Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 15 Jun 2025 10:56:36 +0100 Subject: [PATCH] Cleaned up `PmcWaveGenerator` Improved null handling inside `DatabaseService` --- .../Generators/PmcWaveGenerator.cs | 35 ++++--------------- .../Services/DatabaseService.cs | 20 ++++++----- 2 files changed, 19 insertions(+), 36 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Generators/PmcWaveGenerator.cs b/Libraries/SPTarkov.Server.Core/Generators/PmcWaveGenerator.cs index 655cdf7f..68b334ad 100644 --- a/Libraries/SPTarkov.Server.Core/Generators/PmcWaveGenerator.cs +++ b/Libraries/SPTarkov.Server.Core/Generators/PmcWaveGenerator.cs @@ -4,32 +4,16 @@ using SPTarkov.Server.Core.Models.Spt.Config; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Servers; using SPTarkov.Server.Core.Services; -using SPTarkov.Server.Core.Utils; namespace SPTarkov.Server.Core.Generators; [Injectable] -public class PmcWaveGenerator +public class PmcWaveGenerator( + ISptLogger logger, + DatabaseService databaseService, + ConfigServer configServer) { - protected ConfigServer _configServer; - protected DatabaseService _databaseService; - protected ISptLogger _logger; - protected PmcConfig _pmcConfig; - protected RandomUtil _randomUtil; - - public PmcWaveGenerator( - ISptLogger _logger, - RandomUtil _randomUtil, - DatabaseService _databaseService, - ConfigServer _configServer - ) - { - this._logger = _logger; - this._randomUtil = _randomUtil; - this._databaseService = _databaseService; - this._configServer = _configServer; - _pmcConfig = _configServer.GetConfig(); - } + protected readonly PmcConfig _pmcConfig = configServer.GetConfig(); /// /// Add a pmc wave to a map @@ -63,13 +47,8 @@ public class PmcWaveGenerator return; } - var location = _databaseService.GetLocation(name); - if (location is null) - { - return; - } - - location.Base.BossLocationSpawn.AddRange(pmcWavesToAdd); + var location = databaseService.GetLocation(name); + location?.Base.BossLocationSpawn.AddRange(pmcWavesToAdd); } /// diff --git a/Libraries/SPTarkov.Server.Core/Services/DatabaseService.cs b/Libraries/SPTarkov.Server.Core/Services/DatabaseService.cs index 2614a739..d2303c7e 100644 --- a/Libraries/SPTarkov.Server.Core/Services/DatabaseService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/DatabaseService.cs @@ -26,7 +26,7 @@ public class DatabaseService( HashUtil _hashUtil ) { - protected bool isDataValid = true; + private bool _isDataValid = true; /// assets/database/ public DatabaseTables GetTables() @@ -105,13 +105,15 @@ public class DatabaseService( /// /// Desired location ID /// assets/database/locations/ - public Location GetLocation(string locationId) + public Location? GetLocation(string locationId) { var locations = GetLocations(); var desiredLocation = locations.GetByJsonProp(locationId.ToLower()); if (desiredLocation == null) { - throw new Exception(_localisationService.GetText("database-no_location_found_with_id", locationId)); + _logger.Error(_localisationService.GetText("database-no_location_found_with_id", locationId)); + + return null; } return desiredLocation; @@ -297,12 +299,14 @@ public class DatabaseService( /// /// Desired trader ID /// assets/database/traders/ - public Trader GetTrader(string traderId) + public Trader? GetTrader(string traderId) { var traders = GetTraders(); if (!traders.TryGetValue(traderId, out var desiredTrader)) { - throw new Exception(_localisationService.GetText("database-no_trader_found_with_id", traderId)); + _logger.Error(_localisationService.GetText("database-no_trader_found_with_id", traderId)); + + return null; } return desiredTrader; @@ -326,13 +330,13 @@ public class DatabaseService( { var start = Stopwatch.StartNew(); - isDataValid = + _isDataValid = ValidateTable(GetQuests(), "quest") && ValidateTable(GetTraders(), "trader") && ValidateTable(GetItems(), "item") && ValidateTable(GetCustomization(), "customization"); - if (!isDataValid) + if (!_isDataValid) { _logger.Error(_localisationService.GetText("database-invalid_data")); } @@ -370,6 +374,6 @@ public class DatabaseService( /// True if the database contains valid data, false otherwise public bool IsDatabaseValid() { - return isDataValid; + return _isDataValid; } }