Skip to content

Commit

Permalink
Merge branch 'hotfix/722-eddn-commodities' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
richardbuckle committed Jul 20, 2018
2 parents 34343d9 + 4125393 commit 51d268b
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 3,415 deletions.
31 changes: 5 additions & 26 deletions CompanionAppService/CompanionAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,38 +680,17 @@ public static List<String> ProhibitedCommoditiesFromProfile(dynamic json)
}

// Obtain the list of commodities from the profile
private static List<CommodityMarketQuote> CommodityQuotesFromProfile(dynamic json)
private static List<CommodityMarketQuote> CommodityQuotesFromProfile(JObject json)
{
List<CommodityMarketQuote> quotes = new List<CommodityMarketQuote>();

if (json["lastStarport"] != null && json["lastStarport"]["commodities"] != null)
{
foreach (dynamic commodity in json["lastStarport"]["commodities"])
foreach (JObject commodityJSON in json["lastStarport"]["commodities"])
{
CommodityDefinition commodityDef = CommodityDefinition.CommodityDefinitionFromEliteID((long)commodity["id"]);
CommodityMarketQuote quote = new CommodityMarketQuote(commodityDef);
quote.buyprice = (int)commodity["buyPrice"];
quote.stock = (int)commodity["stock"];
quote.stockbracket = (int)commodity["stockBracket"];
quote.sellprice = commodity["sellPrice"] as int? ?? 0;
quote.demand = (int)commodity["demand"];
quote.demandbracket = commodity["demandBracket"] as int? ?? 0;

List<string> StatusFlags = new List<string>();
foreach (dynamic statusFlag in commodity["statusFlags"])
CommodityMarketQuote quote = CommodityMarketQuote.FromCapiJson(commodityJSON);
if (quote != null)
{
StatusFlags.Add((string)statusFlag);
}
quote.StatusFlags = StatusFlags;
quotes.Add(quote);

if (commodityDef == null || (string)commodity["name"] != commodityDef.edname)
{
if (commodityDef.edname != "Drones")
{
// Unknown commodity; report the full object so that we can update the definitions
Logging.Info("Commodity definition error: " + (string)commodity["name"], JsonConvert.SerializeObject(commodity));
}
quotes.Add(quote);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions DataDefinitions/CommodityDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ public CommodityDefinition() : this(0, null, "", Unknown)

private CommodityDefinition(long EliteID, long? EDDBID, string edname, CommodityCategory Category, int AveragePrice = 0, bool Rare = false) : base(edname, edname)
{
this.EliteID = EliteID;
this.EDDBID = EDDBID;
this.category = Category;
this.avgprice = AveragePrice;
Expand Down
44 changes: 35 additions & 9 deletions DataDefinitions/CommodityMarketQuote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Utilities;

namespace EddiDataDefinitions
{
Expand Down Expand Up @@ -61,15 +62,15 @@ public string name
}

// Per-station information
public int? buyprice { get; set; }
public int? stock { get; set; }
public int buyprice { get; set; }
public int stock { get; set; }

// StockBracket can contain the values 0, 1, 2, 3 or "" (yes, really) so needs to be dynamic
// VB: it can be an optional enum no prob
public dynamic stockbracket { get; set; }

public int? sellprice { get; set; }
public int? demand { get; set; }
public int sellprice { get; set; }
public int demand { get; set; }

// DemandBracket can contain the values 0, 1, 2, 3 or "" (yes, really) so needs to be dynamic
// VB: same type as stockbracket above
Expand All @@ -82,18 +83,43 @@ public string name
[Obsolete("Please use localizedName or InvariantName")]
public string category => definition?.category.localizedName;
public int? avgprice => definition?.avgprice;
public bool? rare => definition?.rare;
public bool rare => definition?.rare ?? false;

public CommodityMarketQuote(CommodityDefinition definition)
{
this.definition = definition;
buyprice = null;
stock = null;
stockbracket = "";
sellprice = null;
demand = null;
demandbracket = "";
StatusFlags = new List<string>();
}

public static CommodityMarketQuote FromCapiJson(JObject capiJSON)
{
CommodityDefinition commodityDef = CommodityDefinition.CommodityDefinitionFromEliteID((long)capiJSON["id"]);
if (commodityDef == null || (string)capiJSON["name"] != commodityDef.edname)
{
if (commodityDef.edname != "Drones")
{
// Unknown commodity; report the full object so that we can update the definitions
Logging.Info("Commodity definition error: " + (string)capiJSON["name"], JsonConvert.SerializeObject(capiJSON));
}
return null;
}
CommodityMarketQuote quote = new CommodityMarketQuote(commodityDef);
quote.buyprice = (int)capiJSON["buyPrice"];
quote.stock = (int)capiJSON["stock"];
quote.stockbracket = (int)capiJSON["stockBracket"];
quote.sellprice = (int)capiJSON["sellPrice"];
quote.demand = (int)capiJSON["demand"];
quote.demandbracket = (int)capiJSON["demandBracket"];

List<string> StatusFlags = new List<string>();
foreach (dynamic statusFlag in capiJSON["statusFlags"])
{
StatusFlags.Add((string)statusFlag);
}
quote.StatusFlags = StatusFlags;
return quote;
}
}
}
17 changes: 9 additions & 8 deletions DataProviderService/DataProviderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ public static StarSystem StarSystemFromEDDP(dynamic json, decimal? x, decimal? y
return StarSystem;
}

public static List<Station> StationsFromEDDP(string systemName, dynamic json)
public static List<Station> StationsFromEDDP(string systemName, JObject json)
{
List<Station> Stations = new List<Station>();

if (json["stations"] != null)
{
foreach (dynamic station in json["stations"])
foreach (JObject station in json["stations"])
{
Station Station = new Station();
Station.EDDBID = (long)station["id"];
Expand Down Expand Up @@ -160,19 +160,20 @@ public static List<Station> StationsFromEDDP(string systemName, dynamic json)
return Stations;
}

public static List<CommodityMarketQuote> CommodityQuotesFromEDDP(dynamic json)
public static List<CommodityMarketQuote> CommodityQuotesFromEDDP(JObject json)
{
var quotes = new List<CommodityMarketQuote>();
if (json["commodities"] != null)
{
foreach (dynamic commodity in json["commodities"])
foreach (JObject commodity in json["commodities"])
{
CommodityDefinition commodityDefinition = CommodityDefinition.FromName((string)commodity["name"]);
CommodityMarketQuote quote = new CommodityMarketQuote(commodityDefinition);
quote.buyprice = (int?)(long?)commodity["buy_price"];
quote.sellprice = (int?)(long?)commodity["sell_price"];
quote.demand = (int?)(long?)commodity["demand"];
quote.stock = (int?)(long?)commodity["supply"];
// Annoyingly, these double-casts seem to be necessary because the boxed type is `long`. A direct cast to `int?` always returns null.
quote.buyprice = (int?)(long?)commodity["buy_price"] ?? quote.buyprice;
quote.sellprice = (int?)(long?)commodity["sell_price"] ?? quote.sellprice;
quote.demand = (int?)(long?)commodity["demand"] ?? quote.demand;
quote.stock = (int?)(long?)commodity["supply"] ?? quote.stock;
quotes.Add(quote);
}
}
Expand Down
2 changes: 2 additions & 0 deletions EDDI/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Full details of the variables available for each noted event, and VoiceAttack in
* "rebuy" The rebuy value of the ship
* Revised `Insurance check` script to take advantage of the new ship "rebuy" property.
* Added variety to the `Ship targeted` script and made it less verbose, as it fires a lot in the heat of combat.
* EDDN responder
* Fixed an issue whereby incomplete commodity data could be sent to EDDN.
* VoiceAttack responder
* Dramatically reduced CPU load.

Expand Down
25 changes: 22 additions & 3 deletions EDDNResponder/EDDNCommodity.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
using System.Collections.Generic;
using EddiDataDefinitions;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace EDDNResponder
{
class EDDNCommodity
public class EDDNCommodity
{
// Schema reference: https://github.com/EDSM-NET/EDDN/blob/master/schemas/commodity-v3.0.json
public string name;
[JsonIgnore] // do not send until #731 is fixed https://github.com/EDCD/EDDI/issues/731
public int meanPrice;
public int buyPrice;
public int stock;
public dynamic stockBracket; // Possible values are 0, 1, 2, 3, or ""
public int sellPrice;
public int demand;
public dynamic demandBracket; // Possible values are 0, 1, 2, 3, or ""
public List<string> statusFlags;
public List<string> statusFlags = new List<string>();

public bool ShouldSerializestatusFlags()
{
// Don't serialize status flags if they are empty as the schema requires that if present they contain at least 1 element
return (statusFlags != null && statusFlags.Count > 0);
}

public EDDNCommodity(CommodityMarketQuote quote)
{
name = quote.definition.edname;
meanPrice = quote.definition.avgprice;
buyPrice = quote.buyprice;
stock = quote.stock;
stockBracket = quote.stockbracket;
sellPrice = quote.sellprice;
demand = quote.demand;
demandBracket = quote.demandbracket;
if (quote.StatusFlags.Count > 0)
{
statusFlags = quote.StatusFlags;
}
}
}
}
12 changes: 10 additions & 2 deletions EDDNResponder/EDDNEconomy.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
namespace EDDNResponder
using EddiDataDefinitions;

namespace EDDNResponder
{
class EDDNEconomy
{
public string name;
public decimal proportion;
public decimal proportion = 0M;

public EDDNEconomy(CompanionAppEconomy companionAppEconomy)
{
name = companionAppEconomy.name;
proportion = companionAppEconomy.proportion;
}
}
}
24 changes: 5 additions & 19 deletions EDDNResponder/EDDNResponder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,37 +206,23 @@ private void sendCommodityInformation()
{
foreach (CompanionAppEconomy economy in EDDI.Instance.CurrentStation.economies)
{
EDDNEconomy eddnEconomy = new EDDNEconomy();
eddnEconomy.name = economy.name;
eddnEconomy.proportion = economy.proportion;
EDDNEconomy eddnEconomy = new EDDNEconomy(economy);
eddnEconomies.Add(eddnEconomy);
}
}

List<EDDNCommodity> eddnCommodities = new List<EDDNCommodity>();
foreach (CommodityMarketQuote commodity in EDDI.Instance.CurrentStation.commodities)
foreach (CommodityMarketQuote quote in EDDI.Instance.CurrentStation.commodities)
{
if (commodity.definition == null)
if (quote.definition == null)
{
continue;
}
if (commodity.definition.category == CommodityCategory.NonMarketable)
if (quote.definition.category == CommodityCategory.NonMarketable)
{
continue;
}
EDDNCommodity eddnCommodity = new EDDNCommodity();
eddnCommodity.name = commodity.definition.edname;
eddnCommodity.meanPrice = commodity.definition.avgprice;
eddnCommodity.buyPrice = commodity.buyprice ?? 0;
eddnCommodity.stock = commodity.stock ?? 0;
eddnCommodity.stockBracket = commodity.stockbracket;
eddnCommodity.sellPrice = commodity.sellprice ?? 0;
eddnCommodity.demand = commodity.demand ?? 0;
eddnCommodity.demandBracket = commodity.demandbracket;
if (commodity.StatusFlags.Count > 0)
{
eddnCommodity.statusFlags = commodity.StatusFlags;
}
EDDNCommodity eddnCommodity = new EDDNCommodity(quote);
eddnCommodities.Add(eddnCommodity);
};

Expand Down
Loading

0 comments on commit 51d268b

Please sign in to comment.