view Modules/Queue.lua @ 84:3bec0ea44607

Cleaned the Inventorium folder; moved all classes to classes directory, modules to modules directory and support addons to plugins directory. In addition support addons are now references within XML files rather than inside the TOC. Fixed the default local item count setting, you can now exclude bag and AH data from it. Fixed some mover algorithm bugs. Mover can no longer freeze the game but instead will terminate the process after a 1000 passes. Now reversing the moves table after making it, rather than every single time it is used. Fixed guild bank support. Now displaying the amount of items moved. Scanner now scans all guild bank tabs rather than only the current. Fixed a bug with local item data not being retrieved properly. Disabled ?enterClicksFirstButton? within dialogs as this causes the dialog to consume all keypress. Events are now at the addon object rather than local.
author Zerotorescue
date Thu, 06 Jan 2011 20:05:30 +0100
parents Queue.lua@2127ab01ed4a
children a12d22ef3f39
line wrap: on
line source
local addon = select(2, ...);
local mod = addon:NewModule("Queue", "AceEvent-3.0", "AceTimer-3.0");

local pairs = pairs;

function mod:OnEnable()
	-- Register our own slash commands
	-- /im queue
	addon:RegisterSlash(function()
		mod:QueueAll();
	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.");
	
	self:RegisterMessage("IM_QUEUE_ALL");
	self:RegisterMessage("IM_QUEUE_GROUP");
end

function mod:IM_QUEUE_ALL()
	self:QueueAll();
end

function mod:IM_QUEUE_GROUP(event, groupName)
	self:QueueGroup(groupName);
end

function mod:QueueAll()
	local playerName = UnitName("player");
	
	-- Go through all groups
	for groupName, values in pairs(addon.db.profile.groups) do
		local trackAt = addon:GetOptionByKey(groupName, "trackAtCharacters");
		
		if trackAt[playerName] then
			self:QueueGroup(groupName);
		end
	end
end

function mod:QueueGroup(groupName)
	if not addon.db.profile.groups[groupName] then
		print(("Tried to queue items from a group named \"%s\", but no such group exists."):format(groupName));
		return;
	end
	
	local temp = {};
	
	local tradeskillName, currentLevel, maxLevel = GetTradeSkillLine();
	
	if tradeskillName ~= "UNKNOWN" then
		-- Go through all trade skills for the profession
		for i = 1, GetNumTradeSkills() do
			-- Process every single tradeskill
			self:ProcessTradeSkill(i, groupName, temp);
		end
	end
	
	if addon.db.profile.groups[groupName].items then
		for itemId, _ in pairs(addon.db.profile.groups[groupName].items) do
			if not temp[itemId] then
				local itemLink = select(2, GetItemInfo(itemId));
				
				print("Couldn't queue " .. (itemLink or itemId or "???") .. " (not part of this profession)");
			end
		end
	end
end

function mod:ProcessTradeSkill(i, groupName, temp)
	-- Try to retrieve the itemlink, this will be nil if current item is a group instead
	local itemLink = GetTradeSkillItemLink(i);
	
	if itemLink then
		local itemId = addon:GetItemId(itemLink);
		if not itemId then
			-- If this isn't an item, it can only be an enchant instead
			itemId = tonumber(itemLink:match("|Henchant:([-0-9]+)|h"));
			
			itemId = addon.scrollIds[itemId]; -- change enchantIds into scrollIds
		end
		
		if addon.db.profile.groups[groupName].items[itemId] then
			-- This item is in this group, queue it!
			
			if temp then
				-- Remember which items have been processed
				temp[itemId] = true;
			end
			
			local currentStock = addon:GetItemCount(itemId, groupName);
			
			if currentStock >= 0 then
				-- Current stock will be -1 when no itemcount addon was found
				
				-- Retrieve group settings
				local restockTarget = addon:GetOptionByKey(groupName, "restockTarget");
				local bonusQueue = addon:GetOptionByKey(groupName, "bonusQueue");
				local minCraftingQueue = floor( addon:GetOptionByKey(groupName, "minCraftingQueue") * restockTarget );
				
				-- Calculate the amount to be queued
				local amount = ( restockTarget - currentStock );
				
				if currentStock == 0 and bonusQueue > 0 then
					-- If we have none left and the bonus queue is enabled, modify the amount to  be queued
					
					amount = floor( ( amount * ( bonusQueue + 1 ) ) + .5 ); -- round
				end
				
				if amount > 0 and amount >= minCraftingQueue then
					-- If we are queueing at least one AND more than the minimum amount, then proceed
					
					-- Auction value settings
					local priceThreshold = addon:GetOptionByKey(groupName, "priceThreshold");
					
					if priceThreshold == 0 or addon:GetAuctionValue(itemLink, groupName) >= 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(i, amount, groupName);
						
						print("Queued " .. amount .. " of " .. itemLink);
					end
				end
			else
				print("No usable itemcount addon found.");
			end
		end
	end
end

function mod:Queue(tradeSkillIndex, amount, group)
	tradeSkillIndex = tonumber(tradeSkillIndex);
	amount = tonumber(amount);
	
	if not tradeSkillIndex or not amount then return; end
	
	local selectedExternalAddon = addon:GetOptionByKey(group, "craftingAddon");
	
	if addon.supportedAddons.crafting[selectedExternalAddon] and addon.supportedAddons.crafting[selectedExternalAddon].IsEnabled() then
		-- Try to use the default auction pricing addon
		
		return addon.supportedAddons.crafting[selectedExternalAddon].Queue(tradeSkillIndex, amount);
	else
		-- Default not available, get the first one then
		
		for name, value in pairs(addon.supportedAddons.crafting) do
			if value.IsEnabled() then
				return value.Queue(tradeSkillIndex, amount);
			end
		end
	end
	
	return -2;
end