annotate Modules/Queue.lua @ 143:8eb0f5b5a885

Fixed ?fully stocked? tooltip to say it uses the restock target setting rather than the global stock setting. Making sure the inventorium queuer frame is available before hiding it. Fixed an error with queueing items. Skip reasons are now sorted by importance (also by default). Now closing the queue window when you close your profession window.
author Zerotorescue
date Tue, 18 Jan 2011 23:48:16 +0100
parents cd461a41723c
children 12a8ea5af671
rev   line source
Zerotorescue@17 1 local addon = select(2, ...);
Zerotorescue@13 2 local mod = addon:NewModule("Queue", "AceEvent-3.0", "AceTimer-3.0");
Zerotorescue@13 3
Zerotorescue@132 4 local _G = _G;
Zerotorescue@143 5 local tonumber, pairs, sformat, smatch, slower, floor, ceil, tinsert, twipe = _G.tonumber, _G.pairs, _G.string.format, _G.string.match, _G.string.lower, _G.floor, _G.ceil, _G.table.insert, _G.table.wipe;
Zerotorescue@132 6
Zerotorescue@132 7 local queue, skipped = {}, {};
Zerotorescue@132 8
Zerotorescue@132 9 -- strings are passed by reference, so it takes no additional memory if one string was used in a thousand tables compared to any other reference type
Zerotorescue@132 10 local skipReasons = {
Zerotorescue@143 11 ["NOT_CRAFTABLE"] = {
Zerotorescue@143 12 "|cff3d3d3dNot in profession|r", -- gray
Zerotorescue@143 13 "This item is not part of this profession.",
Zerotorescue@143 14 0,
Zerotorescue@143 15 },
Zerotorescue@143 16 ["CAPPED"] = {
Zerotorescue@143 17 "|cff66ff33Fully stocked|r", -- lime/green
Zerotorescue@143 18 "The recorded item count is above or equal to your minimum restock target setting.",
Zerotorescue@143 19 5,
Zerotorescue@143 20 },
Zerotorescue@143 21 ["MIN_CRAFTING_QUEUE"] = {
Zerotorescue@143 22 "|cffffff00Min crafting queue|r", -- yellow
Zerotorescue@143 23 "The amount of missing items is below or equal to your \"don't queue if I only miss\"-setting.",
Zerotorescue@143 24 10,
Zerotorescue@143 25 },
Zerotorescue@143 26 ["LOW_VALUE"] = {
Zerotorescue@143 27 "|cffff6633Underpriced|r", -- orange
Zerotorescue@143 28 "The recorded auction value of this item is below your price threshold.",
Zerotorescue@143 29 15,
Zerotorescue@143 30 },
Zerotorescue@143 31 ["NO_ITEMCOUNT_ADDON"] = {
Zerotorescue@143 32 "|cffff0000No itemcount addon|r", -- red
Zerotorescue@143 33 "No compatible item count could be found.",
Zerotorescue@143 34 20,
Zerotorescue@143 35 },
Zerotorescue@132 36 };
Zerotorescue@132 37
Zerotorescue@132 38 local function OnQueueCancel()
Zerotorescue@132 39 twipe(queue);
Zerotorescue@132 40 twipe(skipped);
Zerotorescue@132 41
Zerotorescue@143 42 if InventoriumQueuer then
Zerotorescue@143 43 InventoriumQueuer:Hide();
Zerotorescue@143 44 end
Zerotorescue@132 45 end
Zerotorescue@132 46
Zerotorescue@132 47 local function OnQueueAccept()
Zerotorescue@132 48 -- Prepare a table with all possible tradeskill craftables
Zerotorescue@132 49 local craftables = mod:GetTradeskillCraftables();
Zerotorescue@132 50
Zerotorescue@132 51 for _, q in pairs(queue) do
Zerotorescue@132 52 if craftables[q.itemId] then
Zerotorescue@132 53 if mod:QueueWithAddon(craftables[q.itemId].no, ceil(q.amount / craftables[q.itemId].quantity), q.groupName) == -1 then
Zerotorescue@132 54 addon:Print("Couldn't queue, no supported crafting addon found.", addon.Colors.Red);
Zerotorescue@132 55
Zerotorescue@132 56 OnQueueCancel();
Zerotorescue@132 57 return;
Zerotorescue@140 58 else
Zerotorescue@140 59 -- Update the crafted-item count
Zerotorescue@140 60 for groupName, values in pairs(addon.db.profile.groups) do
Zerotorescue@140 61 if values.items and values.items[q.itemId] then
Zerotorescue@143 62 values.items[q.itemId] = (tonumber(values.items[q.itemId]) or 0) + 1;
Zerotorescue@140 63 break;
Zerotorescue@140 64 end
Zerotorescue@140 65 end
Zerotorescue@132 66 end
Zerotorescue@132 67 else
Zerotorescue@132 68 addon:Debug("Lost %s", IdToItemLink(q.itemId));
Zerotorescue@132 69 end
Zerotorescue@132 70 end
Zerotorescue@132 71
Zerotorescue@132 72 twipe(queue);
Zerotorescue@132 73 twipe(skipped);
Zerotorescue@132 74
Zerotorescue@143 75 if InventoriumQueuer then
Zerotorescue@143 76 InventoriumQueuer:Hide();
Zerotorescue@143 77 end
Zerotorescue@132 78 end
Zerotorescue@132 79
Zerotorescue@143 80
Zerotorescue@143 81 --[[Allowing the queue window to actually initiate crafts is going to be a likely feature.
Zerotorescue@143 82 do
Zerotorescue@143 83 -- Start crafting the selected skill (or the first in line)
Zerotorescue@143 84 local function StartSelectedCraft()
Zerotorescue@143 85 local craftables = mod:GetTradeskillCraftables();
Zerotorescue@143 86
Zerotorescue@143 87 for _, q in pairs(queue) do
Zerotorescue@143 88 if craftables[q.itemId] then
Zerotorescue@143 89 DoTradeSkill(craftables[q.itemId].no, ceil(q.amount / craftables[q.itemId].quantity));
Zerotorescue@143 90 return;
Zerotorescue@143 91 end
Zerotorescue@143 92 end
Zerotorescue@143 93 end
Zerotorescue@143 94
Zerotorescue@143 95 -- Remove from queue and if it was selected, auto-select the next
Zerotorescue@143 96 local function CraftFinished()
Zerotorescue@143 97
Zerotorescue@143 98 end
Zerotorescue@143 99
Zerotorescue@143 100 -- Refresh the item count for this item
Zerotorescue@143 101 local function RefreshItem()
Zerotorescue@143 102 q.amount = mod:GetRestockAmount(q.itemId, q.groupName);
Zerotorescue@143 103
Zerotorescue@143 104 if q.amount < 1 then
Zerotorescue@143 105 table.remove(crafts, i);
Zerotorescue@143 106 end
Zerotorescue@143 107
Zerotorescue@143 108 DisplayQueue();
Zerotorescue@143 109 end
Zerotorescue@143 110
Zerotorescue@143 111 local function OnSpellFinished(event, unit, spellName, _, _, spellId)
Zerotorescue@143 112 if unit == "player" then
Zerotorescue@143 113 for i, q in pairs(queue) do
Zerotorescue@143 114 if q.craft.spellId == spellId then
Zerotorescue@143 115 -- We just finished this spell
Zerotorescue@143 116
Zerotorescue@143 117 print("pass");
Zerotorescue@143 118
Zerotorescue@143 119
Zerotorescue@143 120 return;
Zerotorescue@143 121 end
Zerotorescue@143 122 end
Zerotorescue@143 123 end
Zerotorescue@143 124 end
Zerotorescue@143 125 function Test() OnSpellFinished("SomeSpell", "player", "Delicate Inferno Ruby", nil, nil, 55400); end
Zerotorescue@143 126
Zerotorescue@143 127 tinsert(queue, {
Zerotorescue@143 128 ["itemId"] = itemId, -- needed to display the queued item in the queue window
Zerotorescue@143 129 ["amount"] = amount, -- the amount missing
Zerotorescue@143 130 ["bonus"] = bonus, -- the amount queued by the bonus queue
Zerotorescue@143 131 ["craft"] = craft, -- (craftable) - needed to find the proper element of this parent array when crafting has finished (spellId), and to update the numCrafts (quantity)
Zerotorescue@143 132 ["groupName"] = groupName, -- related group, needed to find the selected crafting addon
Zerotorescue@143 133 });
Zerotorescue@143 134 [1] = “player”;
Zerotorescue@143 135 [2] = “Delicate Inferno Ruby”;
Zerotorescue@143 136 [3] = “”;
Zerotorescue@143 137 [4] = 19;
Zerotorescue@143 138 [5] = 73336;
Zerotorescue@143 139 arg1
Zerotorescue@143 140 Unit casting the spell - "player"
Zerotorescue@143 141 arg2
Zerotorescue@143 142 Spell name - "“Delicate Inferno Ruby”"
Zerotorescue@143 143 arg3
Zerotorescue@143 144 Spell rank (deprecated in 4.0) - ""
Zerotorescue@143 145 arg4
Zerotorescue@143 146 Spell lineID counter - 19
Zerotorescue@143 147 arg5
Zerotorescue@143 148 Spell ID (added in 4.0) - 73336
Zerotorescue@143 149
Zerotorescue@143 150 end]]
Zerotorescue@143 151
Zerotorescue@132 152 local function MakeQueueWindow()
Zerotorescue@143 153 if not InventoriumQueuer then
Zerotorescue@143 154 addon:CreateQueueFrame();
Zerotorescue@143 155
Zerotorescue@132 156 local frame = InventoriumQueuer; -- both for speed as code-consistency
Zerotorescue@132 157
Zerotorescue@132 158 -- Scrolling table with a list of items to be moved
Zerotorescue@132 159 local scrollTableWidth = ( frame.frmMeasureDummy:GetWidth() - 30 ); -- adjust width by the scrollbar size
Zerotorescue@132 160 local headers = {
Zerotorescue@132 161 {
Zerotorescue@132 162 ["name"] = "Item",
Zerotorescue@132 163 ["width"] = (scrollTableWidth * .60),
Zerotorescue@132 164 ["defaultsort"] = "asc",
Zerotorescue@132 165 ["comparesort"] = function(this, aRow, bRow, column)
Zerotorescue@132 166 local aName, _, aRarity = GetItemInfo(this:GetRow(aRow).rowData.itemId);
Zerotorescue@132 167 local bName, _, bRarity = GetItemInfo(this:GetRow(bRow).rowData.itemId);
Zerotorescue@132 168 local template = "%d%s";
Zerotorescue@143 169 aName = sformat(template, (10 - (aRarity or 10)), slower(aName or ""));
Zerotorescue@143 170 bName = sformat(template, (10 - (bRarity or 10)), slower(bName or ""));
Zerotorescue@132 171
Zerotorescue@132 172 if this.cols[column].sort == "dsc" then
Zerotorescue@132 173 return aName > bName;
Zerotorescue@132 174 else
Zerotorescue@132 175 return aName < bName;
Zerotorescue@132 176 end
Zerotorescue@132 177 end,
Zerotorescue@132 178 ["sort"] = "asc", -- when the data is set, use this column so sort the default data
Zerotorescue@132 179 ["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Item"),
Zerotorescue@132 180 ["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by item quality then item name."),
Zerotorescue@132 181 },
Zerotorescue@132 182 {
Zerotorescue@132 183 ["name"] = "Amount",
Zerotorescue@132 184 ["width"] = (scrollTableWidth * .20),
Zerotorescue@132 185 ["align"] = "RIGHT",
Zerotorescue@132 186 ["defaultsort"] = "dsc",
Zerotorescue@143 187 ["comparesort"] = function(this, aRow, bRow, column)
Zerotorescue@143 188 if this.cols[column].sort == "dsc" then
Zerotorescue@143 189 return (this:GetRow(aRow).rowData.amount > this:GetRow(bRow).rowData.amount);
Zerotorescue@143 190 else
Zerotorescue@143 191 return (this:GetRow(aRow).rowData.amount < this:GetRow(bRow).rowData.amount);
Zerotorescue@143 192 end
Zerotorescue@143 193 end,
Zerotorescue@132 194 ["sortnext"] = 1,
Zerotorescue@143 195 ["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Amount needed"),
Zerotorescue@143 196 ["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the amount of items needed to reach the restock target."),
Zerotorescue@132 197 },
Zerotorescue@132 198 {
Zerotorescue@132 199 ["name"] = "Extra",
Zerotorescue@132 200 ["width"] = (scrollTableWidth * .20),
Zerotorescue@132 201 ["align"] = "RIGHT",
Zerotorescue@132 202 ["defaultsort"] = "dsc",
Zerotorescue@143 203 ["comparesort"] = function(this, aRow, bRow, column)
Zerotorescue@143 204 if this.cols[column].sort == "dsc" then
Zerotorescue@143 205 return (this:GetRow(aRow).rowData.bonus > this:GetRow(bRow).rowData.bonus);
Zerotorescue@143 206 else
Zerotorescue@143 207 return (this:GetRow(aRow).rowData.bonus < this:GetRow(bRow).rowData.bonus);
Zerotorescue@143 208 end
Zerotorescue@143 209 end,
Zerotorescue@132 210 ["sortnext"] = 1,
Zerotorescue@143 211 ["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Extra items"),
Zerotorescue@132 212 ["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the amount of bonus items."),
Zerotorescue@132 213 },
Zerotorescue@132 214 };
Zerotorescue@132 215
Zerotorescue@132 216 local scrollTableWidth = ( InventoriumQueuerUnqueueables.frmMeasureDummy:GetWidth() - 30 ); -- adjust width by the scrollbar size
Zerotorescue@132 217 local unqueueablesHeaders = {
Zerotorescue@132 218 {
Zerotorescue@132 219 ["name"] = "Item",
Zerotorescue@132 220 ["width"] = (scrollTableWidth * .6),
Zerotorescue@132 221 ["defaultsort"] = "asc",
Zerotorescue@132 222 ["comparesort"] = function(this, aRow, bRow, column)
Zerotorescue@132 223 local aName, _, aRarity = GetItemInfo(this:GetRow(aRow).rowData.itemId);
Zerotorescue@132 224 local bName, _, bRarity = GetItemInfo(this:GetRow(bRow).rowData.itemId);
Zerotorescue@132 225 local template = "%d%s";
Zerotorescue@143 226 aName = sformat(template, (10 - (aRarity or 10)), slower(aName or ""));
Zerotorescue@143 227 bName = sformat(template, (10 - (bRarity or 10)), slower(bName or ""));
Zerotorescue@132 228
Zerotorescue@132 229 if this.cols[column].sort == "dsc" then
Zerotorescue@132 230 return aName > bName;
Zerotorescue@132 231 else
Zerotorescue@132 232 return aName < bName;
Zerotorescue@132 233 end
Zerotorescue@132 234 end,
Zerotorescue@132 235 ["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Item"),
Zerotorescue@132 236 ["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by item quality then item name."),
Zerotorescue@132 237 },
Zerotorescue@132 238 {
Zerotorescue@132 239 ["name"] = "Reason",
Zerotorescue@132 240 ["width"] = (scrollTableWidth * .4),
Zerotorescue@132 241 ["defaultsort"] = "dsc",
Zerotorescue@143 242 ["comparesort"] = function(this, aRow, bRow, column)
Zerotorescue@143 243 local result = (this:GetRow(aRow).rowData.reason[3] > this:GetRow(bRow).rowData.reason[3]);
Zerotorescue@143 244
Zerotorescue@143 245 if this.cols[column].sort == "dsc" then
Zerotorescue@143 246 return result;
Zerotorescue@143 247 else
Zerotorescue@143 248 return (not result);
Zerotorescue@143 249 end
Zerotorescue@143 250 end,
Zerotorescue@143 251 ["sort"] = "dsc", -- when the data is set, use this column to sort the default data
Zerotorescue@132 252 ["sortnext"] = 1,
Zerotorescue@132 253 ["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Reason"),
Zerotorescue@132 254 ["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the reason the items couldn't be queued."),
Zerotorescue@132 255 },
Zerotorescue@132 256 };
Zerotorescue@132 257
Zerotorescue@132 258 local proceedButton = {
Zerotorescue@132 259 text = "Queue",
Zerotorescue@132 260 tooltipTitle = (not addon.db.profile.defaults.hideHelp and "Queue"),
Zerotorescue@132 261 tooltip = (not addon.db.profile.defaults.hideHelp and "Add these items to the queue of your crafting addon."),
Zerotorescue@132 262 onClick = OnQueueAccept,
Zerotorescue@132 263 };
Zerotorescue@132 264 local cancelButton = {
Zerotorescue@132 265 text = "Cancel",
Zerotorescue@132 266 tooltipTitle = (not addon.db.profile.defaults.hideHelp and "Cancel"),
Zerotorescue@132 267 tooltip = (not addon.db.profile.defaults.hideHelp and "Do not queue anything and close the window."),
Zerotorescue@132 268 onClick = OnQueueCancel,
Zerotorescue@132 269 };
Zerotorescue@132 270
Zerotorescue@132 271 addon:SetQueueFrameSettings("Inventorium Queue", "The following items can be added to the queue of your crafting addon. Do you wish to proceed?", proceedButton, cancelButton, headers, unqueueablesHeaders);
Zerotorescue@132 272 end
Zerotorescue@132 273 end
Zerotorescue@132 274
Zerotorescue@132 275 local function DisplayQueue()
Zerotorescue@132 276 MakeQueueWindow();
Zerotorescue@132 277
Zerotorescue@132 278 -- This table is never copied, just referenced. It is the same for every row.
Zerotorescue@132 279 local queueablesColumns = {
Zerotorescue@132 280 {
Zerotorescue@132 281 ["value"] = function(data, cols, realrow, column, table)
Zerotorescue@132 282 return IdToItemLink(data[realrow].rowData.itemId);
Zerotorescue@132 283 end,
Zerotorescue@132 284 }, -- item
Zerotorescue@132 285 {
Zerotorescue@132 286 ["value"] = function(data, cols, realrow, column, table)
Zerotorescue@132 287 return data[realrow].rowData.amount;
Zerotorescue@132 288 end,
Zerotorescue@132 289 }, -- amount
Zerotorescue@132 290 {
Zerotorescue@132 291 ["value"] = function(data, cols, realrow, column, table)
Zerotorescue@143 292 return ((data[realrow].rowData.bonus == 0) and 0) or "+" .. data[realrow].rowData.bonus;
Zerotorescue@132 293 end,
Zerotorescue@132 294 ["color"] = function(data, cols, realrow, column, table)
Zerotorescue@132 295 return ((data[realrow].rowData.bonus == 0) and { r = 1, g = 1, b = 1, a = 0.5 }) or { r = 0, g = 1, b = 0, a = 1 };
Zerotorescue@132 296 end,
Zerotorescue@132 297 }, -- extra
Zerotorescue@132 298 };
Zerotorescue@132 299
Zerotorescue@132 300 -- Store the list with rows in this
Zerotorescue@132 301 local queueables = {};
Zerotorescue@132 302
Zerotorescue@132 303 for _, q in pairs(queue) do
Zerotorescue@132 304 tinsert(queueables, {
Zerotorescue@132 305 ["rowData"] = q, -- this is not a key usually found in a row item and ignored by the library
Zerotorescue@132 306 ["cols"] = queueablesColumns,
Zerotorescue@132 307 });
Zerotorescue@132 308 end
Zerotorescue@132 309
Zerotorescue@132 310 -- This table is never copied, just referenced. It is the same for every row.
Zerotorescue@132 311 local unqueueablesColumns = {
Zerotorescue@132 312 {
Zerotorescue@132 313 ["value"] = function(data, cols, realrow, column, table)
Zerotorescue@132 314 return IdToItemLink(data[realrow].rowData.itemId);
Zerotorescue@132 315 end,
Zerotorescue@132 316 }, -- item
Zerotorescue@132 317 {
Zerotorescue@132 318 ["value"] = function(data, cols, realrow, column, table)
Zerotorescue@132 319 return data[realrow].rowData.reason[1];
Zerotorescue@132 320 end,
Zerotorescue@132 321 }, -- reason
Zerotorescue@132 322 };
Zerotorescue@132 323
Zerotorescue@132 324 -- Store the list with rows in this
Zerotorescue@132 325 local unqueueables = {};
Zerotorescue@132 326
Zerotorescue@132 327 for _, s in pairs(skipped) do
Zerotorescue@132 328 tinsert(unqueueables, {
Zerotorescue@132 329 ["rowData"] = s, -- this is not a key usually found in a row item and ignored by the library
Zerotorescue@132 330 ["cols"] = unqueueablesColumns,
Zerotorescue@132 331 });
Zerotorescue@132 332 end
Zerotorescue@132 333
Zerotorescue@132 334 addon:SetQueueFrameData(queueables, unqueueables);
Zerotorescue@132 335 end
Zerotorescue@62 336
Zerotorescue@13 337 function mod:OnEnable()
Zerotorescue@13 338 -- Register our own slash commands
Zerotorescue@62 339 -- /im queue
Zerotorescue@13 340 addon:RegisterSlash(function()
Zerotorescue@36 341 mod:QueueAll();
Zerotorescue@36 342 end, { "q", "que", "queue" }, "|Hfunction:InventoriumCommandHandler:queue|h|cff00fff7/im queue|r|h (or /im q) - Queue all items found in the currently opened profession that are within the groups tracked at this current character.");
Zerotorescue@14 343
Zerotorescue@14 344 self:RegisterMessage("IM_QUEUE_ALL");
Zerotorescue@14 345 self:RegisterMessage("IM_QUEUE_GROUP");
Zerotorescue@132 346
Zerotorescue@143 347 self:RegisterEvent("TRADE_SKILL_CLOSE", OnQueueCancel);
Zerotorescue@143 348
Zerotorescue@143 349 --self:RegisterEvent("UNIT_SPELLCAST_STOP", OnSpellFinished);
Zerotorescue@143 350 --self:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP", OnSpellFinished);
Zerotorescue@14 351 end
Zerotorescue@14 352
Zerotorescue@14 353 function mod:IM_QUEUE_ALL()
Zerotorescue@14 354 self:QueueAll();
Zerotorescue@14 355 end
Zerotorescue@14 356
Zerotorescue@14 357 function mod:IM_QUEUE_GROUP(event, groupName)
Zerotorescue@14 358 self:QueueGroup(groupName);
Zerotorescue@13 359 end
Zerotorescue@13 360
Zerotorescue@13 361 function mod:QueueAll()
Zerotorescue@132 362 -- Prepare a table with all possible tradeskill craftables
Zerotorescue@132 363 local craftables = self:GetTradeskillCraftables();
Zerotorescue@132 364
Zerotorescue@132 365 -- Forget old queue
Zerotorescue@132 366 twipe(queue);
Zerotorescue@132 367 twipe(skipped);
Zerotorescue@132 368
Zerotorescue@14 369 local playerName = UnitName("player");
Zerotorescue@14 370
Zerotorescue@14 371 -- Go through all groups
Zerotorescue@61 372 for groupName, values in pairs(addon.db.profile.groups) do
Zerotorescue@62 373 local trackAt = addon:GetOptionByKey(groupName, "trackAtCharacters");
Zerotorescue@14 374
Zerotorescue@14 375 if trackAt[playerName] then
Zerotorescue@132 376 self:QueueGroup(groupName, craftables);
Zerotorescue@13 377 end
Zerotorescue@13 378 end
Zerotorescue@132 379
Zerotorescue@132 380 DisplayQueue();
Zerotorescue@13 381 end
Zerotorescue@13 382
Zerotorescue@132 383 function mod:QueueGroup(groupName, craftables)
Zerotorescue@132 384 -- Prepare a table with all possible tradeskill craftables
Zerotorescue@132 385 if not craftables then
Zerotorescue@132 386 craftables = self:GetTradeskillCraftables(); -- nil when no tradeskill window is open
Zerotorescue@132 387 end
Zerotorescue@132 388
Zerotorescue@132 389 if not craftables then
Zerotorescue@132 390 addon:Print("No tradeskill window detected.", addon.Colors.Red);
Zerotorescue@132 391 return;
Zerotorescue@132 392 elseif not addon.db.profile.groups[groupName] then
Zerotorescue@132 393 addon:Print(sformat("Tried to queue items from a group named \"%s\", but no such group exists.", groupName), addon.Colors.Red);
Zerotorescue@132 394 return;
Zerotorescue@132 395 elseif not addon.db.profile.groups[groupName].items then
Zerotorescue@132 396 addon:Debug("This group (%s) has no items.", groupName);
Zerotorescue@14 397 return;
Zerotorescue@14 398 end
Zerotorescue@14 399
Zerotorescue@132 400 -- Retrieve group settings
Zerotorescue@143 401 local minCraftingQueue = floor( addon:GetOptionByKey(groupName, "minCraftingQueue") * addon:GetOptionByKey(groupName, "restockTarget") ); -- If the minCraftingQueue is 5% and restockTarget is 60, this will result in 3
Zerotorescue@132 402 local priceThreshold = addon:GetOptionByKey(groupName, "priceThreshold");
Zerotorescue@13 403
Zerotorescue@140 404 for itemId, count in pairs(addon.db.profile.groups[groupName].items) do
Zerotorescue@132 405 if craftables[itemId] then
Zerotorescue@143 406 local amount, bonus = self:GetRestockAmount(itemId, groupName);
Zerotorescue@31 407
Zerotorescue@143 408 if amount and amount >= minCraftingQueue then
Zerotorescue@143 409 -- If we are queuing at least one AND more than the minimum amount, then proceed
Zerotorescue@31 410
Zerotorescue@143 411 -- Auction value settings
Zerotorescue@143 412 local value = (priceThreshold ~= 0 and addon:GetAuctionValue(itemLink, groupName));
Zerotorescue@17 413
Zerotorescue@143 414 if priceThreshold == 0 or value == -1 or value >= priceThreshold then
Zerotorescue@143 415 -- If no price threshold is set or the auction value is equal to or larger than the price threshold, then proceed
Zerotorescue@31 416
Zerotorescue@143 417 self:Queue(itemId, amount, bonus, craftables[itemId], groupName);
Zerotorescue@132 418 else
Zerotorescue@143 419 self:Skip(itemId, skipReasons.LOW_VALUE);
Zerotorescue@14 420 end
Zerotorescue@14 421 else
Zerotorescue@143 422 if not amount then
Zerotorescue@143 423 -- nil = no item count addon
Zerotorescue@143 424 self:Skip(itemId, skipReasons.NO_ITEMCOUNT_ADDON);
Zerotorescue@143 425 addon:Print("No usable itemcount addon found.");
Zerotorescue@143 426 return;
Zerotorescue@143 427 elseif amount <= 0 then
Zerotorescue@143 428 -- less than 0 = (over)capped
Zerotorescue@143 429 self:Skip(itemId, skipReasons.CAPPED);
Zerotorescue@143 430 else
Zerotorescue@143 431 -- more than 0 = below min crafting queue
Zerotorescue@143 432 self:Skip(itemId, skipReasons.MIN_CRAFTING_QUEUE);
Zerotorescue@143 433 end
Zerotorescue@13 434 end
Zerotorescue@132 435 else
Zerotorescue@132 436 self:Skip(itemId, skipReasons.NOT_CRAFTABLE);
Zerotorescue@13 437 end
Zerotorescue@13 438 end
Zerotorescue@13 439 end
Zerotorescue@13 440
Zerotorescue@143 441 function mod:GetRestockAmount(itemId, groupName)
Zerotorescue@143 442 local currentStock = addon:GetItemCount(itemId, groupName);
Zerotorescue@143 443
Zerotorescue@143 444 if currentStock >= 0 then
Zerotorescue@143 445 -- Current stock will be -1 when no itemcount addon was found
Zerotorescue@143 446
Zerotorescue@143 447 local restockTarget = addon:GetOptionByKey(groupName, "restockTarget");
Zerotorescue@143 448 local bonusQueue = addon:GetOptionByKey(groupName, "bonusQueue");
Zerotorescue@143 449
Zerotorescue@143 450 -- Calculate the amount to be queued
Zerotorescue@143 451 local amount = ( restockTarget - currentStock );
Zerotorescue@143 452 local bonus = 0;
Zerotorescue@143 453
Zerotorescue@143 454 if currentStock == 0 and bonusQueue > 0 then
Zerotorescue@143 455 -- If we have none left and the bonus queue is enabled, modify the amount to be queued
Zerotorescue@143 456
Zerotorescue@143 457 bonus = floor( ( amount * ( bonusQueue ) ) + .5 ); -- round
Zerotorescue@143 458
Zerotorescue@143 459 -- Update amount
Zerotorescue@143 460 amount = (amount + bonus);
Zerotorescue@143 461 end
Zerotorescue@143 462
Zerotorescue@143 463 return amount, bonus;
Zerotorescue@143 464 else
Zerotorescue@143 465 return;
Zerotorescue@143 466 end
Zerotorescue@143 467 end
Zerotorescue@143 468
Zerotorescue@143 469 function mod:Queue(itemId, amount, bonus, craft, groupName)
Zerotorescue@132 470 tinsert(queue, {
Zerotorescue@143 471 ["itemId"] = itemId, -- needed to display the queued item in the queue window
Zerotorescue@143 472 ["amount"] = amount, -- the amount missing
Zerotorescue@143 473 ["bonus"] = bonus, -- the amount queued by the bonus queue
Zerotorescue@143 474 ["craft"] = craft, -- (craftable) - needed to find the proper element of this parent array when crafting has finished (spellId), and to update the numCrafts (quantity)
Zerotorescue@143 475 ["groupName"] = groupName, -- related group, needed to find the selected crafting addon
Zerotorescue@132 476 });
Zerotorescue@132 477 end
Zerotorescue@132 478
Zerotorescue@132 479 function mod:Skip(itemId, reason)
Zerotorescue@132 480 tinsert(skipped, {
Zerotorescue@132 481 ["itemId"] = itemId,
Zerotorescue@132 482 ["reason"] = reason,
Zerotorescue@132 483 });
Zerotorescue@132 484 end
Zerotorescue@132 485
Zerotorescue@132 486 function mod:QueueWithAddon(tradeSkillIndex, amount, group)
Zerotorescue@132 487 -- Sanity check
Zerotorescue@13 488 tradeSkillIndex = tonumber(tradeSkillIndex);
Zerotorescue@13 489 amount = tonumber(amount);
Zerotorescue@13 490
Zerotorescue@23 491 local selectedExternalAddon = addon:GetOptionByKey(group, "craftingAddon");
Zerotorescue@23 492
Zerotorescue@23 493 if addon.supportedAddons.crafting[selectedExternalAddon] and addon.supportedAddons.crafting[selectedExternalAddon].IsEnabled() then
Zerotorescue@13 494 -- Try to use the default auction pricing addon
Zerotorescue@13 495
Zerotorescue@23 496 return addon.supportedAddons.crafting[selectedExternalAddon].Queue(tradeSkillIndex, amount);
Zerotorescue@13 497 else
Zerotorescue@13 498 -- Default not available, get the first one then
Zerotorescue@13 499
Zerotorescue@13 500 for name, value in pairs(addon.supportedAddons.crafting) do
Zerotorescue@13 501 if value.IsEnabled() then
Zerotorescue@13 502 return value.Queue(tradeSkillIndex, amount);
Zerotorescue@13 503 end
Zerotorescue@13 504 end
Zerotorescue@13 505 end
Zerotorescue@13 506
Zerotorescue@132 507 return -1;
Zerotorescue@13 508 end
Zerotorescue@132 509
Zerotorescue@132 510 -- Expand all categories
Zerotorescue@132 511 local function ExpandSubClasses()
Zerotorescue@132 512 for i = GetNumTradeSkills(), 1, -1 do
Zerotorescue@132 513 local _, skillType, _, isExpanded = GetTradeSkillInfo(i);
Zerotorescue@132 514
Zerotorescue@132 515 if skillType == "header" and not isExpanded then
Zerotorescue@132 516 ExpandTradeSkillSubClass(i);
Zerotorescue@132 517 end
Zerotorescue@132 518 end
Zerotorescue@132 519 end
Zerotorescue@132 520
Zerotorescue@132 521 function mod:GetTradeskillCraftables()
Zerotorescue@132 522 local craftables = {};
Zerotorescue@132 523
Zerotorescue@132 524 if GetTradeSkillLine() ~= "UNKNOWN" then
Zerotorescue@132 525 ExpandSubClasses();
Zerotorescue@132 526
Zerotorescue@132 527 -- Cache all craftable items
Zerotorescue@132 528 for i = 1, GetNumTradeSkills() do
Zerotorescue@132 529 local itemLink = GetTradeSkillItemLink(i);
Zerotorescue@132 530
Zerotorescue@132 531 if itemLink then
Zerotorescue@132 532 local itemId = addon:GetItemId(itemLink);
Zerotorescue@132 533 if not itemId then
Zerotorescue@132 534 -- If this isn't an item, it can only be an enchant instead
Zerotorescue@132 535 itemId = tonumber(smatch(itemLink, "|Henchant:([-0-9]+)|h"));
Zerotorescue@132 536
Zerotorescue@132 537 itemId = addon.scrollIds[itemId]; -- change enchantIds into scrollIds
Zerotorescue@132 538 end
Zerotorescue@132 539
Zerotorescue@132 540 -- Remember the average amount of items created per craft (doesn't need to be a round number, since we multiply this by the amount of items to be queued we're better off rounding at that time)
Zerotorescue@132 541 local minMade, maxMade = GetTradeSkillNumMade(i);
Zerotorescue@132 542 local average = ((minMade == maxMade) and minMade) or ((minMade + maxMade) / 2);
Zerotorescue@132 543
Zerotorescue@143 544 local recipeLink = GetTradeSkillRecipeLink(i);
Zerotorescue@143 545 local spellId = tonumber(smatch(recipeLink, "|Henchant:([-0-9]+)|h"));
Zerotorescue@143 546
Zerotorescue@132 547 craftables[itemId] = {
Zerotorescue@143 548 ["no"] = i, -- needed to start crafting at the end of the entire cycle
Zerotorescue@143 549 ["spellId"] = spellId, -- needed to detect creation of this item was finished
Zerotorescue@143 550 ["quantity"] = average, -- needed to calculate the amount of crafts
Zerotorescue@132 551 };
Zerotorescue@132 552 end
Zerotorescue@132 553 end
Zerotorescue@132 554 else
Zerotorescue@132 555 return;
Zerotorescue@132 556 end
Zerotorescue@132 557
Zerotorescue@132 558 return craftables;
Zerotorescue@132 559 end