//
// 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.Threading.Tasks;
using RestSharp;
namespace alterNERDtive.Edna.Edsm
{
///
/// A Commander in the galaxy of Elite Dangerous, as returned from EDSM’s
/// “Logs” API.
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:File name should match first type name", Justification = "Either this or wrong class/struct order 🤷")]
public struct ApiCmdr
{
///
/// Gets or sets EDSM’s internal message code for the request; see
/// https://www.edsm.net/en/api-logs-v1 for details.
///
public int MsgNum { get; set; }
///
/// Gets or sets the API’s status message; see
/// https://www.edsm.net/en/api-logs-v1 for details.
///
public string Msg { get; set; }
///
/// Gets or sets the commander’s current system. Will be “null” if the
/// commander’s flight log is hidden.
///
public string? System { get; set; }
///
/// Gets or sets whether the commander has first discovered their
/// current system.
///
public bool? FirstDiscover { get; set; }
///
/// Gets or sets the date and time of the commander’s jump into the
/// current system. Will be null if the commander’s flight log
/// timestamps are hidden.
///
public string? Date { get; set; }
///
/// Gets or sets EDSM’s internal ID of the commander’s current system.
///
public ulong? SystemId { get; set; }
///
/// Gets or sets the ID64 of the commander’s current system.
///
public ulong? SystemId64 { get; set; }
///
/// Gets or sets the coordinates of the commander’s current system. Will
/// be null if the commander’s flight log is hidden.
///
public Coordinates? Coordinates { get; set; }
///
/// Gets or sets whether the commander is currently docked to a station.
///
public bool? IsDocked { get; set; }
///
/// Gets or sets the station the commander is currently docked at.
///
public string Station { get; set; }
///
/// Gets or sets EDSM’s internal ID of the station the commander is
/// currently docked at.
///
public int? StationId { get; set; }
///
/// Gets or sets the date time of docking at the commander’s currently
/// docked station.
///
public string? DateDocked { get; set; }
///
/// Gets or sets the ship ID of the commander’s current ship. That is
/// the slot ID of the ship, not the “ID” chosen in livery.
///
public int? ShipId { get; set; }
///
/// Gets or sets the ship type of the commander’s current ship.
///
public string ShipType { get; set; }
///
/// Gets or sets the fuel status of the commander’s current ship. Seems
/// to always be “null”.
///
public object ShipFuel { get; set; }
///
/// 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 string? DateLastActivity { get; set; }
///
/// Gets or sets the commander’s EDSM profile URL. Will be “null” if the
/// commander’s profile page is hidden.
///
public string Url { get; set; }
}
///
/// Pulls data about CMDRs from the EDSM Logs API.
///
public class LogsApi
{
private static readonly Uri ApiUrl = new Uri("https://www.edsm.net/api-logs-v1");
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("showCoordinates", 1);
if (apiKey != null)
{
request.AddQueryParameter("apiKey", apiKey);
}
ApiCmdr response = await ApiClient.GetAsync(request);
if (response.MsgNum == 203)
{
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)
{
throw new AccessViolationException($"Cmdr “{name}” has not made their profile and/or flight log public.");
}
return response;
}
}
}