From 9506254d6c7c3bf6207e5f11c795577b3a567a9d Mon Sep 17 00:00:00 2001 From: TheDarkGlobe Date: Tue, 19 Nov 2024 20:45:57 +0100 Subject: [PATCH] WIP Space station raw endpoint mapping --- .../Controllers/ArrowHeadController.cs | 13 +++++++++ src/Helldivers-2-API/Program.cs | 1 + .../Mapping/MappingContext.cs | 6 ++++- .../Mapping/V1/SpaceStationMapper.cs | 2 +- .../Storage/ArrowHead/ArrowHeadStore.cs | 16 ++++++++++- src/Helldivers-2-Core/StorageFacade.cs | 13 ++++++--- .../ArrowHead/SpaceStation.cs | 25 +++++++++++++++++ .../ArrowHead/SpaceStations/Cost.cs | 21 +++++++++++++++ .../ArrowHead/SpaceStations/TacticalAction.cs | 27 +++++++++++++++++++ .../ArrowHeadSerializerContext.cs | 1 + .../Hosted/ArrowHeadSyncService.cs | 10 ++++++- .../Services/ArrowHeadApiService.cs | 13 +++++++++ 12 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 src/Helldivers-2-Models/ArrowHead/SpaceStation.cs create mode 100644 src/Helldivers-2-Models/ArrowHead/SpaceStations/Cost.cs create mode 100644 src/Helldivers-2-Models/ArrowHead/SpaceStations/TacticalAction.cs diff --git a/src/Helldivers-2-API/Controllers/ArrowHeadController.cs b/src/Helldivers-2-API/Controllers/ArrowHeadController.cs index 426fc93..5523a69 100644 --- a/src/Helldivers-2-API/Controllers/ArrowHeadController.cs +++ b/src/Helldivers-2-API/Controllers/ArrowHeadController.cs @@ -75,4 +75,17 @@ public static async Task Assignments(HttpContext context, ArrowHeadStor return Results.Bytes(assignments, contentType: "application/json"); } + + /// + /// Fetches THE specific (749875195). + /// + [ProducesResponseType>(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status503ServiceUnavailable)] + public static async Task SpaceStation(HttpContext context, ArrowHeadStore store) + { + // TODO extract ID from route + var spaceStation = await store.GetSpaceStations(749875195, context.RequestAborted); + + return Results.Bytes(spaceStation, contentType: "application/json"); + } } diff --git a/src/Helldivers-2-API/Program.cs b/src/Helldivers-2-API/Program.cs index 7527ead..fe51efc 100644 --- a/src/Helldivers-2-API/Program.cs +++ b/src/Helldivers-2-API/Program.cs @@ -249,6 +249,7 @@ raw.MapGet("/api/Stats/war/801/summary", ArrowHeadController.Summary); raw.MapGet("/api/NewsFeed/801", ArrowHeadController.NewsFeed); raw.MapGet("/api/v2/Assignment/War/801", ArrowHeadController.Assignments); +raw.MapGet("/api/v2/SpaceStation/War/801/749875195", ArrowHeadController.SpaceStation); #endregion diff --git a/src/Helldivers-2-Core/Mapping/MappingContext.cs b/src/Helldivers-2-Core/Mapping/MappingContext.cs index 32364c7..ba3f5c9 100644 --- a/src/Helldivers-2-Core/Mapping/MappingContext.cs +++ b/src/Helldivers-2-Core/Mapping/MappingContext.cs @@ -31,6 +31,9 @@ public sealed class MappingContext /// The s currently being mapped. public Dictionary> Assignments { get; private init; } + /// The s currently being mapped. + public Dictionary> SpaceStations { get; private init; } + /// /// A that represents the 'start' of the time in Helldivers 2. /// This accounts for the and . @@ -44,7 +47,7 @@ public sealed class MappingContext public TimeSpan GameTimeDeviation { get; private init; } /// Initializes a new . - internal MappingContext(WarId warId, WarInfo warInfo, Dictionary warStatuses, WarSummary warSummary, Dictionary> newsFeeds, Dictionary> assignments) + internal MappingContext(WarId warId, WarInfo warInfo, Dictionary warStatuses, WarSummary warSummary, Dictionary> newsFeeds, Dictionary> assignments, Dictionary> spaceStations) { WarId = warId; WarInfo = warInfo; @@ -52,6 +55,7 @@ internal MappingContext(WarId warId, WarInfo warInfo, Dictionary MapToV1(MappingContext context, List pl yield return Map(context, station, planets); } - private SpaceStation Map(MappingContext context, Helldivers.Models.ArrowHead.Status.SpaceStation raw, List planets) + private SpaceStation Map(MappingContext context, Helldivers.Models.ArrowHead.SpaceStation raw, List planets) { var planet = planets.First(p => p.Index == raw.PlanetIndex); diff --git a/src/Helldivers-2-Core/Storage/ArrowHead/ArrowHeadStore.cs b/src/Helldivers-2-Core/Storage/ArrowHead/ArrowHeadStore.cs index 3b8c610..5d6f788 100644 --- a/src/Helldivers-2-Core/Storage/ArrowHead/ArrowHeadStore.cs +++ b/src/Helldivers-2-Core/Storage/ArrowHead/ArrowHeadStore.cs @@ -14,6 +14,7 @@ public sealed class ArrowHeadStore private CultureDictionary> _statuses = null!; private CultureDictionary> _feeds = null!; private CultureDictionary> _assignments = null!; + private CultureDictionary> _spaceStations = null!; private readonly TaskCompletionSource _syncState = new(); /// @@ -25,7 +26,8 @@ public void UpdateRawStore( Memory warSummary, IEnumerable>> statuses, IEnumerable>> feeds, - IEnumerable>> assignments + IEnumerable>> assignments, + IEnumerable>> spaceStations ) { _warId = warId; @@ -34,6 +36,7 @@ IEnumerable>> assignments _statuses = new(statuses); _feeds = new(feeds); _assignments = new(assignments); + _spaceStations = new(spaceStations); _syncState.TrySetResult(); } @@ -97,4 +100,15 @@ public async Task> GetAssignments(CancellationToken cancellationTok return _assignments.Get(); } + + /// + /// returns the raw payload for s. + /// + public async Task> GetSpaceStations(int id, CancellationToken cancellationToken) + { + await _syncState.Task.WaitAsync(cancellationToken); + + // TODO use ID to get relevant space station + return _spaceStations.Get(); + } } diff --git a/src/Helldivers-2-Core/StorageFacade.cs b/src/Helldivers-2-Core/StorageFacade.cs index b417ee0..95f2e5c 100644 --- a/src/Helldivers-2-Core/StorageFacade.cs +++ b/src/Helldivers-2-Core/StorageFacade.cs @@ -25,7 +25,8 @@ public ValueTask UpdateStores(SteamNewsFeed feed) /// public async ValueTask UpdateStores(Memory rawWarId, Memory rawWarInfo, Dictionary> rawWarStatuses, Memory rawWarSummary, - Dictionary> rawNewsFeeds, Dictionary> rawAssignments) + Dictionary> rawNewsFeeds, Dictionary> rawAssignments, + Dictionary> rawStations) { arrowHead.UpdateRawStore( rawWarId, @@ -33,7 +34,8 @@ public async ValueTask UpdateStores(Memory rawWarId, Memory rawWarIn rawWarSummary, rawWarStatuses, rawNewsFeeds, - rawAssignments + rawAssignments, + rawStations ); var warId = DeserializeOrThrow(rawWarId, ArrowHeadSerializerContext.Default.WarId); @@ -51,6 +53,10 @@ public async ValueTask UpdateStores(Memory rawWarId, Memory rawWarIn pair => pair.Key, pair => DeserializeOrThrow(pair.Value, ArrowHeadSerializerContext.Default.ListAssignment) ); + var spaceStations = rawAssignments.ToDictionary( + pair => pair.Key, + pair => DeserializeOrThrow(pair.Value, ArrowHeadSerializerContext.Default.ListSpaceStation) + ); var context = new MappingContext( warId, @@ -58,7 +64,8 @@ public async ValueTask UpdateStores(Memory rawWarId, Memory rawWarIn warStatuses, warSummary, newsFeeds, - assignments + assignments, + spaceStations ); await v1.UpdateStores(context); diff --git a/src/Helldivers-2-Models/ArrowHead/SpaceStation.cs b/src/Helldivers-2-Models/ArrowHead/SpaceStation.cs new file mode 100644 index 0000000..66e7e1e --- /dev/null +++ b/src/Helldivers-2-Models/ArrowHead/SpaceStation.cs @@ -0,0 +1,25 @@ +using Helldivers.Models.ArrowHead.SpaceStations; + +namespace Helldivers.Models.ArrowHead; + +/// +/// Represents an assignment given from Super Earth to the Helldivers. +/// +/// The unique identifier of the station. +/// The id of the planet it's currently orbiting +/// The id of the previous planet election. +/// The id of the current planet election. +/// The id of the next planet election. +/// When the election for the next planet will end (in seconds relative to game start). +/// A set of flags, purpose currently unknown. +/// The list of actions the space station crew can perform. +public sealed record SpaceStation( + long Id32, + int PlanetIndex, + string LastElectionId, + string CurrentElectionId, + string NextElectionId, + ulong CurrentElectionEndWarTime, + int Flags, + List TacticalActions +); diff --git a/src/Helldivers-2-Models/ArrowHead/SpaceStations/Cost.cs b/src/Helldivers-2-Models/ArrowHead/SpaceStations/Cost.cs new file mode 100644 index 0000000..12da8e2 --- /dev/null +++ b/src/Helldivers-2-Models/ArrowHead/SpaceStations/Cost.cs @@ -0,0 +1,21 @@ +namespace Helldivers.Models.ArrowHead.SpaceStations; + +/// +/// Represents the "Cost" of a tactical action +/// +/// +/// +/// +/// +/// +/// +/// +public sealed record Cost( + string Id, + long ItemMixId, + int TargetValue, + int CurrentValue, + int DeltaPerSecond, + int MaxDonationAmmount, + int MaxDonationPeriodSeconds +); diff --git a/src/Helldivers-2-Models/ArrowHead/SpaceStations/TacticalAction.cs b/src/Helldivers-2-Models/ArrowHead/SpaceStations/TacticalAction.cs new file mode 100644 index 0000000..ee4b81e --- /dev/null +++ b/src/Helldivers-2-Models/ArrowHead/SpaceStations/TacticalAction.cs @@ -0,0 +1,27 @@ +namespace Helldivers.Models.ArrowHead.SpaceStations; + +/// +/// Represents information of a space station from the 'SpaceStation' endpoint returned by ArrowHead's API. +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +public sealed record TacticalAction( + long Id32, + long MediaId32, + string Name, + string Description, + string StrategicDescription, + int Status, + int StatusExpireAtWarTimeSeconds, + List Cost, + List EffectIds, + List ActiveEffectIds +); diff --git a/src/Helldivers-2-Models/ArrowHeadSerializerContext.cs b/src/Helldivers-2-Models/ArrowHeadSerializerContext.cs index 008c0e5..2649e6a 100644 --- a/src/Helldivers-2-Models/ArrowHeadSerializerContext.cs +++ b/src/Helldivers-2-Models/ArrowHeadSerializerContext.cs @@ -12,6 +12,7 @@ namespace Helldivers.Models; [JsonSerializable(typeof(NewsFeedItem))] [JsonSerializable(typeof(WarSummary))] [JsonSerializable(typeof(List))] +[JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] [JsonSourceGenerationOptions(PropertyNameCaseInsensitive = true)] public sealed partial class ArrowHeadSerializerContext : JsonSerializerContext diff --git a/src/Helldivers-2-Sync/Hosted/ArrowHeadSyncService.cs b/src/Helldivers-2-Sync/Hosted/ArrowHeadSyncService.cs index 4a93343..27651e4 100644 --- a/src/Helldivers-2-Sync/Hosted/ArrowHeadSyncService.cs +++ b/src/Helldivers-2-Sync/Hosted/ArrowHeadSyncService.cs @@ -89,6 +89,13 @@ private async Task SynchronizeAsync(IServiceProvider services, CancellationToken async language => await api.LoadAssignments(season, language, cancellationToken), cancellationToken ); + + // For each language, load space stations + var spaceStations = await DownloadTranslations( + // TODO extract station id's from war status + async language => await api.LoadSpaceStations(season, "749875195", language, cancellationToken), + cancellationToken + ); await storage.UpdateStores( rawWarId, @@ -96,7 +103,8 @@ await storage.UpdateStores( statuses, warSummary, feeds, - assignments + assignments, + spaceStations ); } diff --git a/src/Helldivers-2-Sync/Services/ArrowHeadApiService.cs b/src/Helldivers-2-Sync/Services/ArrowHeadApiService.cs index 34db6e1..a4a41e5 100644 --- a/src/Helldivers-2-Sync/Services/ArrowHeadApiService.cs +++ b/src/Helldivers-2-Sync/Services/ArrowHeadApiService.cs @@ -80,6 +80,19 @@ public async Task> LoadAssignments(string season, string language, return await CollectStream(stream, cancellationToken); } + /// + /// Loads space station of a given and in . + /// + public async Task> LoadSpaceStations(string season, string id, string language, + CancellationToken cancellationToken) + { + var request = BuildRequest($"/api/SpaceStation/{season}/{id}", language); + using var response = await http.SendAsync(request, cancellationToken); + await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); + + return await CollectStream(stream, cancellationToken); + } + /// /// Fetch from ArrowHead's API. ///