Skip to content

Commit

Permalink
Added extended tooltip for raw resources.
Browse files Browse the repository at this point in the history
Improved exception logging.
  • Loading branch information
josdemmers committed Dec 10, 2022
1 parent 662cea6 commit 9b2b712
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 21 deletions.
1 change: 1 addition & 0 deletions NewWorldCompanion.Entities/NwmarketpriceJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ 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<GrapData> price_graph_data { get; set; } = new List<GrapData>();
Expand Down
1 change: 1 addition & 0 deletions NewWorldCompanion.Interfaces/INewWorldDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public interface INewWorldDataStore
List<MasterItemDefinitionsJson> GetOverlayResources();
string GetItemLocalisation(string itemMasterName);
List<CraftingRecipeJson> GetRelatedRecipes(string itemId);
CraftingRecipeJson GetCraftingRecipeDetails(string itemId);
}
}
2 changes: 2 additions & 0 deletions NewWorldCompanion.Interfaces/IPriceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface IPriceManager
List<PriceServer> Servers { get; }

NwmarketpriceJson GetPriceData(string itemName);
double GetCraftingCosts(string itemId);
List<NwmarketpriceJson> GetExtendedPriceData(string itemName);
void UpdatePriceData(string itemName);
}
}
2 changes: 1 addition & 1 deletion NewWorldCompanion.Services/HttpClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task<string> GetRequest(string uri)
}
catch (Exception ex)
{
_logger.LogError(ex, MethodBase.GetCurrentMethod()?.Name);
_logger.LogError(ex, $"GetRequest({uri})");

return string.Empty;
}
Expand Down
7 changes: 7 additions & 0 deletions NewWorldCompanion.Services/NewWorldDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,13 @@ public List<CraftingRecipeJson> GetRelatedRecipes(string itemId)
recipe.Ingredient7.Equals(itemId)));
}

public CraftingRecipeJson GetCraftingRecipeDetails(string itemId)
{
var craftingRecipesJson = new CraftingRecipeJson();
craftingRecipesJson = _craftingRecipesJson.FirstOrDefault(recipe => recipe.ItemID.ToLower().Equals(itemId), craftingRecipesJson);
return craftingRecipesJson;
}

#endregion

}
Expand Down
48 changes: 37 additions & 11 deletions NewWorldCompanion.Services/OverlayHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace NewWorldCompanion.Services
public class OverlayHandler : IOverlayHandler
{
private readonly IEventAggregator _eventAggregator;
private readonly ISettingsManager _settingsManager;
private readonly ICraftingRecipeManager _craftingRecipeManager;
private readonly INewWorldDataStore _newWorldDataStore;
private readonly IOcrHandler _ocrHandler;
Expand All @@ -40,7 +41,7 @@ public class OverlayHandler : IOverlayHandler

#region Constructor

public OverlayHandler(IEventAggregator eventAggregator, ICraftingRecipeManager craftingRecipeManager, INewWorldDataStore newWorldDataStore,
public OverlayHandler(IEventAggregator eventAggregator, ISettingsManager settingsManager, ICraftingRecipeManager craftingRecipeManager, INewWorldDataStore newWorldDataStore,
IOcrHandler ocrHandler, IPriceManager priceManager, IScreenProcessHandler screenProcessHandler)
{
// Init IEventAggregator
Expand All @@ -51,6 +52,7 @@ public OverlayHandler(IEventAggregator eventAggregator, ICraftingRecipeManager c
_eventAggregator.GetEvent<RoiImageReadyEvent>().Subscribe(HandleRoiImageReadyEvent);

// Init services
_settingsManager = settingsManager;
_craftingRecipeManager = craftingRecipeManager;
_newWorldDataStore = newWorldDataStore;
_ocrHandler = ocrHandler;
Expand Down Expand Up @@ -112,11 +114,6 @@ private void DrawGraphics(object? sender, DrawGraphicsEventArgs e)
return;
}

_window.X = _overlayX;
_window.Y = _overlayY;
_window.Width = _overlayWidth;
_window.Height = _overlayHeigth;

string itemName = _itemName;
string itemId = _newWorldDataStore.GetItemId(itemName);
var craftingRecipe = string.IsNullOrWhiteSpace(itemId)
Expand All @@ -136,10 +133,11 @@ private void DrawGraphics(object? sender, DrawGraphicsEventArgs e)
private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName)
{
NwmarketpriceJson nwmarketpriceJson = _priceManager.GetPriceData(itemName);
List<NwmarketpriceJson> extendedNwmarketpriceJson = _priceManager.GetExtendedPriceData(itemName);

string infoItemName = itemName;
string infoPrice = "Loading...";
string infoPriceAvg = string.Empty;
string infoPriceAvg = string.Empty;

if (!string.IsNullOrWhiteSpace(nwmarketpriceJson.item_name))
{
Expand All @@ -152,12 +150,35 @@ private void DrawGraphicsItem(DrawGraphicsEventArgs e, string itemName)
// Do not show Bind on pickup items.
if (!_newWorldDataStore.IsBindOnPickup(itemName))
{
int ExtendedTooltipOffset = _settingsManager.Settings.ExtendedTooltipEnabled ? Math.Min(extendedNwmarketpriceJson.Count, 3) * 20 : 0;
// Add some extra margin
ExtendedTooltipOffset = ExtendedTooltipOffset == 0 ? 0 : ExtendedTooltipOffset + 40;

_window.X = _overlayX;
_window.Y = _overlayY - ExtendedTooltipOffset;
_window.Width = _overlayWidth;
_window.Height = _overlayHeigth + ExtendedTooltipOffset;

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, infoPrice);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 60, infoPriceAvg);
gfx.DrawRectangle(_brushes["border"], 0, 0, _overlayWidth, _overlayHeigth, 1);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, ExtendedTooltipOffset+20, infoItemName);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, ExtendedTooltipOffset+40, infoPrice);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, ExtendedTooltipOffset+60, infoPriceAvg);
gfx.DrawRectangle(_brushes["border"], 0, 0, _overlayWidth, _overlayHeigth + ExtendedTooltipOffset, 1);

// Extended text
if (ExtendedTooltipOffset > 0)
{
for (int i = 0; i < Math.Min(extendedNwmarketpriceJson.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) " +
$"{craftingCosts.ToString("F2")} (Craft) " +
$"{extendedNwmarketpriceJson[i].recent_lowest_price - craftingCosts:F2} (Profit)");
}
gfx.DrawRectangle(_brushes["border"], 0, 0, _overlayWidth, ExtendedTooltipOffset, 1);
}
}
else
{
Expand All @@ -183,6 +204,11 @@ private void DrawGraphicsRecipe(DrawGraphicsEventArgs e, CraftingRecipe crafting
infoPriceAvg = string.IsNullOrWhiteSpace(infoPriceAvg) ? infoPriceAvg : $"{infoPriceAvg} (15-day avg) ({nwmarketpriceJson.last_checked_string})";
}

_window.X = _overlayX;
_window.Y = _overlayY;
_window.Width = _overlayWidth;
_window.Height = _overlayHeigth;

var gfx = e.Graphics;
gfx.ClearScene(_brushes["background"]);
gfx.DrawText(_fonts["consolas"], _brushes["text"], 20, 20, infoItemName);
Expand Down
147 changes: 143 additions & 4 deletions NewWorldCompanion.Services/PriceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;

namespace NewWorldCompanion.Services
Expand All @@ -22,6 +23,7 @@ public class PriceManager : IPriceManager
private readonly ISettingsManager _settingsManager;
private readonly IHttpClientHandler _httpClientHandler;
private readonly INewWorldDataStore _newWorldDataStore;
private readonly IRelatedPriceManager _relatedPriceManager;

private readonly object priceRequestLock = new object();

Expand All @@ -34,7 +36,7 @@ public class PriceManager : IPriceManager

#region Constructor

public PriceManager(IEventAggregator eventAggregator, ILogger<PriceManager> logger, ISettingsManager settingsManager, IHttpClientHandler httpClientHandler, INewWorldDataStore newWorldDataStore)
public PriceManager(IEventAggregator eventAggregator, ILogger<PriceManager> logger, ISettingsManager settingsManager, IHttpClientHandler httpClientHandler, INewWorldDataStore newWorldDataStore, IRelatedPriceManager relatedPriceManager)
{
// Init IEventAggregator
_eventAggregator = eventAggregator;
Expand All @@ -46,9 +48,10 @@ public PriceManager(IEventAggregator eventAggregator, ILogger<PriceManager> logg
_settingsManager = settingsManager;
_httpClientHandler = httpClientHandler;
_newWorldDataStore = newWorldDataStore;
_relatedPriceManager = relatedPriceManager;

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

#endregion
Expand Down Expand Up @@ -108,8 +111,135 @@ public NwmarketpriceJson GetPriceData(string itemName)
return _priceCache.GetValueOrDefault(itemName, nwmarketpriceJson); ;
}

public double GetCraftingCosts(string itemId)
{
var recipe = _newWorldDataStore.GetCraftingRecipeDetails(itemId);
double craftingCosts = 0;

var ingredient = string.Empty;
var nwmarketpriceJson = new NwmarketpriceJson();
var qty = recipe.Qty1;
if (!string.IsNullOrWhiteSpace(recipe.Ingredient1) && qty != 0)
{
ingredient = Char.IsDigit(recipe.Ingredient1.Last()) ? recipe.Ingredient1 : $"{recipe.Ingredient1}T1";

string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty;
ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName);
UpdatePriceData(ingredientName);
nwmarketpriceJson = GetPriceData(ingredientName);
craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty);
}

qty = recipe.Qty2;
if (!string.IsNullOrWhiteSpace(recipe.Ingredient2) && qty != 0)
{
ingredient = Char.IsDigit(recipe.Ingredient2.Last()) ? recipe.Ingredient2 : $"{recipe.Ingredient2}T1";

string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty;
ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName);
UpdatePriceData(ingredientName);
nwmarketpriceJson = GetPriceData(ingredientName);
craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty);
}

qty = recipe.Qty3;
if (!string.IsNullOrWhiteSpace(recipe.Ingredient3) && qty != 0)
{
ingredient = Char.IsDigit(recipe.Ingredient3.Last()) ? recipe.Ingredient3 : $"{recipe.Ingredient3}T1";

string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty;
ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName);
UpdatePriceData(ingredientName);
nwmarketpriceJson = GetPriceData(ingredientName);
craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty);
}

qty = recipe.Qty4;
if (!string.IsNullOrWhiteSpace(recipe.Ingredient4) && qty != 0)
{
ingredient = Char.IsDigit(recipe.Ingredient4.Last()) ? recipe.Ingredient4 : $"{recipe.Ingredient4}T1";

string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty;
ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName);
UpdatePriceData(ingredientName);
nwmarketpriceJson = GetPriceData(ingredientName);
craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty);
}

qty = recipe.Qty5;
if (!string.IsNullOrWhiteSpace(recipe.Ingredient5) && qty != 0)
{
ingredient = Char.IsDigit(recipe.Ingredient5.Last()) ? recipe.Ingredient5 : $"{recipe.Ingredient5}T1";

string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty;
ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName);
UpdatePriceData(ingredientName);
nwmarketpriceJson = GetPriceData(ingredientName);
craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty);
}

qty = recipe.Qty6;
if (!string.IsNullOrWhiteSpace(recipe.Ingredient6) && qty != 0)
{
ingredient = Char.IsDigit(recipe.Ingredient6.Last()) ? recipe.Ingredient6 : $"{recipe.Ingredient6}T1";

string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty;
ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName);
UpdatePriceData(ingredientName);
nwmarketpriceJson = GetPriceData(ingredientName);
craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty);
}

qty = recipe.Qty7;
if (!string.IsNullOrWhiteSpace(recipe.Ingredient7) && qty != 0)
{
ingredient = Char.IsDigit(recipe.Ingredient7.Last()) ? recipe.Ingredient7 : $"{recipe.Ingredient7}T1";

string ingredientName = _newWorldDataStore.GetItem(ingredient)?.Name ?? string.Empty;
ingredientName = _newWorldDataStore.GetItemLocalisation(ingredientName);
UpdatePriceData(ingredientName);
nwmarketpriceJson = GetPriceData(ingredientName);
craftingCosts = craftingCosts + (nwmarketpriceJson.recent_lowest_price * qty);
}

return craftingCosts;
}

public List<NwmarketpriceJson> GetExtendedPriceData(string itemName)
{
var extendedPriceDataList = new List<NwmarketpriceJson>();
var itemId = _newWorldDataStore.GetItemId(itemName);
var overlayResource = _relatedPriceManager.PersistableOverlayResources.FirstOrDefault(item => item.ItemId.Equals(itemId));
if (overlayResource != null)
{
foreach (var recipe in overlayResource.PersistableOverlayResourceRecipes)
{
if (!recipe.IsVisible) continue;

string recipeName = _newWorldDataStore.GetItem(recipe.ItemId)?.Name ?? string.Empty;
recipeName = _newWorldDataStore.GetItemLocalisation(recipeName);
if (string.IsNullOrWhiteSpace(recipeName))
{
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);
Expand Down Expand Up @@ -164,6 +294,8 @@ public void UpdatePriceData(string itemName)
last_checked = DateTime.MinValue,
};
}

//Thread.Sleep(100);
}
catch (Exception ex)
{
Expand All @@ -174,7 +306,14 @@ public void UpdatePriceData(string itemName)
// Always remove from queue, even with exceptions.
_priceRequestQueue.RemoveAll(item => item.Equals(itemName));
_priceRequestQueueBusy = false;
Task.Delay(100).Wait();


var sw = new Stopwatch();
sw.Start();
//Task delay = Task.Delay(100);
//await delay;
Task.Delay(250).Wait();
Debug.WriteLine($"async: Running for {sw.Elapsed.TotalSeconds} seconds");

if (_priceRequestQueue.Any())
{
Expand Down
6 changes: 4 additions & 2 deletions NewWorldCompanion/Config/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
<targets>
<!--
Default layout:
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}
Example:
<target name="logfile" xsi:type="File" layout="${longdate}|${level:uppercase=true}|${logger}|${message:withexception=true}" fileName="Logging/logging.log" />
<target name="logfile" xsi:type="File" layout="${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=ToString}" maxArchiveFiles="4" archiveAboveSize="1024000" fileName="Logging/logging.log" />
-->
<target name="logfile" xsi:type="File" maxArchiveFiles="4" archiveAboveSize="1024000" fileName="Logging/logging.log" />
<target name="logfile" xsi:type="File" layout="${longdate}|${level:uppercase=true}|${logger}|${message:exceptionSeparator=\r\n:withException=true}" maxArchiveFiles="4" archiveAboveSize="1024000" fileName="Logging/logging.log" />
<target name="logconsole" xsi:type="Console" />
</targets>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public bool ToggleExtendedTooltip
_toggleExtendedTooltip = value;
_settingsManager.Settings.ExtendedTooltipEnabled = value;
_settingsManager.SaveSettings();
// TODO: Notify tooltip handler
}
}

Expand Down
4 changes: 2 additions & 2 deletions NewWorldCompanion/common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<FileVersion>1.0.9.1</FileVersion>
<Version>1.0.9.1</Version>
<FileVersion>1.0.9.2</FileVersion>
<Version>1.0.9.2</Version>
<Copyright>Copyright © 2022</Copyright>
<TargetFramework>net6.0-windows</TargetFramework>
</PropertyGroup>
Expand Down

0 comments on commit 9b2b712

Please sign in to comment.