Quantcast

Updated LibArtifactData-1.0

Mikeprod [11-17-16 - 19:40]
Updated LibArtifactData-1.0
Filename
Libs/LibArtifactData-1.0/LibArtifactData-1.0.lua
Libs/LibArtifactData-1.0/LibArtifactData-1.0.toc
diff --git a/Libs/LibArtifactData-1.0/LibArtifactData-1.0.lua b/Libs/LibArtifactData-1.0/LibArtifactData-1.0.lua
index 3767d5f..eae1ba2 100644
--- a/Libs/LibArtifactData-1.0/LibArtifactData-1.0.lua
+++ b/Libs/LibArtifactData-1.0/LibArtifactData-1.0.lua
@@ -1,4 +1,4 @@
-local MAJOR, MINOR = "LibArtifactData-1.0", 7
+local MAJOR, MINOR = "LibArtifactData-1.0", 11

 assert(_G.LibStub, MAJOR .. " requires LibStub")
 local lib = _G.LibStub:NewLibrary(MAJOR, MINOR)
@@ -22,6 +22,7 @@ local _G                       = _G
 local BACKPACK_CONTAINER       = _G.BACKPACK_CONTAINER
 local BANK_CONTAINER           = _G.BANK_CONTAINER
 local INVSLOT_MAINHAND         = _G.INVSLOT_MAINHAND
+local LE_ITEM_CLASS_ARMOR      = _G.LE_ITEM_CLASS_ARMOR
 local LE_ITEM_CLASS_WEAPON     = _G.LE_ITEM_CLASS_WEAPON
 local LE_ITEM_QUALITY_ARTIFACT = _G.LE_ITEM_QUALITY_ARTIFACT
 local NUM_BAG_SLOTS            = _G.NUM_BAG_SLOTS
@@ -66,9 +67,6 @@ local frame = lib.frame
 frame:UnregisterAllEvents() -- deactivate old versions
 frame:SetScript("OnEvent", function(_, event, ...) private[event](event, ...) end)
 frame:RegisterEvent("PLAYER_ENTERING_WORLD")
-frame:RegisterEvent("ARTIFACT_CLOSE")
-frame:RegisterEvent("ARTIFACT_XP_UPDATE")
-frame:RegisterUnitEvent("PLAYER_SPECIALIZATION_CHANGED", "player")

 local function CopyTable(tbl)
 	if not tbl then return {} end
@@ -242,79 +240,86 @@ local function GetViewedArtifactData()
 	end
 end

+local function ScanEquipped()
+	if HasArtifactEquipped() then
+		PrepareForScan()
+		SocketInventoryItem(INVSLOT_MAINHAND)
+		GetViewedArtifactData()
+		Clear()
+		RestoreStateAfterScan()
+		frame:UnregisterEvent("UNIT_INVENTORY_CHANGED")
+	end
+end
+
 local function ScanContainer(container, numObtained)
 	for slot = 1, GetContainerNumSlots(container) do
 		local _, _, _, quality, _, _, _, _, _, itemID = GetContainerItemInfo(container, slot)
 		if quality == LE_ITEM_QUALITY_ARTIFACT then
 			local classID = select(12, GetItemInfo(itemID))
-			if classID == LE_ITEM_CLASS_WEAPON then
+			if classID == LE_ITEM_CLASS_WEAPON or classID == LE_ITEM_CLASS_ARMOR then
 				Debug("ARTIFACT_FOUND", "in", container, slot)
 				SocketContainerItem(container, slot)
 				GetViewedArtifactData()
 				Clear()
-				numObtained = numObtained - 1
-				if numObtained <= 0 then break end
+				if numObtained <= lib:GetNumObtainedArtifacts() then break end
 			end
 		end
 	end
-
-	return numObtained
 end

 local function IterateContainers(from, to, numObtained)
+	PrepareForScan()
 	for container = from, to do
-		numObtained = ScanContainer(container, numObtained)
-		if numObtained <= 0 then break end
+		ScanContainer(container, numObtained)
+		if numObtained <= lib:GetNumObtainedArtifacts() then break end
 	end
-
-	return numObtained
+	RestoreStateAfterScan()
 end

 local function ScanBank(numObtained)
-	PrepareForScan()
-	numObtained = ScanContainer(BANK_CONTAINER, numObtained)
-	if numObtained > 0 then
+	if numObtained > lib:GetNumObtainedArtifacts() then
+		PrepareForScan()
+		ScanContainer(BANK_CONTAINER, numObtained)
+		RestoreStateAfterScan()
+	end
+	if numObtained > lib:GetNumObtainedArtifacts() then
 		IterateContainers(NUM_BAG_SLOTS + 1, NUM_BAG_SLOTS + NUM_BANKBAGSLOTS, numObtained)
 	end
-	RestoreStateAfterScan()
 end

-local function InitializeScan(event)
-	if _G.ArtifactFrame and _G.ArtifactFrame:IsShown() then
-		Debug("InitializeScan", "aborted because ArtifactFrame is open.")
-		return
-	end
+function private.PLAYER_ENTERING_WORLD(event)
+	frame:UnregisterEvent(event)
+	frame:RegisterUnitEvent("UNIT_INVENTORY_CHANGED", "player")
+	frame:RegisterEvent("BAG_UPDATE_DELAYED")
+	frame:RegisterEvent("BANKFRAME_OPENED")
+	frame:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
+	frame:RegisterEvent("CURRENCY_DISPLAY_UPDATE")
+	frame:RegisterEvent("ARTIFACT_CLOSE")
+	frame:RegisterEvent("ARTIFACT_XP_UPDATE")
+	frame:RegisterUnitEvent("PLAYER_SPECIALIZATION_CHANGED", "player")
+end

-	local numObtained = GetNumObtainedArtifacts() -- not available at cold login
-	Debug("InitializeScan", event, "numObtained", numObtained)
+-- bagged artifact data becomes obtainable
+function private.BAG_UPDATE_DELAYED(event)
+	local numObtained = GetNumObtainedArtifacts()
+	if numObtained <= 0 then return end

-	if numObtained > 0 then
-		PrepareForScan()
-		if HasArtifactEquipped() then -- scan equipped
-			SocketInventoryItem(INVSLOT_MAINHAND)
-			GetViewedArtifactData()
-			Clear()
-			numObtained = numObtained - 1
-		end
-		if numObtained > 0 then -- scan bags
-			numObtained = IterateContainers(BACKPACK_CONTAINER, NUM_BAG_SLOTS, numObtained)
-		end
-		if numObtained > 0 then -- scan bank
-			frame:RegisterEvent("BANKFRAME_OPENED")
-			Debug("ARTIFACT_DATA_MISSING", "artifact", numObtained)
-			lib.callbacks:Fire("ARTIFACT_DATA_MISSING", numObtained)
-		end
-		RestoreStateAfterScan()
+	-- prevent double-scanning if UNIT_INVENTORY_CHANGED fired first
+	-- UNIT_INVENTORY_CHANGED does not fire after /reload
+	if not equippedID and HasArtifactEquipped() then
+		ScanEquipped()
+	end
+
+	if numObtained > lib:GetNumObtainedArtifacts() then
+		IterateContainers(BACKPACK_CONTAINER, NUM_BAG_SLOTS, numObtained)
 	end
-end

-function private.PLAYER_ENTERING_WORLD(event)
 	frame:UnregisterEvent(event)
-	_G.C_Timer.After(5, function()
-		InitializeScan(event)
-		frame:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
-		frame:RegisterEvent("CURRENCY_DISPLAY_UPDATE")
-	end)
+end
+
+-- equipped artifact data becomes obtainable
+function private.UNIT_INVENTORY_CHANGED(event)
+	ScanEquipped(event)
 end

 function private.ARTIFACT_CLOSE()
@@ -326,6 +331,10 @@ function private.ARTIFACT_UPDATE(event, newItem)
 	if newItem then
 		GetViewedArtifactData()
 	else
+		if not GetNumRelicSlots() then
+			Debug("|cffff0000ERROR:|r", "artifact data unobtainable.")
+			return
+		end
 		local newRelics = ScanRelics()
 		local oldRelics = artifacts[viewedID].relics

@@ -353,9 +362,9 @@ function private.ARTIFACT_XP_UPDATE(event)

 	local artifact = artifacts[itemID]
 	if not artifact then
-		Debug("|cffff0000ERROR:|r", "artifact", itemID, "not found.")
-		return
+		return lib.ForceUpdate()
 	end
+
 	local diff = unspentPower - artifact.unspentPower

 	if numRanksPurchased ~= artifact.numRanksPurchased then
@@ -378,8 +387,8 @@ function private.ARTIFACT_XP_UPDATE(event)
 end

 function private.BANKFRAME_OPENED()
-	local numObtained = lib:GetNumObtainedArtifacts()
-	if numObtained ~= GetNumObtainedArtifacts() then
+	local numObtained = GetNumObtainedArtifacts()
+	if numObtained > lib:GetNumObtainedArtifacts() then
 		ScanBank(numObtained)
 	end
 end
@@ -398,7 +407,7 @@ function private.PLAYER_EQUIPMENT_CHANGED(event, slot)
 		local itemID = GetEquippedArtifactInfo()

 		if itemID and not artifacts[itemID] then
-			InitializeScan(event)
+			ScanEquipped(event)
 		end

 		InformEquippedArtifactChanged(itemID)
@@ -475,7 +484,7 @@ function lib.GetAcquiredArtifactPower(_, artifactID)
 		local data = artifacts[artifactID]
 		total = total + data.unspentPower
 		local rank = 1
-		while rank <= data.numRanksPurchased do
+		while rank < data.numRanksPurchased do
 			total = total + GetCostForPointAtRank(rank)
 			rank = rank + 1
 		end
@@ -487,7 +496,7 @@ function lib.GetAcquiredArtifactPower(_, artifactID)
 		if tonumber(itemID) then
 			total = total + data.unspentPower
 			local rank = 1
-			while rank <= data.numRanksPurchased do
+			while rank < data.numRanksPurchased do
 				total = total + GetCostForPointAtRank(rank)
 				rank = rank + 1
 			end
@@ -498,5 +507,13 @@ function lib.GetAcquiredArtifactPower(_, artifactID)
 end

 function lib.ForceUpdate()
-	InitializeScan("FORCE_UPDATE")
+	if _G.ArtifactFrame and _G.ArtifactFrame:IsShown() then
+		Debug("ForceUpdate", "aborted because ArtifactFrame is open.")
+		return
+	end
+	local numObtained = GetNumObtainedArtifacts()
+	if numObtained > 0 then
+		ScanEquipped("FORCE_UPDATE")
+		IterateContainers(BACKPACK_CONTAINER, NUM_BAG_SLOTS, numObtained)
+	end
 end
diff --git a/Libs/LibArtifactData-1.0/LibArtifactData-1.0.toc b/Libs/LibArtifactData-1.0/LibArtifactData-1.0.toc
index 57abd20..38b1b08 100644
--- a/Libs/LibArtifactData-1.0/LibArtifactData-1.0.toc
+++ b/Libs/LibArtifactData-1.0/LibArtifactData-1.0.toc
@@ -1,4 +1,4 @@
-## Interface: 70000
+## Interface: 70100
 ## Title: Lib: ArtifactData-1.0
 ## Notes: Provides data about all artifacts in the player's possession.
 ## Author: Rainrider