Skip to content

Commit

Permalink
Improvements to Supermarket Sweep
Browse files Browse the repository at this point in the history
Added "Import from Clipboard" button that accepts standard Artisan/Teamcraft/Workshoppa format
Reworked the region dropdown to be able to specify either an entire region or a specific DC
  • Loading branch information
NostraThomas99 committed Oct 8, 2024
1 parent c17b63d commit 32cbc4b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 32 deletions.
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();
}
}
}

0 comments on commit 32cbc4b

Please sign in to comment.