view ItemCountAddons/DataStore (with guilds).lua @ 61:d903b0a151d3

Command handler function is now private, no need to keep it global when you can use the global SlashCmdList["INVENTORIUM"](msg) instead. Added database updating mechanism. All database vars are now stored in profiles rather than global. Now refreshing options when a different profile is selected. Fixed an issue with minimum local and global stock values not being inherited from the defaults.
author Zerotorescue
date Wed, 22 Dec 2010 15:04:09 +0100
parents 06fee4208bf2
children
line wrap: on
line source
do
	
	local function GetTotalCount(itemId)
		local realm = GetRealmName();
		
		local totalCount = 0;
		
		local guilds = {};
		
		-- Process all accounts
		for accountName in pairs(DataStore:GetAccounts()) do
		
			-- Process all charracters
			for characterName, character in pairs(DataStore:GetCharacters(realm, accountName)) do
				-- Get only useful info (currency / gear shouldn't contain the stuff we are interested in)
				local bag, bank = DataStore:GetContainerItemCount(character, itemId) or 0, 0;
				local auctionHouse = DataStore:GetAuctionHouseItemCount(character, itemId) or 0;
				local mail = DataStore:GetMailItemCount(character, itemId) or 0;
				
				totalCount = totalCount + bag + bank + auctionHouse + mail;
			end
			
			-- Process all guilds
			for guildName, guild in pairs(DataStore:GetGuilds(realm, accountName)) do
				if not guilds[guildName] then
					-- We don't want itemcounts from a single guild to be counted twice, so first to present data wins
					
					guilds[guildName] = true;
					
					local guild = DataStore:GetGuildBankItemCount(guild, itemId) or 0;
				
					totalCount = totalCount + guild;
				end
			end
		end
		
		return totalCount or -1;
	end
	
	local function GetCharacterCount(itemId)
		local character = DataStore:GetCharacter();
		
		local bag, bank = DataStore:GetContainerItemCount(character, itemId) or 0, 0;
		local auctionHouse = DataStore:GetAuctionHouseItemCount(character, itemId) or 0;
		local mail = DataStore:GetMailItemCount(character, itemId) or 0;
		
		return bag, bank, auctionHouse, mail;
	end
	
	local function IsEnabled()
		return (DataStore and DataStore.GetContainerItemCount and DataStore.GetAuctionHouseItemCount and DataStore.GetMailItemCount);
	end
	
	IMRegisterItemCountAddon("DataStore (with guilds)", GetTotalCount, GetCharacterCount, IsEnabled);
	
end