From cdd1681fc2810817fc86816c23ae69767c1732ff Mon Sep 17 00:00:00 2001 From: Aaron Schultz Date: Wed, 9 Dec 2020 10:28:25 +0100 Subject: [PATCH] Average prices added at the bottom of the item window --- .../Languages/de-DE.xml | 2 + .../Languages/en-US.xml | 2 + .../Models/ItemWindowTranslation.cs | 2 + .../ViewModels/ItemWindowViewModel.cs | 78 +++++++++++++++---- .../Views/ItemWindow.xaml | 4 +- 5 files changed, 70 insertions(+), 18 deletions(-) diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/de-DE.xml b/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/de-DE.xml index be5c949eb..25081c36d 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/de-DE.xml +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/de-DE.xml @@ -313,4 +313,6 @@ ist unterboten worden Ist Alarm aktiv Zu unterbietender Preis + Durchschnittspreise + Verk.-Preis min | Verk.-Preis max | Kauf-Preis min | Kauf-Preis max \ No newline at end of file diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/en-US.xml b/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/en-US.xml index 009f40dae..be45d3360 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/en-US.xml +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Languages/en-US.xml @@ -314,4 +314,6 @@ has been undercut Is alert activate Undercutting price + Average prices + Sell price min | Sell price max | Buy price min | Buy price max \ No newline at end of file diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/ItemWindowTranslation.cs b/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/ItemWindowTranslation.cs index b2136bbe1..f71da800a 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/ItemWindowTranslation.cs +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Models/ItemWindowTranslation.cs @@ -42,5 +42,7 @@ public class ItemWindowTranslation public string ItemPower => LanguageController.Translation("ITEM_POWER"); public string Durability => LanguageController.Translation("DURABILITY"); public string RealMoneyPrice => LanguageController.Translation("REAL_MONEY_PRICE"); + public string AveragePrices => LanguageController.Translation("AVERAGE_PRICES"); + public string AveragePricesTooltip => LanguageController.Translation("AVERAGE_PRICES_TOOLTIP"); } } \ No newline at end of file diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/ViewModels/ItemWindowViewModel.cs b/StatisticsAnalysisTool/StatisticsAnalysisTool/ViewModels/ItemWindowViewModel.cs index 2088d6cb6..baabb6ffa 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/ViewModels/ItemWindowViewModel.cs +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/ViewModels/ItemWindowViewModel.cs @@ -53,7 +53,7 @@ public class ItemWindowViewModel : INotifyPropertyChanged private Visibility _loadingImageVisibility; private FontAwesomeIcon _loadingImageIcon; private bool _loadingImageSpin; - private string _differentCalculation; + private string _averagePrices; private List _realMoneyPriceList; private GoldResponseModel _currentGoldPrice; private List _currentCityPrices; @@ -463,7 +463,7 @@ public void GetMainPriceStats() } MarketCurrentPricesItemList = marketCurrentPricesItemList; - SetDifferenceCalculationText(statsPricesTotalList); + SetAveragePricesString(); HasItemPrices = true; RefreshIconTooltipText = $"{LanguageController.Translation("LAST_UPDATE")}: {Formatting.CurrentDateTimeFormat(DateTime.Now)}"; @@ -583,22 +583,47 @@ private static ulong GetMinPrice(List list) return min; } - private void SetDifferenceCalculationText(List statsPricesTotalList) + private void SetAveragePricesString() { - ulong? bestBuyMaxPrice = 0UL; - ulong? bestSellMinPrice = 0UL; + var cityPrices = GetFilteredCityPrices(false, false, true, false, false); - if (statsPricesTotalList?.Count > 0) + var sellPriceMin = new List(); + var sellPriceMax = new List(); + var buyPriceMin = new List(); + var buyPriceMax = new List(); + + foreach (var price in cityPrices) { - bestBuyMaxPrice = statsPricesTotalList.FirstOrDefault(s => s.BestBuyMaxPrice)?.BuyPriceMax ?? 0UL; - bestSellMinPrice = statsPricesTotalList.FirstOrDefault(s => s.BestSellMinPrice)?.SellPriceMin ?? 0UL; - } + if (price.SellPriceMin != 0) + { + sellPriceMin.Add(price.SellPriceMin); + } - var diffPrice = (int)bestBuyMaxPrice - (int)bestSellMinPrice; + if (price.SellPriceMax != 0) + { + sellPriceMax.Add(price.SellPriceMax); + } - DifferentCalculation = $"{LanguageController.Translation("BOUGHT_FOR")} {string.Format(LanguageController.CurrentCultureInfo, "{0:n0}", bestSellMinPrice)} | " + - $"{LanguageController.Translation("SELL_FOR")} {string.Format(LanguageController.CurrentCultureInfo, "{0:n0}", bestBuyMaxPrice)} | " + - $"{LanguageController.Translation("PROFIT")} {string.Format(LanguageController.CurrentCultureInfo, "{0:n0}", diffPrice)}"; + if (price.BuyPriceMin != 0) + { + buyPriceMin.Add(price.BuyPriceMin); + } + + if (price.BuyPriceMax != 0) + { + buyPriceMax.Add(price.BuyPriceMax); + } + } + + var sellPriceMinAverage = Average(sellPriceMin.ToArray()); + var sellPriceMaxAverage = Average(sellPriceMax.ToArray()); + var buyPriceMinAverage = Average(buyPriceMin.ToArray()); + var buyPriceMaxAverage = Average(buyPriceMax.ToArray()); + + AveragePrices = $"{string.Format(LanguageController.CurrentCultureInfo, "{0:n0}", sellPriceMinAverage)} | " + + $"{string.Format(LanguageController.CurrentCultureInfo, "{0:n0}", sellPriceMaxAverage)} | " + + $"{string.Format(LanguageController.CurrentCultureInfo, "{0:n0}", buyPriceMinAverage)} | " + + $"{string.Format(LanguageController.CurrentCultureInfo, "{0:n0}", buyPriceMaxAverage)}"; } #endregion Prices @@ -653,10 +678,10 @@ public List CurrentCityPrices { } } - public string DifferentCalculation { - get => _differentCalculation; + public string AveragePrices { + get => _averagePrices; set { - _differentCalculation = value; + _averagePrices = value; OnPropertyChanged(); } } @@ -869,5 +894,26 @@ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName } #endregion Bindings + + #region Helper + + public ulong Sum(params ulong[] values) + { + return values.Aggregate(0UL, (current, t) => current + t); + } + + public ulong Average(params ulong[] values) + { + if (values.Length == 0) + { + return 0; + } + + var sum = Sum(values); + var result = sum / (ulong) values.Length; + return result; + } + + #endregion } } \ No newline at end of file diff --git a/StatisticsAnalysisTool/StatisticsAnalysisTool/Views/ItemWindow.xaml b/StatisticsAnalysisTool/StatisticsAnalysisTool/Views/ItemWindow.xaml index cddb451b4..0ae67f8ff 100644 --- a/StatisticsAnalysisTool/StatisticsAnalysisTool/Views/ItemWindow.xaml +++ b/StatisticsAnalysisTool/StatisticsAnalysisTool/Views/ItemWindow.xaml @@ -379,8 +379,8 @@ -