refactoring

This commit is contained in:
Boki 2026-02-13 08:42:46 -05:00
parent 696fd07e86
commit 50d32abd49
20 changed files with 334 additions and 225 deletions

View file

@ -0,0 +1,20 @@
using Poe2Trade.Core;
namespace Poe2Trade.Inventory;
public interface IInventoryManager
{
InventoryTracker Tracker { get; }
bool IsAtOwnHideout { get; }
string SellerAccount { get; }
void SetLocation(bool atHome, string? seller = null);
Task ScanInventory(PostAction defaultAction = PostAction.Stash);
Task ClearToStash();
Task<bool> EnsureAtOwnHideout();
Task ProcessInventory();
Task<bool> WaitForAreaTransition(int timeoutMs, Func<Task>? triggerAction = null);
Task<(int X, int Y)?> FindAndClickNameplate(string name, int maxRetries = 3, int retryDelayMs = 1000);
Task DepositItemsToStash(List<PlacedItem> items);
Task<bool> SalvageItems(List<PlacedItem> items);
(bool[,] Grid, List<PlacedItem> Items, int Free) GetInventoryState();
}

View file

@ -6,7 +6,7 @@ using Serilog;
namespace Poe2Trade.Inventory;
public class InventoryManager
public class InventoryManager : IInventoryManager
{
private static readonly string SalvageTemplate = Path.Combine("assets", "salvage.png");
@ -14,15 +14,15 @@ public class InventoryManager
private bool _atOwnHideout = true;
private string _sellerAccount = "";
private readonly GameController _game;
private readonly ScreenReader _screen;
private readonly ClientLogWatcher _logWatcher;
private readonly AppConfig _config;
private readonly IGameController _game;
private readonly IScreenReader _screen;
private readonly IClientLogWatcher _logWatcher;
private readonly SavedSettings _config;
public bool IsAtOwnHideout => _atOwnHideout;
public string SellerAccount => _sellerAccount;
public InventoryManager(GameController game, ScreenReader screen, ClientLogWatcher logWatcher, AppConfig config)
public InventoryManager(IGameController game, IScreenReader screen, IClientLogWatcher logWatcher, SavedSettings config)
{
_game = game;
_screen = screen;
@ -40,7 +40,7 @@ public class InventoryManager
{
Log.Information("Scanning inventory...");
await _game.FocusGame();
await Helpers.Sleep(300);
await Helpers.Sleep(Delays.PostFocus);
await _game.OpenInventory();
var result = await _screen.Grid.Scan("inventory");
@ -54,7 +54,7 @@ public class InventoryManager
Tracker.InitFromScan(cells, result.Items, defaultAction);
await _game.PressEscape();
await Helpers.Sleep(300);
await Helpers.Sleep(Delays.PostFocus);
}
public async Task ClearToStash()
@ -83,7 +83,7 @@ public class InventoryManager
}
await _game.FocusGame();
await Helpers.Sleep(300);
await Helpers.Sleep(Delays.PostFocus);
var arrived = await WaitForAreaTransition(_config.TravelTimeoutMs, () => _game.GoToHideout());
if (!arrived)
@ -92,7 +92,7 @@ public class InventoryManager
return false;
}
await Helpers.Sleep(1500);
await Helpers.Sleep(Delays.PostTravel);
_atOwnHideout = true;
_sellerAccount = "";
return true;
@ -108,23 +108,13 @@ public class InventoryManager
Log.Error("Could not find Stash nameplate");
return;
}
await Helpers.Sleep(1000);
await Helpers.Sleep(Delays.PostStashOpen);
var inventoryLayout = GridLayouts.Inventory;
Log.Information("Depositing {Count} items to stash", items.Count);
await _game.HoldCtrl();
foreach (var item in items)
{
var center = _screen.Grid.GetCellCenter(inventoryLayout, item.Row, item.Col);
await _game.LeftClickAt(center.X, center.Y);
await Helpers.Sleep(150);
}
await _game.ReleaseCtrl();
await Helpers.Sleep(500);
await CtrlClickItems(items, GridLayouts.Inventory);
await _game.PressEscape();
await Helpers.Sleep(500);
await Helpers.Sleep(Delays.PostEscape);
Log.Information("Items deposited to stash");
}
@ -138,35 +128,38 @@ public class InventoryManager
Log.Error("Could not find Salvage nameplate");
return false;
}
await Helpers.Sleep(1000);
await Helpers.Sleep(Delays.PostStashOpen);
var salvageBtn = await _screen.TemplateMatch(SalvageTemplate);
if (salvageBtn != null)
{
await _game.LeftClickAt(salvageBtn.X, salvageBtn.Y);
await Helpers.Sleep(500);
await Helpers.Sleep(Delays.PostEscape);
}
else
{
Log.Warning("Could not find salvage button via template match");
}
var inventoryLayout = GridLayouts.Inventory;
Log.Information("Salvaging {Count} inventory items", items.Count);
await CtrlClickItems(items, GridLayouts.Inventory);
await _game.PressEscape();
await Helpers.Sleep(Delays.PostEscape);
return true;
}
private async Task CtrlClickItems(List<PlacedItem> items, GridLayout layout, int clickDelayMs = Delays.ClickInterval)
{
await _game.HoldCtrl();
foreach (var item in items)
{
var center = _screen.Grid.GetCellCenter(inventoryLayout, item.Row, item.Col);
var center = _screen.Grid.GetCellCenter(layout, item.Row, item.Col);
await _game.LeftClickAt(center.X, center.Y);
await Helpers.Sleep(150);
await Helpers.Sleep(clickDelayMs);
}
await _game.ReleaseCtrl();
await Helpers.Sleep(500);
await _game.PressEscape();
await Helpers.Sleep(500);
return true;
await Helpers.Sleep(Delays.PostEscape);
}
public async Task ProcessInventory()
@ -201,7 +194,7 @@ public class InventoryManager
catch (Exception ex)
{
Log.Error(ex, "Inventory processing failed");
try { await _game.PressEscape(); await Helpers.Sleep(300); } catch { }
try { await _game.PressEscape(); await Helpers.Sleep(Delays.PostFocus); } catch { }
Tracker.Clear();
}
}