Quantcast

- XP : Fix for xp not showing on button

urnati [01-26-24 - 15:38]
- XP : Fix for xp not showing on button
Filename
TitanXP/TitanXP.lua
diff --git a/TitanXP/TitanXP.lua b/TitanXP/TitanXP.lua
index e74010f..919e90f 100644
--- a/TitanXP/TitanXP.lua
+++ b/TitanXP/TitanXP.lua
@@ -18,6 +18,8 @@ local TitanPanelXPButton_ButtonAdded = nil;
 local found = nil;
 local lastMobXP, lastXP, XPGain = 0, 0, 0
 local L = {} -- For AceLocale
+
+local trace = false
 -- ******************************** Functions *******************************

 --[[
@@ -89,8 +91,11 @@ end
 -- **************************************************************************
 --]]
 function TitanPanelXPButton_OnShow(self)
+	local ttext = ""
+
 	if (not self.sessionTime) then -- initial login / PEW
 		self.sessionTime = time();
+		ttext = ttext.."Sess reset"
 	end
 	if (not self.initXP) then -- initial login / PEW
 		self.initXP = UnitXP("player");
@@ -98,6 +103,7 @@ function TitanPanelXPButton_OnShow(self)
 		self.sessionXP = 0;
 		self.startSessionTime = time();
 		lastXP = self.initXP;
+		ttext = ttext.." | Init"
 	end
 	self:RegisterEvent("TIME_PLAYED_MSG");
 	self:RegisterEvent("PLAYER_XP_UPDATE");
@@ -105,10 +111,19 @@ function TitanPanelXPButton_OnShow(self)
 	self:RegisterEvent("CHAT_MSG_COMBAT_XP_GAIN");

 	TitanPanelXPButton_SetIcon();
+	ttext = ttext.." | Events"
+
 	found = nil;
 	if not TitanPanelXPButton_ButtonAdded then
 		RequestTimePlayed();
 		TitanPanelXPButton_ButtonAdded = true;
+		ttext = ttext.." | Time played"
+	end
+
+	if trace then
+		print("XP _OnShow"
+		.." "..tostring(ttext)..""
+		)
 	end
 end

@@ -139,26 +154,38 @@ end
 -- **************************************************************************
 --]]
 function TitanPanelXPButton_OnEvent(self, event, a1, a2, ...)
---[[
-print("XP _OnEvent"
-.." "..tostring(event)..""
-)
---]]
+	if trace then
+		print("XP _OnEvent"
+		.." "..tostring(event)..""
+		)
+	end
+
 	if (event == "PLAYER_ENTERING_WORLD") then
 	elseif (event == "TIME_PLAYED_MSG") then
 		-- Remember play time
 		self.totalTime = a1;
 		self.levelTime = a2;
 	elseif (event == "PLAYER_XP_UPDATE") then
-		self.initXP = UnitXP("player");
-		self.accumXP = 0;
-		self.sessionXP = 0;
-		self.startSessionTime = time();
+		if self.initXP then
+			-- has been initialized
+		else
+			self.initXP = UnitXP("player");
+			self.accumXP = 0;
+			self.sessionXP = 0;
+			self.startSessionTime = time();
+		end

 		XPGain = UnitXP("player") - lastXP;
 		lastXP = UnitXP("player");
 		if XPGain < 0 then XPGain = 0 end
 		self.sessionXP = UnitXP("player") - self.initXP + self.accumXP;
+		if trace then
+			print("XP Ev "
+			.." unit "..tostring(format("%0.1f", UnitXP("player")))..""
+			.." init "..tostring(format("%0.1f", self.initXP))..""
+			.." acc "..tostring(format("%0.1f", self.accumXP))..""
+			)
+		end
 	elseif (event == "PLAYER_LEVEL_UP") then
 		self.levelTime = 0;
 		self.accumXP = self.accumXP + UnitXPMax("player") - self.initXP;
@@ -201,81 +228,106 @@ end
 --]]
 function TitanPanelXPButton_GetButtonText(id)
 	if (TitanPanelXPButton.startSessionTime == nil) then
-		return;
-	end
-
-	local button, id = TitanUtils_GetButton(id, true);
-	local totalXP = UnitXPMax("player");
-	local currentXP = UnitXP("player");
-	local toLevelXP = totalXP - currentXP;
-	local sessionXP = button.sessionXP;
-	local xpPerHour, xpPerHourText, timeToLevel, timeToLevelText;
-	local sessionTime = time() - TitanPanelXPButton.startSessionTime;
-	local levelTime = TitanPanelXPButton.levelTime;
-	local numofkills, numofgains;
-	if lastMobXP ~= 0 then numofkills = math.ceil(toLevelXP / lastMobXP) else numofkills = _G["UNKNOWN"] end
-	if XPGain ~= 0 then numofgains = math.ceil(toLevelXP / XPGain) else numofgains = _G["UNKNOWN"] end
+		if trace then
+			print("XP "
+			.." "..tostring("start not set - too early")..""
+			)
+		end
+		return
+	else
+		local button, id = TitanUtils_GetButton(id, true);
+		local totalXP = UnitXPMax("player");
+		local currentXP = UnitXP("player");
+		local toLevelXP = totalXP - currentXP;
+		local sessionXP = button.sessionXP;
+		local xpPerHour, xpPerHourText, timeToLevel, timeToLevelText;
+		local sessionTime = time() - TitanPanelXPButton.startSessionTime;
+		local levelTime = TitanPanelXPButton.levelTime;
+		local numofkills, numofgains;
+		if lastMobXP ~= 0 then numofkills = math.ceil(toLevelXP / lastMobXP) else numofkills = _G["UNKNOWN"] end
+		if XPGain ~= 0 then numofgains = math.ceil(toLevelXP / XPGain) else numofgains = _G["UNKNOWN"] end
+		if trace then
+			print("XP / Hr"
+			.." sxp"..tostring(format("%0.1f", button.sessionXP))..""
+			.." st"..tostring(format("%0.1f", TitanPanelXPButton.startSessionTime))..""
+			)
+		end

-	if (levelTime) then
-		if (TitanGetVar(TITAN_XP_ID, "DisplayType") == "ShowXPPerHourSession") then
-			xpPerHour = sessionXP / sessionTime * 3600;
---			timeToLevel = TitanUtils_Ternary((sessionXP == 0), -1, toLevelXP / sessionXP * sessionTime);
-			timeToLevel = (sessionXP == 0) and -1 or toLevelXP / sessionXP * sessionTime;
-
-			xpPerHourText = comma_value(math.floor(xpPerHour+0.5));
-			timeToLevelText = TitanUtils_GetEstTimeText(timeToLevel);
-
-			return L["TITAN_XP_BUTTON_LABEL_XPHR_SESSION"], TitanUtils_GetHighlightText(xpPerHourText),
-				L["TITAN_XP_BUTTON_LABEL_TOLEVEL_TIME_LEVEL"], TitanUtils_GetHighlightText(timeToLevelText);
-		elseif (TitanGetVar(TITAN_XP_ID,"DisplayType") == "ShowXPPerHourLevel") then
-			xpPerHour = currentXP / levelTime * 3600;
---			timeToLevel = TitanUtils_Ternary((currentXP == 0), -1, toLevelXP / currentXP * levelTime);
-			timeToLevel = (currentXP == 0) and -1 or toLevelXP / currentXP * levelTime;
-
-			xpPerHourText = comma_value(math.floor(xpPerHour+0.5));
-			timeToLevelText = TitanUtils_GetEstTimeText(timeToLevel);
-
-			return L["TITAN_XP_BUTTON_LABEL_XPHR_LEVEL"], TitanUtils_GetHighlightText(xpPerHourText),
-				L["TITAN_XP_BUTTON_LABEL_TOLEVEL_TIME_LEVEL"], TitanUtils_GetHighlightText(timeToLevelText);
-		elseif (TitanGetVar(TITAN_XP_ID,"DisplayType") == "ShowSessionTime") then
-			return L["TITAN_XP_BUTTON_LABEL_SESSION_TIME"], TitanUtils_GetHighlightText(TitanUtils_GetAbbrTimeText(sessionTime));
-		elseif (TitanGetVar(TITAN_XP_ID,"DisplayType") == "ShowXPSimple") then
-			local toLevelXPText = "";
-			local rest = "";
-			local labelrested = "";
-			local labeltolevel = "";
-			local labelnumofkills = "";
-			local labelnumofgains = "";
-			local percent = floor(10000*(currentXP/totalXP)+0.5)/100;
-			if TitanGetVar(TITAN_XP_ID,"ShowSimpleToLevel") then
-				toLevelXPText = TitanUtils_GetColoredText(format(L["TITAN_XP_FORMAT"], comma_value(math.floor(toLevelXP+0.5))), _G["GREEN_FONT_COLOR"]);
-				labeltolevel = L["TITAN_XP_XPTOLEVELUP"];
-			end
-			if TitanGetVar(TITAN_XP_ID,"ShowSimpleRested") then
-				rest = TitanUtils_GetColoredText(comma_value(GetXPExhaustion()==nil and "0" or GetXPExhaustion()),{r=0.44, g=0.69, b=0.94});
-				labelrested = L["TITAN_XP_TOTAL_RESTED"];
+		if (levelTime) then
+			if (TitanGetVar(TITAN_XP_ID, "DisplayType") == "ShowXPPerHourSession") then
+				xpPerHour = sessionXP / sessionTime * 3600;
+	--			timeToLevel = TitanUtils_Ternary((sessionXP == 0), -1, toLevelXP / sessionXP * sessionTime);
+				timeToLevel = (sessionXP == 0) and -1 or toLevelXP / sessionXP * sessionTime;
+
+				xpPerHourText = comma_value(math.floor(xpPerHour+0.5));
+				timeToLevelText = TitanUtils_GetEstTimeText(timeToLevel);
+
+				if trace then
+					print("XP / Hr"
+					.." hr: "..tostring(format("%0.1f", xpPerHour))..""
+					.." '"..tostring(xpPerHourText).."'"
+					.." lvl: "..tostring(format("%0.1f", timeToLevel))..""
+					.." '"..tostring(timeToLevelText).."'"
+					.." '"..tostring(res).."'"
+					)
+				end
+				return L["TITAN_XP_BUTTON_LABEL_XPHR_SESSION"], TitanUtils_GetHighlightText(xpPerHourText),
+					L["TITAN_XP_BUTTON_LABEL_TOLEVEL_TIME_LEVEL"], TitanUtils_GetHighlightText(timeToLevelText);
+			elseif (TitanGetVar(TITAN_XP_ID,"DisplayType") == "ShowXPPerHourLevel") then
+				xpPerHour = currentXP / levelTime * 3600;
+	--			timeToLevel = TitanUtils_Ternary((currentXP == 0), -1, toLevelXP / currentXP * levelTime);
+				timeToLevel = (currentXP == 0) and -1 or toLevelXP / currentXP * levelTime;
+
+				xpPerHourText = comma_value(math.floor(xpPerHour+0.5));
+				timeToLevelText = TitanUtils_GetEstTimeText(timeToLevel);
+
+				return L["TITAN_XP_BUTTON_LABEL_XPHR_LEVEL"], TitanUtils_GetHighlightText(xpPerHourText),
+					L["TITAN_XP_BUTTON_LABEL_TOLEVEL_TIME_LEVEL"], TitanUtils_GetHighlightText(timeToLevelText);
+			elseif (TitanGetVar(TITAN_XP_ID,"DisplayType") == "ShowSessionTime") then
+				return L["TITAN_XP_BUTTON_LABEL_SESSION_TIME"], TitanUtils_GetHighlightText(TitanUtils_GetAbbrTimeText(sessionTime));
+			elseif (TitanGetVar(TITAN_XP_ID,"DisplayType") == "ShowXPSimple") then
+				local toLevelXPText = "";
+				local rest = "";
+				local labelrested = "";
+				local labeltolevel = "";
+				local labelnumofkills = "";
+				local labelnumofgains = "";
+				local percent = floor(10000*(currentXP/totalXP)+0.5)/100;
+				if TitanGetVar(TITAN_XP_ID,"ShowSimpleToLevel") then
+					toLevelXPText = TitanUtils_GetColoredText(format(L["TITAN_XP_FORMAT"], comma_value(math.floor(toLevelXP+0.5))), _G["GREEN_FONT_COLOR"]);
+					labeltolevel = L["TITAN_XP_XPTOLEVELUP"];
+				end
+				if TitanGetVar(TITAN_XP_ID,"ShowSimpleRested") then
+					rest = TitanUtils_GetColoredText(comma_value(GetXPExhaustion()==nil and "0" or GetXPExhaustion()),{r=0.44, g=0.69, b=0.94});
+					labelrested = L["TITAN_XP_TOTAL_RESTED"];
+				end
+				if TitanGetVar(TITAN_XP_ID,"ShowSimpleNumOfKills") then
+					numofkills = TitanUtils_GetColoredText(comma_value(numofkills), {r=0.24, g=0.7, b=0.44})
+					labelnumofkills = L["TITAN_XP_KILLS_LABEL_SHORT"];
+				else
+					numofkills = ""
+				end
+				if TitanGetVar(TITAN_XP_ID,"ShowSimpleNumOfGains") then
+					numofgains = TitanUtils_GetColoredText(comma_value(numofgains), {r=1, g=0.49, b=0.04})
+					labelnumofgains = L["TITAN_XP_XPGAINS_LABEL_SHORT"];
+				else
+					numofgains = ""
+				end
+
+				if TitanGetVar(TITAN_XP_ID,"ShowSimpleNumOfGains") then
+					return L["TITAN_XP_LEVEL_COMPLETE"], TitanUtils_GetHighlightText(percent .. "%"), labelrested, rest , labeltolevel, toLevelXPText, labelnumofgains, numofgains
+				else
+					return L["TITAN_XP_LEVEL_COMPLETE"], TitanUtils_GetHighlightText(percent .. "%"), labelrested, rest , labeltolevel, toLevelXPText, labelnumofkills, numofkills
+				end
 			end
-			if TitanGetVar(TITAN_XP_ID,"ShowSimpleNumOfKills") then
-				numofkills = TitanUtils_GetColoredText(comma_value(numofkills), {r=0.24, g=0.7, b=0.44})
-				labelnumofkills = L["TITAN_XP_KILLS_LABEL_SHORT"];
-			else
-				numofkills = ""
-			end
-			if TitanGetVar(TITAN_XP_ID,"ShowSimpleNumOfGains") then
-				numofgains = TitanUtils_GetColoredText(comma_value(numofgains), {r=1, g=0.49, b=0.04})
-				labelnumofgains = L["TITAN_XP_XPGAINS_LABEL_SHORT"];
-			else
-				numofgains = ""
-			end
-
-			if TitanGetVar(TITAN_XP_ID,"ShowSimpleNumOfGains") then
-				return L["TITAN_XP_LEVEL_COMPLETE"], TitanUtils_GetHighlightText(percent .. "%"), labelrested, rest , labeltolevel, toLevelXPText, labelnumofgains, numofgains
-			else
-				return L["TITAN_XP_LEVEL_COMPLETE"], TitanUtils_GetHighlightText(percent .. "%"), labelrested, rest , labeltolevel, toLevelXPText, labelnumofkills, numofkills
+		else
+			if trace then
+				print("XP "
+				.."("..L["TITAN_XP_UPDATE_PENDING"]..")"
+				)
 			end
+			return "("..L["TITAN_XP_UPDATE_PENDING"]..")";
 		end
-	else
-		return "("..L["TITAN_XP_UPDATE_PENDING"]..")";
 	end
 end