Cleaned up PmcWaveGenerator

Improved null handling inside `DatabaseService`
This commit is contained in:
Chomp
2025-06-15 10:56:36 +01:00
parent 07f57e16a8
commit 1da156ef4f
2 changed files with 19 additions and 36 deletions
@@ -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<PmcWaveGenerator> logger,
DatabaseService databaseService,
ConfigServer configServer)
{
protected ConfigServer _configServer;
protected DatabaseService _databaseService;
protected ISptLogger<PmcWaveGenerator> _logger;
protected PmcConfig _pmcConfig;
protected RandomUtil _randomUtil;
public PmcWaveGenerator(
ISptLogger<PmcWaveGenerator> _logger,
RandomUtil _randomUtil,
DatabaseService _databaseService,
ConfigServer _configServer
)
{
this._logger = _logger;
this._randomUtil = _randomUtil;
this._databaseService = _databaseService;
this._configServer = _configServer;
_pmcConfig = _configServer.GetConfig<PmcConfig>();
}
protected readonly PmcConfig _pmcConfig = configServer.GetConfig<PmcConfig>();
/// <summary>
/// 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);
}
/// <summary>
@@ -26,7 +26,7 @@ public class DatabaseService(
HashUtil _hashUtil
)
{
protected bool isDataValid = true;
private bool _isDataValid = true;
/// <returns> assets/database/ </returns>
public DatabaseTables GetTables()
@@ -105,13 +105,15 @@ public class DatabaseService(
/// </summary>
/// <param name="locationId"> Desired location ID </param>
/// <returns> assets/database/locations/ </returns>
public Location GetLocation(string locationId)
public Location? GetLocation(string locationId)
{
var locations = GetLocations();
var desiredLocation = locations.GetByJsonProp<Location>(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(
/// </summary>
/// <param name="traderId"> Desired trader ID </param>
/// <returns> assets/database/traders/ </returns>
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(
/// <returns> True if the database contains valid data, false otherwise </returns>
public bool IsDatabaseValid()
{
return isDataValid;
return _isDataValid;
}
}