Quantcast

Update bag for ammo and shard bags; streamlined code

urnati [08-09-19 - 03:45]
Update bag for ammo and shard bags; streamlined code
Filename
TitanClassicBag/TitanClassicBag.lua
diff --git a/TitanClassicBag/TitanClassicBag.lua b/TitanClassicBag/TitanClassicBag.lua
index e4027c2..63066f2 100644
--- a/TitanClassicBag/TitanClassicBag.lua
+++ b/TitanClassicBag/TitanClassicBag.lua
@@ -18,6 +18,173 @@ local AceTimer = LibStub("AceTimer-3.0")
 local BagTimer
 -- ******************************** Functions *******************************

+--[[
+-- **************************************************************************
+-- NAME : IsAmmoPouch(name)
+-- DESC : Test to see if bag is an ammo pouch
+-- VARS : name = item name
+-- **************************************************************************
+--]]
+local function IsAmmoPouch(name)
+	local bagType = ""
+	local color = {r=1,g=1,b=1}; -- WHITE
+	if (name) then
+		for index, value in pairs(L["TITAN_BAG_AMMO_POUCH_NAMES"]) do
+			if (string.find(name, value)) then
+				bagType = "AMMO";
+				color = {r=1,g=1,b=1}; -- WHITE
+			end
+		end
+	end
+	return bagType, color
+end
+
+--[[
+-- **************************************************************************
+-- NAME : IsShardBag(name)
+-- DESC : Test to see if bag is a shard bag
+-- VARS : name = item name
+-- **************************************************************************
+--]]
+local function IsShardBag(name)
+	local bagType = ""
+	local color = {r=1,g=1,b=1}; -- WHITE
+	if (name) then
+		for index, value in pairs(L["TITAN_BAG_SHARD_BAG_NAMES"]) do
+			if (string.find(name, value)) then
+				bagType = "SHARD";
+				color = {r=1,g=1,b=1}; -- WHITE
+				return bagType, color;
+			end
+		end
+	end
+	return bagType, color
+end
+
+--[[
+-- **************************************************************************
+-- NAME : IsProfBag(name)
+-- DESC : Test to see if bag is a profession bag
+-- VARS : name = item name
+-- **************************************************************************
+--]]
+local function IsProfBag(name)
+	local bagType = ""
+	local color = {r=1,g=1,b=1}; -- WHITE
+	-- each if returns if bag name is found, cleaner but could be confusing
+	if (name) then
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_ENCHANTING"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "ENCHANTING";
+				color = {r=0,g=0,b=1}; -- BLUE
+				return bagType, color;
+			end
+		end
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_ENGINEERING"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "ENGINEERING";
+				color = {r=1,g=0.49,b=0.04}; -- ORANGE
+				return bagType, color;
+			end
+		end
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_HERBALISM"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "HERBALISM";
+				color = {r=0,g=1,b=0}; -- GREEN
+				return bagType, color;
+			end
+		end
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_INSCRIPTION"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "INSCRIPTION";
+				color = {r=0.58,g=0.51,b=0.79}; -- PURPLE
+				return bagType, color;
+			end
+		end
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_JEWELCRAFTING"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "JEWELCRAFTING";
+				color = {r=1,g=0,b=0}; -- RED
+				return bagType, color;
+			end
+		end
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_LEATHERWORKING"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "LEATHERWORKING";
+				color = {r=0.78,g=0.61,b=0.43}; -- TAN
+				return bagType, color;
+			end
+		end
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_MINING"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "MINING";
+				color = {r=1,g=1,b=1}; -- WHITE
+				return bagType, color;
+			end
+		end
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_FISHING"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "FISHING";
+				color = {r=0.41,g=0.8,b=0.94}; -- LIGHT_BLUE
+				return bagType, color;
+			end
+		end
+		for index, value in pairs(L["TITAN_BAG_PROF_BAG_COOKING"]) do
+			if (string.find(name, value, 1, true)) then
+				bagType = "COOKING";
+				color = {r=0.96,g=0.55,b=0.73}; -- PINK
+				return bagType, color;
+			end
+		end
+	end
+	return bagType, color
+end
+
+--[[
+-- **************************************************************************
+-- NAME : CountMe(name)
+-- DESC : Test to see if bag should be counted
+-- VARS : name = item name
+-- **************************************************************************
+--]]
+local function CountMe(bag)
+	-- defaults as if bag does not exist
+	local name = (GetBagName(bag) or "")
+	local size = (GetContainerNumSlots(bag) or 0)
+	local bagType = ""
+	local color = {r=1,g=1,b=1} -- WHITE
+	local used = 0
+
+	if name ~= "" then -- a bag is in the slot
+		for slot = 1, size do
+			if (GetContainerItemInfo(bag, slot)) then
+				used = used + 1;
+			end
+		end
+		-- check for a special storage bag
+		if (TitanGetVar(TITAN_BAG_ID, "CountAmmoPouchSlots") and bagType == "") then
+			bagType, color = IsAmmoPouch(name)
+		end
+		if (TitanGetVar(TITAN_BAG_ID, "CountShardBagSlots") and bagType == "") then
+			bagType, color = IsShardBag(name)
+		end
+		if (TitanGetVar(TITAN_BAG_ID, "CountProfBagSlots") and bagType == "") then
+			bagType, color = IsProfBag(name)
+		end
+		if (bagType == "") then
+			bagType = "NORMAL"
+		end
+	end
+TitanDebug("CountMe:"
+.." size "..tostring(size)
+.." used "..tostring(used)
+.." bagType "..tostring(bagType)
+.." name "..tostring(name)
+)
+	return {size = size, used = used, bagType = bagType, name = name, color = color}
+end
+--]]
+
 -- **************************************************************************
 -- NAME : TitanPanelBagButton_OnLoad()
 -- DESC : Registers the plugin upon it loading
@@ -100,34 +267,37 @@ function TitanPanelBagButton_GetButtonText(id)
 	local usedProfBagSlots = {0,0,0,0,0};
 	local availableProfBagSlots = {0,0,0,0,0};
 	local bagRichTextProf = {"","","","",""};
+	local bagInfo = {}

 	totalBagSlots = 0;
 	usedBagSlots = 0;
+	bagRichText = ""
 	for bag = 0, 4 do
-		if not TitanBag_IsProfBag(GetBagName(bag)) then
-			local size = GetContainerNumSlots(bag);
-			if (size and size > 0) then
-				totalBagSlots = totalBagSlots + size;
-				for slot = 1, size do
-					if (GetContainerItemInfo(bag, slot)) then
-						usedBagSlots = usedBagSlots + 1;
-					end
-				end
+		local info = CountMe(bag)
+
+		if info.bagType == "" then
+			-- no bag in slot
+		elseif info.bagType == "NORMAL" then
+			usedBagSlots = usedBagSlots + info.used
+			totalBagSlots = totalBagSlots + info.size
+		else -- process special storage bag
+			totalProfBagSlots[bag+1] = info.size
+			usedProfBagSlots[bag+1] = info.used
+			availableProfBagSlots[bag+1] = info.size - info.used
+			-- prepare text for the special bag
+			if (TitanGetVar(TITAN_BAG_ID, "ShowUsedSlots")) then
+				bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], usedProfBagSlots[bag+1], totalProfBagSlots[bag+1]) .. "]";
+			else
+				bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], availableProfBagSlots[bag+1], totalProfBagSlots[bag+1]) .. "]";
 			end
-		end
-		if TitanGetVar(TITAN_BAG_ID, "CountProfBagSlots") and TitanBag_IsProfBag(GetBagName(bag)) then
-			local size = GetContainerNumSlots(bag);
-			if (size and size > 0) then
-				totalProfBagSlots[bag+1] = size;
-				for slot = 1, size do
-					if (GetContainerItemInfo(bag, slot)) then
-						usedProfBagSlots[bag+1] = usedProfBagSlots[bag+1] + 1;
-					end
-				end
-				availableProfBagSlots[bag+1] = totalProfBagSlots[bag+1] - usedProfBagSlots[bag+1];
+			if ( TitanGetVar(TITAN_BAG_ID, "ShowColoredText") ) then
+				bagRichTextProf[bag+1] = TitanUtils_GetColoredText(bagText, info.color);
+			else
+				bagRichTextProf[bag+1] = TitanUtils_GetHighlightText(bagText);
 			end
 		end
 	end
+	-- process normal bags as one set
 	availableBagSlots = totalBagSlots - usedBagSlots;

 	if (TitanGetVar(TITAN_BAG_ID, "ShowUsedSlots")) then
@@ -143,21 +313,6 @@ function TitanPanelBagButton_GetButtonText(id)
 		bagRichText = TitanUtils_GetHighlightText(bagText);
 	end

-	for bag = 1, 5 do
-		if totalProfBagSlots[bag] > 0 then
-			if (TitanGetVar(TITAN_BAG_ID, "ShowUsedSlots")) then
-				bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], usedProfBagSlots[bag], totalProfBagSlots[bag]) .. "]";
-			else
-				bagText = "  [" .. format(L["TITAN_BAG_FORMAT"], availableProfBagSlots[bag], totalProfBagSlots[bag]) .. "]";
-			end
-			if ( TitanGetVar(TITAN_BAG_ID, "ShowColoredText") ) then
-				bagType, color = TitanBag_IsProfBag(GetBagName(bag-1));
-				bagRichTextProf[bag] = TitanUtils_GetColoredText(bagText, color);
-			else
-				bagRichTextProf[bag] = TitanUtils_GetHighlightText(bagText);
-			end
-		end
-	end
 	bagRichText = bagRichText..bagRichTextProf[1]..bagRichTextProf[2]..bagRichTextProf[3]..bagRichTextProf[4]..bagRichTextProf[5];
 	return L["TITAN_BAG_BUTTON_LABEL"], bagRichText;
 end
@@ -315,6 +470,24 @@ function TitanPanelBagButton_ShowAvailableSlots()
 end

 -- **************************************************************************
+-- NAME : TitanPanelBagButton_ToggleIgnoreAmmoPouchSlots()
+-- DESC : Set option to count ammo pouch slots
+-- **************************************************************************
+function TitanPanelBagButton_ToggleIgnoreAmmoPouchSlots()
+	TitanToggleVar(TITAN_BAG_ID, "CountAmmoPouchSlots");
+	TitanPanelButton_UpdateButton(TITAN_BAG_ID);
+end
+
+-- **************************************************************************
+-- NAME : TitanPanelBagButton_ToggleIgnoreShardBagSlots()
+-- DESC : Set option to count shard bag slots
+-- **************************************************************************
+function TitanPanelBagButton_ToggleIgnoreShardBagSlots()
+	TitanToggleVar(TITAN_BAG_ID, "CountShardBagSlots");
+	TitanPanelButton_UpdateButton(TITAN_BAG_ID);
+end
+
+-- **************************************************************************
 -- NAME : TitanPanelBagButton_ToggleIgnoreProfBagSlots()
 -- DESC : Set option to count profession bag slots
 -- **************************************************************************
@@ -326,80 +499,3 @@ end
 function TitanPanelBagButton_ShowDetailedInfo()
 	TitanToggleVar(TITAN_BAG_ID, "ShowDetailedInfo");
 end
-
--- **************************************************************************
--- NAME : TitanBag_IsProfBag(name)
--- DESC : est to see if bag is a profession bag
--- VARS : name = item name
--- OUT  : bagType = type of profession matching bag name
---        color = the color associated with the profession
--- **************************************************************************
-function TitanBag_IsProfBag(name)
-	local bagType, color;
-	if (name) then
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_ENCHANTING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "ENCHANTING";
-				color = {r=0,g=0,b=1}; -- BLUE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_ENGINEERING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "ENGINEERING";
-				color = {r=1,g=0.49,b=0.04}; -- ORANGE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_HERBALISM"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "HERBALISM";
-				color = {r=0,g=1,b=0}; -- GREEN
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_INSCRIPTION"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "INSCRIPTION";
-				color = {r=0.58,g=0.51,b=0.79}; -- PURPLE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_JEWELCRAFTING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "JEWELCRAFTING";
-				color = {r=1,g=0,b=0}; -- RED
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_LEATHERWORKING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "LEATHERWORKING";
-				color = {r=0.78,g=0.61,b=0.43}; -- TAN
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_MINING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "MINING";
-				color = {r=1,g=1,b=1}; -- WHITE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_FISHING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "FISHING";
-				color = {r=0.41,g=0.8,b=0.94}; -- LIGHT_BLUE
-				return bagType, color;
-			end
-		end
-		for index, value in pairs(L["TITAN_BAG_PROF_BAG_COOKING"]) do
-			if (string.find(name, value, 1, true)) then
-				bagType = "COOKING";
-				color = {r=0.96,g=0.55,b=0.73}; -- PINK
-				return bagType, color;
-			end
-		end
-	end
-	return false;
-end
\ No newline at end of file