Quantcast

Better encapsulation of OvaleEquipement module.

Johnny C. Lam [02-22-13 - 09:21]
Better encapsulation of OvaleEquipement module.

Only refresh snapshot of equipped items when PLAYER_EQUIPMENT_CHANGED
fires instead of when UNIT_INVENTORY_CHANGED fires.  The latter event
fires every time an item is added or removed from a player's bags, which
can cause unnecessary script compilations.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@698 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
Ovale.lua
OvaleCompile.lua
OvaleCondition.lua
OvaleEquipement.lua
diff --git a/Ovale.lua b/Ovale.lua
index bedb986..895b096 100644
--- a/Ovale.lua
+++ b/Ovale.lua
@@ -127,13 +127,13 @@ function Ovale:OnEnable()

     -- Called when the addon is enabled
 	RegisterAddonMessagePrefix("Ovale")
-    self:RegisterEvent("PLAYER_REGEN_ENABLED");
-    self:RegisterEvent("PLAYER_REGEN_DISABLED");
-    self:RegisterEvent("PLAYER_TARGET_CHANGED")
+	self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
+	self:RegisterEvent("PLAYER_REGEN_ENABLED");
+	self:RegisterEvent("PLAYER_REGEN_DISABLED");
+	self:RegisterEvent("PLAYER_TARGET_CHANGED")
 	self:RegisterEvent("CHAT_MSG_ADDON")
 	self:RegisterEvent("GLYPH_UPDATED")
 	self:RegisterEvent("GLYPH_ADDED")
-	self:RegisterEvent("UNIT_INVENTORY_CHANGED")
 	self:RegisterMessage("Ovale_UpdateShapeshiftForm")

 	self:UpdateVisibility()
@@ -141,18 +141,18 @@ end

 function Ovale:OnDisable()
     -- Called when the addon is disabled
+	self:UnregisterEvent("PLAYER_EQUIPMENT_CHANGED")
 	self:UnregisterEvent("PLAYER_REGEN_ENABLED")
-    self:UnregisterEvent("PLAYER_REGEN_DISABLED")
-    self:UnregisterEvent("PLAYER_TARGET_CHANGED")
-    self:UnregisterEvent("CHAT_MSG_ADDON")
-    self:UnregisterEvent("GLYPH_UPDATED")
-    self:UnregisterEvent("GLYPH_ADDED")
-	self:UnregisterEvent("UNIT_INVENTORY_CHANGED")
+	self:UnregisterEvent("PLAYER_REGEN_DISABLED")
+	self:UnregisterEvent("PLAYER_TARGET_CHANGED")
+	self:UnregisterEvent("CHAT_MSG_ADDON")
+	self:UnregisterEvent("GLYPH_UPDATED")
+	self:UnregisterEvent("GLYPH_ADDED")
 	self:UnregisterMessage("Ovale_UpdateShapeshiftForm")
-    self.frame:Hide()
+	self.frame:Hide()
 end

-function Ovale:UNIT_INVENTORY_CHANGED(event)
+function Ovale:PLAYER_EQUIPMENT_CHANGED(event, slot, hasItem)
 	if self.compileOnItems then
 		self:debugPrint("compile", event)
 		self.needCompile = true
diff --git a/OvaleCompile.lua b/OvaleCompile.lua
index 899e931..2b5a67f 100644
--- a/OvaleCompile.lua
+++ b/OvaleCompile.lua
@@ -135,14 +135,9 @@ local function TestConditions(paramList)
 		end
 	end
 	if paramList.itemset and paramList.itemcount then
-		local set = paramList.itemset
-		local count = paramList.itemcount
-		local nombre = 0
+		local equippedCount = OvaleEquipement:GetArmorSetCount(paramList.itemset)
 		Ovale.compileOnItems = true
-		if OvaleEquipement.nombre[set] then
-			nombre = OvaleEquipement.nombre[set]
-		end
-		if nombre < count then
+		if equippedCount < paramList.itemcount then
 			return false
 		end
 	end
diff --git a/OvaleCondition.lua b/OvaleCondition.lua
index f65b1f6..971cd95 100644
--- a/OvaleCondition.lua
+++ b/OvaleCondition.lua
@@ -462,11 +462,7 @@ OvaleCondition.spellbookConditions = { spell = true }
 --     Spell(ferocious_bite)

 OvaleCondition.conditions.armorsetparts = function(condition)
-	local nombre = 0
-	if OvaleEquipement.nombre[condition[1]] then
-		nombre = OvaleEquipement.nombre[condition[1]]
-	end
-	return compare(nombre, condition[2], condition[3])
+	return compare(OvaleEquipement:GetArmorSetCount(condition[1]), condition[2], condition[3])
 end

 --- Get the current attack power of the player.
diff --git a/OvaleEquipement.lua b/OvaleEquipement.lua
index 3275848..4571c33 100644
--- a/OvaleEquipement.lua
+++ b/OvaleEquipement.lua
@@ -10,35 +10,19 @@
 OvaleEquipement = LibStub("AceAddon-3.0"):NewAddon("OvaleEquipement", "AceEvent-3.0")

 --<private-static-properties>
-local strfind, tonumber = string.find, tonumber
-local GetInventoryItemLink = GetInventoryItemLink
---</private-static-properties>
-
---<public-static-properties>
-OvaleEquipement.nombre = {}
---</public-static-properties>
-
---<public-static-methods>
+local Ovale = LibStub("AceAddon-3.0"):GetAddon("Ovale")

-function OvaleEquipement:OnEnable()
-	self:RegisterEvent("UNIT_INVENTORY_CHANGED")
-	self:RegisterEvent("PLAYER_ENTERING_WORLD")
-end
-
-function OvaleEquipement:OnDisable()
-	self:UnregisterEvent("UNIT_INVENTORY_CHANGED")
-	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
-end
-
-function OvaleEquipement:GetItemId(slot)
-	local link = GetInventoryItemLink("player", GetInventorySlotInfo(slot))
-	if not link then return nil end
-	local a, b, itemId = strfind(link, "item:(%d+)");
-	return tonumber(itemId);
-end
+local pairs = pairs
+local strfind = string.find
+local tonumber = tonumber
+local wipe = wipe
+local GetInventoryItemLink = GetInventoryItemLink
+local GetInventorySlotInfo = GetInventorySlotInfo

-local itemTier =
-{
+-- count of equipped pieces of an armor set: armorSetCount[armorSetName] = equippedCount
+local armorSetCount = {}
+-- database of armor set items: armorSet[itemId] = armorSetName
+local armorSet = {
 	-- Death Knight
 	[85314] = "T14_tank",
 	[85315] = "T14_tank",
@@ -511,35 +495,65 @@ local itemTier =
 	[87200] = "T14_tank",
 	[87201] = "T14_tank",
 }
+--</private-static-properties>

-local itemSlots = {"HeadSlot", "ShoulderSlot", "ChestSlot", "HandsSlot", "LegsSlot"}
+--<public-static-methods>
+function OvaleEquipement:OnEnable()
+	self:RegisterEvent("PLAYER_ENTERING_WORLD")
+	self:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
+end
+
+function OvaleEquipement:OnDisable()
+	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
+	self:UnregisterEvent("PLAYER_EQUIPMENT_CHANGED")
+end
+
+function OvaleEquipement:PLAYER_EQUIPMENT_CHANGED(event, slot, hasItem)
+	self:Refresh()
+end
+
+function OvaleEquipement:PLAYER_ENTERING_WORLD(event)
+	self:Refresh()
+end
+
+function OvaleEquipement:GetArmorSetCount(name)
+	if not armorSetCount[name] then
+		return 0
+	else
+		return armorSetCount[name]
+	end
+end
+
+function OvaleEquipement:Debug()
+	for k, v in pairs(armorSetCount) do
+		Ovale:Print("Player has " ..v.. "piece(s) of " ..k.. " armor set")
+	end
+end
+
+function OvaleEquipement:GetItemId(slot)
+	local link = GetInventoryItemLink("player", GetInventorySlotInfo(slot))
+	if not link then return nil end
+	local a, b, itemId = strfind(link, "item:(%d+)");
+	return tonumber(itemId);
+end
+
+-- slots that can contain pieces from armor sets
+local itemSlots = { "HeadSlot", "ShoulderSlot", "ChestSlot", "HandsSlot", "LegsSlot" }

 function OvaleEquipement:Refresh()
-	self.nombre = {}
-	for i=1,#itemSlots do
+	wipe(armorSetCount)
+	for i = 1, #itemSlots do
 		local itemId = self:GetItemId(itemSlots[i])
 		if itemId then
-			local tier = itemTier[itemId]
-			if tier~=nil then
-				if not self.nombre[tier] then
-					self.nombre[tier] = 1
+			local name = armorSet[itemId]
+			if name then
+				if not armorSetCount[name] then
+					armorSetCount[name] = 1
 				else
-					self.nombre[tier] = self.nombre[tier] + 1
+					armorSetCount[name] = armorSetCount[name] + 1
 				end
 			end
 		end
 	end
 end
-
-function OvaleEquipement:UNIT_INVENTORY_CHANGED(event, arg1)
-	if (arg1 == "player") then
-		self:Refresh()
-	end
-end
-
-function OvaleEquipement:PLAYER_ENTERING_WORLD(event)
-	self:Refresh()
-end
-
 --</public-static-methods>
-