Skip to content

Commit 1242b2b

Browse files
committed
clean up WIP implementation of space stations
see #118 see #120
1 parent aaf2f03 commit 1242b2b

File tree

20 files changed

+113
-52
lines changed

20 files changed

+113
-52
lines changed

src/Helldivers-2-API/Controllers/ArrowHeadController.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ public static async Task<IResult> Assignments(HttpContext context, ArrowHeadStor
8181
/// </summary>
8282
[ProducesResponseType<List<Assignment>>(StatusCodes.Status200OK)]
8383
[ProducesResponseType(StatusCodes.Status503ServiceUnavailable)]
84-
public static async Task<IResult> SpaceStation(HttpContext context, ArrowHeadStore store)
84+
public static async Task<IResult> SpaceStation(HttpContext context, ArrowHeadStore store, [FromRoute] long index)
8585
{
86-
// TODO extract ID from route
87-
var spaceStation = await store.GetSpaceStations(749875195, context.RequestAborted);
86+
var spaceStation = await store.GetSpaceStation(index, context.RequestAborted);
87+
if (spaceStation is { } bytes)
88+
return Results.Bytes(bytes, contentType: "application/json");
8889

89-
return Results.Bytes(spaceStation, contentType: "application/json");
90+
return Results.NotFound();
9091
}
9192
}

src/Helldivers-2-API/Controllers/V1/SpaceStationController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ public static class SpaceStationController
1313
/// Fetches a list of all available <see cref="SpaceStation" /> information available.
1414
/// </summary>
1515
[ProducesResponseType<List<SpaceStation>>(StatusCodes.Status200OK)]
16-
public static async Task<IResult> Index(HttpContext context, IStore<SpaceStation, int> store)
16+
public static async Task<IResult> Index(HttpContext context, IStore<SpaceStation, long> store)
1717
{
18+
// TODO: check implementation.
1819
var stations = await store.AllAsync(context.RequestAborted);
1920

2021
return Results.Ok(stations);
@@ -24,7 +25,7 @@ public static async Task<IResult> Index(HttpContext context, IStore<SpaceStation
2425
/// Fetches a specific <see cref="SpaceStation" /> identified by <paramref name="index" />.
2526
/// </summary>
2627
[ProducesResponseType<SpaceStation>(StatusCodes.Status200OK)]
27-
public static async Task<IResult> Show(HttpContext context, IStore<SpaceStation, int> store, [FromRoute] int index)
28+
public static async Task<IResult> Show(HttpContext context, IStore<SpaceStation, long> store, [FromRoute] int index)
2829
{
2930
var station = await store.GetAsync(index, context.RequestAborted);
3031

src/Helldivers-2-API/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@
255255
raw.MapGet("/api/Stats/war/801/summary", ArrowHeadController.Summary);
256256
raw.MapGet("/api/NewsFeed/801", ArrowHeadController.NewsFeed);
257257
raw.MapGet("/api/v2/Assignment/War/801", ArrowHeadController.Assignments);
258-
raw.MapGet("/api/v2/SpaceStation/War/801/749875195", ArrowHeadController.SpaceStation);
258+
raw.MapGet("/api/v2/SpaceStation/War/801/{index:long}", ArrowHeadController.SpaceStation);
259259

260260
#endregion
261261

src/Helldivers-2-API/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"Synchronization": {
2525
"IntervalSeconds": 20,
2626
"DefaultLanguage": "en-US",
27+
"SpaceStations": [
28+
749875195
29+
],
2730
"Languages": [
2831
"en-US",
2932
"de-DE",

src/Helldivers-2-Core/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static IServiceCollection AddV1Stores(this IServiceCollection services)
7474
services.AddSingleton<IStore<Campaign, int>, CampaignStore>();
7575
services.AddSingleton<IStore<Models.V1.Assignment, long>, Storage.V1.AssignmentStore>();
7676
services.AddSingleton<IStore<Dispatch, int>, DispatchStore>();
77-
services.AddSingleton<IStore<SpaceStation, int>, SpaceStationStore>();
77+
services.AddSingleton<IStore<SpaceStation, long>, SpaceStationStore>();
7878

7979
// Register mappers
8080
services.AddSingleton<AssignmentMapper>();

src/Helldivers-2-Core/Facades/V1Facade.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public sealed class V1Facade(
2020
AssignmentMapper assignmentMapper,
2121
IStore<Dispatch, int> dispatchStore,
2222
DispatchMapper dispatchMapper,
23-
IStore<SpaceStation, int> spaceStationStore,
23+
IStore<SpaceStation, long> spaceStationStore,
2424
SpaceStationMapper spaceStationMapper
2525
)
2626
{

src/Helldivers-2-Core/Mapping/MappingContext.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ public sealed class MappingContext
4747
public TimeSpan GameTimeDeviation { get; private init; }
4848

4949
/// <summary>Initializes a new <see cref="MappingContext" />.</summary>
50-
internal MappingContext(WarId warId, WarInfo warInfo, Dictionary<string, WarStatus> warStatuses, WarSummary warSummary, Dictionary<string, List<NewsFeedItem>> newsFeeds, Dictionary<string, List<Assignment>> assignments, Dictionary<string, List<SpaceStation>> spaceStations)
50+
internal MappingContext(WarId warId,
51+
WarInfo warInfo,
52+
Dictionary<string, WarStatus> warStatuses,
53+
WarSummary warSummary,
54+
Dictionary<string, List<NewsFeedItem>> newsFeeds,
55+
Dictionary<string, List<Assignment>> assignments,
56+
Dictionary<string, List<SpaceStation>> spaceStations)
5157
{
5258
WarId = warId;
5359
WarInfo = warInfo;

src/Helldivers-2-Core/Mapping/V1/SpaceStationMapper.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Helldivers.Models;
22
using Helldivers.Models.V1;
3+
using Helldivers.Models.V1.SpaceStations;
34

45
namespace Helldivers.Core.Mapping.V1;
56

@@ -16,19 +17,43 @@ public sealed class SpaceStationMapper
1617
/// <returns>An enumerable list of space stations mapped to the V1 model.</returns>
1718
public IEnumerable<SpaceStation> MapToV1(MappingContext context, List<Planet> planets)
1819
{
19-
foreach (var station in context.InvariantWarStatus.SpaceStations)
20-
yield return Map(context, station, planets);
20+
// Get a list of all assignments across all translations.
21+
var invariants = context.SpaceStations
22+
.SelectMany(pair => pair.Value)
23+
.DistinctBy(spaceStation => spaceStation.Id32);
24+
25+
foreach (var spaceStation in invariants)
26+
{
27+
// Build a dictionary of all translations for this assignment
28+
var translations = context.SpaceStations.Select(pair =>
29+
new KeyValuePair<string, Models.ArrowHead.SpaceStation?>(
30+
pair.Key,
31+
pair.Value.FirstOrDefault(a => a.Id32 == spaceStation.Id32)
32+
)
33+
).Where(pair => pair.Value is not null)
34+
.ToDictionary(pair => pair.Key, pair => pair.Value!);
35+
36+
yield return Map(translations, context, planets);
37+
}
2138
}
2239

23-
private SpaceStation Map(MappingContext context, Helldivers.Models.ArrowHead.SpaceStation raw, List<Planet> planets)
40+
private SpaceStation Map(Dictionary<string, Models.ArrowHead.SpaceStation> translations, MappingContext context, List<Planet> planets)
2441
{
25-
var planet = planets.First(p => p.Index == raw.PlanetIndex);
42+
var invariant = translations.First().Value;
43+
var planet = planets.First(p => p.Index == invariant.PlanetIndex);
2644

2745
return new SpaceStation(
28-
Id32: raw.Id32,
46+
Id32: invariant.Id32,
2947
Planet: planet,
30-
ElectionEnd: context.RelativeGameStart.AddSeconds(raw.CurrentElectionEndWarTime),
31-
Flags: raw.Flags
48+
ElectionEnd: context.RelativeGameStart.AddSeconds(invariant.CurrentElectionEndWarTime),
49+
Flags: invariant.Flags,
50+
TacticalActions: invariant.TacticalActions.Select(rawAction => MapTacticalAction(context, rawAction)).ToList()
3251
);
3352
}
53+
54+
private TacticalAction MapTacticalAction(MappingContext context, Helldivers.Models.ArrowHead.SpaceStations.TacticalAction raw)
55+
{
56+
// TODO: map actions.
57+
return new TacticalAction();
58+
}
3459
}

src/Helldivers-2-Core/Storage/ArrowHead/ArrowHeadStore.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public sealed class ArrowHeadStore
1414
private CultureDictionary<Memory<byte>> _statuses = null!;
1515
private CultureDictionary<Memory<byte>> _feeds = null!;
1616
private CultureDictionary<Memory<byte>> _assignments = null!;
17-
private CultureDictionary<Memory<byte>> _spaceStations = null!;
17+
private Dictionary<long, CultureDictionary<Memory<byte>>> _spaceStations = null!;
1818
private readonly TaskCompletionSource _syncState = new();
1919

2020
/// <summary>
@@ -27,7 +27,7 @@ public void UpdateRawStore(
2727
IEnumerable<KeyValuePair<string, Memory<byte>>> statuses,
2828
IEnumerable<KeyValuePair<string, Memory<byte>>> feeds,
2929
IEnumerable<KeyValuePair<string, Memory<byte>>> assignments,
30-
IEnumerable<KeyValuePair<string, Memory<byte>>> spaceStations
30+
Dictionary<long, Dictionary<string, Memory<byte>>> spaceStations
3131
)
3232
{
3333
_warId = warId;
@@ -36,7 +36,8 @@ IEnumerable<KeyValuePair<string, Memory<byte>>> spaceStations
3636
_statuses = new(statuses);
3737
_feeds = new(feeds);
3838
_assignments = new(assignments);
39-
_spaceStations = new(spaceStations);
39+
_spaceStations =
40+
spaceStations.ToDictionary(pair => pair.Key, pair => new CultureDictionary<Memory<byte>>(pair.Value));
4041

4142
_syncState.TrySetResult();
4243
}
@@ -104,11 +105,10 @@ public async Task<Memory<byte>> GetAssignments(CancellationToken cancellationTok
104105
/// <summary>
105106
/// returns the raw payload for <see cref="SpaceStation" />s.
106107
/// </summary>
107-
public async Task<Memory<byte>> GetSpaceStations(int id, CancellationToken cancellationToken)
108+
public async Task<Memory<byte>?> GetSpaceStation(long id, CancellationToken cancellationToken)
108109
{
109110
await _syncState.Task.WaitAsync(cancellationToken);
110111

111-
// TODO use ID to get relevant space station
112-
return _spaceStations.Get();
112+
return _spaceStations.TryGetValue(id, out var spaceStations) ? spaceStations.Get() : null;
113113
}
114114
}

src/Helldivers-2-Core/Storage/V1/SpaceStationStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
namespace Helldivers.Core.Storage.V1;
55

66
/// <inheritdoc cref="IStore{T,TKey}" />
7-
public sealed class SpaceStationStore : StoreBase<SpaceStation, int>
7+
public sealed class SpaceStationStore : StoreBase<SpaceStation, long>
88
{
99
/// <inheritdoc />
10-
protected override bool GetAsyncPredicate(SpaceStation station, int index) => station.Id32 == index;
10+
protected override bool GetAsyncPredicate(SpaceStation station, long index) => station.Id32 == index;
1111
}

0 commit comments

Comments
 (0)