From f21f1b243dc0c4396e1eecf6a03aa06d69536f4c Mon Sep 17 00:00:00 2001 From: SupaStuff Date: Sun, 16 May 2021 20:07:57 -0400 Subject: [PATCH 1/5] fix(init): don't refresh on init because one of the events will fire and then we can properly refresh --- .../src/Plugin/Service/InventoryService.cs | 31 ++++--------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/ItemBoxTracker/src/Plugin/Service/InventoryService.cs b/ItemBoxTracker/src/Plugin/Service/InventoryService.cs index 190900c..6fb0a9f 100644 --- a/ItemBoxTracker/src/Plugin/Service/InventoryService.cs +++ b/ItemBoxTracker/src/Plugin/Service/InventoryService.cs @@ -1,7 +1,6 @@ using System; using System.Linq; -using System.Threading; using System.Threading.Tasks; using HunterPie.Core; using HunterPie.Core.Craft; @@ -11,8 +10,6 @@ namespace MHWItemBoxTracker.Service { public class InventoryService { - // TODO: Probably need this - // private static readonly SemaphoreSlim locke = new(1, 1); private Game Context; private ConfigService Config; private SettingsModel Settings; @@ -26,15 +23,13 @@ public InventoryService(Game Context, ConfigService Config) { public async Task LoadAsync() { if (Data == null) { Settings = await Config.LoadAsync(); - Data = new() { - InVillage = Context.Player.InHarvestZone, - }; + Data = new(); } - Refresh(); return Data; } public void Refresh() { + Data.InVillage = Context.Player.InHarvestZone; var always = Settings.Always.Tracking.Select(i => new InventoryItemModel() { Item = i, TrackInVillage = true, @@ -108,35 +103,21 @@ public void Refresh() { // TODO: Probably move these to an event handler class or something public void Subscribe() { var player = Context.Player; - player.OnVillageEnter += EnterVillage; - player.OnVillageLeave += LeaveVillage; + player.OnVillageEnter += Refresh; + player.OnVillageLeave += Refresh; player.ItemBox.OnItemBoxUpdate += Refresh; // TODO: subscribe to save event? } public void Unsubscribe() { var player = Context.Player; - player.OnVillageEnter -= EnterVillage; - player.OnVillageLeave -= LeaveVillage; + player.OnVillageEnter -= Refresh; + player.OnVillageLeave -= Refresh; player.ItemBox.OnItemBoxUpdate -= Refresh; } public void Refresh(object source, EventArgs e) { Dispatcher.Dispatch(Refresh); } - - private void EnterVillage(object source, EventArgs e) { - Dispatcher.Dispatch(() => { - Data.InVillage = true; - Refresh(); - }); - } - - private void LeaveVillage(object source, EventArgs e) { - Dispatcher.Dispatch(() => { - Data.InVillage = false; - Refresh(); - }); - } } } From fdfcc8ec96c8015d574374a042cb8ba3c78b079b Mon Sep 17 00:00:00 2001 From: SupaStuff Date: Sun, 16 May 2021 20:36:50 -0400 Subject: [PATCH 2/5] fix(overlay): refresh displayed counts correctly --- .../Converter/InventoryToAmountsText.cs | 23 +++++++++++++------ .../Plugin/GUI/InventoryItemDataTemplate.xaml | 15 ++++++++++-- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/ItemBoxTracker/src/Plugin/Converter/InventoryToAmountsText.cs b/ItemBoxTracker/src/Plugin/Converter/InventoryToAmountsText.cs index d3f1970..a00950e 100644 --- a/ItemBoxTracker/src/Plugin/Converter/InventoryToAmountsText.cs +++ b/ItemBoxTracker/src/Plugin/Converter/InventoryToAmountsText.cs @@ -2,13 +2,22 @@ using MHWItemBoxTracker.Model; namespace MHWItemBoxTracker.Converter { - public class InventoryToAmountsText : GenericValueConverter { - public override string Convert(InventoryItemModel value, object parameter) { - var pouch = value.TrackPouch ? $"{value.AmountInPouch}" : ""; - var box = value.TrackBox ? $"{value.AmountInBox}" : ""; - var craftable = value.TrackCraftable ? $" (+{value.AmountCraftable})" : ""; - var divider = (value.TrackPouch && value.TrackBox) ? " | " : ""; - var wanted = (value.TrackPouch || value.TrackBox || value.TrackCraftable) ? $" / {value.Item.Amount}" : ""; + public class InventoryToAmountsText : GenericMultiValueConverter { + public override string Convert(object[] values) { + var AmountInBox = (int)values[0]; + var AmountInPouch = (int)values[1]; + var AmountCraftable = (int)values[2]; + var AmountWanted = (int)values[3]; + + var TrackPouch = (bool)values[4]; + var TrackBox = (bool)values[5]; + var TrackCraftable = (bool)values[6]; + + var pouch = TrackPouch ? $"{AmountInPouch}" : ""; + var box = TrackBox ? $"{AmountInBox}" : ""; + var craftable = TrackCraftable ? $" (+{AmountCraftable})" : ""; + var divider = (TrackPouch && TrackBox) ? " | " : ""; + var wanted = (TrackPouch || TrackBox || TrackCraftable) ? $" / {AmountWanted}" : ""; return $"{pouch}{divider}{box}{craftable}{wanted}"; } diff --git a/ItemBoxTracker/src/Plugin/GUI/InventoryItemDataTemplate.xaml b/ItemBoxTracker/src/Plugin/GUI/InventoryItemDataTemplate.xaml index f0b845d..6d832bf 100644 --- a/ItemBoxTracker/src/Plugin/GUI/InventoryItemDataTemplate.xaml +++ b/ItemBoxTracker/src/Plugin/GUI/InventoryItemDataTemplate.xaml @@ -20,10 +20,21 @@ Content="{Binding Item}" /> + Padding="0,0,10,0"> + + + + + + + + + + + + From 839b962a11d4f500cea76b0b85f15a6112097085 Mon Sep 17 00:00:00 2001 From: SupaStuff Date: Sun, 16 May 2021 20:52:21 -0400 Subject: [PATCH 3/5] fix(refresh): update on inventory(pouch) update --- ItemBoxTracker/src/Plugin/Service/InventoryService.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ItemBoxTracker/src/Plugin/Service/InventoryService.cs b/ItemBoxTracker/src/Plugin/Service/InventoryService.cs index 6fb0a9f..438b242 100644 --- a/ItemBoxTracker/src/Plugin/Service/InventoryService.cs +++ b/ItemBoxTracker/src/Plugin/Service/InventoryService.cs @@ -106,6 +106,7 @@ public void Subscribe() { player.OnVillageEnter += Refresh; player.OnVillageLeave += Refresh; player.ItemBox.OnItemBoxUpdate += Refresh; + player.Inventory.OnInventoryUpdate += Refresh; // TODO: subscribe to save event? } @@ -114,6 +115,7 @@ public void Unsubscribe() { player.OnVillageEnter -= Refresh; player.OnVillageLeave -= Refresh; player.ItemBox.OnItemBoxUpdate -= Refresh; + player.Inventory.OnInventoryUpdate -= Refresh; } public void Refresh(object source, EventArgs e) { From 9b45d39d89e847b41611eeb6ffc9b1cd8217c06c Mon Sep 17 00:00:00 2001 From: SupaStuff Date: Sun, 16 May 2021 21:00:45 -0400 Subject: [PATCH 4/5] refactor(Resources): rename GUI directory to Resources --- ItemBoxTracker/src/Plugin/{GUI => Resource}/ButtonStyle.xaml | 0 .../Plugin/{GUI => Resource}/InventoryItemDataTemplate.xaml | 0 .../src/Plugin/{GUI => Resource}/ItemDataTemplate.xaml | 0 .../src/Plugin/{GUI => Resource}/ProgressBarDataTemplate.xaml | 0 ItemBoxTracker/src/Plugin/Views/InventoryView.xaml | 4 ++-- ItemBoxTracker/src/Plugin/Views/SettingsView.xaml | 4 ++-- 6 files changed, 4 insertions(+), 4 deletions(-) rename ItemBoxTracker/src/Plugin/{GUI => Resource}/ButtonStyle.xaml (100%) rename ItemBoxTracker/src/Plugin/{GUI => Resource}/InventoryItemDataTemplate.xaml (100%) rename ItemBoxTracker/src/Plugin/{GUI => Resource}/ItemDataTemplate.xaml (100%) rename ItemBoxTracker/src/Plugin/{GUI => Resource}/ProgressBarDataTemplate.xaml (100%) diff --git a/ItemBoxTracker/src/Plugin/GUI/ButtonStyle.xaml b/ItemBoxTracker/src/Plugin/Resource/ButtonStyle.xaml similarity index 100% rename from ItemBoxTracker/src/Plugin/GUI/ButtonStyle.xaml rename to ItemBoxTracker/src/Plugin/Resource/ButtonStyle.xaml diff --git a/ItemBoxTracker/src/Plugin/GUI/InventoryItemDataTemplate.xaml b/ItemBoxTracker/src/Plugin/Resource/InventoryItemDataTemplate.xaml similarity index 100% rename from ItemBoxTracker/src/Plugin/GUI/InventoryItemDataTemplate.xaml rename to ItemBoxTracker/src/Plugin/Resource/InventoryItemDataTemplate.xaml diff --git a/ItemBoxTracker/src/Plugin/GUI/ItemDataTemplate.xaml b/ItemBoxTracker/src/Plugin/Resource/ItemDataTemplate.xaml similarity index 100% rename from ItemBoxTracker/src/Plugin/GUI/ItemDataTemplate.xaml rename to ItemBoxTracker/src/Plugin/Resource/ItemDataTemplate.xaml diff --git a/ItemBoxTracker/src/Plugin/GUI/ProgressBarDataTemplate.xaml b/ItemBoxTracker/src/Plugin/Resource/ProgressBarDataTemplate.xaml similarity index 100% rename from ItemBoxTracker/src/Plugin/GUI/ProgressBarDataTemplate.xaml rename to ItemBoxTracker/src/Plugin/Resource/ProgressBarDataTemplate.xaml diff --git a/ItemBoxTracker/src/Plugin/Views/InventoryView.xaml b/ItemBoxTracker/src/Plugin/Views/InventoryView.xaml index e0e393c..07b729f 100644 --- a/ItemBoxTracker/src/Plugin/Views/InventoryView.xaml +++ b/ItemBoxTracker/src/Plugin/Views/InventoryView.xaml @@ -18,8 +18,8 @@ - - + + diff --git a/ItemBoxTracker/src/Plugin/Views/SettingsView.xaml b/ItemBoxTracker/src/Plugin/Views/SettingsView.xaml index 43537f2..b86c65a 100644 --- a/ItemBoxTracker/src/Plugin/Views/SettingsView.xaml +++ b/ItemBoxTracker/src/Plugin/Views/SettingsView.xaml @@ -14,8 +14,8 @@ - - + + From 736cdc52ce10d5b961bac387741798389cad3da6 Mon Sep 17 00:00:00 2001 From: SupaStuff Date: Sun, 16 May 2021 22:39:50 -0400 Subject: [PATCH 5/5] refactor(Service): break InventoryService up into smaller services fix(loading): only display widget while player is logged in --- ItemBoxTracker/src/Plugin/Main.cs | 10 ++- .../src/Plugin/Service/EventService.cs | 48 +++++++++++++ .../src/Plugin/Service/HunterPieService.cs | 64 +++++++++++++++++ .../src/Plugin/Service/InventoryService.cs | 72 ++++--------------- .../src/Plugin/Views/InventoryView.xaml.cs | 13 ++-- 5 files changed, 141 insertions(+), 66 deletions(-) create mode 100644 ItemBoxTracker/src/Plugin/Service/EventService.cs create mode 100644 ItemBoxTracker/src/Plugin/Service/HunterPieService.cs diff --git a/ItemBoxTracker/src/Plugin/Main.cs b/ItemBoxTracker/src/Plugin/Main.cs index 8db07d5..1569e46 100644 --- a/ItemBoxTracker/src/Plugin/Main.cs +++ b/ItemBoxTracker/src/Plugin/Main.cs @@ -14,6 +14,8 @@ public class Main : IPlugin, ISettingsOwner { public Game Context { get; set; } private readonly ConfigService Config = new(); + private EventService Events; + private HunterPieService HP; private InventoryService Inventory; private InventoryView GUI; @@ -30,11 +32,13 @@ public Main() { public void Initialize(Game context) { Context = context; - Inventory = new(Context, Config); + HP = new(Context); + Inventory = new(HP, Config); Dispatcher.Dispatch(async () => { GUI = new(await Inventory.LoadAsync()); - Inventory.Subscribe(); + Events = new(Context, GUI, Inventory); + Events.Subscribe(); Overlay.RegisterWidget(GUI); await GUI.Initialize(); }); @@ -42,7 +46,7 @@ public void Initialize(Game context) { public void Unload() { Dispatcher.Dispatch(() => { - Inventory.Unsubscribe(); + Events.Unsubscribe(); GUI.Unload(); Overlay.UnregisterWidget(GUI); }); diff --git a/ItemBoxTracker/src/Plugin/Service/EventService.cs b/ItemBoxTracker/src/Plugin/Service/EventService.cs new file mode 100644 index 0000000..bda3646 --- /dev/null +++ b/ItemBoxTracker/src/Plugin/Service/EventService.cs @@ -0,0 +1,48 @@ +using System; +using HunterPie.Core; +using MHWItemBoxTracker.Utils; +using MHWItemBoxTracker.Views; + +namespace MHWItemBoxTracker.Service { + public class EventService { + private Game Context; + InventoryView GUI; + InventoryService Inventory; + public EventService(Game Context, InventoryView GUI, InventoryService Inventory) { + this.Context = Context; + this.GUI = GUI; + this.Inventory = Inventory; + } + + public void Subscribe() { + var player = Context.Player; + player.OnVillageEnter += Refresh; + player.OnVillageLeave += Refresh; + player.ItemBox.OnItemBoxUpdate += Refresh; + player.Inventory.OnInventoryUpdate += Refresh; + player.OnCharacterLogin += ShowWidget; + player.OnCharacterLogout += HideWidget; + // TODO: subscribe to save event? + } + + public void Unsubscribe() { + var player = Context.Player; + player.OnVillageEnter -= Refresh; + player.OnVillageLeave -= Refresh; + player.ItemBox.OnItemBoxUpdate -= Refresh; + player.Inventory.OnInventoryUpdate -= Refresh; + player.OnCharacterLogin -= ShowWidget; + player.OnCharacterLogout -= HideWidget; + } + + public void Refresh(object source, EventArgs e) { + Dispatcher.Dispatch(Inventory.Refresh); + } + public void ShowWidget(object source, EventArgs e) { + Dispatcher.Dispatch(() => GUI.ShouldShow = true); + } + public void HideWidget(object source, EventArgs e) { + Dispatcher.Dispatch(() => GUI.ShouldShow = false); + } + } +} diff --git a/ItemBoxTracker/src/Plugin/Service/HunterPieService.cs b/ItemBoxTracker/src/Plugin/Service/HunterPieService.cs new file mode 100644 index 0000000..7ecef72 --- /dev/null +++ b/ItemBoxTracker/src/Plugin/Service/HunterPieService.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using HunterPie.Core; +using HunterPie.Core.Craft; +using HunterPie.Core.Definitions; + +namespace MHWItemBoxTracker.Service { + public class HunterPieService { + private Game Context; + public bool InHarvestZone { + get => Context.Player.InHarvestZone; + } + public HunterPieService(Game Context) { + this.Context = Context; + } + + public Dictionary FindItemsInBox(HashSet ids) { + return Context.Player.ItemBox?.FindItemsInBox(ids) ?? new(); + } + + public Dictionary FindItemsInPouch(HashSet ids) { + return Context.Player.Inventory?.FindItemsAndAmmos(ids)?.ToDictionary(i => i.ItemId, i => i.Amount) ?? new(); + } + + public HashSet GetMaterialsForRecipes(List recipes) { + return recipes + .SelectMany(r => r.MaterialsNeeded) + .Select(m => m.ItemId) + .ToHashSet(); + } + + public int FindCraftableInBox(List recipeList) { + return FindCraftable(recipeList, ids => SearchBox(ids)); + } + + public int FindCraftableInPouch(List recipeList) { + return FindCraftable(recipeList, ids => SearchPouch(ids)); + } + public Dictionary> FindRecipes(HashSet ids) { + return ids.ToDictionary(id => id, id => Recipes.FindRecipes(id) ?? new()); + } + + private int FindCraftable(List recipeList, Func, sItem[]> FindItems) { + var recipes = recipeList ?? new(); + var ids = GetMaterialsForRecipes(recipes); + var materials = FindItems(ids) ?? new sItem[0]; + return recipes.Select(r => r.Calculate(materials)).Sum(); + } + + private sItem[] SearchBox(HashSet ids) { + return (Context.Player.ItemBox?.FindItemsInBox(ids) ?? new()) + .Select( + i => new sItem() { + ItemId = i.Key, + Amount = i.Value, + }) + .ToArray(); + } + private sItem[] SearchPouch(HashSet ids) { + return Context.Player.Inventory?.FindItemsAndAmmos(ids); + } + } +} diff --git a/ItemBoxTracker/src/Plugin/Service/InventoryService.cs b/ItemBoxTracker/src/Plugin/Service/InventoryService.cs index 438b242..d5a1ee5 100644 --- a/ItemBoxTracker/src/Plugin/Service/InventoryService.cs +++ b/ItemBoxTracker/src/Plugin/Service/InventoryService.cs @@ -1,22 +1,16 @@ - -using System; using System.Linq; using System.Threading.Tasks; -using HunterPie.Core; -using HunterPie.Core.Craft; -using HunterPie.Core.Definitions; using MHWItemBoxTracker.Model; -using MHWItemBoxTracker.Utils; namespace MHWItemBoxTracker.Service { public class InventoryService { - private Game Context; + private HunterPieService HP; private ConfigService Config; private SettingsModel Settings; private InventoryModel Data; - public InventoryService(Game Context, ConfigService Config) { - this.Context = Context; + public InventoryService(HunterPieService HP, ConfigService Config) { + this.HP = HP; this.Config = Config; } @@ -29,7 +23,7 @@ public async Task LoadAsync() { } public void Refresh() { - Data.InVillage = Context.Player.InHarvestZone; + Data.InVillage = HP.InHarvestZone; var always = Settings.Always.Tracking.Select(i => new InventoryItemModel() { Item = i, TrackInVillage = true, @@ -64,62 +58,24 @@ public void Refresh() { foreach (var item in add) { Data.Items.Add(item); } - var ids = Data.Items.Select(i => i.Item.ItemId).ToHashSet(); - var box = Context.Player.ItemBox?.FindItemsInBox(ids) ?? new(); - var pouch = Context.Player.Inventory?.FindItemsAndAmmos(ids)?.ToDictionary(i => i.ItemId, i => i.Amount) ?? new(); - foreach (var item in Data.Items) { - box.TryGetValue(item.Item.ItemId, out int AmountInBox); - pouch.TryGetValue(item.Item.ItemId, out int AmountInPouch); - var recipes = Recipes.FindRecipes(item.Item.ItemId) ?? new(); - var materials = recipes - .SelectMany(r => r.MaterialsNeeded) - .Select(m => m.ItemId) - .ToHashSet(); - - var pouchCraft = item.TrackPouch - ? Context.Player.Inventory?.FindItemsAndAmmos(materials) ?? new sItem[0] - : new sItem[0]; + var ids = Data.Items.Select(i => i.Item.ItemId).ToHashSet(); + var box = HP.FindItemsInBox(ids); + var pouch = HP.FindItemsInPouch(ids); + var recipeList = HP.FindRecipes(ids); - var boxCraft = (item.TrackBox - ? Context.Player.ItemBox?.FindItemsInBox(materials) ?? new() - : new()) - .Select( - i => new sItem() { - ItemId = i.Key, - Amount = i.Value, - }) - .ToArray(); + foreach (var item in Data.Items) { + box.TryGetValue(item.Item.ItemId, out var AmountInBox); + pouch.TryGetValue(item.Item.ItemId, out var AmountInPouch); + recipeList.TryGetValue(item.Item.ItemId, out var recipes); - int AmountCraftable = (item.TrackPouch ? recipes.Select(r => r.Calculate(pouchCraft)).Sum() : 0) - + (item.TrackBox ? recipes.Select(r => r.Calculate(boxCraft)).Sum() : 0); + var AmountCraftable = (item.TrackPouch ? HP.FindCraftableInPouch(recipes) : 0) + + (item.TrackBox ? HP.FindCraftableInBox(recipes) : 0); item.AmountInBox = item.TrackBox ? AmountInBox : 0; item.AmountInPouch = item.TrackPouch ? AmountInPouch : 0; item.AmountCraftable = item.TrackCraftable ? AmountCraftable : 0; } } - - // TODO: Probably move these to an event handler class or something - public void Subscribe() { - var player = Context.Player; - player.OnVillageEnter += Refresh; - player.OnVillageLeave += Refresh; - player.ItemBox.OnItemBoxUpdate += Refresh; - player.Inventory.OnInventoryUpdate += Refresh; - // TODO: subscribe to save event? - } - - public void Unsubscribe() { - var player = Context.Player; - player.OnVillageEnter -= Refresh; - player.OnVillageLeave -= Refresh; - player.ItemBox.OnItemBoxUpdate -= Refresh; - player.Inventory.OnInventoryUpdate -= Refresh; - } - - public void Refresh(object source, EventArgs e) { - Dispatcher.Dispatch(Refresh); - } } } diff --git a/ItemBoxTracker/src/Plugin/Views/InventoryView.xaml.cs b/ItemBoxTracker/src/Plugin/Views/InventoryView.xaml.cs index bf55a9b..9d667e6 100644 --- a/ItemBoxTracker/src/Plugin/Views/InventoryView.xaml.cs +++ b/ItemBoxTracker/src/Plugin/Views/InventoryView.xaml.cs @@ -12,6 +12,13 @@ public partial class InventoryView : Widget { private ItemBoxWidgetSettings widgetSettings { get; set; } public override IWidgetSettings Settings => widgetSettings; private InventoryViewModel VM; + public bool ShouldShow { + set { + WidgetHasContent = value; + ChangeVisibility(); + } + } + public InventoryView(InventoryModel Inventory) : base() { InitializeComponent(); VM = new(Inventory); @@ -21,14 +28,10 @@ public InventoryView(InventoryModel Inventory) : base() { public async Task Initialize() { widgetSettings = await Plugin.LoadJson(settingsJson); ApplySettings(); - - WidgetHasContent = true; - ChangeVisibility(); } public void Unload() { - WidgetHasContent = false; - ChangeVisibility(); + ShouldShow = false; } public override void EnterWidgetDesignMode() {