Skip to content

Commit

Permalink
Allow the EDDN responder to determine whether to handle market, outfi…
Browse files Browse the repository at this point in the history
…tting, and shipyard events. Suppress once after a Frontier API update (to prevent doubling up messages right after docking).
  • Loading branch information
Tkael committed Nov 23, 2022
1 parent a7c260e commit fab217d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 41 deletions.
22 changes: 3 additions & 19 deletions EDDI/EDDI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ public class EDDI: INotifyPropertyChanged
private static bool started;
internal static bool running = true;

private static bool allowMarketUpdate = false;
private static bool allowOutfittingUpdate = false;
private static bool allowShipyardUpdate = false;

public bool inTelepresence { get; private set; } = false;

public bool inHorizons
Expand Down Expand Up @@ -1984,11 +1980,6 @@ private bool eventDocked(DockedEvent theEvent)
bool passEvent = !string.IsNullOrEmpty(theEvent.station);
updateCurrentSystem(theEvent.system);

// Upon docking, allow manual station updates once
allowMarketUpdate = true;
allowOutfittingUpdate = true;
allowShipyardUpdate = true;

Station station = CurrentStarSystem.stations.Find(s => s.name == theEvent.station);
if (Environment == Constants.ENVIRONMENT_DOCKED && CurrentStation?.marketId == station?.marketId)
{
Expand Down Expand Up @@ -2131,7 +2122,7 @@ private bool eventLiftoff(LiftoffEvent theEvent)
private bool eventMarket(MarketEvent theEvent)
{
// Don't proceed if we've already viewed the market while docked or when loading pre-existing logs
if (!allowMarketUpdate || theEvent.fromLoad) { return false; }
if (theEvent.fromLoad) { return false; }

// Don't proceed if the event data isn't what we expect
if (theEvent.system != CurrentStarSystem?.systemname) { return false; }
Expand All @@ -2145,7 +2136,6 @@ private bool eventMarket(MarketEvent theEvent)
// Update the current station commodities
if (CurrentStation != null && CurrentStation?.marketId == theEvent.marketId)
{
allowMarketUpdate = false;
CurrentStation.commodities = theEvent.info.Items.Select(q => q.ToCommodityMarketQuote()).ToList();
CurrentStation.commoditiesupdatedat = Dates.fromDateTimeToSeconds(theEvent.timestamp);

Expand Down Expand Up @@ -2174,7 +2164,7 @@ private bool eventMarket(MarketEvent theEvent)
private bool eventOutfitting(OutfittingEvent theEvent)
{
// Don't proceed if we've already viewed outfitting while docked or when loading pre-existing logs
if (!allowOutfittingUpdate || theEvent.fromLoad) { return false; }
if (theEvent.fromLoad) { return false; }

// Don't proceed if the event data isn't what we expect
if (theEvent.system != CurrentStarSystem?.systemname) { return false; }
Expand All @@ -2189,7 +2179,6 @@ private bool eventOutfitting(OutfittingEvent theEvent)
// Update the current station outfitting
if (CurrentStation?.marketId != null && CurrentStation?.marketId == theEvent.marketId)
{
allowOutfittingUpdate = false;
CurrentStation.outfitting = modules;
CurrentStation.outfittingupdatedat = Dates.fromDateTimeToSeconds(theEvent.info.timestamp);

Expand Down Expand Up @@ -2218,7 +2207,7 @@ private bool eventOutfitting(OutfittingEvent theEvent)
private bool eventShipyard(ShipyardEvent theEvent)
{
// Don't proceed if we've already viewed outfitting while docked or when loading pre-existing logs
if (!allowShipyardUpdate || theEvent.fromLoad) { return false; }
if (theEvent.fromLoad) { return false; }

// Don't proceed if the event data isn't what we expect
if (theEvent.system != CurrentStarSystem?.systemname) { return false; }
Expand All @@ -2233,7 +2222,6 @@ private bool eventShipyard(ShipyardEvent theEvent)
if (CurrentStation?.marketId != null && CurrentStation?.marketId == theEvent.marketId)
{
// Update the current station shipyard
allowShipyardUpdate = false;
CurrentStation.shipyard = ships;
CurrentStation.shipyardupdatedat = Dates.fromDateTimeToSeconds(theEvent.info.timestamp);

Expand Down Expand Up @@ -3319,10 +3307,6 @@ private void conditionallyRefreshStationProfile()
"Star system information updated from Frontier API server; updating local copy");
CurrentStation = station;
StarSystemSqLiteRepository.Instance.SaveStarSystem(CurrentStarSystem);

allowMarketUpdate = !(profileStation.eddnCommodityMarketQuotes?.Count > 0);
allowOutfittingUpdate = !(profileStation.outfitting?.Count > 0);
allowShipyardUpdate = !(profileStation.ships?.Count > 0);
}
}
catch (Exception ex)
Expand Down
13 changes: 7 additions & 6 deletions EDDNResponder/Schemas/CommoditySchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ public bool Handle(string edType, ref IDictionary<string, object> data, EDDNStat
if (eddnState?.GameVersion is null) { return false; }

var marketID = JsonParsing.getLong(data, "MarketID");
if (lastSentMarketID == marketID) { return false; }
if (lastSentMarketID == marketID)
{
lastSentMarketID = null;
return false;
}

// Only send the message if we have commodities
if (data.TryGetValue("Items", out var commoditiesList) &&
commoditiesList is List<object> commodities &&
commodities.Any())
{
lastSentMarketID = marketID;

var handledData = new Dictionary<string, object>() as IDictionary<string, object>;
handledData["timestamp"] = data["timestamp"];
handledData["systemName"] = data["StarSystem"];
Expand All @@ -49,8 +51,8 @@ commoditiesList is List<object> commodities &&
// Apply data augments
handledData = eddnState.GameVersion.AugmentVersion(handledData);

EDDNSender.SendToEDDN("https://eddn.edcd.io/schemas/commodity/3", handledData, eddnState);
data = handledData;
EDDNSender.SendToEDDN("https://eddn.edcd.io/schemas/commodity/3", handledData, eddnState);
return true;
}
}
Expand Down Expand Up @@ -107,8 +109,6 @@ public IDictionary<string, object> Handle(JObject profileJson, JObject marketJso
// Continue if our commodities list is not empty
if (commodities.Any())
{
lastSentMarketID = marketID;

var data = new Dictionary<string, object>() as IDictionary<string, object>;
data.Add("timestamp", timestamp);
data.Add("systemName", systemName);
Expand All @@ -125,6 +125,7 @@ public IDictionary<string, object> Handle(JObject profileJson, JObject marketJso
data = eddnState.GameVersion.AugmentVersion(data);

EDDNSender.SendToEDDN("https://eddn.edcd.io/schemas/commodity/3", data, eddnState, "CAPI-market");
lastSentMarketID = marketID;
return data;
}
}
Expand Down
11 changes: 6 additions & 5 deletions EDDNResponder/Schemas/FCMaterialsSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public bool Handle(string edType, ref IDictionary<string, object> data, EDDNStat
if (!edTypes.Contains(edType)) { return false; }
if (eddnState?.GameVersion is null) { return false; }
var marketID = JsonParsing.getLong(data, "MarketID");
if (lastSentMarketID == marketID) return false;

lastSentMarketID = marketID;
if (lastSentMarketID == marketID)
{
lastSentMarketID = null;
return false;
}

// Strip localized names
data = eddnState.PersonalData.Strip(data);
Expand Down Expand Up @@ -61,8 +63,6 @@ public IDictionary<string, object> Handle(JObject profileJson, JObject marketJso

if (marketID != null && lastSentMarketID != marketID)
{
lastSentMarketID = marketID;

var items = marketJson["orders"]?["onfootmicroresources"]?.ToObject<JObject>();
var saleItems = items["sales"]?.ToObject<JToken>();
var purchaseItems = items["purchases"]?.ToObject<JToken>();
Expand All @@ -87,6 +87,7 @@ public IDictionary<string, object> Handle(JObject profileJson, JObject marketJso
data = eddnState.GameVersion.AugmentVersion(data);

EDDNSender.SendToEDDN("https://eddn.edcd.io/schemas/fcmaterials_capi/1", data, eddnState, "CAPI-market");
lastSentMarketID = marketID;
return data;
}
}
Expand Down
14 changes: 8 additions & 6 deletions EDDNResponder/Schemas/OutfittingSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ public bool Handle(string edType, ref IDictionary<string, object> data, EDDNStat
if (eddnState?.GameVersion is null) { return false; }

var marketID = JsonParsing.getLong(data, "MarketID");
if (lastSentMarketID != marketID && data.TryGetValue("Items", out var modulesList))
if (lastSentMarketID == marketID)
{
lastSentMarketID = null;
return false;
}

if (data.TryGetValue("Items", out var modulesList))
{
// Only send the message if we have modules
if (modulesList is List<object> modules && modules.Any())
{
lastSentMarketID = marketID;

var handledData = new Dictionary<string, object>() as IDictionary<string, object>;
handledData["timestamp"] = data["timestamp"];
handledData["systemName"] = data["StarSystem"];
Expand Down Expand Up @@ -87,9 +91,6 @@ public IDictionary<string, object> Handle(JObject profileJson, JObject marketJso
// Continue if our modules list is not empty
if (modules.Any())
{
lastSentMarketID = marketID;


var data = new Dictionary<string, object>() as IDictionary<string, object>;
data.Add("timestamp", timestamp);
data.Add("systemName", systemName);
Expand All @@ -101,6 +102,7 @@ public IDictionary<string, object> Handle(JObject profileJson, JObject marketJso
data = eddnState.GameVersion.AugmentVersion(data);

EDDNSender.SendToEDDN("https://eddn.edcd.io/schemas/outfitting/2", data, eddnState, "CAPI-shipyard");
lastSentMarketID = marketID;
return data;
}
}
Expand Down
13 changes: 8 additions & 5 deletions EDDNResponder/Schemas/ShipyardSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ public bool Handle(string edType, ref IDictionary<string, object> data, EDDNStat
if (eddnState?.GameVersion is null) { return false; }

var marketID = JsonParsing.getLong(data, "MarketID");
if (lastSentMarketID != marketID && data.TryGetValue("PriceList", out var shipsList))
if (lastSentMarketID == marketID)
{
lastSentMarketID = null;
return false;
}

if (data.TryGetValue("PriceList", out var shipsList))
{
// Only send the message if we have ships
if (shipsList is List<object> ships && ships.Any())
{
lastSentMarketID = marketID;

var handledData = new Dictionary<string, object>() as IDictionary<string, object>;
handledData["timestamp"] = data["timestamp"];
handledData["systemName"] = data["StarSystem"];
Expand Down Expand Up @@ -87,8 +91,6 @@ public IDictionary<string, object> Handle(JObject profileJson, JObject marketJso
// Continue if our ships list is not empty
if (ships.Any())
{
lastSentMarketID = marketID;

var data = new Dictionary<string, object>() as IDictionary<string, object>;
data.Add("timestamp", timestamp);
data.Add("systemName", systemName);
Expand All @@ -101,6 +103,7 @@ public IDictionary<string, object> Handle(JObject profileJson, JObject marketJso
data = eddnState.GameVersion.AugmentVersion(data);

EDDNSender.SendToEDDN("https://eddn.edcd.io/schemas/shipyard/2", data, eddnState, "CAPI-shipyard");
lastSentMarketID = marketID;
return data;
}
}
Expand Down

0 comments on commit fab217d

Please sign in to comment.