Quantcast

Using ElvUI's calculation functions instead of having a million duplicate code lines everywhere

Darth Predator [06-12-17 - 13:01]
Using ElvUI's calculation functions instead of having a million duplicate code lines everywhere
Filename
ElvUI_SLE/modules/Armory/CharacterArmory/CharacterArmory.lua
ElvUI_SLE/modules/bags/artifactpower.lua
diff --git a/ElvUI_SLE/modules/Armory/CharacterArmory/CharacterArmory.lua b/ElvUI_SLE/modules/Armory/CharacterArmory/CharacterArmory.lua
index a935f36..412543f 100644
--- a/ElvUI_SLE/modules/Armory/CharacterArmory/CharacterArmory.lua
+++ b/ElvUI_SLE/modules/Armory/CharacterArmory/CharacterArmory.lua
@@ -3,7 +3,7 @@
 local _G = _G
 local SLE, T, E, L, V, P, G = unpack(select(2, ...))
 local KF, Info, Timer = unpack(ElvUI_KnightFrame)
-local ElvUI_BagModule = E:GetModule('Bags')
+local ElvUI_BagModule, ElvUI_DataBars = SLE:GetElvModules("Bags", "DataBars")
 local Lib_Search = LibStub('LibItemSearch-1.2-ElvUI')
 --GLOBALS: CreateFrame, UIParent, SLE_ArmoryDB, hooksecurefunc, GetInventoryItemGems

@@ -1140,22 +1140,6 @@ end

 do --<< Artifact Monitor >>
 	local EnchantError, EnchantError_MainHand, EnchantError_SecondaryHand
-	local apItemCache = {}
-	local apStringValueMillion = {
-		["enUS"] = "(%d*[%p%s]?%d+) million",
-		["enGB"] = "(%d*[%p%s]?%d+) million",
-		["ptBR"] = "(%d*[%p%s]?%d+) [[milhão][milhões]]?",
-		["esMX"] = "(%d*[%p%s]?%d+) [[millón][millones]]?",
-		["deDE"] = "(%d*[%p%s]?%d+) [[Million][Millionen]]?",
-		["esES"] = "(%d*[%p%s]?%d+) [[millón][millones]]?",
-		["frFR"] = "(%d*[%p%s]?%d+) [[million][millions]]?",
-		["itIT"] = "(%d*[%p%s]?%d+) [[milione][milioni]]?",
-		["ruRU"] = "(%d*[%p%s]?%d+) млн",
-		["koKR"] = "(%d*[%p%s]?%d+)만",
-		["zhTW"] = "(%d*[%p%s]?%d+)萬",
-		["zhCN"] = "(%d*[%p%s]?%d+)万",
-	}
-	local apStringValueMillionLocal = apStringValueMillion[GetLocale()]

 	function CA:LegionArtifactMonitor_UpdateLayout()
 		if Legion_ArtifactData.ItemID then
@@ -1314,87 +1298,16 @@ do --<< Artifact Monitor >>
 			CA:LegionArtifactMonitor_UpdateData()
 		end

-		LowestPower = nil
-		TotalPower = 0
+		-- LowestPower = nil

 		if Legion_ArtifactData.ItemID then
-			for BagID = 0, NUM_BAG_SLOTS do
-				for SlotID = 1, GetContainerNumSlots(BagID) do
-					_, _, _, _, _, _, PowerItemLink = GetContainerItemInfo(BagID, SlotID)
-
-					if PowerItemLink then
-						if GetItemInfo(PowerItemLink) then
-							-- print(GetItemInfo(PowerItemLink))
-							-- print(PowerItemLink)
-							CurrentItemPower = 0
-							if apItemCache[PowerItemLink] then
-								if apItemCache[PowerItemLink] ~= false then
-									CurrentItemPower = apItemCache[PowerItemLink]
-									if not LowestPower or LowestPower > CurrentItemPower then
-										LowestPower = CurrentItemPower
-										LowestPower_BagID = BagID
-										LowestPower_SlotID = SlotID
-										LowestPower_Link = PowerItemLink
-									end
-								end
-							else
-								if IsArtifactPowerItem(PowerItemLink) then
-									self:ClearTooltip(self.ArtifactMonitor.ScanTT)
-									local success = pcall(self.ArtifactMonitor.ScanTT.SetHyperlink, self.ArtifactMonitor.ScanTT, PowerItemLink)
-									if success then
-										local apFound
-										for i = 3, 7 do
-											local tooltipText = _G["Knight_CharacterArmory_ArtifactScanTTTextLeft"..i]:GetText()
-											if (tooltipText and not T.match(tooltipText, AP_NAME)) then
-												local digit1, digit2, digit3, ap
-												local value = T.match(tooltipText, apStringValueMillionLocal)
-												if value then
-													digit1, digit2 = T.match(value, "(%d+)[%p%s](%d+)")
-													if digit1 and digit2 then
-														ap = T.tonumber(T.format("%s.%s", digit1, digit2)) * 1e6 --Multiply by one million
-													else
-														ap = T.tonumber(value) * 1e6 --Multiply by one million
-													end
-												else
-													digit1, digit2, digit3 = T.match(tooltipText,"(%d+)[%p%s]?(%d+)[%p%s]?(%d+)")
-													ap = T.tonumber(T.format("%s%s%s", digit1 or "", digit2 or "", (digit2 and digit3) and digit3 or ""))
-												end
-
-												if ap then
-													CurrentItemPower = ap
-													CurrentItemPower = T.tonumber(CurrentItemPower)
-													apItemCache[PowerItemLink] = CurrentItemPower
-													apFound = true
-													if not LowestPower or LowestPower > CurrentItemPower then
-														LowestPower = CurrentItemPower
-														LowestPower_BagID = BagID
-														LowestPower_SlotID = SlotID
-														LowestPower_Link = PowerItemLink
-													end
-													break
-												end
-											end
-										end
-
-										if (not apFound) then
-											apItemCache[PowerItemLink] = false --Cache item as not granting AP
-										end
-									end
-								else
-									apItemCache[PowerItemLink] = false --Cache item as not granting AP
-								end
-							end
-							TotalPower = TotalPower + CurrentItemPower
-						end
-					end
-				end
-			end
+			TotalPower = ElvUI_DataBars:GetArtifactPowerInBags()

-			if LowestPower then
+			if TotalPower then
 				self.ArtifactMonitor.AddPower.Texture:Show()
 				self.ArtifactMonitor.AddPower.Button.Link = LowestPower_Link
-
-				if LowestPower > 0 then
+
+				if TotalPower > 0 then
 					if E.db.sle.Armory.Character.Artifact.ShortValues then
 						self.ArtifactMonitor.BarExpected.AvailablePower:SetText(KF:Color_Value('+'..E:ShortValue(TotalPower)))
 					else
@@ -1410,7 +1323,7 @@ do --<< Artifact Monitor >>
 				self.ArtifactMonitor.BarExpected.AvailablePower:SetText()
 				self:LegionArtifactMonitor_ClearPowerItemSearching()
 			end
-
+
 			if Legion_ArtifactData.XP then
 				if TotalPower + Legion_ArtifactData.XP > Legion_ArtifactData.XPForNextPoint then
 					TotalPower = Legion_ArtifactData.XPForNextPoint
@@ -1418,7 +1331,7 @@ do --<< Artifact Monitor >>
 					TotalPower = TotalPower + Legion_ArtifactData.XP
 				end
 			end
-
+
 			self.ArtifactMonitor.BarExpected:SetValue(TotalPower)
 		end

diff --git a/ElvUI_SLE/modules/bags/artifactpower.lua b/ElvUI_SLE/modules/bags/artifactpower.lua
index 33dafde..e384d93 100644
--- a/ElvUI_SLE/modules/bags/artifactpower.lua
+++ b/ElvUI_SLE/modules/bags/artifactpower.lua
@@ -1,6 +1,6 @@
 local SLE, T, E, L, V, P, G = unpack(select(2, ...))
 local AP = SLE:NewModule("ArtifactPowerBags", 'AceHook-3.0', 'AceEvent-3.0')
-local B = E:GetModule('Bags')
+local B, DB = SLE:GetElvModules("Bags", "DataBars")

 --GLOBALS: CreateFrame, hooksecurefunc
 local _G = _G
@@ -11,72 +11,6 @@ local AP_NAME = format("%s|r", ARTIFACT_POWER)
 local pcall = pcall
 local GetItemSpell = GetItemSpell

--- local apLineIndex
-local apItemCache = {}
-local apStringValueMillion = {
-	["enUS"] = "(%d*[%p%s]?%d+) million",
-	["enGB"] = "(%d*[%p%s]?%d+) million",
-	["ptBR"] = "(%d*[%p%s]?%d+) [[milhão][milhões]]?",
-	["esMX"] = "(%d*[%p%s]?%d+) [[millón][millones]]?",
-	["deDE"] = "(%d*[%p%s]?%d+) [[Million][Millionen]]?",
-	["esES"] = "(%d*[%p%s]?%d+) [[millón][millones]]?",
-	["frFR"] = "(%d*[%p%s]?%d+) [[million][millions]]?",
-	["itIT"] = "(%d*[%p%s]?%d+) [[milione][milioni]]?",
-	["ruRU"] = "(%d*[%p%s]?%d+) млн",
-	["koKR"] = "(%d*[%p%s]?%d+)만",
-	["zhTW"] = "(%d*[%p%s]?%d+)萬",
-	["zhCN"] = "(%d*[%p%s]?%d+)万",
-}
-local apStringValueMillionLocal = apStringValueMillion[GetLocale()]
-local function GetItemLinkArtifactPower(slotLink)
-	local apValue
-	if not slotLink then return nil end
-	if IsArtifactPowerItem(slotLink) then
-		tooltipScanner:ClearLines()
-		local success = pcall(tooltipScanner.SetHyperlink, tooltipScanner, slotLink)
-		if (not success) then
-			return nil
-		end
-
-		local apFound
-		for i = 3, 7 do
-			local tooltipText = _G[tooltipName.."TextLeft"..i]:GetText()
-			if (tooltipText and not T.match(tooltipText, AP_NAME)) then
-				local digit1, digit2, digit3, ap
-				local value = T.match(tooltipText, apStringValueMillionLocal)
-
-				if value then
-					digit1, digit2 = T.match(value, "(%d+)[%p%s](%d+)")
-					if digit1 and digit2 then
-						ap = T.tonumber(T.format("%s.%s", digit1, digit2)) * 1e6 --Multiply by one million
-					else
-						ap = T.tonumber(value) * 1e6 --Multiply by one million
-					end
-				else
-					digit1, digit2, digit3 = T.match(tooltipText,"(%d+)[%p%s]?(%d+)[%p%s]?(%d+)")
-					ap = T.tonumber(T.format("%s%s%s", digit1 or "", digit2 or "", (digit2 and digit3) and digit3 or ""))
-				end
-
-				if ap then
-					apValue = ap
-					apValue = T.tonumber(apValue)
-					apFound = true
-					break
-				end
-			end
-		end
-
-		if (not apFound) then
-			apItemCache[slotLink] = false --Cache item as not granting AP
-		end
-	else
-		apItemCache[slotLink] = false --Cache item as not granting AP
-	end
-
-	return apValue
-end
-
-
 local function SlotUpdate(self, bagID, slotID)
 	if (not bagID or not slotID) or bagID == -3 then return end
 	if not self.Bags[bagID] or not self.Bags[bagID][slotID] then
@@ -99,17 +33,9 @@ local function SlotUpdate(self, bagID, slotID)
 			local ID = T.select(10, T.GetContainerItemInfo(bagID, slotID))
 			local slotLink = T.GetContainerItemLink(bagID,slotID)
 			if (ID and slotLink) then
-				local arcanePower
-				if apItemCache[slotLink] then
-					if apItemCache[slotLink] ~= false then
-						arcanePower = apItemCache[slotLink]
-					end
-				else
-					arcanePower = GetItemLinkArtifactPower(slotLink)
-					apItemCache[slotLink] = arcanePower
-				end
+				local arcanePower = DB:GetAPForItem(slotLink)
 				if E.db.sle.bags.artifactPower.short and arcanePower then arcanePower = E:ShortValue(arcanePower) end
-				frame.artifactpowerinfo:SetText(arcanePower)
+				if arcanePower ~= "0" then frame.artifactpowerinfo:SetText(arcanePower) end
 			end
 		end
 	elseif not E.db.sle.bags.artifactPower.enable and frame.artifactpowerinfo then