Skip to content

Commit

Permalink
Added Spansh trade tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Somfic committed Aug 10, 2023
1 parent 09e608e commit c5b1d2c
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 16 deletions.
57 changes: 57 additions & 0 deletions EliteAPI.Web.Spansh/RoutePlanner/Requests/TradeRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using EliteAPI.Web.Attributes;
using EliteAPI.Web.Models;

namespace EliteAPI.Web.Spansh.RoutePlanner.Requests;

public class TradeRequest : IWebApiRequest
{
public TradeRequest(string startingSystem, string startingStation, int range, int capacity)
{
StartingSystem = startingSystem;
StartingStation = startingStation;
JumpRange = range;
CargoCapacity = capacity;
}

[QueryParameter("system")]
public string StartingSystem { get; init; }

[QueryParameter("station")]
public string StartingStation { get; init; }

[QueryParameter("starting_capital")]
public int StartingCapital { get; init; } = 100_000_000;

[QueryParameter("max_hop_distance")]
public int JumpRange { get; init; }

[QueryParameter("max_cargo")]
public int CargoCapacity { get; init; }

[QueryParameter("max_hops")]
public int AmountOfStops { get; init; } = 5;

[QueryParameter("max_system_distance")]
public int MaxDistanceToArrival { get; init; } = 10_000;

[QueryParameter("max_price_age")]
public int MaxPriceAge { get; init; } = 60 * 60 * 24 * 7; // 7 days

[QueryParameter("requires_large_pad")]
public bool RequiresLargePad { get; init; } = false;

[QueryParameter("allow_planetary")]
public bool AllowPlanetary { get; init; } = false;

[QueryParameter("allow_prohibited")]
public bool AllowProhibited { get; init; } = false;

[QueryParameter("permit")]
public bool AllowPermitSystems { get; init; } = false;

[QueryParameter("unique")]
public bool AvoidLoops { get; init; } = false;

public string Endpoint => "trade/route";
public HttpMethod Method => HttpMethod.Post;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace EliteAPI.Web.Spansh.RoutePlanner.Responses;

public class NeutronResponse : IJobResponse
public class NeutronResponse
{
[JsonProperty("destination_system")]
public string DestinationSystem { get; init; }
Expand Down
67 changes: 67 additions & 0 deletions EliteAPI.Web.Spansh/RoutePlanner/Responses/TradeResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using EliteAPI.Web.Models;
using Newtonsoft.Json;

namespace EliteAPI.Web.Spansh.RoutePlanner.Responses;

public class TradeResponse : IWebApiResponse
{
[JsonProperty("commodities")] public IReadOnlyCollection<Commodity> Commodities { get; set; }

[JsonProperty("cumulative_profit")] public long CumulativeProfit { get; set; }

[JsonProperty("destination")]public SystemInfo Destination { get; set; }

[JsonProperty("distance")] public double Distance { get; set; }

[JsonProperty("source")] public SystemInfo System { get; set; }

[JsonProperty("total_profit")] public long TotalProfit { get; set; }

public class Commodity
{
[JsonProperty("amount")] public long Amount { get; set; }

[JsonProperty("destination_commodity")]
public TradeCommodity DestinationCommodity { get; set; }

[JsonProperty("name")] public string Name { get; set; }

[JsonProperty("profit")] public long Profit { get; set; }

[JsonProperty("source_commodity")] public TradeCommodity SourceCommodity { get; set; }

[JsonProperty("total_profit")] public long TotalProfit { get; set; }
}

public class TradeCommodity
{
[JsonProperty("buy_price")] public long BuyPrice { get; set; }

[JsonProperty("demand")] public long Demand { get; set; }

[JsonProperty("sell_price")] public long SellPrice { get; set; }

[JsonProperty("supply")] public long Supply { get; set; }
}

public class SystemInfo
{
[JsonProperty("distance_to_arrival")] public long DistanceToArrival { get; set; }

[JsonProperty("market_id")] public long MarketId { get; set; }

[JsonProperty("market_updated_at")] public long MarketUpdatedAt { get; set; }

[JsonProperty("station")] public string Station { get; set; }

[JsonProperty("system")] public string Name { get; set; }

[JsonProperty("system_id64")] public long Id { get; set; }

[JsonProperty("x")] public double X { get; set; }

[JsonProperty("y")] public double Y { get; set; }

[JsonProperty("z")] public double Z { get; set; }
}
}
7 changes: 7 additions & 0 deletions EliteAPI.Web.Spansh/RoutePlanner/RoutePlannerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@ public async Task<Result<NeutronResponse>> Neutron(NeutronRequest request)

return await api.Utilities.FromJob<NeutronResponse>(job);
}

public async Task<Result<IReadOnlyCollection<TradeResponse>>> Trade(TradeRequest request)
{
var api = Api as SpanshApi;
var job = await Execute<TradeRequest, JobResponse>(request);
return await api.Utilities.FromJob<IReadOnlyCollection<TradeResponse>>(job);
}

}
12 changes: 0 additions & 12 deletions EliteAPI.Web.Spansh/SpanshApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,4 @@ public SpanshApi(IServiceProvider services) : base(services)
public RoutePlannerApi Routes { get; }

internal UtilitiesApi Utilities { get; }
}

public interface IJobResponse : IWebApiResponse
{
[JsonProperty("job")]
public string Job { get; init; }
}

public interface IJobRequest : IWebApiRequest
{
[JsonProperty("job")]
public string Job { get; init; }
}
2 changes: 1 addition & 1 deletion EliteAPI.Web.Spansh/Utilities/Requests/JobRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace EliteAPI.Web.Spansh.Utilities.Requests;

public class JobRequest : IJobRequest
public class JobRequest : IWebApiRequest
{
public JobRequest(string job)
{
Expand Down
4 changes: 2 additions & 2 deletions EliteAPI.Web.Spansh/Utilities/UtilitiesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ internal async Task<WebApiResponse<WebApiResult<JobResponse<T>>>> Job<T>(JobRequ
}
}

internal async Task<Result<TResponse>> FromJob<TResponse>(WebApiResponse<WebApiResult<JobResponse>> job) where TResponse : class, IJobResponse
internal async Task<Result<TResponse>> FromJob<TResponse>(WebApiResponse<WebApiResult<JobResponse>> job) where TResponse : class
{
var api = Api as SpanshApi;

var result = await job.MapAsync(
ok: x => api.Utilities.Job<NeutronResponse>(new JobRequest(x.Content.Job)),
ok: x => api.Utilities.Job<TResponse>(new JobRequest(x.Content.Job)),
error: x => x
);

Expand Down

0 comments on commit c5b1d2c

Please sign in to comment.