From c1b509e3a84d8a79f5c1c7db8ca8d8aafa7dc27f Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 12 Jan 2025 17:24:26 +0000 Subject: [PATCH] Expanded `LocationControler` implementation --- Core/Controllers/LocationController.cs | 55 ++++++++++++++++++- .../Models/Eft/Common/Tables/LocationsBase.cs | 4 +- .../Tables/LocationsGenerateAllResponse.cs | 6 +- Core/Models/Spt/Server/Locations.cs | 24 +++++++- 4 files changed, 80 insertions(+), 9 deletions(-) diff --git a/Core/Controllers/LocationController.cs b/Core/Controllers/LocationController.cs index 4daa285e..ee48620d 100644 --- a/Core/Controllers/LocationController.cs +++ b/Core/Controllers/LocationController.cs @@ -1,12 +1,33 @@ using Core.Annotations; +using Core.Models.Eft.Common; using Core.Models.Eft.Common.Tables; using Core.Models.Eft.Location; +using Core.Services; +using Core.Utils.Cloners; +using ILogger = Core.Models.Utils.ILogger; namespace Core.Controllers; [Injectable] public class LocationController { + private readonly ILogger _logger; + private readonly DatabaseService _databaseService; + private readonly AirdropService _airdropService; + private readonly JsonCloner _cloner; + + public LocationController( + ILogger logger, + DatabaseService databaseService, + AirdropService airdropService, + JsonCloner cloner) + { + _logger = logger; + _databaseService = databaseService; + _airdropService = airdropService; + _cloner = cloner; + } + /// /// Handle client/locations /// Get all maps base location properties without loot data @@ -15,7 +36,32 @@ public class LocationController /// LocationsGenerateAllResponse public LocationsGenerateAllResponse GenerateAll(string sessionId) { - throw new NotImplementedException(); + var locationsFromDb = _databaseService.GetLocations(); + var maps = locationsFromDb.GetDictionary(); + + // keyed by _id location property + var locationResult = new Dictionary(); + + foreach (var location in maps) + { + var mapBase = location.Value?.Base; + if (mapBase == null) + { + _logger.Debug($"Map: {location} has no base json file, skipping generation"); + continue; + } + + // Clear out loot array + mapBase.Loot = []; + // Add map base data to dictionary + locationResult.Add(mapBase.IdField, mapBase); + } + + return new LocationsGenerateAllResponse + { + Locations = locationResult, + Paths = locationsFromDb.Base.Paths + }; } /// @@ -25,6 +71,11 @@ public class LocationController /// public GetAirdropLootResponse GetAirDropLoot(GetAirdropLootRequest request) { - throw new NotImplementedException(); + if (request.ContainerId is not null) + { + return this._airdropService.GenerateCustomAirdropLoot(request); + } + + return this._airdropService.GenerateAirdropLoot(); } } diff --git a/Core/Models/Eft/Common/Tables/LocationsBase.cs b/Core/Models/Eft/Common/Tables/LocationsBase.cs index 338d8468..ab2d3c4d 100644 --- a/Core/Models/Eft/Common/Tables/LocationsBase.cs +++ b/Core/Models/Eft/Common/Tables/LocationsBase.cs @@ -1,4 +1,4 @@ -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace Core.Models.Eft.Common.Tables; @@ -25,4 +25,4 @@ public class Path public string? Destination { get; set; } public bool? Event { get; set; } -} \ No newline at end of file +} diff --git a/Core/Models/Eft/Common/Tables/LocationsGenerateAllResponse.cs b/Core/Models/Eft/Common/Tables/LocationsGenerateAllResponse.cs index 2bb0ccc3..4a6c36f1 100644 --- a/Core/Models/Eft/Common/Tables/LocationsGenerateAllResponse.cs +++ b/Core/Models/Eft/Common/Tables/LocationsGenerateAllResponse.cs @@ -1,12 +1,12 @@ -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace Core.Models.Eft.Common.Tables; public class LocationsGenerateAllResponse { [JsonPropertyName("locations")] - public Locations? Locations { get; set; } + public Dictionary Locations { get; set; } [JsonPropertyName("paths")] public List? Paths { get; set; } -} \ No newline at end of file +} diff --git a/Core/Models/Spt/Server/Locations.cs b/Core/Models/Spt/Server/Locations.cs index b3040be8..1e67a739 100644 --- a/Core/Models/Spt/Server/Locations.cs +++ b/Core/Models/Spt/Server/Locations.cs @@ -1,4 +1,5 @@ -using System.Text.Json.Serialization; +using System.Reflection; +using System.Text.Json.Serialization; using Core.Models.Eft.Common.Tables; using Core.Models.Eft.Common; @@ -81,4 +82,23 @@ public class Locations .Invoke(this, [value]); } } -} \ No newline at end of file + + private Dictionary? _locationDictionaryCache; + + /// + /// Get map locations as a dictionary, keyed by its name e.g. factory4_day + /// + /// + public Dictionary GetDictionary() + { + if (_locationDictionaryCache is null) + { + var classProps = GetType() + .GetProperties(); + _locationDictionaryCache = classProps + .ToDictionary(propertyInfo => propertyInfo.Name, propertyInfo => propertyInfo.GetValue(this, null) as Eft.Common.Location); + } + + return _locationDictionaryCache; + } +}