Skip to content

Commit

Permalink
add SpaceStation to ArrowHead API (#120)
Browse files Browse the repository at this point in the history
* add SpaceStation to ArrowHead API

see #118

* add v1 support for space stations
  • Loading branch information
dealloc authored Nov 14, 2024
1 parent 58a5aa5 commit 86336c2
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public static IServiceCollection AddV1Stores(this IServiceCollection services)
services.AddSingleton<IStore<Campaign, int>, CampaignStore>();
services.AddSingleton<IStore<Models.V1.Assignment, long>, Storage.V1.AssignmentStore>();
services.AddSingleton<IStore<Dispatch, int>, DispatchStore>();
services.AddSingleton<IStore<SpaceStation, int>, SpaceStationStore>();

// Register mappers
services.AddSingleton<AssignmentMapper>();
Expand All @@ -79,6 +80,7 @@ public static IServiceCollection AddV1Stores(this IServiceCollection services)
services.AddSingleton<PlanetMapper>();
services.AddSingleton<StatisticsMapper>();
services.AddSingleton<WarMapper>();
services.AddSingleton<SpaceStationMapper>();

return services;
}
Expand Down
14 changes: 13 additions & 1 deletion src/Helldivers-2-Core/Facades/V1Facade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public sealed class V1Facade(
IStore<Assignment, long> assignmentStore,
AssignmentMapper assignmentMapper,
IStore<Dispatch, int> dispatchStore,
DispatchMapper dispatchMapper
DispatchMapper dispatchMapper,
IStore<SpaceStation, int> spaceStationStore,
SpaceStationMapper spaceStationMapper
)
{
/// <see cref="IStore{T,TKey}.SetStore" />
Expand All @@ -35,6 +37,7 @@ public async ValueTask UpdateStores(MappingContext context)
await UpdateCampaignStore(context, planets);
await UpdateAssignmentsStore(context);
await UpdateDispatchStore(context);
await UpdateSpaceStationStore(context, planets);
}

private async ValueTask UpdateWarStore(MappingContext context, List<Planet> planets)
Expand Down Expand Up @@ -76,4 +79,13 @@ private async ValueTask UpdateDispatchStore(MappingContext context)

await dispatchStore.SetStore(dispatches);
}

private async ValueTask UpdateSpaceStationStore(MappingContext context, List<Planet> planets)
{
var spaceStations = spaceStationMapper
.MapToV1(context, planets)
.ToList();

await spaceStationStore.SetStore(spaceStations);
}
}
34 changes: 34 additions & 0 deletions src/Helldivers-2-Core/Mapping/V1/SpaceStationMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Helldivers.Models;
using Helldivers.Models.V1;

namespace Helldivers.Core.Mapping.V1;

/// <summary>
/// Handles mapping for <see cref="SpaceStation" />.
/// </summary>
public sealed class SpaceStationMapper
{
/// <summary>
/// Maps all space stations in the <see cref="MappingContext" />.
/// </summary>
/// <param name="context">The mapping context containing the invariant war status and other relevant data.</param>
/// <param name="planets">The list of planets to map with.</param>
/// <returns>An enumerable list of space stations mapped to the V1 model.</returns>
public IEnumerable<SpaceStation> MapToV1(MappingContext context, List<Planet> planets)
{
foreach (var station in context.InvariantWarStatus.SpaceStations)
yield return Map(context, station, planets);
}

private SpaceStation Map(MappingContext context, Helldivers.Models.ArrowHead.Status.SpaceStation raw, List<Planet> planets)
{
var planet = planets.First(p => p.Index == raw.PlanetIndex);

return new SpaceStation(
Id32: raw.Id32,
Planet: planet,
ElectionEnd: context.RelativeGameStart.AddSeconds(raw.CurrentElectionEndWarTime),
Flags: raw.Flags
);
}
}
11 changes: 11 additions & 0 deletions src/Helldivers-2-Core/Storage/V1/SpaceStationStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Helldivers.Core.Contracts.Collections;
using Helldivers.Models.V1;

namespace Helldivers.Core.Storage.V1;

/// <inheritdoc cref="IStore{T,TKey}" />
public sealed class SpaceStationStore : StoreBase<SpaceStation, int>
{
/// <inheritdoc />
protected override bool GetAsyncPredicate(SpaceStation station, int index) => station.Id32 == index;
}
16 changes: 16 additions & 0 deletions src/Helldivers-2-Models/ArrowHead/Status/SpaceStation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Helldivers.Models.ArrowHead.Status;

/// <summary>
/// Represents one of the space stations as passed from the ArrowHead API.
/// </summary>
/// <param name="Id32">The unique identifier of the station.</param>
/// <param name="PlanetIndex">The id of the planet it's currently orbiting</param>
/// <param name="CurrentElectionEndWarTime">When the election for the next planet will end (in seconds relative to game start).</param>
/// <param name="Flags">A set of flags, purpose currently unknown.</param>
public sealed record SpaceStation(
long Id32,
int PlanetIndex,
// TODO PlanetActiveEffects
ulong CurrentElectionEndWarTime,
int Flags
);
4 changes: 3 additions & 1 deletion src/Helldivers-2-Models/ArrowHead/WarStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Helldivers.Models.ArrowHead;
/// <param name="Campaigns">A list of ongoing campaigns in the galactic war.</param>
/// <param name="JointOperations">A list of <see cref="JointOperation" />s.</param>
/// <param name="PlanetEvents">A list of ongoing <see cref="PlanetEvent" />s.</param>
/// <param name="SpaceStations">A list of <see cref="SpaceStation" />s.</param>
public sealed record WarStatus(
int WarId,
long Time,
Expand All @@ -24,9 +25,10 @@ public sealed record WarStatus(
List<Campaign> Campaigns,
// TODO CommunityTargets
List<JointOperation> JointOperations,
List<PlanetEvent> PlanetEvents
List<PlanetEvent> PlanetEvents,
// TODO PlanetActiveEffects
// TODO activeElectionPolicyEffects
// TODO globalEvents
// TODO superEarthWarResults
List<SpaceStation> SpaceStations
);
15 changes: 15 additions & 0 deletions src/Helldivers-2-Models/V1/SpaceStation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Helldivers.Models.V1;

/// <summary>
/// Represents a Super Earth Democracy Space Station.
/// </summary>
/// <param name="Id32">The unique identifier of the station.</param>
/// <param name="Planet">The planet it's currently orbiting.</param>
/// <param name="ElectionEnd">When the election for the next planet will end.</param>
/// <param name="Flags">A set of flags, purpose currently unknown.</param>
public sealed record SpaceStation(
long Id32,
Planet Planet,
DateTime ElectionEnd,
int Flags
);

0 comments on commit 86336c2

Please sign in to comment.