diff 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
line wrap: on
line diff
--- a/Modules/Queue.lua	Tue Jan 18 23:00:18 2011 +0100
+++ b/Modules/Queue.lua	Tue Jan 18 23:48:16 2011 +0100
@@ -2,24 +2,46 @@
 local mod = addon:NewModule("Queue", "AceEvent-3.0", "AceTimer-3.0");
 
 local _G = _G;
-local tonumber, pairs, sformat, smatch, floor, ceil, tinsert, twipe = _G.tonumber, _G.pairs, _G.string.format, _G.string.match, _G.floor, _G.ceil, _G.table.insert, _G.table.wipe;
+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;
 
 local queue, skipped = {}, {};
 
 -- 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
 local skipReasons = {
-	["LOW_VALUE"] = { "|cffff6633Underpriced|r", "The recorded auction value of this item is below your price threshold." },
-	["CAPPED"] = { "|cff66ff33Fully stocked|r", "The recorded item count is above or equal to your minimum global stock setting." },
-	["MIN_CRAFTING_QUEUE"] = { "|cffffff00Min crafting queue|r", "The amount of missing items is below or equal to your \"don't queue if I only miss\"-setting." },
-	["NO_ITEMCOUNT_ADDON"] = { "|cffff0000No itemcount addon|r", "No compatible item count could be found." },
-	["NOT_CRAFTABLE"] = { "|cff3d3d3dNot in profession|r", "This item is not part of this profession." },
+	["NOT_CRAFTABLE"] = {
+		"|cff3d3d3dNot in profession|r", -- gray
+		"This item is not part of this profession.",
+		0,
+	},
+	["CAPPED"] = {
+		"|cff66ff33Fully stocked|r", -- lime/green
+		"The recorded item count is above or equal to your minimum restock target setting.",
+		5,
+	},
+	["MIN_CRAFTING_QUEUE"] = {
+		"|cffffff00Min crafting queue|r", -- yellow
+		"The amount of missing items is below or equal to your \"don't queue if I only miss\"-setting.",
+		10,
+	},
+	["LOW_VALUE"] = {
+		"|cffff6633Underpriced|r", -- orange
+		"The recorded auction value of this item is below your price threshold.",
+		15,
+	},
+	["NO_ITEMCOUNT_ADDON"] = {
+		"|cffff0000No itemcount addon|r", -- red
+		"No compatible item count could be found.",
+		20,
+	},
 };
 
 local function OnQueueCancel()
 	twipe(queue);
 	twipe(skipped);
 	
-	InventoriumQueuer:Hide();
+	if InventoriumQueuer then
+		InventoriumQueuer:Hide();
+	end
 end
 
 local function OnQueueAccept()
@@ -37,7 +59,7 @@
 				-- Update the crafted-item count
 				for groupName, values in pairs(addon.db.profile.groups) do
 					if values.items and values.items[q.itemId] then
-						values.items[q.itemId] = tonumber(values.items[q.itemId]) + 1;
+						values.items[q.itemId] = (tonumber(values.items[q.itemId]) or 0) + 1;
 						break;
 					end
 				end
@@ -50,11 +72,87 @@
 	twipe(queue);
 	twipe(skipped);
 	
-	InventoriumQueuer:Hide();
+	if InventoriumQueuer then
+		InventoriumQueuer:Hide();
+	end
 end
 
+
+--[[Allowing the queue window to actually initiate crafts is going to be a likely feature.
+do
+	-- Start crafting the selected skill (or the first in line)
+	local function StartSelectedCraft()
+		local craftables = mod:GetTradeskillCraftables();
+		
+		for _, q in pairs(queue) do
+			if craftables[q.itemId] then
+				DoTradeSkill(craftables[q.itemId].no, ceil(q.amount / craftables[q.itemId].quantity));
+				return;
+			end
+		end
+	end
+	
+	-- Remove from queue and if it was selected, auto-select the next
+	local function CraftFinished()
+		
+	end
+	
+	-- Refresh the item count for this item
+	local function RefreshItem()
+		q.amount = mod:GetRestockAmount(q.itemId, q.groupName);
+		
+		if q.amount < 1 then
+			table.remove(crafts, i);
+		end
+		
+		DisplayQueue();
+	end
+	
+	local function OnSpellFinished(event, unit, spellName, _, _, spellId)
+		if unit == "player" then
+			for i, q in pairs(queue) do
+				if q.craft.spellId == spellId then
+					-- We just finished this spell
+					
+					print("pass");
+					
+					
+					return;
+				end
+			end
+		end
+	end
+	function Test() OnSpellFinished("SomeSpell", "player", "Delicate Inferno Ruby", nil, nil, 55400); end
+	
+	tinsert(queue, {
+		["itemId"] = itemId, -- needed to display the queued item in the queue window
+		["amount"] = amount, -- the amount missing
+		["bonus"] = bonus, -- the amount queued by the bonus queue
+		["craft"] = craft, -- (craftable) - needed to find the proper element of this parent array when crafting has finished (spellId), and to update the numCrafts (quantity)
+		["groupName"] = groupName, -- related group, needed to find the selected crafting addon
+	});
+	[1] = “player”;
+	[2] = “Delicate Inferno Ruby”;
+	[3] = “”;
+	[4] = 19;
+	[5] = 73336;
+	arg1 
+		Unit casting the spell - "player"
+	arg2 
+		Spell name - "“Delicate Inferno Ruby”"
+	arg3 
+		Spell rank (deprecated in 4.0)  - ""
+	arg4 
+		Spell lineID counter - 19
+	arg5 
+		Spell ID (added in 4.0) - 73336
+	
+end]]
+
 local function MakeQueueWindow()
-	do
+	if not InventoriumQueuer then
+		addon:CreateQueueFrame();
+		
 		local frame = InventoriumQueuer; -- both for speed as code-consistency
 		
 		-- Scrolling table with a list of items to be moved
@@ -68,8 +166,8 @@
 					local aName, _, aRarity = GetItemInfo(this:GetRow(aRow).rowData.itemId);
 					local bName, _, bRarity = GetItemInfo(this:GetRow(bRow).rowData.itemId);
 					local template = "%d%s";
-					aName = template:format((10 - (aRarity or 10)), (aName or ""):lower());
-					bName = template:format((10 - (bRarity or 10)), (bName or ""):lower());
+					aName = sformat(template, (10 - (aRarity or 10)), slower(aName or ""));
+					bName = sformat(template, (10 - (bRarity or 10)), slower(bName or ""));
 					
 					if this.cols[column].sort == "dsc" then
 						return aName > bName;
@@ -86,17 +184,31 @@
 				["width"] = (scrollTableWidth * .20),
 				["align"] = "RIGHT",
 				["defaultsort"] = "dsc",
+				["comparesort"] = function(this, aRow, bRow, column)
+					if this.cols[column].sort == "dsc" then
+						return (this:GetRow(aRow).rowData.amount > this:GetRow(bRow).rowData.amount);
+					else
+						return (this:GetRow(aRow).rowData.amount < this:GetRow(bRow).rowData.amount);
+					end
+				end,
 				["sortnext"] = 1,
-				["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Amount"),
-				["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the amount of items to be queued."),
+				["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Amount needed"),
+				["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the amount of items needed to reach the restock target."),
 			},
 			{
 				["name"] = "Extra",
 				["width"] = (scrollTableWidth * .20),
 				["align"] = "RIGHT",
 				["defaultsort"] = "dsc",
+				["comparesort"] = function(this, aRow, bRow, column)
+					if this.cols[column].sort == "dsc" then
+						return (this:GetRow(aRow).rowData.bonus > this:GetRow(bRow).rowData.bonus);
+					else
+						return (this:GetRow(aRow).rowData.bonus < this:GetRow(bRow).rowData.bonus);
+					end
+				end,
 				["sortnext"] = 1,
-				["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Extra"),
+				["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Extra items"),
 				["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the amount of bonus items."),
 			},
 		};
@@ -111,8 +223,8 @@
 					local aName, _, aRarity = GetItemInfo(this:GetRow(aRow).rowData.itemId);
 					local bName, _, bRarity = GetItemInfo(this:GetRow(bRow).rowData.itemId);
 					local template = "%d%s";
-					aName = template:format((10 - (aRarity or 10)), (aName or ""):lower());
-					bName = template:format((10 - (bRarity or 10)), (bName or ""):lower());
+					aName = sformat(template, (10 - (aRarity or 10)), slower(aName or ""));
+					bName = sformat(template, (10 - (bRarity or 10)), slower(bName or ""));
 					
 					if this.cols[column].sort == "dsc" then
 						return aName > bName;
@@ -120,7 +232,6 @@
 						return aName < bName;
 					end
 				end,
-				["sort"] = "asc", -- when the data is set, use this column so sort the default data
 				["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Item"),
 				["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by item quality then item name."),
 			},
@@ -128,6 +239,16 @@
 				["name"] = "Reason",
 				["width"] = (scrollTableWidth * .4),
 				["defaultsort"] = "dsc",
+				["comparesort"] = function(this, aRow, bRow, column)
+					local result = (this:GetRow(aRow).rowData.reason[3] > this:GetRow(bRow).rowData.reason[3]);
+					
+					if this.cols[column].sort == "dsc" then
+						return result;
+					else
+						return (not result);
+					end
+				end,
+				["sort"] = "dsc", -- when the data is set, use this column to sort the default data
 				["sortnext"] = 1,
 				["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Reason"),
 				["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the reason the items couldn't be queued."),
@@ -168,7 +289,7 @@
 		}, -- amount
 		{
 			["value"] = function(data, cols, realrow, column, table)
-				return (data[realrow].rowData.bonus == 0 and 0) or "+" .. data[realrow].rowData.bonus;
+				return ((data[realrow].rowData.bonus == 0) and 0) or "+" .. data[realrow].rowData.bonus;
 			end,
 			["color"] = function(data, cols, realrow, column, table)
 				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 };
@@ -223,9 +344,10 @@
 	self:RegisterMessage("IM_QUEUE_ALL");
 	self:RegisterMessage("IM_QUEUE_GROUP");
 	
-	if not InventoriumQueuer then
-		addon:CreateQueueFrame(OnMoveAccept, OnMoveCancel);
-	end
+	self:RegisterEvent("TRADE_SKILL_CLOSE", OnQueueCancel);
+	
+	--self:RegisterEvent("UNIT_SPELLCAST_STOP", OnSpellFinished);
+	--self:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP", OnSpellFinished);
 end
 
 function mod:IM_QUEUE_ALL()
@@ -276,55 +398,39 @@
 	end
 	
 	-- Retrieve group settings
-	local restockTarget = addon:GetOptionByKey(groupName, "restockTarget");
-	local bonusQueue = addon:GetOptionByKey(groupName, "bonusQueue");
-	local minCraftingQueue = floor( addon:GetOptionByKey(groupName, "minCraftingQueue") * restockTarget ); -- If the minCraftingQueue is 5% and restockTarget is 60, this will result in 3
+	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
 	local priceThreshold = addon:GetOptionByKey(groupName, "priceThreshold");
 	
 	for itemId, count in pairs(addon.db.profile.groups[groupName].items) do
 		if craftables[itemId] then
-			local currentStock = addon:GetItemCount(itemId, groupName);
+			local amount, bonus = self:GetRestockAmount(itemId, groupName);
 			
-			if currentStock >= 0 then
-				-- Current stock will be -1 when no itemcount addon was found
+			if amount and amount >= minCraftingQueue then
+				-- If we are queuing at least one AND more than the minimum amount, then proceed
 				
-				-- Calculate the amount to be queued
-				local amount = ( restockTarget - currentStock );
-				local bonus = 0;
+				-- Auction value settings
+				local value = (priceThreshold ~= 0 and addon:GetAuctionValue(itemLink, groupName));
 				
-				if currentStock == 0 and bonusQueue > 0 then
-					-- If we have none left and the bonus queue is enabled, modify the amount to  be queued
+				if priceThreshold == 0 or value == -1 or value >= priceThreshold then
+					-- If no price threshold is set or the auction value is equal to or larger than the price threshold, then proceed
 					
-					bonus = floor( ( amount * ( bonusQueue ) ) + .5 ); -- round
-					
-					-- Update amount
-					amount = (amount + bonus);
-				end
-				
-				if amount > 0 and amount >= minCraftingQueue then
-					-- If we are queuing at least one AND more than the minimum amount, then proceed
-					
-					-- Auction value settings
-					local value = (priceThreshold ~= 0 and addon:GetAuctionValue(itemLink, groupName));
-					
-					if priceThreshold == 0 or value == -1 or value >= priceThreshold then
-						-- If no price threshold is set or the auction value is equal to or larger than the price threshold, then proceed
-						
-						self:Queue(itemId, amount, bonus, groupName);
-					else
-						self:Skip(itemId, skipReasons.LOW_VALUE);
-					end
+					self:Queue(itemId, amount, bonus, craftables[itemId], groupName);
 				else
-					if amount <= 0 then
-						self:Skip(itemId, skipReasons.CAPPED);
-					else
-						self:Skip(itemId, skipReasons.MIN_CRAFTING_QUEUE);
-					end
+					self:Skip(itemId, skipReasons.LOW_VALUE);
 				end
 			else
-				self:Skip(itemId, skipReasons.NO_ITEMCOUNT_ADDON);
-				addon:Print("No usable itemcount addon found.");
-				return;
+				if not amount then
+					-- nil = no item count addon
+					self:Skip(itemId, skipReasons.NO_ITEMCOUNT_ADDON);
+					addon:Print("No usable itemcount addon found.");
+					return;
+				elseif amount <= 0 then
+					-- less than 0 = (over)capped
+					self:Skip(itemId, skipReasons.CAPPED);
+				else
+					-- more than 0 = below min crafting queue
+					self:Skip(itemId, skipReasons.MIN_CRAFTING_QUEUE);
+				end
 			end
 		else
 			self:Skip(itemId, skipReasons.NOT_CRAFTABLE);
@@ -332,12 +438,41 @@
 	end
 end
 
-function mod:Queue(itemId, amount, bonus, groupName)
+function mod:GetRestockAmount(itemId, groupName)
+	local currentStock = addon:GetItemCount(itemId, groupName);
+	
+	if currentStock >= 0 then
+		-- Current stock will be -1 when no itemcount addon was found
+		
+		local restockTarget = addon:GetOptionByKey(groupName, "restockTarget");
+		local bonusQueue = addon:GetOptionByKey(groupName, "bonusQueue");
+		
+		-- Calculate the amount to be queued
+		local amount = ( restockTarget - currentStock );
+		local bonus = 0;
+		
+		if currentStock == 0 and bonusQueue > 0 then
+			-- If we have none left and the bonus queue is enabled, modify the amount to  be queued
+			
+			bonus = floor( ( amount * ( bonusQueue ) ) + .5 ); -- round
+			
+			-- Update amount
+			amount = (amount + bonus);
+		end
+		
+		return amount, bonus;
+	else
+		return;
+	end
+end
+
+function mod:Queue(itemId, amount, bonus, craft, groupName)
 	tinsert(queue, {
-		["itemId"] = itemId,
-		["amount"] = amount,
-		["bonus"] = bonus,
-		["groupName"] = groupName,
+		["itemId"] = itemId, -- needed to display the queued item in the queue window
+		["amount"] = amount, -- the amount missing
+		["bonus"] = bonus, -- the amount queued by the bonus queue
+		["craft"] = craft, -- (craftable) - needed to find the proper element of this parent array when crafting has finished (spellId), and to update the numCrafts (quantity)
+		["groupName"] = groupName, -- related group, needed to find the selected crafting addon
 	});
 end
 
@@ -406,9 +541,13 @@
 				local minMade, maxMade = GetTradeSkillNumMade(i);
 				local average = ((minMade == maxMade) and minMade) or ((minMade + maxMade) / 2);
 				
+				local recipeLink = GetTradeSkillRecipeLink(i);
+				local spellId = tonumber(smatch(recipeLink, "|Henchant:([-0-9]+)|h"));
+				
 				craftables[itemId] = {
-					["no"] = i,
-					["quantity"] = average,
+					["no"] = i, -- needed to start crafting at the end of the entire cycle
+					["spellId"] = spellId, -- needed to detect creation of this item was finished
+					["quantity"] = average, -- needed to calculate the amount of crafts
 				};
 			end
 		end