From c6cd57f4abf1636c97cd7dec65800b53462a879f Mon Sep 17 00:00:00 2001 From: Jos Demmers <8627157+josdemmers@users.noreply.github.com> Date: Tue, 29 Oct 2024 17:30:18 +0100 Subject: [PATCH] Price data is now using https://nwmp.gaming.tools instead of https://nwmarketprices.com. --- NewWorldCompanion.Entities/Nwmarketprice.cs | 38 ++++ .../NwmarketpriceJson.cs | 87 -------- NewWorldCompanion.Entities/ServerPriceData.cs | 45 ++++ NewWorldCompanion.Events/PriceServerEvents.cs | 4 + NewWorldCompanion.Interfaces/IPriceManager.cs | 5 +- NewWorldCompanion.Services/OverlayHandler.cs | 39 ++-- NewWorldCompanion.Services/PriceManager.cs | 211 +++++++----------- .../Tabs/Config/ConfigOverlayViewModel.cs | 1 + .../ViewModels/Tabs/CraftingViewModel.cs | 38 ++-- NewWorldCompanion/common.props | 4 +- 10 files changed, 208 insertions(+), 264 deletions(-) create mode 100644 NewWorldCompanion.Entities/Nwmarketprice.cs delete mode 100644 NewWorldCompanion.Entities/NwmarketpriceJson.cs create mode 100644 NewWorldCompanion.Entities/ServerPriceData.cs diff --git a/NewWorldCompanion.Entities/Nwmarketprice.cs b/NewWorldCompanion.Entities/Nwmarketprice.cs new file mode 100644 index 0000000..3f55b4d --- /dev/null +++ b/NewWorldCompanion.Entities/Nwmarketprice.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using System.Threading.Tasks; +using System.Windows; + +namespace NewWorldCompanion.Entities +{ + public class Nwmarketprice + { + public string ItemId { get; set; } = string.Empty; + public string ItemName { get; set; } = string.Empty; + + public int Days { get; set; } = 0; + public DateTime LastUpdated { get; set; } = DateTime.MinValue; + public string LastUpdatedString + { + get + { + return LastUpdated.ToString(); + } + } + /// + /// Lowest price seen in the most recent scan compared to the lowest price seen the previous day + /// + public int PriceChange { get; set; } + public double RecentLowestPrice { get; set; } = 0.0; + /// + /// RollingAverage + /// + public double RecentLowestPriceAvg { get; set; } = 0.0; + } +} diff --git a/NewWorldCompanion.Entities/NwmarketpriceJson.cs b/NewWorldCompanion.Entities/NwmarketpriceJson.cs deleted file mode 100644 index c084587..0000000 --- a/NewWorldCompanion.Entities/NwmarketpriceJson.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Text; -using System.Text.Json; -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; -using System.Threading.Tasks; -using System.Windows; - -namespace NewWorldCompanion.Entities -{ - public class NwmarketpriceJson - { - public string item_name { get; set; } = string.Empty; - public string nwdb_id { get; set; } = string.Empty; - public DateTime last_checked { get; set; } = DateTime.MinValue; - public double recent_lowest_price { get; set; } = 0.0; - public List price_graph_data { get; set; } = new List(); - /// - /// Lowest price seen in the most recent scan compared to the lowest price seen the previous day - /// - public int price_change { get; set; } - - [JsonIgnore] - public string last_checked_string - { - get - { - return last_checked.ToString(); - } - } - - [JsonIgnore] - public string RecentLowestPriceAvg - { - get - { - string price = string.Empty; - - try - { - if (price_graph_data.Count() > 0) - { - GrapData priceData = price_graph_data.Last(); - price = priceData.RollingAverage.ToString("F2"); - } - } - catch (Exception) { } - - return price; - } - } - } - - public class GrapData - { - /* - // Is the total available of that day. All listings. - [JsonPropertyName("avail")] - public int Avail { get; set; } - [JsonPropertyName("date_only")] - public string DateOnly { get; set; } = string.Empty; - [JsonPropertyName("price_date")] - public DateTime PriceDate { get; set; } = DateTime.MinValue; - // Lowest price seen that day of any scan. - [JsonPropertyName("lowest_price")] - public double LowestPrice { get; set; } - // The available of the lowest price on the server that day. - [JsonPropertyName("single_price_avail")] - public int SinglePriceAvail { get; set; } - // Average price of the lowest 10 prices of the last scan. - [JsonPropertyName("avg_price")] - public double AvgPrice { get; set; } - // Average available of the lowest 10 prices of the last scan - [JsonPropertyName("avg_qty")] - public double AvgQty { get; set; } - */ - - /// - /// The exponential moving average of the lowest prices seen each day for the past 15 days. - /// - [JsonPropertyName("rolling_average")] - public double RollingAverage { get; set; } - } -} diff --git a/NewWorldCompanion.Entities/ServerPriceData.cs b/NewWorldCompanion.Entities/ServerPriceData.cs new file mode 100644 index 0000000..5368aea --- /dev/null +++ b/NewWorldCompanion.Entities/ServerPriceData.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace NewWorldCompanion.Entities +{ + public class ServerPriceData + { + [JsonPropertyName("server_id")] + public string ServerId { get; set; } = string.Empty; + + [JsonPropertyName("daily")] + public Dictionary> Daily { get; set; } = new(); + + [JsonPropertyName("hourly")] + public Dictionary> Hourly { get; set; } = new(); + } + + public class PriceData + { + [JsonPropertyName("timestamp")] + public int Timestamp { get; set; } = 0; + + [JsonPropertyName("min_price")] + public int MinPrice { get; set; } = 0; + + [JsonPropertyName("max_price")] + public int MaxPrice { get; set; } = 0; + + [JsonPropertyName("mean_price")] + public int MeanPrice { get; set; } = 0; + + [JsonPropertyName("median_price")] + public int MedianPrice { get; set; } = 0; + + [JsonPropertyName("quantity")] + public int Quantity { get; set; } = 0; + + [JsonPropertyName("means")] + public List> Means { get; set; } = new(); + } +} diff --git a/NewWorldCompanion.Events/PriceServerEvents.cs b/NewWorldCompanion.Events/PriceServerEvents.cs index 89623c2..696fff6 100644 --- a/NewWorldCompanion.Events/PriceServerEvents.cs +++ b/NewWorldCompanion.Events/PriceServerEvents.cs @@ -14,4 +14,8 @@ public class PriceServerListUpdatedEvent : PubSubEvent public class PriceCacheUpdatedEvent : PubSubEvent { } + + public class SelectedServerChanged : PubSubEvent + { + } } diff --git a/NewWorldCompanion.Interfaces/IPriceManager.cs b/NewWorldCompanion.Interfaces/IPriceManager.cs index ecbadd3..8784a8a 100644 --- a/NewWorldCompanion.Interfaces/IPriceManager.cs +++ b/NewWorldCompanion.Interfaces/IPriceManager.cs @@ -11,9 +11,8 @@ public interface IPriceManager { List Servers { get; } - NwmarketpriceJson GetPriceData(string itemName); + Nwmarketprice GetPriceData(string itemName); double GetCraftingCosts(string itemId); - List GetExtendedPriceData(string itemName); - void UpdatePriceData(string itemName); + List GetExtendedPriceData(string itemName); } } diff --git a/NewWorldCompanion.Services/OverlayHandler.cs b/NewWorldCompanion.Services/OverlayHandler.cs index f3eee3d..c46f8b2 100644 --- a/NewWorldCompanion.Services/OverlayHandler.cs +++ b/NewWorldCompanion.Services/OverlayHandler.cs @@ -140,25 +140,25 @@ private void DrawGraphics(object? sender, DrawGraphicsEventArgs e) private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName) { - NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(itemName); - List extendedNwmarketpriceJson = _priceManager.GetExtendedPriceData(itemName); + Nwmarketprice nwmarketprice = _priceManager.GetPriceData(itemName); + List extendedNwmarketprice = _priceManager.GetExtendedPriceData(itemName); string infoItemName = itemName; string infoPrice = "Loading..."; string infoPriceAvg = string.Empty; - if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name)) + if (!string.IsNullOrWhiteSpace(nwmarketprice.ItemName)) { - var priceChange = nwmarketpriceJson.price_change >= 0 ? $"+{nwmarketpriceJson.price_change}" : $"{nwmarketpriceJson.price_change}"; - infoPrice = $"{nwmarketpriceJson.recent_lowest_price.ToString("F2")} ({priceChange}%) ({nwmarketpriceJson.last_checked_string})"; - infoPriceAvg = nwmarketpriceJson.RecentLowestPriceAvg; - infoPriceAvg = string.IsNullOrWhiteSpace(infoPriceAvg) ? infoPriceAvg : $"{infoPriceAvg} (15-day avg) ({nwmarketpriceJson.last_checked_string})"; + var priceChange = nwmarketprice.PriceChange >= 0 ? $"+{nwmarketprice.PriceChange}" : $"{nwmarketprice.PriceChange}"; + infoPrice = $"{nwmarketprice.RecentLowestPrice.ToString("F2")} ({priceChange}%) ({nwmarketprice.LastUpdatedString})"; + infoPriceAvg = nwmarketprice.RecentLowestPriceAvg.ToString("F2"); + infoPriceAvg = string.IsNullOrWhiteSpace(infoPriceAvg) ? infoPriceAvg : $"{infoPriceAvg} ({nwmarketprice.Days}-day avg) ({nwmarketprice.LastUpdatedString})"; } // Do not show Bind on pickup items. if (!_newWorldDataStore.IsBindOnPickup(itemName)) { - int ExtendedTooltipOffset = _settingsManager.Settings.ExtendedTooltipEnabled ? Math.Min(extendedNwmarketpriceJson.Count, 3) * 20 : 0; + int ExtendedTooltipOffset = _settingsManager.Settings.ExtendedTooltipEnabled ? Math.Min(extendedNwmarketprice.Count, 3) * 20 : 0; // Add some extra margin ExtendedTooltipOffset = ExtendedTooltipOffset == 0 ? 0 : ExtendedTooltipOffset + 40; @@ -177,13 +177,13 @@ private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName) // Extended text if (ExtendedTooltipOffset > 0) { - for (int i = 0; i < Math.Min(extendedNwmarketpriceJson.Count, 3); i++) + for (int i = 0; i < Math.Min(extendedNwmarketprice.Count, 3); i++) { - var craftingCosts = _priceManager.GetCraftingCosts(extendedNwmarketpriceJson[i].nwdb_id); - gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, ((i+1)*20), $"{extendedNwmarketpriceJson[i].item_name} " + - $"{extendedNwmarketpriceJson[i].recent_lowest_price.ToString("F2")} (Sell) " + + var craftingCosts = _priceManager.GetCraftingCosts(extendedNwmarketprice[i].ItemId); + gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, ((i+1)*20), $"{extendedNwmarketprice[i].ItemName} " + + $"{extendedNwmarketprice[i].RecentLowestPrice.ToString("F2")} (Sell) " + $"{craftingCosts.ToString("F2")} (Craft) " + - $"{extendedNwmarketpriceJson[i].recent_lowest_price - craftingCosts:F2} (Profit)"); + $"{extendedNwmarketprice[i].RecentLowestPrice - craftingCosts:F2} (Profit)"); } gfx.DrawRectangle(_brushes["border"], 0, 0, _overlayWidth, ExtendedTooltipOffset, 1); } @@ -214,7 +214,7 @@ private void DrawGraphicsNamedItem(DrawGraphicsEventArgs e, string itemName) private void DrawGraphicsRecipe(DrawGraphicsEventArgs e, CraftingRecipe craftingRecipe) { - NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(craftingRecipe.LocalisationUserFriendly); + Nwmarketprice nwmarketprice = _priceManager.GetPriceData(craftingRecipe.LocalisationUserFriendly); bool learnedStatus = craftingRecipe.Learned; string infoItemName = craftingRecipe.LocalisationUserFriendly; @@ -222,12 +222,12 @@ private void DrawGraphicsRecipe(DrawGraphicsEventArgs e, CraftingRecipe crafting string infoPrice = "Loading..."; string infoPriceAvg = string.Empty; - if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name)) + if (!string.IsNullOrWhiteSpace(nwmarketprice.ItemName)) { - var priceChange = nwmarketpriceJson.price_change >= 0 ? $"+{nwmarketpriceJson.price_change}" : $"{nwmarketpriceJson.price_change}"; - infoPrice = $"{nwmarketpriceJson.recent_lowest_price.ToString("F2")} ({priceChange}%) ({nwmarketpriceJson.last_checked_string})"; - infoPriceAvg = nwmarketpriceJson.RecentLowestPriceAvg; - infoPriceAvg = string.IsNullOrWhiteSpace(infoPriceAvg) ? infoPriceAvg : $"{infoPriceAvg} (15-day avg) ({nwmarketpriceJson.last_checked_string})"; + var priceChange = nwmarketprice.PriceChange >= 0 ? $"+{nwmarketprice.PriceChange}" : $"{nwmarketprice.PriceChange}"; + infoPrice = $"{nwmarketprice.RecentLowestPrice.ToString("F2")} ({priceChange}%) ({nwmarketprice.LastUpdatedString})"; + infoPriceAvg = nwmarketprice.RecentLowestPriceAvg.ToString("F2"); + infoPriceAvg = string.IsNullOrWhiteSpace(infoPriceAvg) ? infoPriceAvg : $"{infoPriceAvg} (15-day avg) ({nwmarketprice.LastUpdatedString})"; } _window.X = _overlayX; @@ -273,7 +273,6 @@ private void HandleOcrTextReadyEvent() _itemName = _ocrHandler.OcrText; if (!_itemName.Equals(_itemNamePrevious)) { - _priceManager.UpdatePriceData(_itemName); _itemNamePrevious = _itemName; } } diff --git a/NewWorldCompanion.Services/PriceManager.cs b/NewWorldCompanion.Services/PriceManager.cs index ec9c47c..48174ae 100644 --- a/NewWorldCompanion.Services/PriceManager.cs +++ b/NewWorldCompanion.Services/PriceManager.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Linq; using System.Reflection; using System.Text.Json; @@ -26,12 +27,8 @@ public class PriceManager : IPriceManager private readonly INewWorldDataStore _newWorldDataStore; private readonly IRelatedPriceManager _relatedPriceManager; - private readonly object priceRequestLock = new object(); - - private Dictionary _priceCache = new Dictionary(); - private List _priceRequestQueue = new List(); - private bool _priceRequestQueueBusy = false; private List _servers = new List(); + private ServerPriceData _serverPriceData = new ServerPriceData(); // Start of Constructor region @@ -41,6 +38,7 @@ public PriceManager(IEventAggregator eventAggregator, ILogger logg { // Init IEventAggregator _eventAggregator = eventAggregator; + _eventAggregator.GetEvent().Subscribe(HandleSelectedServerChangedEvent); // Init logger _logger = logger; @@ -70,6 +68,37 @@ public PriceManager(IEventAggregator eventAggregator, ILogger logg #region Events + private async void HandleSelectedServerChangedEvent() + { + try + { + // Clear old data + _serverPriceData = new ServerPriceData(); + + string serverId = _settingsManager.Settings.PriceServerIdNwm; + PriceServer? server = _servers.FirstOrDefault(s => s.Id.Equals(serverId)); + + // Skip invalid servers + // Skip servers with no price data + if (server != null && server.Updated.Year > 1970) + { + string json = await _httpClientHandler.GetRequest($"https://scdn.gaming.tools/nwmp/history/servers/{server.Id}.json.gz") ?? "{}"; + + var options = new JsonSerializerOptions(); + options.Converters.Add(new BoolConverter()); + options.Converters.Add(new DoubleConverter()); + options.Converters.Add(new IntConverter()); + _serverPriceData = JsonSerializer.Deserialize(json, options) ?? new ServerPriceData(); + } + + _eventAggregator.GetEvent().Publish(); + } + catch (Exception ex) + { + _logger.LogError(ex, MethodBase.GetCurrentMethod()?.Name); + } + } + #endregion // Start of Methods region @@ -117,10 +146,33 @@ private async void UpdateServerList() _eventAggregator.GetEvent().Publish(); } - public NwmarketpriceJson GetPriceData(string itemName) + public Nwmarketprice GetPriceData(string itemName) { - var nwmarketpriceJson = new NwmarketpriceJson(); - return _priceCache.GetValueOrDefault(itemName, nwmarketpriceJson); ; + var nwmarketprice = new Nwmarketprice(); + + string itemId = _newWorldDataStore.GetItemId(itemName).ToLower(); + List priceData = new List(); + if (_serverPriceData.Daily.TryGetValue(itemId, out priceData)) + { + double recentLowestPricePrev = priceData.Count >= 2 ? priceData[priceData.Count - 2].Means[0][1] / 100.0 : 0.0; + double recentLowestPrice = priceData.Count >= 1 ? priceData[priceData.Count - 1].Means[0][1] / 100.0 : 0.0; + int priceChange = recentLowestPricePrev > 0 ? (int)(((recentLowestPrice - recentLowestPricePrev) / (recentLowestPricePrev))*100.0) : 0; + double recentLowestPriceAvg = priceData.Average(i => i.Means[0][1] / 100.0); + + int timeStamp = priceData[priceData.Count - 1].Timestamp; + DateTime lastUpdated = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); + lastUpdated = lastUpdated.AddSeconds(timeStamp).ToLocalTime(); + + nwmarketprice.ItemId = itemId; + nwmarketprice.ItemName = itemName; + nwmarketprice.Days = priceData.Count; + nwmarketprice.LastUpdated = lastUpdated; + nwmarketprice.PriceChange = priceChange; + nwmarketprice.RecentLowestPrice = recentLowestPrice; + nwmarketprice.RecentLowestPriceAvg = recentLowestPriceAvg; + } + + return nwmarketprice; } public double GetCraftingCosts(string itemId) @@ -129,7 +181,7 @@ public double GetCraftingCosts(string itemId) double craftingCosts = 0; var ingredient = string.Empty; - var nwmarketpriceJson = new NwmarketpriceJson(); + var nwmarketprice = new Nwmarketprice(); var qty = recipe.Qty1; if (!string.IsNullOrWhiteSpace(recipe.Ingredient1) && qty != 0) { @@ -137,9 +189,8 @@ public double GetCraftingCosts(string itemId) string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty; ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName); - UpdatePriceData(ingredientName); - nwmarketpriceJson = GetPriceData(ingredientName); - craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty); + nwmarketprice = GetPriceData(ingredientName); + craftingCosts = craftingCosts + (nwmarketprice.RecentLowestPrice * qty); } qty = recipe.Qty2; @@ -149,9 +200,8 @@ public double GetCraftingCosts(string itemId) string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty; ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName); - UpdatePriceData(ingredientName); - nwmarketpriceJson = GetPriceData(ingredientName); - craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty); + nwmarketprice = GetPriceData(ingredientName); + craftingCosts = craftingCosts + (nwmarketprice.RecentLowestPrice * qty); } qty = recipe.Qty3; @@ -161,9 +211,8 @@ public double GetCraftingCosts(string itemId) string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty; ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName); - UpdatePriceData(ingredientName); - nwmarketpriceJson = GetPriceData(ingredientName); - craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty); + nwmarketprice = GetPriceData(ingredientName); + craftingCosts = craftingCosts + (nwmarketprice.RecentLowestPrice * qty); } qty = recipe.Qty4; @@ -173,9 +222,8 @@ public double GetCraftingCosts(string itemId) string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty; ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName); - UpdatePriceData(ingredientName); - nwmarketpriceJson = GetPriceData(ingredientName); - craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty); + nwmarketprice = GetPriceData(ingredientName); + craftingCosts = craftingCosts + (nwmarketprice.RecentLowestPrice * qty); } qty = recipe.Qty5; @@ -185,9 +233,8 @@ public double GetCraftingCosts(string itemId) string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty; ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName); - UpdatePriceData(ingredientName); - nwmarketpriceJson = GetPriceData(ingredientName); - craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty); + nwmarketprice = GetPriceData(ingredientName); + craftingCosts = craftingCosts + (nwmarketprice.RecentLowestPrice * qty); } qty = recipe.Qty6; @@ -197,9 +244,8 @@ public double GetCraftingCosts(string itemId) string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty; ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName); - UpdatePriceData(ingredientName); - nwmarketpriceJson = GetPriceData(ingredientName); - craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty); + nwmarketprice = GetPriceData(ingredientName); + craftingCosts = craftingCosts + (nwmarketprice.RecentLowestPrice * qty); } qty = recipe.Qty7; @@ -209,17 +255,16 @@ public double GetCraftingCosts(string itemId) string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty; ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName); - UpdatePriceData(ingredientName); - nwmarketpriceJson = GetPriceData(ingredientName); - craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty); + nwmarketprice = GetPriceData(ingredientName); + craftingCosts = craftingCosts + (nwmarketprice.RecentLowestPrice * qty); } return craftingCosts; } - public List GetExtendedPriceData(string itemName) + public List GetExtendedPriceData(string itemName) { - var extendedPriceDataList = new List(); + var extendedPriceDataList = new List(); var itemId = _newWorldDataStore.GetItemId(itemName); var overlayResource = _relatedPriceManager.PersistableOverlayResources.FirstOrDefault(item => item.ItemId.Equals(itemId)); if (overlayResource != null) @@ -234,111 +279,21 @@ public List GetExtendedPriceData(string itemName) { continue; } - UpdatePriceData(recipeName); - - var nwmarketpriceJson = new NwmarketpriceJson(); - nwmarketpriceJson = _priceCache.GetValueOrDefault(recipeName, nwmarketpriceJson); - extendedPriceDataList.Add(nwmarketpriceJson); - } - } - return extendedPriceDataList; - } - - public void UpdatePriceData(string itemName) - { - if (string.IsNullOrWhiteSpace(itemName)) - { - return; - } - - if (!_priceCache.ContainsKey(itemName)) - { - if (!_priceRequestQueue.Contains(itemName)) _priceRequestQueue.Add(itemName); - lock (priceRequestLock) - { - if (!_priceRequestQueueBusy) + var nwmarketprice = new Nwmarketprice(); + nwmarketprice = GetPriceData(recipeName); + if (!string.IsNullOrWhiteSpace(nwmarketprice.ItemId)) { - _priceRequestQueueBusy = true; - Task task = Task.Run(async () => - { - string itemId = _newWorldDataStore.GetItemId(itemName); - bool isBindOnPickup = _newWorldDataStore.IsBindOnPickup(itemName); - if (!string.IsNullOrWhiteSpace(itemId) && !isBindOnPickup) - { - try - { - // TODO Update with https://nwmp.gaming.tools/ API. - //string uri = $"https://nwmarketprices.com/0/{ServerId}?cn_id={itemId.ToLower()}"; - //string json = await _httpClientHandler.GetRequest(uri); - string uri = string.Empty; - string json = string.Empty; - - Debug.WriteLine($"uri: {uri}"); - Debug.WriteLine($"json: {json}"); - - if (!string.IsNullOrWhiteSpace(json)) - { - // create the options - var options = new JsonSerializerOptions() - { - WriteIndented = true - }; - // register the converter - options.Converters.Add(new DoubleConverter()); - - var nwmarketpriceJson = JsonSerializer.Deserialize(json, options); - if (nwmarketpriceJson != null) - { - Debug.WriteLine($"item_name: {nwmarketpriceJson.item_name}"); - Debug.WriteLine($"recent_lowest_price: {nwmarketpriceJson.recent_lowest_price}"); - Debug.WriteLine($"last_checked: {nwmarketpriceJson.last_checked_string}"); - - nwmarketpriceJson.item_name = string.IsNullOrEmpty(nwmarketpriceJson.item_name) ? itemName : nwmarketpriceJson.item_name; - - _priceCache[itemName] = nwmarketpriceJson; - _eventAggregator.GetEvent().Publish(); - } - } - else - { - _priceCache[itemName] = new NwmarketpriceJson - { - item_name = itemName, - recent_lowest_price = 0.00, - last_checked = DateTime.MinValue, - }; - } - } - catch (Exception ex) - { - _logger.LogError(ex, MethodBase.GetCurrentMethod()?.Name); - } - } - - // Always remove from queue, even with exceptions. - _priceRequestQueue.RemoveAll(item => item.Equals(itemName)); - _priceRequestQueueBusy = false; - - Task.Delay(250).Wait(); - - if (_priceRequestQueue.Any()) - { - UpdatePriceData(_priceRequestQueue.First()); - } - }); + extendedPriceDataList.Add(nwmarketprice); } } } + + return extendedPriceDataList; } #endregion - private class UpdatedServers - { - public List> ServersLastUpdated { get; set; } = new List> { }; - } - private class ServerInfo { [JsonPropertyName("id")] diff --git a/NewWorldCompanion/ViewModels/Tabs/Config/ConfigOverlayViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/Config/ConfigOverlayViewModel.cs index 56b3371..06f9a1f 100644 --- a/NewWorldCompanion/ViewModels/Tabs/Config/ConfigOverlayViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/Config/ConfigOverlayViewModel.cs @@ -135,6 +135,7 @@ public int ServerIndex { _settingsManager.Settings.PriceServerIdNwm = Servers[value].Id; _settingsManager.SaveSettings(); + _eventAggregator.GetEvent().Publish(); } } } diff --git a/NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs b/NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs index b8cf26b..20ea639 100644 --- a/NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs +++ b/NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs @@ -263,12 +263,11 @@ public string SelectedCraftingRecipePrice if(SelectedCraftingRecipe != null) { - _priceManager.UpdatePriceData(SelectedCraftingRecipe.LocalisationUserFriendly); - NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(SelectedCraftingRecipe.LocalisationUserFriendly); - if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name)) + Nwmarketprice nwmarketprice = _priceManager.GetPriceData(SelectedCraftingRecipe.LocalisationUserFriendly); + if (!string.IsNullOrWhiteSpace(nwmarketprice.ItemName)) { - var priceChange = nwmarketpriceJson.price_change >= 0 ? $"+{nwmarketpriceJson.price_change}" : $"{nwmarketpriceJson.price_change}"; - _selectedCraftingRecipePrice = $"{nwmarketpriceJson.recent_lowest_price.ToString("F2")} ({priceChange}%) ({nwmarketpriceJson.last_checked_string})"; + var priceChange = nwmarketprice.PriceChange >= 0 ? $"+{nwmarketprice.PriceChange}" : $"{nwmarketprice.PriceChange}"; + _selectedCraftingRecipePrice = $"{nwmarketprice.RecentLowestPrice.ToString("F2")} ({priceChange}%) ({nwmarketprice.LastUpdatedString})"; } } return _selectedCraftingRecipePrice; @@ -283,12 +282,11 @@ public string SelectedCraftingRecipePriceTooltip if (SelectedCraftingRecipe != null) { - _priceManager.UpdatePriceData(SelectedCraftingRecipe.LocalisationUserFriendly); - NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(SelectedCraftingRecipe.LocalisationUserFriendly); - if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name)) + Nwmarketprice nwmarketprice = _priceManager.GetPriceData(SelectedCraftingRecipe.LocalisationUserFriendly); + if (!string.IsNullOrWhiteSpace(nwmarketprice.ItemName)) { - var priceChange = nwmarketpriceJson.price_change >= 0 ? $"+{nwmarketpriceJson.price_change}" : $"{nwmarketpriceJson.price_change}"; - _selectedCraftingRecipePriceTooltip = $"The lowest price at ({nwmarketpriceJson.last_checked_string}) was {nwmarketpriceJson.recent_lowest_price.ToString("F2")} ({priceChange}%)"; + var priceChange = nwmarketprice.PriceChange >= 0 ? $"+{nwmarketprice.PriceChange}" : $"{nwmarketprice.PriceChange}"; + _selectedCraftingRecipePriceTooltip = $"The lowest price at ({nwmarketprice.LastUpdatedString}) was {nwmarketprice.RecentLowestPrice.ToString("F2")} ({priceChange}%)"; } } return _selectedCraftingRecipePriceTooltip; @@ -303,14 +301,10 @@ public string SelectedCraftingRecipePriceAvg if (SelectedCraftingRecipe != null) { - _priceManager.UpdatePriceData(SelectedCraftingRecipe.LocalisationUserFriendly); - NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(SelectedCraftingRecipe.LocalisationUserFriendly); - if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name)) + Nwmarketprice nwmarketprice = _priceManager.GetPriceData(SelectedCraftingRecipe.LocalisationUserFriendly); + if (!string.IsNullOrWhiteSpace(nwmarketprice.ItemName)) { - string recentLowestPriceAvg = nwmarketpriceJson.RecentLowestPriceAvg; - _selectedCraftingRecipePriceAvg = string.IsNullOrWhiteSpace(recentLowestPriceAvg) ? - _selectedCraftingRecipePriceAvg : - $"{recentLowestPriceAvg} (15-day avg) ({nwmarketpriceJson.last_checked_string})"; + _selectedCraftingRecipePriceAvg = $"{nwmarketprice.RecentLowestPriceAvg.ToString("F2")} ({nwmarketprice.Days}-day avg) ({nwmarketprice.LastUpdatedString})"; } } return _selectedCraftingRecipePriceAvg; @@ -325,14 +319,10 @@ public string SelectedCraftingRecipePriceAvgTooltip if (SelectedCraftingRecipe != null) { - _priceManager.UpdatePriceData(SelectedCraftingRecipe.LocalisationUserFriendly); - NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(SelectedCraftingRecipe.LocalisationUserFriendly); - if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name)) + Nwmarketprice nwmarketprice = _priceManager.GetPriceData(SelectedCraftingRecipe.LocalisationUserFriendly); + if (!string.IsNullOrWhiteSpace(nwmarketprice.ItemName)) { - string recentLowestPriceAvg = nwmarketpriceJson.RecentLowestPriceAvg; - _selectedCraftingRecipePriceAvgToolip = string.IsNullOrWhiteSpace(recentLowestPriceAvg) ? - _selectedCraftingRecipePriceAvgToolip : - $"The lowest 15-day average price at ({nwmarketpriceJson.last_checked_string}) was {recentLowestPriceAvg}"; + _selectedCraftingRecipePriceAvgToolip = $"The lowest {nwmarketprice.Days}-day average price at ({nwmarketprice.LastUpdatedString}) was {nwmarketprice.RecentLowestPriceAvg.ToString("F2")}"; } } return _selectedCraftingRecipePriceAvgToolip; diff --git a/NewWorldCompanion/common.props b/NewWorldCompanion/common.props index 8493c29..9434f21 100644 --- a/NewWorldCompanion/common.props +++ b/NewWorldCompanion/common.props @@ -1,7 +1,7 @@ - 1.12.11.0 - 1.12.11.0 + 1.13.0.0 + 1.13.0.0 Copyright © 2024 net6.0-windows