Skip to content

Commit

Permalink
Merge pull request #67 from Stuff-Mods/refresh
Browse files Browse the repository at this point in the history
MIsc. fixes
  • Loading branch information
DevilPepper authored May 17, 2021
2 parents ef5aead + 736cdc5 commit 6e61cd2
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 97 deletions.
23 changes: 16 additions & 7 deletions ItemBoxTracker/src/Plugin/Converter/InventoryToAmountsText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@
using MHWItemBoxTracker.Model;

namespace MHWItemBoxTracker.Converter {
public class InventoryToAmountsText : GenericValueConverter<InventoryItemModel, string> {
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<string> {
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}";
}
Expand Down
10 changes: 7 additions & 3 deletions ItemBoxTracker/src/Plugin/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,19 +32,21 @@ 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();
});
}

public void Unload() {
Dispatcher.Dispatch(() => {
Inventory.Unsubscribe();
Events.Unsubscribe();
GUI.Unload();
Overlay.UnregisterWidget(GUI);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@
Content="{Binding Item}" />
<TextBlock
Grid.Column="1"
Text="{Binding Path=., Converter={convert:InventoryToAmountsText}}"
Foreground="#ffbfbfbf"
HorizontalAlignment="Right"
Padding="0,0,10,0" />
Padding="0,0,10,0">
<TextBlock.Text>
<MultiBinding Converter="{convert:InventoryToAmountsText}">
<Binding Path="AmountInBox" />
<Binding Path="AmountInPouch" />
<Binding Path="AmountCraftable" />
<Binding Path="Item.Amount" />
<Binding Path="TrackPouch" />
<Binding Path="TrackBox" />
<Binding Path="TrackCraftable" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="125" />
Expand Down
48 changes: 48 additions & 0 deletions ItemBoxTracker/src/Plugin/Service/EventService.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
64 changes: 64 additions & 0 deletions ItemBoxTracker/src/Plugin/Service/HunterPieService.cs
Original file line number Diff line number Diff line change
@@ -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<int, int> FindItemsInBox(HashSet<int> ids) {
return Context.Player.ItemBox?.FindItemsInBox(ids) ?? new();
}

public Dictionary<int, int> FindItemsInPouch(HashSet<int> ids) {
return Context.Player.Inventory?.FindItemsAndAmmos(ids)?.ToDictionary(i => i.ItemId, i => i.Amount) ?? new();
}

public HashSet<int> GetMaterialsForRecipes(List<Recipe> recipes) {
return recipes
.SelectMany(r => r.MaterialsNeeded)
.Select(m => m.ItemId)
.ToHashSet();
}

public int FindCraftableInBox(List<Recipe> recipeList) {
return FindCraftable(recipeList, ids => SearchBox(ids));
}

public int FindCraftableInPouch(List<Recipe> recipeList) {
return FindCraftable(recipeList, ids => SearchPouch(ids));
}
public Dictionary<int, List<Recipe>> FindRecipes(HashSet<int> ids) {
return ids.ToDictionary(id => id, id => Recipes.FindRecipes(id) ?? new());
}

private int FindCraftable(List<Recipe> recipeList, Func<HashSet<int>, 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<int> ids) {
return (Context.Player.ItemBox?.FindItemsInBox(ids) ?? new())
.Select(
i => new sItem() {
ItemId = i.Key,
Amount = i.Value,
})
.ToArray();
}
private sItem[] SearchPouch(HashSet<int> ids) {
return Context.Player.Inventory?.FindItemsAndAmmos(ids);
}
}
}
91 changes: 15 additions & 76 deletions ItemBoxTracker/src/Plugin/Service/InventoryService.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,29 @@

using System;
using System.Linq;
using System.Threading;
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 {
// TODO: Probably need this
// private static readonly SemaphoreSlim locke = new(1, 1);
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;
}

public async Task<InventoryModel> LoadAsync() {
if (Data == null) {
Settings = await Config.LoadAsync();
Data = new() {
InVillage = Context.Player.InHarvestZone,
};
Data = new();
}
Refresh();
return Data;
}

public void Refresh() {
Data.InVillage = HP.InHarvestZone;
var always = Settings.Always.Tracking.Select(i => new InventoryItemModel() {
Item = i,
TrackInVillage = true,
Expand Down Expand Up @@ -69,74 +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 += EnterVillage;
player.OnVillageLeave += LeaveVillage;
player.ItemBox.OnItemBoxUpdate += Refresh;
// TODO: subscribe to save event?
}

public void Unsubscribe() {
var player = Context.Player;
player.OnVillageEnter -= EnterVillage;
player.OnVillageLeave -= LeaveVillage;
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();
});
}
}
}
4 changes: 2 additions & 2 deletions ItemBoxTracker/src/Plugin/Views/InventoryView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../GUI/InventoryItemDataTemplate.xaml" />
<ResourceDictionary Source="../GUI/ItemDataTemplate.xaml" />
<ResourceDictionary Source="../Resource/InventoryItemDataTemplate.xaml" />
<ResourceDictionary Source="../Resource/ItemDataTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
Expand Down
13 changes: 8 additions & 5 deletions ItemBoxTracker/src/Plugin/Views/InventoryView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -21,14 +28,10 @@ public InventoryView(InventoryModel Inventory) : base() {
public async Task Initialize() {
widgetSettings = await Plugin.LoadJson<ItemBoxWidgetSettings>(settingsJson);
ApplySettings();

WidgetHasContent = true;
ChangeVisibility();
}

public void Unload() {
WidgetHasContent = false;
ChangeVisibility();
ShouldShow = false;
}

public override void EnterWidgetDesignMode() {
Expand Down
4 changes: 2 additions & 2 deletions ItemBoxTracker/src/Plugin/Views/SettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../GUI/ItemDataTemplate.xaml" />
<ResourceDictionary Source="../GUI/ButtonStyle.xaml" />
<ResourceDictionary Source="../Resource/ItemDataTemplate.xaml" />
<ResourceDictionary Source="../Resource/ButtonStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Expand Down

0 comments on commit 6e61cd2

Please sign in to comment.