EDNA/Edna/Commander.cs

107 lines
3.7 KiB
C#
Raw Permalink Normal View History

2021-08-10 22:27:19 +02:00
// <copyright file="Commander.cs" company="alterNERDtive">
2022-05-29 13:34:13 +02:00
// Copyright 20212022 alterNERDtive.
2021-08-10 22:27:19 +02:00
//
// 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 &lt;https://www.gnu.org/licenses/&gt;.
// </copyright>
using System;
2021-08-10 15:29:20 +02:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace alterNERDtive.Edna
{
2021-08-10 22:27:19 +02:00
/// <summary>
/// A CMDR in the galaxy of Elite Dangerous.
/// </summary>
public class Commander : Locatable
2021-08-10 15:29:20 +02:00
{
2021-08-10 22:27:19 +02:00
/// Initializes a new instance of the <see cref="Commander"/> class.
/// </summary>
/// <param name="name">The CMDRs name.</param>
2022-05-29 17:33:16 +02:00
/// <param name="apiKey">The CMDRs EDSM API key.</param>
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);
2021-08-10 22:27:19 +02:00
/// <summary>
/// Gets the CMDRs name.
/// </summary>
public string Name { get; }
2022-05-29 17:33:16 +02:00
/// <summary>
/// Gets the CMDRs EDSM profile URL.
/// </summary>
public string? EdsmProfileUrl { get; }
2021-08-10 22:27:19 +02:00
2022-05-29 17:33:16 +02:00
/// <summary>
/// Gets the CMDRs date of last activity.
/// </summary>
public DateTime? LastActiveAt { get; }
/// <summary>
/// Gets the CMDRs current star system.
/// </summary>
public StarSystem? StarSystem { get; }
2021-08-10 22:27:19 +02:00
2022-05-29 17:33:16 +02:00
/// <summary>
/// Gets the CMDRs current coordinates.
/// </summary>
public new Coordinates? Coordinates { get; }
2021-08-10 22:27:19 +02:00
2022-05-29 17:33:16 +02:00
/// <summary>
/// Finds a CMDR by name. Optionally takes an EDSM API key to access a
/// private profile.
/// </summary>
/// <param name="name">The CMDRs name.</param>
/// <param name="apiKey">The CMDRs EDSM API key.</param>
/// <returns>The CMDR.</returns>
public static Commander Find(string name, string? apiKey = null)
{
return FindAsync(name, apiKey).GetAwaiter().GetResult();
}
/// <summary>
///
/// </summary>
/// <param name="name"></param>
/// <param name="apiKey"></param>
/// <returns></returns>
public static async Task<Commander> 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);
}
}
2021-08-10 15:29:20 +02:00
}
}