Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaickler committed Mar 9, 2024
1 parent ed843dd commit 48ad500
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 0 deletions.
31 changes: 31 additions & 0 deletions HellDiversCommandCenter.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HellDiversDotNet", "HellDiversDotNet\HellDiversDotNet.csproj", "{64E709AC-8A65-4DC6-A0F9-DDCD705B8AD5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HellDiversDotNetTests", "HellDiversDotNetTests\HellDiversDotNetTests.csproj", "{33BC23A0-A8D4-47EC-B99A-62FF2DC93835}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{64E709AC-8A65-4DC6-A0F9-DDCD705B8AD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64E709AC-8A65-4DC6-A0F9-DDCD705B8AD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64E709AC-8A65-4DC6-A0F9-DDCD705B8AD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64E709AC-8A65-4DC6-A0F9-DDCD705B8AD5}.Release|Any CPU.Build.0 = Release|Any CPU
{33BC23A0-A8D4-47EC-B99A-62FF2DC93835}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{33BC23A0-A8D4-47EC-B99A-62FF2DC93835}.Debug|Any CPU.Build.0 = Debug|Any CPU
{33BC23A0-A8D4-47EC-B99A-62FF2DC93835}.Release|Any CPU.ActiveCfg = Release|Any CPU
{33BC23A0-A8D4-47EC-B99A-62FF2DC93835}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E4A7C707-3885-41D1-B0F3-B271C028A9D6}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions HellDiversDotNet/HellDiversDotNet.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0-preview.1.24080.9" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0-preview.1.24080.9" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.Json" Version="9.0.0-preview.1.24080.9" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions HellDiversDotNet/Models/BaseContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HellDiversDotNet.Models
{
public class BaseContent
{
[JsonPropertyName("current")]
public UInt16 CurrentSeason { get; set; }
[JsonPropertyName("seasons")]
public List<UInt16> Seasons { get; set; }


public static BaseContent FromJson(string json)
{
// Use JsonSerializer.Deserialize method to deserialize JSON into MyClass object
return JsonSerializer.Deserialize<BaseContent>(json)!;
}

BaseContent(UInt16 currentSeason, List<UInt16> seasons)
{
CurrentSeason = currentSeason;
Seasons = seasons;
}

[JsonConstructorAttribute]
BaseContent()
{

}
}
}
54 changes: 54 additions & 0 deletions HellDiversDotNet/Models/Event.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HellDiversDotNet.Models
{
public class Event
{
[JsonPropertyName("assignment_id_32")]
public uint AssignemntID { get; set; }

[JsonPropertyName("effects")]
public List<string> Effects { get; set; }

[JsonPropertyName("flag")]
public ushort Flag { get; set; }

[JsonPropertyName("id")]
public uint Id { get; set; }

[JsonPropertyName("id_32")]
public uint Id32 { get; set; }

[JsonPropertyName("message")]
public Dictionary<string, string> Message { get; set; }

[JsonPropertyName("message_id_32")]
public uint MessageId32 { get; set; }

[JsonPropertyName("planets")]
public List<uint> PlanetIds { get; set; }

[JsonPropertyName("portrait_id_32")]
public uint PortraitId32 { get; set; }

[JsonPropertyName("race")]
public string Race { get; set; }

[JsonPropertyName("title")]
public string Title { get; set; }

[JsonPropertyName("title_32")]
public uint TitleID { get; set; }

public Event FromJson(string json)
{
return JsonSerializer.Deserialize<Event>(json)!;
}
}
}
24 changes: 24 additions & 0 deletions HellDiversDotNet/Models/WarInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace HellDiversDotNet.Models
{
public class WarInfo
{
[JsonPropertyName("capitals")]
public List<uint> Capitals { get; set; }

[JsonPropertyName("end_date")]
public DateTime EndDate { get; set; }

public WarInfo FromJson(string json)
{
return JsonSerializer.Deserialize<WarInfo>(json);
}
}
}
83 changes: 83 additions & 0 deletions HellDiversDotNet/Services/SuperEarthConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using HellDiversDotNet.Models;
using System.Net.Http.Json;

namespace HellDiversDotNet.Services
{
public class SuperEarthConnection
{
ServiceCollection serviceCollection = new ServiceCollection();

ServiceProvider serviceProvider;

private HttpClient BaseClient { get
{
var builder = serviceProvider.GetRequiredService<IHttpClientFactory>();

return builder.CreateClient("Base");
} }

public async Task<BaseContent?> GetBaseContentAsync(HttpClient? httpClient = null)
{
if (httpClient == null)
{
httpClient = BaseClient;
}

return await httpClient.GetFromJsonAsync<BaseContent>(httpClient.BaseAddress!.ToString());
}

public async Task<List<Event>?> GetEventsAsync(uint? warId = null)
{
if (warId == null)
{
warId = (await GetBaseContentAsync()).CurrentSeason;
}

HttpClient client = BaseClient;
var response = client.GetFromJsonAsync<List<Event>>
(new Uri(string.Join('/',
client.BaseAddress, warId, "events")));

return response.Result;
}

public async Task<Event?> GetLatestEventAsync(uint? warId = null)
{
if (warId == null)
{
warId = (await GetBaseContentAsync()).CurrentSeason;
}

HttpClient client = BaseClient;
var response = client.GetFromJsonAsync<Event>
(new Uri(string.Join('/',
client.BaseAddress, warId, "events", "latest")));

return response.Result;
}


public SuperEarthConnection()
{
ServiceCollection serviceCollection = new ServiceCollection();

serviceCollection.AddHttpClient("Base", client =>
{
client.BaseAddress = new Uri("https://helldivers-2.fly.dev/api");
// Configure other settings as needed
});

serviceCollection.AddLogging();

serviceProvider = serviceCollection.BuildServiceProvider();
}
}
}
1 change: 1 addition & 0 deletions HellDiversDotNetTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Microsoft.VisualStudio.TestTools.UnitTesting;
28 changes: 28 additions & 0 deletions HellDiversDotNetTests/HellDiversDotNetTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.3" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.31.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\HellDiversDotNet\HellDiversDotNet.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions HellDiversDotNetTests/SuperEarthConnectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FluentAssertions;
using HellDiversDotNet.Models;
using HellDiversDotNet.Services;

namespace HellDiversDotNetTests
{
[TestClass]
public class SuperEarthConnectionTests
{
private SuperEarthConnection superEarthConnection = new();

[TestInitialize]
public void Initialize()
{
superEarthConnection = new SuperEarthConnection();
}

[TestMethod]
public async Task GetBaseReturnsBaseContent()
{
var baseContent = await superEarthConnection.GetBaseContentAsync();

baseContent.Should().NotBeNull();
baseContent.Should().BeOfType<BaseContent>(
because: "the function should return a BaseContent object.");
}

[TestMethod]
public async Task GetEventsReturnsList()
{
var events = await superEarthConnection.GetEventsAsync();

events.Should().NotBeNull();
events.Should().BeOfType<List<Event>>(
because: "the function should return a list of events.");
}

[TestMethod]
public async Task GetLatestEventReturnsEvent()
{
var latestEvent = await superEarthConnection.GetLatestEventAsync();

latestEvent.Should().NotBeNull();
latestEvent.Should().BeOfType<Event>(
because: "the function should return an event.");
}
}
}

0 comments on commit 48ad500

Please sign in to comment.