view Core.lua @ 89:a12d22ef3f39

AceSerializer has been enabled again as it?s used when exporting/importing groups. All other unused libraries are now really removed. Adjusted debug function to format only when a debug channel is available. Fixed moving from guild banks, checking if items are locked is different then with banks. Now unregistering the item locking event so it doesn?t continue to try to move every time an item is switched after the automated cycle has finished. (geeezus, this description is a total overkill) Fixed item queueing. Queue all when there?s a group without any items inside no longer crashes. Removing cached container data after closing a container.
author Zerotorescue
date Fri, 07 Jan 2011 22:19:03 +0100
parents 3bec0ea44607
children 31493364b163
line wrap: on
line source
-- You can access this addon's object through: LibStub("AceAddon-3.0"):GetAddon("Inventorium")
local addon = select(2, ...);
addon = LibStub("AceAddon-3.0"):NewAddon(addon, "Inventorium", "AceEvent-3.0");

--@debug@
local addonRevision = 1;
--@end-debug@
--[===[@non-debug@
local addonRevision = @project-revision@;
--@end-non-debug@]===]

local _G = _G;
local print, pairs, tonumber = _G.print, _G.pairs, _G.tonumber;

--  All modules must be able to retrieve our supported addons database, thus keep it a part of the addon object rather than local
addon.supportedAddons = {};
addon.supportedAddons.auctionPricing = {};
addon.supportedAddons.itemCount = {};
addon.supportedAddons.crafting = {};

function addon:OnInitialize()
	-- SAVED VARIABLES
	
	local defaults = {
		global = {
			version = nil,
		},
		profile = {
			defaults = {
				auctionPricingAddon = "Auctioneer",
				itemCountAddon = "Altoholic",
				craftingAddon = "AdvancedTradeSkillWindow",
				minLocalStock = 20,
				alertBelowLocalMinimum = true,
				autoRefill = true,
				minGlobalStock = 60,
				alertBelowGlobalMinimum = true,
				summaryThresholdShow = 10,
				restockTarget = 60,
				minCraftingQueue = 0.05,
				bonusQueue = 0.1,
				priceThreshold = 0,
				summaryHidePriceThreshold = false,
				trackAtCharacters = {
				},
				localItemData = {
					["Bag"] = true,
					["Auction House"] = true,
				},
				summary = {
					speed = 5,
					width = 700,
					height = 600,
				},
				colors = {
					red = 0,
					orange = 0.3,
					yellow = 0.6,
					green = 0.95,
				},
			},
			groups = {
			},
		},
		factionrealm = {
			characters = {
			},
		},
	};
	
	-- Register our saved variables database
	self.db = LibStub("AceDB-3.0"):New("InventoriumDB", defaults, true);
	
	-- SLASH COMMANDS
	
	-- Disable the AddonLoader slash commands
	SLASH_INVENTORIUM1 = nil;
	SLASH_IM1 = nil;
	
	-- Register our own slash commands
	SLASH_INVENTORIUM1 = "/inventorium";
	SLASH_INVENTORIUM2 = "/im";
	SlashCmdList["INVENTORIUM"] = function(msg)
		addon:CommandHandler(msg);
	end;
	
	-- Debug command handling
	self:RegisterSlash(function(this)
		this.debugChannel = false;
		for i = 1, NUM_CHAT_WINDOWS do
			local name = GetChatWindowInfo(i);
			
			if name:upper() == "DEBUG" then
				this.debugChannel = _G["ChatFrame" .. i];
			
				print("A debug channel already exists, used the old one. (" .. i .. ")");
				return;
			end
		end
		
		if not this.debugChannel then
			-- Create a new debug channel
			local chatFrame = FCF_OpenNewWindow('Debug');
			ChatFrame_RemoveAllMessageGroups(chatFrame);
			this.debugChannel = chatFrame;
			
			print("New debug channel created.");
		end
	end, { "d", "debug" });

	-- Remember this character is on this account
	local playerName = UnitName("player");
	if not self.db.factionrealm.characters[playerName] then
		self.db.factionrealm.characters[playerName] = true;
		
		-- Default to tracking on all chars, untracking is a convenience, not tracking by default would probably get multiple issue reports.
		self.db.profile.defaults.trackAtCharacters[playerName] = true;
	end
	
	self:UpdateDatabase();
end





-- Database patching after new revisions

function addon:UpdateDatabase()
	if not self.db.global.version or self.db.global.version < addonRevision then
		-- Is our database outdated? Then patch it.
		
		if not self.db.global.version then
			-- Old version was before version was saved, many changes were done in that revision
			
			print("Updating Inventorium database from version " .. (self.db.global.version or "Unknown") .. " to version " .. addonRevision .. "...");
			
			if self.db.global and self.db.global.defaults then
				print("Moving all global data into your current profile...");
				
				-- All data mustn't be global but profile-based
				self.db.profile.defaults = CopyTable(self.db.global.defaults);
				self.db.profile.groups = CopyTable(self.db.global.groups);
				
				self.db.global.defaults = nil;
				self.db.global.groups = nil;
				
				self.CommandHandler = function()
					message("You must /reload once to finalize the Inventorium database updates. This will only be required once during the BETA.");
				end;
				self:CommandHandler();
			end
			
			if self.db.profile.defaults.minimumStock then
				print("Copying the minimum stock value into the minimum global stock...");
				
				-- We added another stock option and renamed the old to be more obvious about what it means
				self.db.profile.defaults.minGlobalStock = self.db.profile.defaults.minimumStock;
				self.db.profile.defaults.minimumStock = nil;
			end
			
			if self.db.profile.defaults.minimumLocalStock then
				print("Renaming the minimum local stock property...");
				
				-- We added another stock option and then renamed it
				self.db.profile.defaults.minLocalStock = self.db.profile.defaults.minimumLocalStock;
				self.db.profile.defaults.minimumLocalStock = nil;
			end
			
			if self.db.profile.defaults.alertBelowMinimum then
				print("Copying the alert below minimum value into the alert below global minimum value...");
				
				-- We added another stock option and then renamed it
				self.db.profile.defaults.alertBelowGlobalMinimum = self.db.profile.defaults.alertBelowMinimum;
				self.db.profile.defaults.alertBelowMinimum = nil;
			end
			
			-- Go through all groups to see if there's one with the above two renamed variables
			for groupName, values in pairs(self.db.profile.groups) do
				if values.minimumStock then
					values.minGlobalStock = values.minimumStock;
					values.minimumStock = nil;
				end
			end
		end
		
		-- Remember the version of our database
		self.db.global.version = addonRevision;
	end
end

function addon:GetOptionByKey(groupName, optionName, noDefault)
	if groupName and addon.db.profile.groups[groupName] and addon.db.profile.groups[groupName][optionName] ~= nil then
		-- If this option exists within the settings of this group
		
		return addon.db.profile.groups[groupName][optionName];
	elseif groupName and addon.db.profile.groups[groupName] and addon.db.profile.groups[groupName].virtualGroup ~= "" and not noDefault then
		-- If a virtual group was selected
		
		return self:GetOptionByKey(addon.db.profile.groups[groupName].virtualGroup, optionName, noDefault);
	elseif addon.db.profile.defaults[optionName] and not noDefault then
		return addon.db.profile.defaults[optionName];
	else
		return nil;
	end
end

function addon:GetItemCountAddon(group)
	local selectedExternalAddon = self:GetOptionByKey(group, "itemCountAddon");
	
	if self.supportedAddons.itemCount[selectedExternalAddon] and self.supportedAddons.itemCount[selectedExternalAddon].IsEnabled() then
		-- Try to use the default item count addon
		
		return self.supportedAddons.itemCount[selectedExternalAddon], selectedExternalAddon;
	else
		-- Default not available, get the first one then
		
		for name, value in pairs(self.supportedAddons.itemCount) do
			if value.IsEnabled() then
				return value, name;
			end
		end
	end
	
	return;
end

function addon:GetItemCount(itemId, group)
	itemId = tonumber(itemId);
	
	if not itemId then return; end
	
	local itemCountAddon = self:GetItemCountAddon(group);
	
	return (itemCountAddon and itemCountAddon.GetTotalCount(itemId)) or -1;
end

function addon:GetLocalItemCount(itemId, group)
	itemId = tonumber(itemId);
	
	if not itemId then return; end
	
	local itemCountAddon = self:GetItemCountAddon(group);
	
	local currentItemCount;
	
	if itemCountAddon and itemCountAddon.GetCharacterCount then
		local bag, bank, auctionHouse, mail = itemCountAddon.GetCharacterCount(itemId);
		
		local selectedLocalItemCountSources = self:GetOptionByKey(group, "localItemData");
		
		currentItemCount = 0;
		if selectedLocalItemCountSources["Bag"] then
			currentItemCount = currentItemCount + bag;
		end
		if selectedLocalItemCountSources["Bank"] then
			currentItemCount = currentItemCount + bank;
		end
		if selectedLocalItemCountSources["Auction House"] then
			currentItemCount = currentItemCount + auctionHouse;
		end
		if selectedLocalItemCountSources["Mailbox"] then
			currentItemCount = currentItemCount + mail;
		end
	end
	
	return currentItemCount or -1;
end

function addon:GetAuctionValue(itemLink, group)
	if not itemLink then return -5; end
	
	local selectedExternalAddon = self:GetOptionByKey(group, "auctionPricingAddon");
	
	if self.supportedAddons.auctionPricing[selectedExternalAddon] and self.supportedAddons.auctionPricing[selectedExternalAddon].IsEnabled() then
		-- Try to use the default auction pricing addon
		
		return self.supportedAddons.auctionPricing[selectedExternalAddon].GetValue(itemLink);
	else
		-- Default not available, get the first one then
		
		for name, value in pairs(self.supportedAddons.auctionPricing) do
			if value.IsEnabled() then
				return value.GetValue(itemLink);
			end
		end
	end
	
	return -2;
end





-- Slash commands

local slashArgs = {};
local slashError = "Wrong argument, the following arguments are available:";

function addon:CommandHandler(message)
	local cmd, arg = string.split(" ", (message or ""), 2);
	cmd = string.lower(cmd);
	
	if slashArgs[cmd] then
		-- Pass a reference to the addon (to be used as "self") and the provided arg
		slashArgs[cmd](addon, arg);
	else
		print(slashError);
	end
end

function addon:RegisterSlash(func, args, description)
	for _, arg in pairs(args) do
		slashArgs[arg] = func;
	end
	
	if description then
		slashError = slashError .. "\n" .. description;
	end
end





-- Readable money

local goldText = "%s%d|cffffd700g|r ";
local silverText = "%s%d|cffc7c7cfs|r ";
local copperText = "%s%d|cffeda55fc|r";

function addon:ReadableMoney(copper, clean)
	local text = "";
	
	local gold = floor( copper / COPPER_PER_GOLD );
	if gold > 0 then
		text = goldText:format(text, gold);
	end
	
	if not clean or (not gold or gold < 10) then
		local silver = floor( ( copper % COPPER_PER_GOLD ) / COPPER_PER_SILVER );
		if silver > 0 then
			text = silverText:format(text, silver);
		end
		
		if not clean or (not gold or gold < 1) then
			local copper = floor( copper % COPPER_PER_SILVER );
			if copper > 0 or text == "" then
				text = copperText:format(text, copper);
			end
		end
	end
	
	
	return string.trim(text);
end

function addon:ReadableMoneyToCopper(value)
	-- If a player enters a value it will be filled without color codes
	-- If it is retrieved from the database, it will be colored coded
	-- Thus we look for both
	local gold = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+g|r") or string.match(value, "(%d+)g"));
	local silver = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+s|r") or string.match(value, "(%d+)s"));
	local copper = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+c|r") or string.match(value, "(%d+)c"));
		
	return ( (gold or 0) * COPPER_PER_GOLD ) + ( (silver or 0) * COPPER_PER_SILVER ) + (copper or 0);
end

function addon:ValidateReadableMoney(info, value)
	-- If a player enters a value it will be filled without color codes
	-- If it is retrieved from the database, it will be colored coded
	-- Thus we look for both
	local gold = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+g|r") or string.match(value, "(%d+)g"));
	local silver = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+s|r") or string.match(value, "(%d+)s"));
	local copper = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+c|r") or string.match(value, "(%d+)c"));
	
	if not gold and not silver and not copper then
		return "The provided amount of money is invalid. Please provide the amount of money as #g#s#c, e.g. 591617g24s43c.";
	else
		return true;
	end
end





-- Public

function IMRegisterPricingAddon(name, get, enabled, onSelect)
	addon.supportedAddons.auctionPricing[name] = {
		GetValue = get,
		IsEnabled = enabled,
		OnSelect = onSelect,
	};
end

function IMRegisterItemCountAddon(name, getTotal, getCharacter, enabled, onSelect)
	addon.supportedAddons.itemCount[name] = {
		GetTotalCount = getTotal,
		GetCharacterCount = getCharacter,
		IsEnabled = enabled,
		OnSelect = onSelect,
	};
end

function IMRegisterCraftingAddon(name, queue, enabled, onSelect)
	addon.supportedAddons.crafting[name] = {
		Queue = queue,
		IsEnabled = enabled,
		OnSelect = onSelect,
	};
end

-- We need a global command handler for our chat-links
function InventoriumCommandHandler(msg)
	addon:CommandHandler(msg);
end





-- General

addon.Colors = {
	Red = { 1, 0, 0 },
	Green = { 0, 1, 0 },
}; -- easy to extend if more colors are needed
function addon:Print(text, color)
	local red, green, blue;
	
	if color then
		red, green, blue = color[1], color[2], color[3];
	end
	
	DEFAULT_CHAT_FRAME:AddMessage(text or "", red, green, blue, nil, 5);
end

function addon:GetItemID(itemLink)
	itemLink = itemLink and itemLink:match("|Hitem:([-0-9]+):"); -- if itemLink is nil, it won't execute the second part
	itemLink = itemLink and tonumber(itemLink);
	
	return itemLink;
end

-- Debug

function addon:Debug(t, ...)
	if not self.debugChannel and self.debugChannel ~= false then
		-- We want to check just once, so if you add a debug channel later just do a /reload (registering an event for this is wasted resources)
		self.debugChannel = false;
		
		for i = 1, NUM_CHAT_WINDOWS do
			local name = GetChatWindowInfo(i);
			
			if name:upper() == "DEBUG" then
				self.debugChannel = _G["ChatFrame" .. i];
			end
		end
	end
	
	if self.debugChannel then
		self.debugChannel:AddMessage(t:format(...));
	end
end