Quantcast

- Cleanup and comments

urnati [08-20-24 - 17:15]
- Cleanup and comments
Filename
Titan/TitanUtils.lua
TitanGold/TitanGold.lua
TitanLocation/TitanLocation.lua
diff --git a/Titan/TitanUtils.lua b/Titan/TitanUtils.lua
index 0ac9b5e..3311aff 100644
--- a/Titan/TitanUtils.lua
+++ b/Titan/TitanUtils.lua
@@ -796,7 +796,7 @@ end

 ---@param ThresholdTable table holding the list of colors and values
 ---@param value number to check against table ranges (assumes assending values)
----@return string color in table or "GRAY_FONT_COLOR"
+---@return any color in table or "GRAY_FONT_COLOR"
 function TitanUtils_GetThresholdColor(ThresholdTable, value)
 	--[[ example
 	local TITAN_FPS_THRESHOLD_TABLE = {
@@ -808,12 +808,12 @@ function TitanUtils_GetThresholdColor(ThresholdTable, value)
 	--]]
 	if (not tonumber(value) or type(ThresholdTable) ~= "table"
 			or ThresholdTable.Values == nil or ThresholdTable.Colors == nil
-			or table.getn(ThresholdTable.Values) >= table.getn(ThresholdTable.Colors)
+			or (#ThresholdTable.Values >= #ThresholdTable.Colors)
 		) then
-		return _G["GRAY_FONT_COLOR"];
+		return GRAY_FONT_COLOR
 	end

-	local n = table.getn(ThresholdTable.Values) + 1;
+	local n = #ThresholdTable.Values + 1;
 	for i = 1, n do
 		local low = TitanUtils_Ternary(i == 1, nil, ThresholdTable.Values[i - 1]); -- lowest
 		local high = TitanUtils_Ternary(i == n, nil, ThresholdTable.Values[i]); -- highest
@@ -836,7 +836,7 @@ function TitanUtils_GetThresholdColor(ThresholdTable, value)
 	end

 	-- Should never reach here
-	return _G["GRAY_FONT_COLOR"];
+	return GRAY_FONT_COLOR
 end

 ---API Routine that returns the text or an empty string.
diff --git a/TitanGold/TitanGold.lua b/TitanGold/TitanGold.lua
index 1785e80..29e7116 100644
--- a/TitanGold/TitanGold.lua
+++ b/TitanGold/TitanGold.lua
@@ -7,6 +7,9 @@
 -- **************************************************************************
 --]]

+-- WoW method to get addon name
+local addonName = ...
+
 -- ******************************** Constants *******************************
 local TITAN_GOLD_ID = "Gold";
 local TITAN_BUTTON = "TitanPanel" .. TITAN_GOLD_ID .. "Button"
@@ -18,7 +21,6 @@ local updateTable = { TITAN_GOLD_ID, TITAN_PANEL_UPDATE_TOOLTIP };
 GoldSave = {} -- saved vars in TOC
 local GOLD_INITIALIZED = false;
 local GOLD_INDEX = "";
-local WARBAND_INDEX = ""
 local GOLD_STARTINGGOLD;
 local GOLD_SESSIONSTART;
 local AceTimer = LibStub("AceTimer-3.0")
@@ -26,8 +28,8 @@ local L = LibStub("AceLocale-3.0"):GetLocale(TITAN_ID, true)
 local GoldTimer = {};
 local GoldTimerRunning = false
 local _G = getfenv(0);
-local realmName = "" -- fill on PEW
-local realmNames = {} -- fill on PEW
+local realmName = ""     -- fill on PEW
+local realmNames = {}    -- fill on PEW
 local merged_realms = {} -- fill on PEW

 -- English faction for indexing and sorting and coloring
@@ -35,6 +37,7 @@ local TITAN_ALLIANCE = "Alliance"
 local TITAN_HORDE = "Horde"

 local player_faction, player_faction_locale = UnitFactionGroup("Player")
+local player_name = GetUnitName("Player")

 --[[  debug
 local FACTION_ALLIANCE = "Alliance_debug"
@@ -42,6 +45,8 @@ local FACTION_HORDE = "Horde_debug"
 --]]
 -- ******************************** Functions *******************************

+-- A bit overkill but make a class for the Warbank bank functions
+
 local Warband = {
 	bank_sum = 0,
 	active = false,
@@ -49,17 +54,19 @@ local Warband = {
 }
 ---local Warband Bank debug
 function Warband.debug(reason)
-	print("WB "..reason
-	.." "..tostring(Warband.active)
-	.." "..tostring(Warband.label)
-	.." "..tostring(Warband.bank_sum)
+	print("WB " .. reason
+		.. " " .. tostring(Warband.active)
+		.. " " .. tostring(Warband.label)
+		.. " " .. tostring(Warband.bank_sum)
 	)
 end
+
 ---local Update Warband Bank info - sum
 function Warband.SetSum()
 	Warband.bank_sum = C_Bank.FetchDepositedMoney(Enum.BankType.Account)
---	Warband.debug("SetSum")
+	--	Warband.debug("SetSum")
 end
+
 ---local Return Warband Bank info
 function Warband.Init()
 	-- check for func even though it does not work atm... 2024 Aug  (Added 11.0.0)
@@ -70,23 +77,39 @@ function Warband.Init()
 	else
 		-- Likely Classic version
 	end
---	Warband.debug("Init")
-	end
+	--	Warband.debug("Init")
+end
+
 ---local Return Warband Bank info
 ---@return number
 function Warband.GetSum()
---	Warband.debug("GetSum")
+	--	Warband.debug("GetSum")
 	return Warband.bank_sum
 end
+
 ---local Return Warband Bank info
 ---@return string
 function Warband.GetName()
---	Warband.debug("GetName")
+	--	Warband.debug("GetName")
 	return Warband.label
 end

 --===

+---Helper to safely encapsulate WoW API (returns number in form ggsscc)
+---@return number
+local function Get_Money()
+	local money = GetMoney()
+	-- Do safety check to prevent errors
+	if type(money) == "number" then
+		-- assume it is good
+	else
+		-- Not accurate but safe
+		money = 0
+	end
+	return money
+end
+
 local function GetConnectedRealms()
 	local realms = GetAutoCompleteRealms()
 	if #realms == 0 then
@@ -153,8 +176,8 @@ end
 ---@field char_name string Saved toon name
 ---@field server string Saved toon server
 ---@field faction string Saved toon faction
----@field same_faction boolean Saved toon faction is same as player
----@field ignore_faction boolean User selection to ignore faction or not
+---@field same_faction boolean Saved toon faction is same as player
+---@field ignore_faction boolean User selection to ignore faction or not
 ---@field same_realm boolean Saved realm is same as this server
 ---@field merge_realm boolean Saved realm is in mergerd server list (connected servers)
 ---@field show_toon boolean Show server - simple test
@@ -163,7 +186,7 @@ end
 ---@param index string
 ---@return IndexInfo
 local function EvalIndexInfo(index)
-	local res = {valid = false}
+	local res = { valid = false }
 	local character, charserver, char_faction = GetIndexInfo(index)

 	if character then
@@ -195,7 +218,7 @@ local function EvalIndexInfo(index)
 		end

 		if (res.ignore_faction or res.same_faction)
-		and GoldSave[index].show then
+			and GoldSave[index].show then
 			res.show_toon = true
 		else
 			res.show_toon = false
@@ -251,7 +274,7 @@ local function ShowMenuButtons(faction, level)
 	end
 	GoldSorted = SortByIndex(GoldSorted)

-	for i = 1, GoldSorted do
+	for i = 1, #GoldSorted do
 		local index = GoldSorted[i]
 		local character, charserver, char_faction = GetIndexInfo(index)
 		if character and (char_faction == faction) then
@@ -277,7 +300,7 @@ local function DeleteMenuButtons(faction, level)
 	TitanPanelRightClickMenu_AddTitle(L["TITAN_GOLD_DELETE_PLAYER"], level)

 	local info = {};
-	local name = GetUnitName("player");
+	local name = player_name
 	local server = realmName;

 	-- Sort names for the menu list
@@ -296,13 +319,13 @@ local function DeleteMenuButtons(faction, level)
 			info.value = character;
 			info.func = function()
 				print("Del"
-				.." "..tostring(index)..""
-				.." "..tostring(GoldSave[index].gold)..""
+					.. " " .. tostring(index) .. ""
+					.. " " .. tostring(GoldSave[index].gold) .. ""
 				)
 				GoldSave[index] = {}
 				GoldSave[index] = nil
 				print("Del"
-				.." "..tostring(GoldSave[index])..""
+					.. " " .. tostring(GoldSave[index]) .. ""
 				)
 				TitanPanelButton_UpdateButton(TITAN_GOLD_ID)
 			end
@@ -414,13 +437,11 @@ end
 ---local Calculates total gold for display per user selections
 ---@return integer
 local function TotalGold()
+	-- EvalIndexInfo checks the toon info against the user options
+	-- then returns as a table of 'flags'.
+	-- The if within each loop checks the appropriate flags per user server display option.
+
 	local ttlgold = 0;
-	local cnt = 0;
-	local faction = UnitFactionGroup("Player");
-	local coin_str = ""
-	local character, charserver = "", ""
-	local char_faction = ""
-	local ignore_faction = TitanGetVar(TITAN_GOLD_ID, "IgnoreFaction")

 	if TitanGetVar(TITAN_GOLD_ID, "SeparateServers") then
 		-- Parse the database and display all characters on this server
@@ -437,7 +458,7 @@ local function TotalGold()
 		for index, money in pairs(GoldSave) do
 			local char = EvalIndexInfo(index)
 			if char.valid and char.merge_realm and char.show_toon then
-					ttlgold = ToonAdd(GoldSave[index].show, GoldSave[index].gold, ttlgold)
+				ttlgold = ToonAdd(GoldSave[index].show, GoldSave[index].gold, ttlgold)
 			else
 				-- Do not show per flags
 			end
@@ -447,7 +468,7 @@ local function TotalGold()
 		for index, money in pairs(GoldSave) do
 			local char = EvalIndexInfo(index)
 			if char.valid and char.show_toon then
-					ttlgold = ToonAdd(GoldSave[index].show, GoldSave[index].gold, ttlgold)
+				ttlgold = ToonAdd(GoldSave[index].show, GoldSave[index].gold, ttlgold)
 			else
 				-- Do not show per flags
 			end
@@ -474,7 +495,7 @@ local function GetTooltipText()
 	local GoldSorted = {};
 	local currentMoneyRichText = "";
 	local countelements = 0;
-	local faction, faction_locale = UnitFactionGroup("Player") -- get localized faction
+	local faction = player_faction
 	local ignore_faction = TitanGetVar(TITAN_GOLD_ID, "IgnoreFaction")

 	for _ in pairs(realmNames) do
@@ -572,9 +593,9 @@ local function GetTooltipText()
 	--
 	if Warband.active then
 		currentMoneyRichText = currentMoneyRichText .. "\n"
-		.. "------ \t +" .. "\n"
-		.. Warband.GetName() -- localized
-		.. "\t" .. NiceCash(Warband.GetSum(), false, false)
+			.. "------ \t +" .. "\n"
+			.. Warband.GetName() -- localized
+			.. "\t" .. NiceCash(Warband.GetSum(), false, false)
 	end


@@ -603,7 +624,7 @@ print("TG"
 		.. L["TITAN_GOLD_TTL_GOLD"] .. "\t" .. coin_str

 	-- find session earnings and earning per hour
-	local sesstotal = GetMoney() - GOLD_STARTINGGOLD;
+	local sesstotal = Get_Money() - GOLD_STARTINGGOLD;
 	local negative = false;
 	if (sesstotal < 0) then
 		sesstotal = math.abs(sesstotal);
@@ -617,7 +638,7 @@ print("TG"

 	local session_status;
 	local per_hour_status;
-		local sessionMoneyRichText = ""
+	local sessionMoneyRichText = ""
 	if TitanGetVar(TITAN_GOLD_ID, "ShowSessionInfo") then
 		sessionMoneyRichText = "\n\n" .. TitanUtils_GetHighlightText(L["TITAN_GOLD_STATS_TITLE"])
 			.. "\n" .. L["TITAN_GOLD_START_GOLD"] .. "\t" .. coin_str .. "\n"
@@ -691,7 +712,7 @@ local function Sort_Toggle()
 end

 local function ResetSession()
-	GOLD_STARTINGGOLD = GetMoney();
+	GOLD_STARTINGGOLD = Get_Money();
 	GOLD_SESSIONSTART = GetTime();
 	DEFAULT_CHAT_FRAME:AddMessage(TitanUtils_GetGreenText(L["TITAN_GOLD_SESSION_RESET"]));
 end
@@ -700,14 +721,14 @@ end
 ---@param self Button
 local function Initialize_Array(self)
 	if (GOLD_INITIALIZED) then
-		-- nlready done
+		-- already done
 	else
 		self:UnregisterEvent("ADDON_LOADED");

 		-- See if this is a new toon to Gold
 		if (GoldSave[GOLD_INDEX] == nil) then
 			GoldSave[GOLD_INDEX] = {}
-			GoldSave[GOLD_INDEX] = { gold = GetMoney(), name = UnitName("player") }
+			GoldSave[GOLD_INDEX] = { gold = Get_Money(), name = player_name }
 		end

 		-- Ensure the saved vars are usable
@@ -733,10 +754,10 @@ local function Initialize_Array(self)
 				GoldSave[index].faction = FACTION_OTHER
 			end
 		end
-		GOLD_STARTINGGOLD = GetMoney();
+		GOLD_STARTINGGOLD = Get_Money();
 		GOLD_SESSIONSTART = GetTime();
 		GOLD_INITIALIZED = true;
---[[
+		--[[
 print("Init"
 .." "..tostring(GOLD_STARTINGGOLD)..""
 .." "..tostring(GOLD_SESSIONSTART)..""
@@ -1135,7 +1156,7 @@ end
 ---@param self Button
 local function OnShow(self)
 	self:RegisterEvent("PLAYER_MONEY")
-	GoldSave[GOLD_INDEX].gold = GetMoney()
+	GoldSave[GOLD_INDEX].gold = Get_Money()

 	if GoldSave and TitanGetVar(TITAN_GOLD_ID, "DisplayGoldPerHour") then
 		if GoldTimerRunning then
@@ -1150,7 +1171,6 @@ local function OnShow(self)

 	if Warband.active then
 		Warband.SetSum()
----@diagnostic disable-next-line: param-type-mismatch
 		self:RegisterEvent("ACCOUNT_MONEY") -- register for changes
 	end
 end
@@ -1171,7 +1191,7 @@ end
 ---@param self Button
 ---@param event string
 ---@param ... any
-local function OnEvent(self, event, ...)
+local function OnEvent(self, event, a1, ...)
 	--[[
 print("_OnEvent"
 .." "..tostring(event)..""
@@ -1180,38 +1200,31 @@ print("_OnEvent"

 	if (event == "PLAYER_MONEY") then
 		if (GOLD_INITIALIZED) then
-			GoldSave[GOLD_INDEX].gold = GetMoney()
+			GoldSave[GOLD_INDEX].gold = Get_Money()
 			TitanPanelButton_UpdateButton(TITAN_GOLD_ID)
 		end
-		return;
-	end
-
-	if (event == "ACCOUNT_MONEY") then
+	elseif (event == "ACCOUNT_MONEY") then
 		if (GOLD_INITIALIZED) then
 			Warband.SetSum()
 		end
-		return
-	end
+	elseif (event == "ADDON_LOADED") then
+		if a1 == addonName then
+			realmName = GetRealmName() -- this realm
+			realmNames = GetConnectedRealms()
+			-- flip to make a simple lookup later rather than a loop
+			for index, realm in pairs(realmNames) do
+				merged_realms[realm] = true
+			end

-	if (event == "ADDON_LOADED") then
-		realmName = GetRealmName() -- this realm
-		realmNames = GetConnectedRealms()
-		-- flip to make a simple lookup later rather than a loop
-		for index, realm in pairs(realmNames) do
-			merged_realms[realm] = true
+			-- Faction is English to use as index NOT display
+			GOLD_INDEX = CreateIndex(player_name, realmName, player_faction)
+			Initialize_Array(self);
+		else
+			-- Not this addon
 		end
-
-		-- Faction is English to use as index NOT display
-		GOLD_INDEX = CreateIndex(UnitName("player"), realmName, UnitFactionGroup("Player"))
-
-		return;
-	end
-
-	if (event == "PLAYER_ENTERING_WORLD") then
-		Initialize_Array(self);
+	elseif (event == "PLAYER_ENTERING_WORLD") then
 		Warband.Init()
 		TitanPanelButton_UpdateButton(TITAN_GOLD_ID)
-		return;
 	end
 end

@@ -1230,7 +1243,7 @@ function OnClick(self, button)
 			else
 				mark = ""
 			end
-			TitanPrint("- "..tostring(realms[idx])..mark, "plain")
+			TitanPrint("- " .. tostring(realms[idx]) .. mark, "plain")
 		end
 	end
 end
diff --git a/TitanLocation/TitanLocation.lua b/TitanLocation/TitanLocation.lua
index 3c7b0ea..28c97e9 100755
--- a/TitanLocation/TitanLocation.lua
+++ b/TitanLocation/TitanLocation.lua
@@ -307,8 +307,9 @@ debug_msg(msg)
 end

 ---local Set the coordinates text for player and cursor if user requested.
----'start' / map open starts timer for cursor mapping and coords updating.
----'stop' / map close cancels timer.
+---'start' Sets the OnShow and OnHide for the coords frame.
+---'stop' Clears the OnShow and OnHide for the coords frame.
+---OnShow and OnHide are triggered when world map is opened because it is the parent frame.
 ---@param action string start | stop
 local function CoordFrames(action)
 	if addon_conflict then