diff --git a/EDNA/Commander.cs b/EDNA/Commander.cs index 258b770..eecc684 100644 --- a/EDNA/Commander.cs +++ b/EDNA/Commander.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Collections.Generic; using System.Linq; @@ -32,26 +30,77 @@ namespace alterNERDtive.Edna /// public class Commander : Locatable { - /// /// Initializes a new instance of the class. /// /// The CMDR’s name. - public Commander(string name) - { - this.Name = name; - } + /// The CMDR’s EDSM API key. + private Commander(string name, string? edsmProfileUrl, DateTime? lastActiveAt, StarSystem? starsystem, Coordinates? coordinates) + => (this.Name, this.EdsmProfileUrl, this.LastActiveAt, this.StarSystem, this.Coordinates) = (name, edsmProfileUrl, lastActiveAt, starsystem, coordinates); /// /// Gets the CMDR’s name. /// public string Name { get; } - public string EdsmProfileUrl { get; private set; } + /// + /// Gets the CMDR’s EDSM profile URL. + /// + public string? EdsmProfileUrl { get; } - public DateTime LastActiveAt { get; private set; } + /// + /// Gets the CMDR’s date of last activity. + /// + public DateTime? LastActiveAt { get; } - public StarSystem StarSystem { get; private set; } + /// + /// Gets the CMDR’s current star system. + /// + public StarSystem? StarSystem { get; } - public new Coordinates Coordinates { get; private set; } + /// + /// Gets the CMDR’s current coordinates. + /// + public new Coordinates? Coordinates { get; } + + /// + /// Finds a CMDR by name. Optionally takes an EDSM API key to access a + /// private profile. + /// + /// The CMDR’s name. + /// The CMDR’s EDSM API key. + /// The CMDR. + public static Commander Find(string name, string? apiKey = null) + { + return FindAsync(name, apiKey).GetAwaiter().GetResult(); + } + + /// + /// + /// + /// + /// + /// + public static async Task FindAsync(string name, string? apiKey = null) + { + if (apiKey != null) + { + throw new NotImplementedException(); + } + + try + { + Edsm.ApiCmdr cmdr = await Edsm.LogsApi.FindCmdr(name); + + return new Commander(name, cmdr.Url, DateTime.Parse(cmdr.DateLastActivity), StarSystem.Find(cmdr.SystemId64!.Value), new Coordinates(cmdr.Coordinates!.Value)); + } + catch (ArgumentException e) + { + throw new CommanderNotFoundException(e.Message, e); + } + catch (AccessViolationException e) + { + throw new CommanderHiddenException(e.Message, e); + } + } } } diff --git a/EDNA/EDNA.csproj b/EDNA/EDNA.csproj index fa6e1d4..8a1223a 100644 --- a/EDNA/EDNA.csproj +++ b/EDNA/EDNA.csproj @@ -1,7 +1,8 @@  - net48;net5.0 + net48;net6.0 + enable alterNERDtive.EDNA 0.0.1 alterNERDtive diff --git a/EDNA/Exceptions.cs b/EDNA/Exceptions.cs new file mode 100644 index 0000000..c135586 --- /dev/null +++ b/EDNA/Exceptions.cs @@ -0,0 +1,79 @@ +// +// Copyright 2021–2022 alterNERDtive. +// +// This file is part of EDNA. +// +// EDNA is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// EDNA is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with EDNA. If not, see <https://www.gnu.org/licenses/>. +// + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace alterNERDtive.Edna +{ + public class SystemNotFoundException : Exception + { + public SystemNotFoundException(string message) + : base(message) + { + } + } + + public class CommanderNotFoundException : Exception + { + public string? Name { get; private set; } + + public CommanderNotFoundException() + : base() + { + } + + public CommanderNotFoundException(string message) + : base(message) + { + } + + public CommanderNotFoundException(string message, Exception innerException) + :base(message, innerException) + { + } + + public CommanderNotFoundException(string message, ArgumentException innerException) + : base(message, innerException) + { + this.Name = innerException.ParamName; + } + } + + public class CommanderHiddenException : Exception + { + public CommanderHiddenException() + : base() + { + } + + public CommanderHiddenException(string message) + : base(message) + { + } + + public CommanderHiddenException(string message, Exception innerException) + : base(message, innerException) + { + } + } +} diff --git a/EDNA/Locatable.cs b/EDNA/Locatable.cs index 0d7ad21..d10c9a7 100644 --- a/EDNA/Locatable.cs +++ b/EDNA/Locatable.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; namespace alterNERDtive.Edna @@ -65,6 +63,14 @@ namespace alterNERDtive.Edna => (this.X, this.Y, this.Z, this.Precision) = (edsmSystem.Coords.Value.X, edsmSystem.Coords.Value.Y, edsmSystem.Coords.Value.Z, 0); + /// + /// Initializes a new instance of the struct. + /// + /// A set of EDSM coordinates to convert. + public Coordinates(Edsm.Coordinates edsmCoords) + => (this.X, this.Y, this.Z, this.Precision) + = (edsmCoords.X, edsmCoords.Y, edsmCoords.Z, 0); + /// /// Gets the x coordinate. /// diff --git a/EDNA/StarSystem.cs b/EDNA/StarSystem.cs index f356d0b..6e233d3 100644 --- a/EDNA/StarSystem.cs +++ b/EDNA/StarSystem.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/EDNA/StarSystems.cs b/EDNA/StarSystems.cs index 76da735..c97658c 100644 --- a/EDNA/StarSystems.cs +++ b/EDNA/StarSystems.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Collections.Generic; using System.Linq; diff --git a/EDNA/Station.cs b/EDNA/Station.cs index 8e8faff..c11dc5c 100644 --- a/EDNA/Station.cs +++ b/EDNA/Station.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Collections.Generic; using System.Linq; diff --git a/EDNA/Stations.cs b/EDNA/Stations.cs index 9787db7..858c6b5 100644 --- a/EDNA/Stations.cs +++ b/EDNA/Stations.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Collections.Generic; diff --git a/EDSM/EDSM.csproj b/EDSM/EDSM.csproj index 5277439..3f26ccb 100644 --- a/EDSM/EDSM.csproj +++ b/EDSM/EDSM.csproj @@ -1,11 +1,12 @@  - net48;net5.0 + net48;net6.0 + enable - + diff --git a/EDSM/EdsmApi.cs b/EDSM/EdsmApi.cs index 9b0b27c..6d3566d 100644 --- a/EDSM/EdsmApi.cs +++ b/EDSM/EdsmApi.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - namespace alterNERDtive.Edna.Edsm { /// diff --git a/EDSM/LogsApi.cs b/EDSM/LogsApi.cs index 7c76e53..51fa2da 100644 --- a/EDSM/LogsApi.cs +++ b/EDSM/LogsApi.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Threading.Tasks; using RestSharp; @@ -42,7 +40,7 @@ namespace alterNERDtive.Edna.Edsm /// Gets or sets the API’s status message; see /// https://www.edsm.net/en/api-logs-v1 for details. /// - public string Message { get; set; } + public string Msg { get; set; } /// /// Gets or sets the commander’s current system. Will be “null” if the @@ -61,7 +59,7 @@ namespace alterNERDtive.Edna.Edsm /// current system. Will be null if the commander’s flight log /// timestamps are hidden. /// - public DateTime? Date { get; set; } + public string? Date { get; set; } /// /// Gets or sets EDSM’s internal ID of the commander’s current system. @@ -99,7 +97,7 @@ namespace alterNERDtive.Edna.Edsm /// Gets or sets the date time of docking at the commander’s currently /// docked station. /// - public DateTime? DateDocked { get; set; } + public string? DateDocked { get; set; } /// /// Gets or sets the ship ID of the commander’s current ship. That is @@ -122,7 +120,7 @@ namespace alterNERDtive.Edna.Edsm /// Gets or sets the date and time of the commander’s last recorded /// activity. Will be “null” if the commander’s flight log is hidden. /// - public DateTime? DateLastActivity { get; set; } + public string? DateLastActivity { get; set; } /// /// Gets or sets the commander’s EDSM profile URL. Will be “null” if the @@ -132,7 +130,7 @@ namespace alterNERDtive.Edna.Edsm } /// - /// + /// Pulls data about CMDRs from the EDSM Logs API. /// public class LogsApi { @@ -140,16 +138,16 @@ namespace alterNERDtive.Edna.Edsm private static readonly RestClient ApiClient = new RestClient(ApiUrl); /// - /// + /// Pulls data about a single CMDR from the EDSM Logs API. /// - /// - /// + /// The CMDR’s name. + /// The CMDR’s EDSM API key. /// A representing the result of the asynchronous operation. public static async Task FindCmdr(string name, string? apiKey = null) { RestRequest request = new RestRequest("get-position") .AddQueryParameter("commanderName", name) - .AddQueryParameter("showID", 1) + .AddQueryParameter("showId", 1) .AddQueryParameter("showCoordinates", 1); if (apiKey != null) @@ -161,7 +159,7 @@ namespace alterNERDtive.Edna.Edsm if (response.MsgNum == 203) { - throw new ArgumentException($"Cmdr “{name}” not found{(apiKey == null ? string.Empty : " and/or invalid API key")}."); + throw new ArgumentException($"Cmdr not found{(apiKey == null ? string.Empty : " and/or invalid API key")}.", name); } else if (response.MsgNum == 100 && response.System == null && response.FirstDiscover == null && response.Date == null) { diff --git a/EDSM/SystemsApi.cs b/EDSM/SystemsApi.cs index 19dae60..d7668d0 100644 --- a/EDSM/SystemsApi.cs +++ b/EDSM/SystemsApi.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/EDTS/EDTS.csproj b/EDTS/EDTS.csproj index a34c443..3f26ccb 100644 --- a/EDTS/EDTS.csproj +++ b/EDTS/EDTS.csproj @@ -1,12 +1,13 @@  - net48;net5.0 + net48;net6.0 + enable - - + + diff --git a/EDTS/EdtsApi.cs b/EDTS/EdtsApi.cs index b432399..5948148 100644 --- a/EDTS/EdtsApi.cs +++ b/EDTS/EdtsApi.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Threading.Tasks; diff --git a/Spansh/Spansh.csproj b/Spansh/Spansh.csproj index b7bd77e..9328a7f 100644 --- a/Spansh/Spansh.csproj +++ b/Spansh/Spansh.csproj @@ -1,11 +1,12 @@  - net48;net5.0 + net48;net6.0 + enable - + diff --git a/Spansh/SpanshApi.cs b/Spansh/SpanshApi.cs index 34567c6..4ed9ca3 100644 --- a/Spansh/SpanshApi.cs +++ b/Spansh/SpanshApi.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Collections.Generic; using System.Linq; diff --git a/Test/EDNA/CommanderTest.cs b/Test/EDNA/CommanderTest.cs new file mode 100644 index 0000000..a1916d0 --- /dev/null +++ b/Test/EDNA/CommanderTest.cs @@ -0,0 +1,65 @@ +// +// Copyright 2021–2022 alterNERDtive. +// +// This file is part of EDNA. +// +// EDNA is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// EDNA is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with EDNA. If not, see <https://www.gnu.org/licenses/>. +// + +using System; + +using alterNERDtive.Edna; +using Xunit; + +namespace Test.Edna +{ + /// + /// Tests getting CMDR data correctly from the EDSM API. + /// + public class CommanderTest + { + /// + /// Tests getting data correctly for a known CMDR. + /// + [Fact] + public void KnownCmdrTest() + { + Assert.Throws(() => Commander.Find("IHaveFuelYouDont", "sometext")); + Commander cmdr = Commander.Find("IHaveFuelYouDont"); + Assert.Equal(expected: new Coordinates(x: 25.40625, y: -31.0625, z: 41.625), actual: cmdr.Coordinates); + Assert.Equal(expected: "https://www.edsm.net/en/user/profile/id/86423/cmdr/IHaveFuelYouDont", actual: cmdr.EdsmProfileUrl); + Assert.IsType(cmdr.LastActiveAt); + Assert.Equal(expected: "IHaveFuelYouDont", actual: cmdr.Name); + Assert.Equal(expected: "Dromi", actual: cmdr.StarSystem!.Name); + } + + /// + /// Tests correctly getting a CmdrHiddenException for hidden CMDRs. + /// + [Fact] + public void HiddenCmdrTest() + { + Assert.Throws(() => Commander.Find("Hojothefool")); + } + + /// + /// Tests correctly getting a CmdrNotFoundException for nonexistent CMDRs. + /// + [Fact] + public void NonexistentCmdrTest() + { + Assert.Throws(() => Commander.Find("IHaveFuelYouDoButDontExistLOL")); + } + } +} diff --git a/Test/EDNA/DistanceTest.cs b/Test/EDNA/DistanceTest.cs index 0cf0414..198d452 100644 --- a/Test/EDNA/DistanceTest.cs +++ b/Test/EDNA/DistanceTest.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using alterNERDtive.Edna; using Xunit; diff --git a/Test/EDNA/LocationTest.cs b/Test/EDNA/LocationTest.cs index 99df222..9dea680 100644 --- a/Test/EDNA/LocationTest.cs +++ b/Test/EDNA/LocationTest.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using alterNERDtive.Edna; using Xunit; diff --git a/Test/EDNA/StarSystemTest.cs b/Test/EDNA/StarSystemTest.cs index cbd1954..7868b0b 100644 --- a/Test/EDNA/StarSystemTest.cs +++ b/Test/EDNA/StarSystemTest.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using alterNERDtive.Edna; using Xunit; diff --git a/Test/EDSM/LogsApiTest.cs b/Test/EDSM/LogsApiTest.cs index 6a11534..822c27f 100644 --- a/Test/EDSM/LogsApiTest.cs +++ b/Test/EDSM/LogsApiTest.cs @@ -17,17 +17,74 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using alterNERDtive.Edna.Edsm; +using Xunit; + namespace Test.Edsm { - class LogsApiTest + /// + /// Tests correctly getting data from EDSM’s Log API. + /// + public class LogsApiTest { + /// + /// Tests getting data correctly for a known CMDR. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task KnownCmdrTestAsync() + { + ApiCmdr cmdr = await LogsApi.FindCmdr("IHaveFuelYouDont"); + Assert.Equal(expected: 100, actual: cmdr.MsgNum); + Assert.Equal(expected: "OK", actual: cmdr.Msg); + Assert.Equal(expected: "Dromi", actual: cmdr.System); + Assert.Equal(expected: false, actual: cmdr.FirstDiscover); + _ = DateTime.Parse(cmdr.Date); + Assert.Equal(expected: 38324688UL, actual: cmdr.SystemId); + Assert.Equal(expected: 1213084977515UL, actual: cmdr.SystemId64); + Assert.Equal(expected: new Coordinates { X = 25.40625, Y = -31.0625, Z = 41.625 }, actual: cmdr.Coordinates); + Assert.IsType(cmdr.IsDocked); + Assert.IsType(cmdr.ShipId); + Assert.Equal(expected: "Anaconda", actual: cmdr.ShipType); + Assert.Null(cmdr.ShipFuel); + _ = DateTime.Parse(cmdr.DateLastActivity); + Assert.Equal(expected: "https://www.edsm.net/en/user/profile/id/86423/cmdr/IHaveFuelYouDont", actual: cmdr.Url); + } + + /// + /// Tests correctly getting an ArgumentException for wrong API key. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task WrongApiKeyTestAsync() + { + await Assert.ThrowsAsync(() => LogsApi.FindCmdr("IHaveFuelYouDont", "sometext")); + } + + /// + /// Tests correctly getting an AccessViolationException for hidden CMDRs. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task HiddenCmdrTestAsync() + { + await Assert.ThrowsAsync(() => LogsApi.FindCmdr("Hojothefool")); + } + + /// + /// Tests correctly getting an Argumentexception for nonexistent CMDRs. + /// + /// A representing the asynchronous unit test. + [Fact] + public async Task NonexistentCmdrTestAsync() + { + await Assert.ThrowsAsync(() => LogsApi.FindCmdr("IHaveFuelYouDoButDontExistLOL")); + } } } diff --git a/Test/EDSM/SystemsApiTest.cs b/Test/EDSM/SystemsApiTest.cs index e426664..92d665b 100644 --- a/Test/EDSM/SystemsApiTest.cs +++ b/Test/EDSM/SystemsApiTest.cs @@ -17,8 +17,6 @@ // along with EDNA. If not, see <https://www.gnu.org/licenses/>. // -#nullable enable - #pragma warning disable SA1615 // Element return value should be documented using System; diff --git a/Test/EDTS/LocationTest.cs b/Test/EDTS/LocationTest.cs index f93cdb1..95fb41a 100644 --- a/Test/EDTS/LocationTest.cs +++ b/Test/EDTS/LocationTest.cs @@ -20,8 +20,6 @@ #pragma warning disable SA1615 // Element return value should be documented #pragma warning disable SA1201 // Elements should appear in the correct order -#nullable enable - using System; using System.Collections.Generic; using System.Threading.Tasks; diff --git a/Test/Test.csproj b/Test/Test.csproj index 1236766..7b81bc0 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -1,16 +1,17 @@  - net48;net5.0 + net48;net6.0 + enable false - - + + - + runtime; build; native; contentfiles; analyzers; buildtransitive all