Skip to content

Commit e37be8e

Browse files
authored
update planets model to include regions (#141)
* add planet region mapping * map planet regions in WarInfo * map planet regions in WarStatus * map planet regions into v1 * add missing documentation * force enums to generate as strings * change PlanetRegionStatus's regen to double * upload OpenAPI documents as part of CI
1 parent 43b1b84 commit e37be8e

File tree

18 files changed

+220
-13
lines changed

18 files changed

+220
-13
lines changed

.github/workflows/dotnet.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ jobs:
2929
run: dotnet format --no-restore --verify-no-changes --exclude-diagnostics IL2026 IL3050
3030
- name: Check tests
3131
run: dotnet test --no-build --verbosity normal
32+
33+
- name: Upload artifacts
34+
uses: actions/upload-artifact@v4
35+
with:
36+
retention-days: 5
37+
name: openapi-schemas
38+
path: docs/openapi/

src/Helldivers-2-API/Helldivers-2-API.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636

3737
<!-- Only include swagger dependencies in DEBUG builds -->
3838
<ItemGroup Condition="'$(Configuration)' == 'Debug'">
39-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
40-
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.0">
39+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.6" />
40+
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="9.0.6">
4141
<PrivateAssets>all</PrivateAssets>
4242
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4343
</PackageReference>
44-
<PackageReference Include="NSwag.AspNetCore" Version="14.1.0" />
44+
<PackageReference Include="NSwag.AspNetCore" Version="14.4.0" />
4545
</ItemGroup>
4646

4747
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using NJsonSchema;
2+
using NJsonSchema.Generation.TypeMappers;
3+
using System.Collections.ObjectModel;
4+
5+
namespace Helldivers.API.OpenApi.TypeMappers;
6+
7+
/// <summary>
8+
/// Forces enums to be generated as an enumeration of strings rather than integers.
9+
/// </summary>
10+
/// <typeparam name="T">The enum type to convert.</typeparam>
11+
public sealed class EnumStringTypeMapper<T> : ITypeMapper where T : struct, Enum
12+
{
13+
/// <inheritdoc />
14+
public Type MappedType => typeof(T);
15+
16+
/// <inheritdoc />
17+
public bool UseReference => false;
18+
19+
/// <inheritdoc />
20+
public void GenerateSchema(JsonSchema schema, TypeMapperContext context)
21+
{
22+
schema.Type = JsonObjectType.String;
23+
schema.Format = null;
24+
25+
var names = Enum.GetNames<T>();
26+
27+
schema.Enumeration.Clear();
28+
schema.EnumerationNames.Clear();
29+
30+
foreach (var name in names)
31+
schema.Enumeration.Add(name);
32+
33+
schema.EnumerationNames = new Collection<string>(names.ToList());
34+
schema.ExtensionData ??= new Dictionary<string, object>()!;
35+
}
36+
}

src/Helldivers-2-API/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@
165165
document.SchemaSettings.TypeMappers.Add(
166166
new Helldivers.API.OpenApi.TypeMappers.LocalizedMessageTypeMapper(languages)
167167
);
168+
document.SchemaSettings.TypeMappers.Add(
169+
new Helldivers.API.OpenApi.TypeMappers.EnumStringTypeMapper<Helldivers.Models.V1.Planets.RegionSize>()
170+
);
168171

169172
document.OperationProcessors.Add(new Helldivers.API.OpenApi.OperationProcessors.SuperHeadersProcessor());
170173

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ private Planet MapToV1(MappingContext context, PlanetInfo info, PlanetStatus sta
4141
Static.Factions.TryGetValue(info.InitialOwner, out var initialOwner);
4242
Static.Factions.TryGetValue(status.Owner, out var currentOwner);
4343

44+
var regionStates = context.InvariantWarStatus.PlanetRegions.Where(region => region.PlanetIndex == info.Index).ToList();
45+
var regions = context.WarInfo.PlanetRegions
46+
.Where(region => region.PlanetIndex == info.Index)
47+
.Select(region => (region, regionStates.FirstOrDefault(state => state.RegionIndex == region.RegionIndex)))
48+
.ToList();
49+
4450
var (name, sector, biomeKey, environmentals) = planet;
4551

4652
return new Planet(
@@ -60,7 +66,8 @@ private Planet MapToV1(MappingContext context, PlanetInfo info, PlanetStatus sta
6066
RegenPerSecond: status.RegenPerSecond,
6167
Event: MapToV1(@event, context),
6268
Statistics: statisticsMapper.MapToV1(stats, status),
63-
Attacking: attacking
69+
Attacking: attacking,
70+
Regions: regions.Select(region => MapToV1(region.region, region.Item2, context)).ToList()
6471
);
6572
}
6673

@@ -83,4 +90,23 @@ private Planet MapToV1(MappingContext context, PlanetInfo info, PlanetStatus sta
8390
JointOperationIds: @event.JointOperationIds
8491
);
8592
}
93+
94+
private Region MapToV1(Models.ArrowHead.Info.PlanetRegion region, Models.ArrowHead.Status.PlanetRegionStatus? status, MappingContext context)
95+
{
96+
string? owner = null;
97+
if (status is { Owner: var faction })
98+
Static.Factions.TryGetValue(faction, out owner);
99+
100+
Static.PlanetRegion.TryGetValue(region.SettingsHash, out var planetRegion);
101+
return new Region(
102+
Name: planetRegion.Name,
103+
Description: planetRegion.Description,
104+
MaxHealth: region.MaxHealth,
105+
Size: (RegionSize)region.RegionSize,
106+
RegenPerSecond: status?.RegerPerSecond,
107+
AvailabilityFactor: status?.AvailabilityFactor,
108+
IsAvailable: status?.IsAvailable ?? false,
109+
Players: status?.Players ?? 0
110+
);
111+
}
86112
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Helldivers.Models.ArrowHead.Info;
2+
3+
/// <summary>
4+
/// A region of a planet, containing information about its health and size.
5+
/// </summary>
6+
/// <param name="PlanetIndex">The index of the region's planet</param>
7+
/// <param name="RegionIndex">The index of the region</param>
8+
/// <param name="SettingsHash">The ID that identifies the region in the JSON files.</param>
9+
/// <param name="MaxHealth">The maximum health of this region.</param>
10+
/// <param name="RegionSize">The size of the region.</param>
11+
public sealed record PlanetRegion(
12+
int PlanetIndex,
13+
int RegionIndex,
14+
ulong SettingsHash,
15+
ulong MaxHealth,
16+
int RegionSize
17+
);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Helldivers.Models.ArrowHead.Info;
2+
3+
namespace Helldivers.Models.ArrowHead.Status;
4+
5+
/// <summary>
6+
/// Represents the 'current' status of a planet's region in the galactic war.
7+
/// </summary>
8+
/// <param name="PlanetIndex">The identifier of the <see cref="PlanetInfo"/> this region is on.</param>
9+
/// <param name="RegionIndex">The identifier of the <see cref="Info.PlanetRegion" /> this data refers to.</param>
10+
/// <param name="Owner">The current faction that controls the region.</param>
11+
/// <param name="Health">The current health / liberation of the region.</param>
12+
/// <param name="RegerPerSecond">If left alone, how much the health of the region would regenerate.</param>
13+
/// <param name="AvailabilityFactor">Unknown purpose.</param>
14+
/// <param name="IsAvailable">Whether this region is currently available to play on(?).</param>
15+
/// <param name="Players">The amount of helldivers currently active on this planet.</param>
16+
public sealed record PlanetRegionStatus(
17+
int PlanetIndex,
18+
int RegionIndex,
19+
int Owner,
20+
long Health,
21+
double RegerPerSecond,
22+
double AvailabilityFactor,
23+
bool IsAvailable,
24+
long Players
25+
);

src/Helldivers-2-Models/ArrowHead/WarInfo.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ namespace Helldivers.Models.ArrowHead;
1111
/// <param name="MinimumClientVersion">A version string indicating the minimum game client version the API supports.</param>
1212
/// <param name="PlanetInfos">A list of planets involved in this season's war.</param>
1313
/// <param name="HomeWorlds">A list of homeworlds for the races (factions) involved in this war.</param>
14+
/// <param name="PlanetRegions">The regions that can be found on this planet.</param>
1415
public sealed record WarInfo(
1516
int WarId,
1617
long StartDate,
1718
long EndDate,
1819
string MinimumClientVersion,
1920
List<PlanetInfo> PlanetInfos,
20-
List<HomeWorld> HomeWorlds
21+
List<HomeWorld> HomeWorlds,
2122
// TODO: capitalInfo's
2223
// TODO planetPermanentEffects
24+
List<PlanetRegion> PlanetRegions
2325
);

src/Helldivers-2-Models/ArrowHead/WarStatus.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Helldivers.Models.ArrowHead;
1414
/// <param name="Campaigns">A list of ongoing campaigns in the galactic war.</param>
1515
/// <param name="JointOperations">A list of <see cref="JointOperation" />s.</param>
1616
/// <param name="PlanetEvents">A list of ongoing <see cref="PlanetEvent" />s.</param>
17+
/// <param name="PlanetRegions">The regions that have a status.</param>
1718
public sealed record WarStatus(
1819
int WarId,
1920
long Time,
@@ -24,9 +25,10 @@ public sealed record WarStatus(
2425
List<Campaign> Campaigns,
2526
// TODO CommunityTargets
2627
List<JointOperation> JointOperations,
27-
List<PlanetEvent> PlanetEvents
28+
List<PlanetEvent> PlanetEvents,
2829
// TODO PlanetActiveEffects
2930
// TODO activeElectionPolicyEffects
3031
// TODO globalEvents
3132
// TODO superEarthWarResults
33+
List<PlanetRegionStatus> PlanetRegions
3234
);

src/Helldivers-2-Models/Helldivers-2-Models.csproj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
<PropertyGroup>
44
<IsAotCompatible>true</IsAotCompatible>
55
<RootNamespace>Helldivers.Models</RootNamespace>
6+
7+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
68
</PropertyGroup>
79

810
<ItemGroup>
911
<None Remove="json/**/*" Visible="false" />
10-
<AdditionalFiles Include="json/factions.json" Visible="false" />
11-
<AdditionalFiles Include="json/planets/planets.json" Visible="false" />
12-
<AdditionalFiles Include="json/planets/biomes.json" Visible="false" />
13-
<AdditionalFiles Include="json/planets/environmentals.json" Visible="false" />
12+
<AdditionalFiles Include="json/factions.json" />
13+
<AdditionalFiles Include="json/planets/planets.json" />
14+
<AdditionalFiles Include="json/planets/planetRegion.json" />
15+
<AdditionalFiles Include="json/planets/biomes.json" />
16+
<AdditionalFiles Include="json/planets/environmentals.json" />
1417

1518
<ProjectReference Include="..\Helldivers-2-SourceGen\Helldivers-2-SourceGen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
1619
</ItemGroup>

0 commit comments

Comments
 (0)