Skip to content

Commit

Permalink
Dev (#11)
Browse files Browse the repository at this point in the history
* readme

* url

* Client (#9)

* readme

* Client

* trim

* Xunit (#10)

* readme

* fix xunit

* TestGetAllFirstSeen

* Update publish.yml

* cleanup

* forgot some ?'s

* cleanup

Co-authored-by: Scott Milliorn <[email protected]>
  • Loading branch information
urothis and milliorn authored Jul 13, 2021
1 parent 3feebc8 commit b2fd347
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 101 deletions.
2 changes: 0 additions & 2 deletions API/NWN.MasterList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<PackageId>NWN.MasterList</PackageId>
<IsPackable>true</IsPackable>
<Authors>NWN-dotnet</Authors>
<Nullable>enable</Nullable>
<PackageTags>Neverwinter Nights;NWN;</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols>
Expand All @@ -18,6 +17,5 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>
32 changes: 3 additions & 29 deletions API/src/Client.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,11 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NWN.MasterList.Data;
using System.Net.Http;

namespace NWN.MasterList {
public class Client {
private static HttpClient _client { get; set; }
public List<NwServer> Servers { get; set; }

public static async Task<List<NwServer>> GetServers() {
string response = await _client.GetStringAsync("https://api.nwn.beamdog.net/v1/servers");
return JsonConvert.DeserializeObject<List<NwServer>>(response);
}

public static async Task<NwServer> GetServer(string publicKey) {
string response = await _client.GetStringAsync($"https://api.nwn.beamdog.net/v1/servers/{publicKey}");
return JsonConvert.DeserializeObject<NwServer>(response);
}

public static async Task<NwServer> GetServer(string ip, int port) {
string response = await _client.GetStringAsync($"https://api.nwn.beamdog.net/v1/servers/{ip}/{port}");
return JsonConvert.DeserializeObject<NwServer>(response);
}

public static async Task<Me> GetMe() {
string response = await _client.GetStringAsync("https://api.nwn.beamdog.net/v1/me");
return JsonConvert.DeserializeObject<Me>(response);
}
public static HttpClient HttpClient { get; set; }

public Client() {
_client = new HttpClient();
Servers = GetServers().Result;
HttpClient = new HttpClient();
}
}
}
8 changes: 4 additions & 4 deletions API/src/Data/Manifest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace NWN.MasterList.Data {
public struct Manifest {
[JsonProperty("required")]
[JsonPropertyName("required")]
public bool Required { get; set; }
[JsonProperty("hash")]
public string? Hash { get; set; }
[JsonPropertyName("hash")]
public string Hash { get; set; }
}
}
6 changes: 3 additions & 3 deletions API/src/Data/Me.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace NWN.MasterList.Data {
public struct Me {
[JsonProperty("address")]
[JsonPropertyName("address")]
public string Address { get; set; }
[JsonProperty("servers")]
[JsonPropertyName("servers")]
public List<NwServer> Servers { get; set; }
}
}
62 changes: 29 additions & 33 deletions API/src/Data/NwServer.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,62 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace NWN.MasterList.Data {
public struct NwServer {
[JsonProperty("first_seen")]
[JsonPropertyName("first_seen")]
public int FirstSeen { get; set; }
[JsonProperty("last_advertisement")]
[JsonPropertyName("last_advertisement")]
public int LastAdvertisement { get; set; }
[JsonProperty("session_name")]
[JsonPropertyName("session_name")]
public string SessionName { get; set; }
[JsonProperty("module_name")]
[JsonPropertyName("module_name")]
public string ModuleName { get; set; }
[JsonProperty("module_description")]
[JsonPropertyName("module_description")]
public string ModuleDescription { get; set; }
[JsonProperty("passworded")]
[JsonPropertyName("passworded")]
public bool Passworded { get; set; }
[JsonProperty("min_level")]
[JsonPropertyName("min_level")]
public int MinLevel { get; set; }
[JsonProperty("max_level")]
[JsonPropertyName("max_level")]
public int MaxLevel { get; set; }
[JsonProperty("current_players")]
[JsonPropertyName("current_players")]
public int CurrentPlayers { get; set; }
[JsonProperty("max_players")]
[JsonPropertyName("max_players")]
public int MaxPlayers { get; set; }
[JsonProperty("build")]
[JsonPropertyName("build")]
public string Build { get; set; }
[JsonProperty("rev")]
[JsonPropertyName("rev")]
public int Revision { get; set; }
[JsonProperty("pvp")]
[JsonPropertyName("pvp")]
public int PVP { get; set; }
[JsonProperty("servervault")]
[JsonPropertyName("servervault")]
public bool ServerVault { get; set; }
[JsonProperty("elc")]
[JsonPropertyName("elc")]
public bool ELC { get; set; }
[JsonProperty("ilr")]
[JsonPropertyName("ilr")]
public bool ILR { get; set; }
[JsonProperty("one_party")]
[JsonPropertyName("one_party")]
public bool OneParty { get; set; }
[JsonProperty("player_pause")]
[JsonPropertyName("player_pause")]
public bool PlayerPause { get; set; }
[JsonProperty("os")]
[JsonPropertyName("os")]
public int OS { get; set; }
[JsonProperty("language")]
[JsonPropertyName("language")]
public int Language { get; set; }
[JsonProperty("game_type")]
[JsonPropertyName("game_type")]
public int GameType { get; set; }
[JsonProperty("latency")]
[JsonPropertyName("latency")]
public int Latency { get; set; }
[JsonProperty("host")]
[JsonPropertyName("host")]
public string IP { get; set; }
[JsonProperty("port")]
[JsonPropertyName("port")]
public int Port { get; set; }
[JsonProperty("kx_pk")]
[JsonPropertyName("kx_pk")]
public string KxPk { get; set; }
[JsonProperty("sign_pk")]
[JsonPropertyName("sign_pk")]
public string SignPk { get; set; }
[JsonProperty("connecthint")]
[JsonPropertyName("connecthint")]
public string ConnectHint { get; set; }
[JsonProperty("nwsync")]
[JsonPropertyName("nwsync")]
public NwSync NwSync { get; set; }
}
}
10 changes: 5 additions & 5 deletions API/src/Data/NwSync.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace NWN.MasterList.Data {
public struct NwSync {
[JsonProperty("manifests")]
public List<Manifest>? Manifests { get; set; }
[JsonProperty("url")]
public string? URL { get; set; }
[JsonPropertyName("manifests")]
public List<Manifest> Manifests { get; set; }
[JsonPropertyName("url")]
public string URL { get; set; }
}
}
33 changes: 29 additions & 4 deletions API/src/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using NWN.MasterList.Data;

namespace NWN.MasterList {
public class Helper {
public static int MasterListPositionFromSessionName(List<NwServer> servers, string sessionName) =>
public static class Helper {
public static string jsonUrl = "https://api.nwn.beamdog.net/v1/";

public static int MasterListPositionFromSessionName(this Client client, List<NwServer> servers, string sessionName) =>
servers.Where(s => s.SessionName == sessionName).Select(self => servers.IndexOf(self)).FirstOrDefault();

public static IOrderedEnumerable<NwServer> GetAllFirstSeen(List<NwServer> servers) =>
servers.OrderByDescending(x => x.FirstSeen);
public async static Task<IOrderedEnumerable<NwServer>> GetAllFirstSeen(this Client client) {
var response = await client.GetServers();
return response.OrderByDescending(x => x.FirstSeen);
}
public static async Task<List<NwServer>> GetServers(this Client client) {
string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers");
return JsonSerializer.Deserialize<List<NwServer>>(response);
}

public static async Task<NwServer> GetServer(this Client client, string publicKey) {
string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers{publicKey}");
return JsonSerializer.Deserialize<NwServer>(response);
}

public static async Task<NwServer> GetServer(this Client client, string ip, int port) {
string response = await Client.HttpClient.GetStringAsync($"{jsonUrl}/servers/{ip}/{port}");
return JsonSerializer.Deserialize<NwServer>(response);
}

public static async Task<Me> GetMe(this Client client) {
string response = await Client.HttpClient.GetStringAsync(jsonUrl);
return JsonSerializer.Deserialize<Me>(response);
}
}
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# NWN.MasterList
# NWN.MasterList

<https://api.nwn.beamdog.net/v1/>
30 changes: 17 additions & 13 deletions Test/NWN.MasterList.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\API\NWN.MasterList.csproj" />
</ItemGroup>
</Project>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\API\NWN.MasterList.csproj" />
</ItemGroup>
</Project>
24 changes: 17 additions & 7 deletions Test/src/Client.Test.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
using Xunit;

namespace NWN.MasterList.Test {
public class Client {
[Fact]
public void GetServersTest() {
var connection = new MasterList.Client();
Assert.NotEmpty(connection.Servers);
namespace NWN.MasterList.Test
{
public class Client
{
[Fact]
public async void GetServersTest()
{
var connection = new MasterList.Client();
Assert.NotEmpty(await connection.GetServers());
}

[Fact]
public async void TestGetAllFirstSeen()
{
var connection = new MasterList.Client();
Assert.NotEmpty(await connection.GetAllFirstSeen());
}
}
}
}

0 comments on commit b2fd347

Please sign in to comment.