Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to Supermarket Sweep #3

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 76 additions & 21 deletions RebornToolbox/Features/MBShoppingList/MBShoppingList.UI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,32 @@ public MBShoppingList_UI(MBShoppingList manager) : base("Supermarket Sweep", ImG
public override void Draw()
{
DrawItemAdd();
ImGui.SameLine();
if (ImGui.Button("Import MakePlace List"))
{
SelectFile();
}
ImGui.SameLine();
if (ImGui.Button("Import from Clipboard"))
{
ExtractClipboardText();
}
ImGuiEx.Tooltip("Import items from clipboard using the following format:\n'10x Ipe Log'\n'999x Iron Ore");

RenderRegionTypeComboBox();
var maxResults = Plugin.Configuration.ShoppingListConfig.MaxResults;
if (ImGuiUtil.GenericEnumCombo("Region/Datacenter", 300, Plugin.Configuration.ShoppingListConfig.ShoppingRegion, out RegionType newRegion, r => r.ToFriendlyString()))
{
Plugin.Configuration.ShoppingListConfig.ShoppingRegion = newRegion;
Plugin.Configuration.SaveConfig();
}

ImGui.SetNextItemWidth(100);
int maxResults = Plugin.Configuration.ShoppingListConfig.MaxResults;
if (ImGui.InputInt("Max Search Results", ref maxResults))
{
Plugin.Configuration.ShoppingListConfig.MaxResults = maxResults;
Plugin.Configuration.SaveConfig();
}


if (Plugin.Configuration.ExpertMode)
{
if (ImGuiUtil.DrawDisabledButton("Refresh All Market Data", new Vector2(0, 0),
Expand Down Expand Up @@ -84,6 +96,56 @@ public override void Draw()
_fileDialogManager.Draw();
}

private void ExtractClipboardText()
{
var clipboardText = ImGuiUtil.GetClipboardText();
if (!string.IsNullOrEmpty(clipboardText))
{
try
{
Dictionary<string, int> items = new Dictionary<string, int>();

// Regex pattern
var pattern = @"\b(\d+)x\s(.+)\b";
var matches = Regex.Matches(clipboardText, pattern);

// Loop through matches and add them to dictionary
foreach (Match match in matches)
{
var quantity = int.Parse(match.Groups[1].Value);
var itemName = match.Groups[2].Value;
items[itemName] = quantity;
}

bool saveNeeded = false;
foreach (var item in items)
{
var itemObj = MBShoppingList.AllItems.FirstOrDefault(i => string.Equals(i.Name, item.Key, StringComparison.OrdinalIgnoreCase));
if (itemObj is null)
{
Svc.Log.Error($"Item {item.Key} not found");
continue;
}

var shoppingListItem = new ShoppingListItem(itemObj, item.Value);
_manager.WantedItems.Add(shoppingListItem);
saveNeeded = true;
}
if (saveNeeded)
_manager.SaveList();
}
catch (Exception e)
{
Svc.Chat.PrintError("[Reborn Toolbox] Error importing clipboard text. See /xllog for details.");
Svc.Log.Error($"Error importing from clipboard: {e}");
}
}
else
{
Svc.Chat.PrintError($"Clipboard text is empty or invalid");
}
}

private DateTime _lastMassRefresh = DateTime.MinValue;

private void DrawWantedItem(ShoppingListItem? item)
Expand All @@ -101,6 +163,7 @@ private void DrawWantedItem(ShoppingListItem? item)
var seString = new SeStringBuilder().AddText($"[Reborn Toolbox]").AddItemLink(item.ItemId).BuiltString;
Svc.Chat.Print(seString);
}

ImGuiEx.Tooltip("Click to print item link in chat");


Expand All @@ -115,7 +178,8 @@ private void DrawWantedItem(ShoppingListItem? item)
ImGui.PopItemWidth();

ImGui.Text($"Already Owned: {item.InventoryCount}");
ImGuiEx.Tooltip("Amount of this item you have across all characters (including retainers and alts)\nSourced from Allagan Tools\nSee Allagan Tools for detailed information");
ImGuiEx.Tooltip(
"Amount of this item you have across all characters (including retainers and alts)\nSourced from Allagan Tools\nSee Allagan Tools for detailed information");

string buttonLabel;
string buttonDescription;
Expand Down Expand Up @@ -183,7 +247,9 @@ private void DrawRow(ShoppingListItem.WorldListing obj)
Svc.Chat.PrintError($"[Reborn Toolbox] LifeStream is required to move between servers");
return;
}
_manager.TaskManager.Enqueue(() => Lifestream_IPCSubscriber.ExecuteCommand(obj.WorldName), _manager.LifeStreamTaskConfig);

_manager.TaskManager.Enqueue(() => Lifestream_IPCSubscriber.ExecuteCommand(obj.WorldName),
_manager.LifeStreamTaskConfig);
_manager.TaskManager.Enqueue(() => !Lifestream_IPCSubscriber.IsBusy(), _manager.LifeStreamTaskConfig);
_manager.TaskManager.Enqueue(GenericHelpers.IsScreenReady);
_manager.TaskManager.Enqueue(_manager.QueueMoveToMarketboardTasks);
Expand All @@ -200,8 +266,11 @@ private unsafe void DrawItemSearch(ShoppingListItem item)
{
AddonItemSearch* addonItemSearch = (AddonItemSearch*)Svc.GameGui.GetAddonByName("ItemSearch");
var disabled = addonItemSearch == null;
var description = disabled ? "Automatically search for this item on the Marketboard (MarketBoard window must be open)" : "Automatically search for this item on the Marketboard";
if (ImGuiUtil.DrawDisabledButton($"Search Marketboard for Item##{item.ItemId}", new Vector2(), description, disabled))
var description = disabled
? "Automatically search for this item on the Marketboard (MarketBoard window must be open)"
: "Automatically search for this item on the Marketboard";
if (ImGuiUtil.DrawDisabledButton($"Search Marketboard for Item##{item.ItemId}", new Vector2(), description,
disabled))
{
addonItemSearch->SearchTextInput->SetText(item.Name);
addonItemSearch->RunSearch();
Expand Down Expand Up @@ -280,20 +349,6 @@ private void DrawItemAdd()
}
}

private static readonly string[] RegionNames = ["North America", "Europe", "Japan", "Oceania"];

public void RenderRegionTypeComboBox()
{
int currentRegionIndex = (int)Plugin.Configuration.ShoppingListConfig.ShoppingRegion;

if (ImGui.Combo("Select Region", ref currentRegionIndex, RegionNames, RegionNames.Length))
{
Plugin.Configuration.ShoppingListConfig.ShoppingRegion = (RegionType)currentRegionIndex;

Plugin.Configuration.SaveConfig();
}
}

private unsafe void DrawMBButton(ShoppingListItem item)
{
var mbAddon = (AddonItemSearch*)Svc.GameGui.GetAddonByName("ItemSearch");
Expand Down
27 changes: 16 additions & 11 deletions RebornToolbox/Features/MBShoppingList/Models/RegionType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ public enum RegionType
Europe = 1,
Japan = 2,
Oceania = 3,
Aether = 4,
Crystal = 5,
Dynamis = 6,
Primal = 7,
Chaos = 8,
Light = 9,
Elemental = 10,
Gaia = 11,
Mana = 12,
Meteor = 13,
Materia = 14
}

public static class RegionTypeExtensions
Expand All @@ -23,24 +34,18 @@ public static string ToUniversalisString(this RegionType regionType)
case RegionType.Oceania:
return "oceania";
default:
return "north-america";
return regionType.ToString().ToLower();
}
}

public static RegionType ToRegionType(this string regionType)
public static string ToFriendlyString(this RegionType regionType)
{
switch (regionType)
{
case "north-america":
return RegionType.NorthAmerica;
case "europe":
return RegionType.Europe;
case "japan":
return RegionType.Japan;
case "oceania":
return RegionType.Oceania;
case RegionType.NorthAmerica:
return "North America";
default:
return RegionType.NorthAmerica;
return regionType.ToString();
}
}
}