EDTS: changed data model to allow for easy (de-)serialization

This commit is contained in:
alterNERDtive 2022-02-15 13:23:35 +01:00
parent 7de48c5c78
commit 0e22cfe872
2 changed files with 38 additions and 100 deletions

View file

@ -33,63 +33,29 @@ namespace alterNERDtive.Edna.Edts
public struct StarSystem
{
/// <summary>
/// Initializes a new instance of the <see cref="StarSystem"/> struct.
/// Gets or sets the systems name.
/// </summary>
/// <param name="name">The systems name.</param>
/// <param name="coordinates">The systems coordinates.</param>
public StarSystem(string name, Location coordinates)
public string Name { get; set; }
/// <summary>
/// Gets or sets the systems coordinates.
/// </summary>
public Coordinates Position { get; set; }
/// <summary>
/// Gets or sets the systems positional uncertainty in light years.
/// </summary>
public decimal Uncertainty { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1600:Elements should be documented", Justification = "Really? For this?")]
public struct Coordinates
{
(this.Name, this.Coordinates) = (name, coordinates);
public decimal X { get; set; }
public decimal Y { get; set; }
public decimal Z { get; set; }
}
/// <summary>
/// Gets the systems name.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the systems coordinates.
/// </summary>
public Location Coordinates { get; }
}
/// <summary>
/// A location in the galaxy, represented by coordinates and a value for
/// precision (all in ly).
/// </summary>
public struct Location
{
/// <summary>
/// Initializes a new instance of the <see cref="Location"/> struct.
/// </summary>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="z">The z coordinate.</param>
/// <param name="precision">The available precision.</param>
public Location(int x, int y, int z, int precision)
{
(this.X, this.Y, this.Z, this.Precision) = (x, y, z, precision);
}
/// <summary>
/// Gets the x coordinate.
/// </summary>
public int X { get; }
/// <summary>
/// Gets the y coordinate.
/// </summary>
public int Y { get; }
/// <summary>
/// Gets the z coordinate.
/// </summary>
public int Z { get; }
/// <summary>
/// Gets the precision to which the location can be calculated.
/// </summary>
public int Precision { get; private set; }
}
/// <summary>
@ -111,17 +77,8 @@ namespace alterNERDtive.Edna.Edts
{
try
{
ApiResult response = await ApiClient.GetAsync<ApiResult>(new RestRequest($"system_position/{name}"));
ApiResult.ApiSystem result = response.Result!.Value;
return new StarSystem(
name: name,
coordinates: new Location(
x: (int)result.Position.X,
y: (int)result.Position.Y,
z: (int)result.Position.Z,
precision: (int)result.Uncertainty));
ApiResult result = await ApiClient.GetAsync<ApiResult>(new RestRequest($"system_position/{name}"));
return result.Result!.Value;
}
catch (HttpRequestException e)
{
@ -140,25 +97,7 @@ namespace alterNERDtive.Edna.Edts
private struct ApiResult
{
public ApiSystem? Result { get; set; }
public struct ApiSystem
{
public string Name { get; set; }
public ApiLocation Position { get; set; }
public decimal Uncertainty { get; set; }
public struct ApiLocation
{
public decimal X { get; set; }
public decimal Y { get; set; }
public decimal Z { get; set; }
}
}
public StarSystem? Result { get; set; }
}
}
}

View file

@ -39,8 +39,8 @@ namespace Test.Edts
/// </summary>
/// <param name="name">The system name.</param>
[Theory]
[InlineData("Oevasy SG-Y D0")]
[InlineData("Oevasy AB-C D0-1")]
[InlineData("Oevasy SG-Y d0")]
[InlineData("Oevasy AB-C d1-2")]
public async Task ProcGen_Valid(string name)
{
StarSystem system = await EdtsApi.FindSystem(name: name);
@ -53,9 +53,9 @@ namespace Test.Edts
/// </summary>
/// <param name="name">The system name.</param>
[Theory]
[InlineData("Ysaveo YG-S D0")]
[InlineData("Ysaveo YG-S d0")]
[InlineData("Oevasy SG-Y")]
[InlineData("Oevasy SG-Y D")]
[InlineData("Oevasy SG-Y d")]
[InlineData("Oevasy SG-Y 0")]
public async Task ProcGen_Invalid(string name)
{
@ -67,17 +67,16 @@ namespace Test.Edts
/// Pulls some systems from the EDTS API and checks if they still have
/// the previously known coordinates.
/// </summary>
/// <param name="name">The system name.</param>
/// <param name="coords">The expected coordinates.</param>
/// <param name="testSystem">The expected system/coordinates.</param>
[Theory]
[MemberData(nameof(Systems))]
public async Task ProcGen_Coordinates(string name, Location coords)
public async Task ProcGen_Coordinates(StarSystem testSystem)
{
StarSystem system = await EdtsApi.FindSystem(name: name);
Assert.Equal(expected: coords.X, actual: system.Coordinates.X);
Assert.Equal(expected: coords.Y, actual: system.Coordinates.Y);
Assert.Equal(expected: coords.Z, actual: system.Coordinates.Z);
Assert.Equal(expected: coords.Precision, actual: system.Coordinates.Precision);
StarSystem system = await EdtsApi.FindSystem(name: testSystem.Name);
Assert.Equal(expected: testSystem.Position.X, actual: system.Position.X);
Assert.Equal(expected: testSystem.Position.Y, actual: system.Position.Y);
Assert.Equal(expected: testSystem.Position.Z, actual: system.Position.Z);
Assert.Equal(expected: testSystem.Uncertainty, actual: system.Uncertainty);
}
/// <summary>
@ -86,10 +85,10 @@ namespace Test.Edts
public static IEnumerable<object[]> Systems =>
new List<object[]>
{
new object[] { "Oevasy SG-Y D0", new Location(x: -1465, y: 15, z: 65615, precision: 40) },
new object[] { "Lysoorb AA-A b0", new Location(x: -55, y: -15, z: 6625, precision: 10) },
new object[] { "Dryau Aowsy AB-C D1-234", new Location(x: 775, y: 1615, z: 18255, precision: 40) },
new object[] { "Dryau Aowsy DC-B A4-321", new Location(x: 1170, y: 400, z: 18180, precision: 5) },
new object[] { new StarSystem { Name = "Oevasy SG-Y D0", Position = new StarSystem.Coordinates { X = -1465, Y = 15, Z = 65615 }, Uncertainty = 40 } },
new object[] { new StarSystem { Name = "Lysoorb AA-A b0", Position = new StarSystem.Coordinates { X = -55, Y = -15, Z = 6625 }, Uncertainty = 10 } },
new object[] { new StarSystem { Name = "Dryau Aowsy AB-C D1-234", Position = new StarSystem.Coordinates { X = 775, Y = 1615, Z = 18255 }, Uncertainty = 40 } },
new object[] { new StarSystem { Name = "Dryau Aowsy DC-B A4-321", Position = new StarSystem.Coordinates { X = 1170, Y = 400, Z = 18180 }, Uncertainty = 5 } },
};
}
}