Skip to content

Commit

Permalink
- Added automatic updates
Browse files Browse the repository at this point in the history
- Added server health indicator
 - Green: Scanned within the last 24 hours
 - Orange: Scanned between 24-36 hours ago
 - Red: Last scan was over 36 hours ago
- Moved all price overlay settings to Config > Overlay
- Fixed exception in storage filter
  • Loading branch information
josdemmers committed Dec 21, 2022
1 parent 290d300 commit 6d54ca9
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 115 deletions.
1 change: 1 addition & 0 deletions NewWorldCompanion.Entities/PriceServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class PriceServer
{
public string Name { get; set; } = string.Empty;
public int Id { get; set; } = 1;
public DateTime Updated { get; set; } = DateTime.MinValue;
}
}
1 change: 1 addition & 0 deletions NewWorldCompanion.Entities/SettingsNWC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class SettingsNWC
public bool DebugModeActive { get; set; } = false;

// Overlay
public bool TooltipEnabled { get; set; } = true;
public bool ExtendedTooltipEnabled { get; set; } = false;
public int PriceServerId { get; set; } = 1;

Expand Down
2 changes: 1 addition & 1 deletion NewWorldCompanion.Interfaces/IScreenCaptureHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IScreenCaptureHandler
{
Bitmap? CurrentScreen { get; }
Bitmap? CurrentScreenMouseArea { get; }
bool IsActive { get; set; }
bool IsActive { get; }
string MouseCoordinates { get; }
string MouseCoordinatesScaled { get; }
int OffsetX { get; }
Expand Down
42 changes: 25 additions & 17 deletions NewWorldCompanion.Services/PriceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace NewWorldCompanion.Services
{
Expand Down Expand Up @@ -50,8 +51,8 @@ public PriceManager(IEventAggregator eventAggregator, ILogger<PriceManager> logg
_newWorldDataStore = newWorldDataStore;
_relatedPriceManager = relatedPriceManager;

// Init servers
UpdateServerList();
// Init servers
UpdateServerList();
}

#endregion
Expand Down Expand Up @@ -79,23 +80,30 @@ private async void UpdateServerList()
{
try
{
string json = await _httpClientHandler.GetRequest("https://nwmarketprices.com/api/servers/") ?? string.Empty;
var servers = JsonSerializer.Deserialize<Dictionary<string, Server>>(json);
string json = await _httpClientHandler.GetRequest("https://nwmarketprices.com/api/servers_updated/") ?? string.Empty;
var servers = JsonSerializer.Deserialize<UpdatedServers>(json);

foreach (var server in servers ?? new Dictionary<string, Server>())
_servers.Clear();
foreach (var server in servers?.ServersLastUpdated ?? new List<List<object>>())
{
string serverId = server.Key;
string serverName = server.Value.Name;
var serverId = ((JsonElement)server[0]).GetInt32();
var serverName = ((JsonElement)server[1]).GetString() ?? string.Empty;
var updated = ((JsonElement)server[2]).GetDateTime();

if (!string.IsNullOrWhiteSpace(serverId) && !string.IsNullOrWhiteSpace(serverName))
_servers.Add(new PriceServer()
{
_servers.Add(new PriceServer()
{
Id = int.Parse(serverId),
Name = serverName
});
}
Id = serverId,
Name = serverName,
Updated = updated
});
}

// Sort server list by name
_servers.Sort((x, y) =>
{
int result = string.Compare(x.Name, y.Name, StringComparison.Ordinal);
return result;
});
}
catch (Exception ex)
{
Expand Down Expand Up @@ -319,10 +327,10 @@ public void UpdatePriceData(string itemName)

#endregion

private class Server
private class UpdatedServers
{
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
[JsonPropertyName("server_last_updated")]
public List<List<object>> ServersLastUpdated { get; set; } = new List<List<object>> { };
}
}
}
6 changes: 4 additions & 2 deletions NewWorldCompanion.Services/RelatedPriceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ public void SetRawResourceRecipeVisibility(string itemIDRawResource, string item
{
// Recipe resource exists
var recipe = rawResource.PersistableOverlayResourceRecipes.FirstOrDefault(resource => resource.ItemId.Equals(itemID));
recipe.IsVisible = isVisible;

if (recipe != null)
{
recipe.IsVisible = isVisible;
}
}
else
{
Expand Down
12 changes: 9 additions & 3 deletions NewWorldCompanion.Services/ScreenCaptureHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public class ScreenCaptureHandler : IScreenCaptureHandler
{
private readonly IEventAggregator _eventAggregator;
private readonly ILogger _logger;
private readonly ISettingsManager _settingsManager;

private DispatcherTimer _captureTimer = new();
private DispatcherTimer _coordinatesTimer = new();
private Bitmap? _currentScreen = null;
private Bitmap? _currentScreenMouseArea = null;
private int _delay = 100;
private int _delayCoordinates = 100;
private bool _isActive = true;
private int _offsetX = 0;
private int _offsetY = 0;
private ScreenCapture _screenCapture = new ScreenCapture();
Expand All @@ -34,14 +34,17 @@ public class ScreenCaptureHandler : IScreenCaptureHandler

#region Constructor

public ScreenCaptureHandler(IEventAggregator eventAggregator, ILogger<ScreenCaptureHandler> logger)
public ScreenCaptureHandler(IEventAggregator eventAggregator, ILogger<ScreenCaptureHandler> logger, ISettingsManager settingsManager)
{
// Init IEventAggregator
_eventAggregator = eventAggregator;

// Init logger
_logger = logger;

// Init services
_settingsManager = settingsManager;

// Capture timer
_captureTimer = new DispatcherTimer
{
Expand Down Expand Up @@ -69,7 +72,10 @@ public ScreenCaptureHandler(IEventAggregator eventAggregator, ILogger<ScreenCapt
public Bitmap? CurrentScreenMouseArea { get => _currentScreenMouseArea; set => _currentScreenMouseArea = value; }
public int Delay { get => _delay; set => _delay = value; }
public int DelayCoordinates { get => _delayCoordinates; set => _delayCoordinates = value; }
public bool IsActive { get => _isActive; set => _isActive = value; }
public bool IsActive
{
get => _settingsManager.Settings.TooltipEnabled;
}
public string MouseCoordinates { get; set; } = string.Empty;
public string MouseCoordinatesScaled { get; set; } = string.Empty;
public int OffsetX { get => _offsetX; set => _offsetX = value; }
Expand Down
33 changes: 33 additions & 0 deletions NewWorldCompanion/Converters/DateTimeToHealthConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace NewWorldCompanion.Converters
{
class DateTimeToHealthConverter : IValueConverter
{
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
{
if (targetType == typeof(Brush))
{
var updated = System.Convert.ToDateTime(value, culture);
var timeSpan = DateTime.Now - updated;

if (timeSpan < TimeSpan.FromHours(24))
return Brushes.Green;
else if (timeSpan < TimeSpan.FromHours(36))
return Brushes.Orange;
else
return Brushes.Red;
}

throw new InvalidOperationException("Converter can only convert to value of type Brush.");
}

public Object ConvertBack(Object aValue, Type aTargetType, Object aParameter, CultureInfo aCulture)
{
throw new InvalidOperationException("Converter cannot convert back.");
}
}
}
45 changes: 41 additions & 4 deletions NewWorldCompanion/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Microsoft.Extensions.Logging;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Extensions.Logging;
using NewWorldCompanion.Events;
using NewWorldCompanion.Interfaces;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace NewWorldCompanion.ViewModels
{
Expand All @@ -18,14 +19,15 @@ public class MainWindowViewModel : BindableBase
private readonly ISettingsManager _settingsManager;
private readonly IOverlayHandler _overlayHandler;
private readonly IVersionManager _versionManager;
private readonly IDialogCoordinator _dialogCoordinator;

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

// Start of Constructor region

#region Constructor

public MainWindowViewModel(IEventAggregator eventAggregator, ILogger<MainWindowViewModel> logger, ISettingsManager settingsManager, IOverlayHandler overlayHandler, IVersionManager versionManager)
public MainWindowViewModel(IEventAggregator eventAggregator, ILogger<MainWindowViewModel> logger, ISettingsManager settingsManager, IOverlayHandler overlayHandler, IVersionManager versionManager, IDialogCoordinator dialogCoordinator)
{
// Init IEventAggregator
_eventAggregator = eventAggregator;
Expand All @@ -38,9 +40,11 @@ public MainWindowViewModel(IEventAggregator eventAggregator, ILogger<MainWindowV
_settingsManager = settingsManager;
_overlayHandler = overlayHandler;
_versionManager = versionManager;
_dialogCoordinator = dialogCoordinator;

// Init View commands
LaunchNWCOnGitHubCommand = new DelegateCommand(LaunchNWCOnGitHubExecute);
LaunchKofiCommand = new DelegateCommand(LaunchKofiExecute);
}

#endregion
Expand All @@ -50,7 +54,8 @@ public MainWindowViewModel(IEventAggregator eventAggregator, ILogger<MainWindowV
#region Properties

public DelegateCommand LaunchNWCOnGitHubCommand { get; }

public DelegateCommand LaunchKofiCommand { get; }

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

Expand All @@ -66,6 +71,24 @@ private void HandleVersionInfoUpdatedEvent()
!_versionManager.LatestVersion.Equals(_versionManager.CurrentVersion))
{
WindowTitle = $"New World Companion v{_versionManager.CurrentVersion} (v{_versionManager.LatestVersion} available)";

// Ask for update when Updater.GitHub.exe exists.
if (File.Exists("Updater.GitHub.exe"))
{
_dialogCoordinator.ShowMessageAsync(this, $"Update", $"New version available, do you want to download v{_versionManager.LatestVersion}?", MessageDialogStyle.AffirmativeAndNegative).ContinueWith(t =>
{
if (t.Result == MessageDialogResult.Affirmative)
{
var app = System.Diagnostics.Process.GetCurrentProcess().MainModule?.FileName ?? "NewWorldCompanion.exe";
app = Path.GetFileName(app);
Process.Start("Updater.GitHub.exe", $"--repo \"josdemmers/NewWorldCompanion\" --app \"{app}\"");
}
});
}
else
{
_logger.LogWarning("Cannot update applicaiton, Updater.GitHub.exe not available.");
}
}
else
{
Expand Down Expand Up @@ -94,6 +117,20 @@ private void LaunchNWCOnGitHubExecute()
}
}

private void LaunchKofiExecute()
{
{
try
{
Process.Start(new ProcessStartInfo("https://ko-fi.com/josdemmers") { UseShellExecute = true });
}
catch (Exception ex)
{
_logger.LogError(ex, MethodBase.GetCurrentMethod()?.Name);
}
}
}

#endregion

}
Expand Down
25 changes: 25 additions & 0 deletions NewWorldCompanion/ViewModels/Tabs/Config/ConfigOverlayViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class ConfigOverlayViewModel : BindableBase
private ObservableCollection<OverlayResource> _overlayResources = new ObservableCollection<OverlayResource>();

private OverlayResource _selectedOverlayResource = new OverlayResource();
private bool _toggleTooltip = false;
private bool _toggleExtendedTooltip = false;
private int _serverIndex = 0;
private string _itemNameFilter = string.Empty;
Expand Down Expand Up @@ -73,12 +74,35 @@ public OverlayResource SelectedOverlayResource
}
}

public bool ToggleTooltip
{
get => _toggleTooltip;
set
{
_toggleTooltip = value;
RaisePropertyChanged();
if (!ToggleTooltip)
{
ToggleExtendedTooltip = false;
}

_settingsManager.Settings.TooltipEnabled = value;
_settingsManager.SaveSettings();
}
}

public bool ToggleExtendedTooltip
{
get => _toggleExtendedTooltip;
set
{
_toggleExtendedTooltip = value;
RaisePropertyChanged();
if (_toggleExtendedTooltip)
{
ToggleTooltip = true;
}

_settingsManager.Settings.ExtendedTooltipEnabled = value;
_settingsManager.SaveSettings();
}
Expand Down Expand Up @@ -180,6 +204,7 @@ private bool FilterOverlayResources(object overlayResourceObj)
private void InitOverlayResources()
{
// Load extended tooltip toggle
ToggleTooltip = _settingsManager.Settings.TooltipEnabled;
ToggleExtendedTooltip = _settingsManager.Settings.ExtendedTooltipEnabled;

// Get interesting resources for extended overlay
Expand Down
11 changes: 0 additions & 11 deletions NewWorldCompanion/ViewModels/Tabs/CraftingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class CraftingViewModel : BindableBase
private bool _toggleJewelcrafting = true;
private bool _toggleWeaponsmithing = true;
private bool _toggleMusicSheets = true;
private bool _toggleRefresh = true;
private int _counterArcana = 0;
private int _counterArmoring = 0;
private int _counterCooking = 0;
Expand Down Expand Up @@ -263,16 +262,6 @@ public bool ToggleMusicSheets
}
}

public bool ToggleRefresh
{
get => _toggleRefresh;
set
{
_toggleRefresh = value;
_screenCaptureHandler.IsActive = value;
}
}

public string SelectedCraftingRecipePrice
{
get
Expand Down
6 changes: 5 additions & 1 deletion NewWorldCompanion/ViewModels/Tabs/StorageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public string ItemNameFilter

if (ItemsFiltered?.Count == 1)
{
SelectedItem = (Item)ItemsFiltered.GetItemAt(0);
var selection = ItemsFiltered.GetItemAt(0) as Item;
if (selection != null)
{
SelectedItem = selection;
}
}
}
}
Expand Down
Loading

0 comments on commit 6d54ca9

Please sign in to comment.