Skip to content

Commit

Permalink
Added version info.
Browse files Browse the repository at this point in the history
Added price info for each crafting recipe.
Both lowest and lowest avg prices are now visible.
  • Loading branch information
josdemmers committed Mar 27, 2022
1 parent be17f88 commit 68c7981
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 13 deletions.
35 changes: 35 additions & 0 deletions NewWorldCompanion.Entities/NwmarketpriceJson.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
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;

namespace NewWorldCompanion.Entities
Expand All @@ -12,5 +16,36 @@ public class NwmarketpriceJson
public string last_checked { get; set; } = string.Empty;
public string recent_lowest_price { get; set; } = string.Empty;
//public double recent_lowest_price { get; set; } = 0.0;
public List<object> avg_graph_data { get; set; } = new List<object>();

[JsonIgnore]
public string RecentLowestPriceAvg
{
get
{
string price = string.Empty;
NumberStyles style = NumberStyles.AllowDecimalPoint;

try
{
if (avg_graph_data.Count() > 0)
{
JsonElement priceData = (JsonElement)avg_graph_data.Last();
switch (priceData.ValueKind)
{
case JsonValueKind.Number:
price = decimal.Parse(priceData.ToString(), style, CultureInfo.InvariantCulture).ToString("F2");
break;
case JsonValueKind.Array:
price = decimal.Parse(priceData[1].ToString(), style, CultureInfo.InvariantCulture).ToString("F2");
break;
}
}
}
catch (Exception){}

return price;
}
}
}
}
4 changes: 4 additions & 0 deletions NewWorldCompanion.Events/PriceServerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ namespace NewWorldCompanion.Events
public class PriceServerListUpdatedEvent : PubSubEvent
{
}

public class PriceCacheUpdatedEvent : PubSubEvent
{
}
}
13 changes: 13 additions & 0 deletions NewWorldCompanion.Events/VersionEvents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Prism.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewWorldCompanion.Events
{
public class VersionInfoUpdatedEvent : PubSubEvent
{
}
}
14 changes: 14 additions & 0 deletions NewWorldCompanion.Interfaces/IVersionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewWorldCompanion.Interfaces
{
public interface IVersionManager
{
string CurrentVersion { get; }
string LatestVersion { get; }
}
}
36 changes: 30 additions & 6 deletions NewWorldCompanion.Services/OverlayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,20 @@ private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName)
NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(itemName);

string infoItemName = itemName;
string infoPrice = string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name) ? "Price: Loading..." : $"Price: {nwmarketpriceJson.recent_lowest_price}";
string infoPriceDate = string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name) ? string.Empty : nwmarketpriceJson.last_checked;
string infoPrice = "Loading...";
string infoPriceAvg = string.Empty;

if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name))
{
string recentLowestPriceAvgList = nwmarketpriceJson.RecentLowestPriceAvg;

infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ?
nwmarketpriceJson.recent_lowest_price :
$"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})";
infoPriceAvg = string.IsNullOrWhiteSpace(recentLowestPriceAvgList) ?
infoPriceAvg :
$"{recentLowestPriceAvgList} lowest avg ({nwmarketpriceJson.last_checked})";
}

// Do not show Bind on pickup items.
if (!_newWorldDataStore.IsBindOnPickup(itemName))
Expand All @@ -140,7 +152,7 @@ private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName)
gfx.ClearScene(_brushes["background"]);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 20, infoItemName);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 40, infoPrice);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 60, infoPriceDate);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 60, infoPriceAvg);
gfx.DrawRectangle(_brushes["border"], 0, 0, _overlayWidth, _overlayHeigth, 1);
}
else
Expand All @@ -156,15 +168,27 @@ private void DrawGraphicsRecipe(DrawGraphicsEventArgs e, CraftingRecipe crafting
bool learnedStatus = craftingRecipe.Learned;
string infoItemName = craftingRecipe.Localisation;
string infoLearned = $"Learned: {learnedStatus}";
string infoPrice = string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name) ? "Price: Loading..." : $"Price: {nwmarketpriceJson.recent_lowest_price}";
string infoPriceDate = string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name) ? string.Empty : nwmarketpriceJson.last_checked;
string infoPrice = "Loading...";
string infoPriceAvg = string.Empty;

if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name))
{
string recentLowestPriceAvgList = nwmarketpriceJson.RecentLowestPriceAvg;

infoPrice = nwmarketpriceJson.recent_lowest_price.Equals(nwmarketpriceJson.last_checked) ?
nwmarketpriceJson.recent_lowest_price :
$"{nwmarketpriceJson.recent_lowest_price} lowest ({nwmarketpriceJson.last_checked})";
infoPriceAvg = string.IsNullOrWhiteSpace(recentLowestPriceAvgList) ?
infoPriceAvg :
$"{recentLowestPriceAvgList} lowest avg ({nwmarketpriceJson.last_checked})";
}

var gfx = e.Graphics;
gfx.ClearScene(_brushes["background"]);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 20, infoItemName);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 40, infoLearned);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 60, infoPrice);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 80, infoPriceDate);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 80, infoPriceAvg);
gfx.DrawRectangle(_brushes["border"], 0, 0, _overlayWidth, _overlayHeigth, 1);
}

Expand Down
1 change: 1 addition & 0 deletions NewWorldCompanion.Services/PriceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public void UpdatePriceData(string itemName)
Debug.WriteLine($"last_checked: {nwmarketpriceJson.last_checked}");

_priceCache[itemName] = nwmarketpriceJson;
_eventAggregator.GetEvent<PriceCacheUpdatedEvent>().Publish();
}
}
else
Expand Down
91 changes: 91 additions & 0 deletions NewWorldCompanion.Services/VersionManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using NewWorldCompanion.Events;
using NewWorldCompanion.Interfaces;
using Prism.Events;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;

namespace NewWorldCompanion.Services
{
public class VersionManager : IVersionManager
{
private readonly IEventAggregator _eventAggregator;
private readonly IHttpClientHandler _httpClientHandler;

private string _latestVersion = string.Empty;

// Start of Constructor region

#region Constructor

public VersionManager(IEventAggregator eventAggregator, HttpClientHandler httpClientHandler)
{
// Init IEventAggregator
_eventAggregator = eventAggregator;

// Init services
_httpClientHandler = httpClientHandler;

// Init servers
UpdateVersionInfo();
}

#endregion

// Start of Properties region

#region Properties

public string CurrentVersion
{
get
{
Version version = Assembly.GetExecutingAssembly().GetName().Version;
return version.ToString();
}
}

public string LatestVersion { get => _latestVersion; set => _latestVersion = value; }

#endregion

// Start of Events region

#region Events

#endregion

// Start of Methods region

#region Methods

private async void UpdateVersionInfo()
{
string uri = $"https://raw.githubusercontent.com/josdemmers/NewWorldCompanion/master/NewWorldCompanion/common.props";
string xml = await _httpClientHandler.GetRequest(uri);
if (!string.IsNullOrWhiteSpace(xml))
{
var xPathDocument = new XPathDocument(new StringReader(xml));
var xPathNavigator = xPathDocument.CreateNavigator();
var xPathExpression = xPathNavigator.Compile("/Project/PropertyGroup/FileVersion/text()");
var xPathNodeIterator = xPathNavigator.Select(xPathExpression);
while (xPathNodeIterator.MoveNext())
{
LatestVersion = xPathNodeIterator.Current?.ToString() ?? string.Empty;
}
}
else
{
LatestVersion = string.Empty;
}
_eventAggregator.GetEvent<VersionInfoUpdatedEvent>().Publish();
}

#endregion
}
}
1 change: 1 addition & 0 deletions NewWorldCompanion/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
containerRegistry.RegisterSingleton<IOverlayHandler, OverlayHandler>();
containerRegistry.RegisterSingleton<IPriceManager, PriceManager>();
containerRegistry.RegisterSingleton<ISettingsManager, SettingsManager>();
containerRegistry.RegisterSingleton<IVersionManager, VersionManager>();

// Register Metro
containerRegistry.RegisterSingleton<IDialogCoordinator, DialogCoordinator>();
Expand Down
32 changes: 30 additions & 2 deletions NewWorldCompanion/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using NewWorldCompanion.Interfaces;
using NewWorldCompanion.Events;
using NewWorldCompanion.Interfaces;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;

namespace NewWorldCompanion.ViewModels
Expand All @@ -12,19 +14,24 @@ public class MainWindowViewModel : BindableBase
private readonly IEventAggregator _eventAggregator;
private readonly ISettingsManager _settingsManager;
private readonly IOverlayHandler _overlayHandler;
private readonly IVersionManager _versionManager;

private string _windowTitle = $"New World Companion v{Assembly.GetExecutingAssembly().GetName().Version}";

// Start of Constructor region

#region Constructor

public MainWindowViewModel(IEventAggregator eventAggregator, ISettingsManager settingsManager, IOverlayHandler overlayHandler)
public MainWindowViewModel(IEventAggregator eventAggregator, ISettingsManager settingsManager, IOverlayHandler overlayHandler, IVersionManager versionManager)
{
// Init IEventAggregator
_eventAggregator = eventAggregator;
_eventAggregator.GetEvent<VersionInfoUpdatedEvent>().Subscribe(HandleVersionInfoUpdatedEvent);

// Init services
_settingsManager = settingsManager;
_overlayHandler = overlayHandler;
_versionManager = versionManager;
}

#endregion
Expand All @@ -34,6 +41,27 @@ public MainWindowViewModel(IEventAggregator eventAggregator, ISettingsManager se
#region Properties

public bool DebugModeActive { get => _settingsManager.Settings.DebugModeActive; }
public string WindowTitle { get => _windowTitle; set => _windowTitle = value; }

#endregion

// Start of Events region

#region Events

private void HandleVersionInfoUpdatedEvent()
{
if (!string.IsNullOrWhiteSpace(_versionManager.LatestVersion) &&
!_versionManager.LatestVersion.Equals(_versionManager.CurrentVersion))
{
WindowTitle = $"New World Companion v{_versionManager.CurrentVersion} (v{_versionManager.LatestVersion} available)";
}
else
{
WindowTitle = $"New World Companion v{Assembly.GetExecutingAssembly().GetName().Version}";
}
RaisePropertyChanged(nameof(WindowTitle));
}

#endregion

Expand Down
Loading

0 comments on commit 68c7981

Please sign in to comment.