Workship/Workshoppa/Windows/RepairKitWindow.cs

137 lines
4.4 KiB
C#
Raw Normal View History

2023-10-13 20:08:22 +00:00
using System;
using System.Linq;
using Dalamud.Game.Text;
2023-10-13 20:08:22 +00:00
using Dalamud.Interface;
using Dalamud.Interface.Colors;
using Dalamud.Interface.Components;
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Component.GUI;
using ImGuiNET;
using LLib.GameUI;
2023-10-19 18:46:01 +00:00
using Workshoppa.External;
2023-10-24 22:19:42 +00:00
using Workshoppa.GameData.Shops;
2023-10-13 20:08:22 +00:00
using ValueType = FFXIVClientStructs.FFXIV.Component.GUI.ValueType;
namespace Workshoppa.Windows;
2023-10-24 22:19:42 +00:00
internal sealed class RepairKitWindow : ShopWindow
2023-10-13 20:08:22 +00:00
{
private const int DarkMatterCluster6ItemId = 10386;
private readonly IPluginLog _pluginLog;
private readonly Configuration _configuration;
public RepairKitWindow(WorkshopPlugin plugin, IPluginLog pluginLog,
2023-10-19 18:46:01 +00:00
IGameGui gameGui, IAddonLifecycle addonLifecycle, Configuration configuration,
ExternalPluginHandler externalPluginHandler)
2023-10-24 22:19:42 +00:00
: base("Repair Kits###WorkshoppaRepairKitWindow", "Shop", plugin, pluginLog, gameGui, addonLifecycle, externalPluginHandler)
2023-10-13 20:08:22 +00:00
{
_pluginLog = pluginLog;
_configuration = configuration;
}
2023-10-24 22:19:42 +00:00
protected override bool Enabled => _configuration.EnableRepairKitCalculator;
2023-10-13 20:08:22 +00:00
2023-10-24 22:19:42 +00:00
protected override unsafe void UpdateShopStock(AtkUnitBase* addon)
2023-10-13 20:08:22 +00:00
{
if (GetDarkMatterClusterCount() == 0)
{
2023-10-24 22:19:42 +00:00
ItemForSale = null;
2023-10-13 20:08:22 +00:00
return;
}
if (addon->AtkValuesCount != 625)
{
_pluginLog.Error($"Unexpected amount of atkvalues for Shop addon ({addon->AtkValuesCount})");
2023-10-24 22:19:42 +00:00
ItemForSale = null;
2023-10-13 20:08:22 +00:00
return;
}
var atkValues = addon->AtkValues;
// Check if on 'Current Stock' tab?
if (atkValues[0].UInt != 0)
{
2023-10-24 22:19:42 +00:00
ItemForSale = null;
2023-10-13 20:08:22 +00:00
return;
}
uint itemCount = atkValues[2].UInt;
if (itemCount == 0)
{
2023-10-24 22:19:42 +00:00
ItemForSale = null;
2023-10-13 20:08:22 +00:00
return;
}
2023-10-24 22:19:42 +00:00
ItemForSale = Enumerable.Range(0, (int)itemCount)
2023-10-13 20:08:22 +00:00
.Select(i => new ItemForSale
{
Position = i,
ItemName = atkValues[14 + i].ReadAtkString(),
Price = atkValues[75 + i].UInt,
OwnedItems = atkValues[136 + i].UInt,
ItemId = atkValues[441 + i].UInt,
})
.FirstOrDefault(x => x.ItemId == DarkMatterCluster6ItemId);
}
private int GetDarkMatterClusterCount() => GetItemCount(10335);
2023-10-24 22:19:42 +00:00
protected override int GetCurrencyCount() => GetItemCount(1);
2023-10-13 20:08:22 +00:00
public override void Draw()
{
int darkMatterClusters = GetDarkMatterClusterCount();
2023-10-24 22:19:42 +00:00
if (ItemForSale == null || darkMatterClusters == 0)
2023-10-13 20:08:22 +00:00
{
IsOpen = false;
return;
}
ImGui.Text("Inventory");
ImGui.Indent();
ImGui.Text($"Dark Matter Clusters: {darkMatterClusters:N0}");
2023-10-24 22:19:42 +00:00
ImGui.Text($"Grade 6 Dark Matter: {ItemForSale.OwnedItems:N0}");
2023-10-13 20:08:22 +00:00
ImGui.Unindent();
2023-10-24 22:19:42 +00:00
int missingItems = Math.Max(0, darkMatterClusters * 5 - (int)ItemForSale.OwnedItems);
2023-10-19 18:46:01 +00:00
ImGui.TextColored(missingItems == 0 ? ImGuiColors.HealerGreen : ImGuiColors.DalamudRed,
$"Missing Grade 6 Dark Matter: {missingItems:N0}");
2023-10-13 20:08:22 +00:00
2023-10-24 22:19:42 +00:00
if (PurchaseState != null)
2023-10-13 20:08:22 +00:00
{
HandleNextPurchaseStep();
if (PurchaseState != null)
{
if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.Times, "Cancel Auto-Buy"))
CancelAutoPurchase();
}
2023-10-13 20:08:22 +00:00
}
else
{
int toPurchase = Math.Min(GetMaxItemsToPurchase(), missingItems);
if (toPurchase > 0)
{
2023-10-19 18:46:01 +00:00
if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.DollarSign,
2023-10-24 22:19:42 +00:00
$"Auto-Buy missing Dark Matter for {ItemForSale.Price * toPurchase:N0}{SeIconChar.Gil.ToIconString()}"))
2023-10-13 20:08:22 +00:00
{
2023-10-24 22:19:42 +00:00
StartAutoPurchase(toPurchase);
2023-10-13 20:08:22 +00:00
HandleNextPurchaseStep();
}
}
}
}
2023-10-24 22:19:42 +00:00
protected override unsafe void FirePurchaseCallback(AtkUnitBase* addonShop, int buyNow)
2023-10-13 20:08:22 +00:00
{
2023-10-24 22:19:42 +00:00
var buyItem = stackalloc AtkValue[]
2023-10-13 20:08:22 +00:00
{
2023-10-24 22:19:42 +00:00
new() { Type = ValueType.Int, Int = 0 },
new() { Type = ValueType.Int, Int = ItemForSale!.Position },
new() { Type = ValueType.Int, Int = buyNow },
new() { Type = 0, Int = 0 }
};
addonShop->FireCallback(4, buyItem);
2023-10-13 20:08:22 +00:00
}
}