Quantcast

Improve encapsulation of OvalePaperDoll.

Johnny C. Lam [03-12-13 - 04:46]
Improve encapsulation of OvalePaperDoll.

Track player class and level in OvalePaperDoll.  They are player stats,
in a way, and are available in the in-game paper doll.

Use player class and level from OvalePaperDoll in the other modules and
remove the same from OvaleData.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@755 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
OvaleCompile.lua
OvaleCondition.lua
OvaleData.lua
OvaleOptions.lua
OvalePaperDoll.lua
OvaleScripts.lua
OvaleState.lua
diff --git a/OvaleCompile.lua b/OvaleCompile.lua
index 9c70a2b..fa4da50 100644
--- a/OvaleCompile.lua
+++ b/OvaleCompile.lua
@@ -683,7 +683,7 @@ end
 function OvaleCompile:Compile()
 	local profile = OvaleOptions:GetProfile()
 	local source = profile.source
-	local code = OvaleScripts.script[OvaleData.className][source].code
+	local code = OvaleScripts.script[OvalePaperDoll.class][source].code
 	CompileScript(code)
 	Ovale.refreshNeeded.player = true
 	Ovale:UpdateFrame()
diff --git a/OvaleCondition.lua b/OvaleCondition.lua
index 2b2dd6d..1051272 100644
--- a/OvaleCondition.lua
+++ b/OvaleCondition.lua
@@ -1768,10 +1768,10 @@ end
 -- if Level(more 33) Spell(tiger_palm)

 OvaleCondition.conditions.level = function(condition)
-	local level
 	local target = getTarget(condition.target)
+	local level
 	if target == "player" then
-		level = OvaleData.level
+		level = OvalePaperDoll.level
 	else
 		level = UnitLevel(target)
 	end
@@ -2131,13 +2131,17 @@ end
 --     Texture(ability_rogue_sprint)

 OvaleCondition.conditions.relativelevel = function(condition)
-	local difference
+	local difference, level
 	local target = getTarget(condition.target)
-	local targetLevel = UnitLevel(target)
-	if targetLevel < 0 then
+	if target == "player" then
+		level = OvalePaperDoll.level
+	else
+		level = UnitLevel(target)
+	end
+	if level < 0 then
 		difference = 3
 	else
-		difference = targetLevel - OvaleData.level
+		difference = level - OvalePaperDoll.level
 	end
 	return compare(difference, condition[1], condition[2])
 end
diff --git a/OvaleData.lua b/OvaleData.lua
index 51e2d03..6d1e930 100644
--- a/OvaleData.lua
+++ b/OvaleData.lua
@@ -36,8 +36,6 @@ local SPELL_POWER_SOUL_SHARDS = SPELL_POWER_SOUL_SHARDS
 --<public-static-properties>
 OvaleData.spellList = {}
 OvaleData.itemList = {}
-OvaleData.className = nil
-OvaleData.level = nil
 --allows to fill the player talent tables on first use
 OvaleData.listeTalentsRemplie = false
 --key: talentId / value: points in this talent
@@ -290,15 +288,12 @@ local rootSpellList = nil

 --<public-static-methods>
 function OvaleData:OnInitialize()
-	self.className = select(2, UnitClass("player"))
 	for k,v in pairs(self.power) do
 		self.powerType[v.id] = k
 	end
 end

 function OvaleData:OnEnable()
-	self.level = UnitLevel("player")
-
 	self:RegisterEvent("CHARACTER_POINTS_CHANGED", "RemplirListeTalents")
 	self:RegisterEvent("GLYPH_ADDED", "UpdateGlyphs")
 	self:RegisterEvent("GLYPH_DISABLED", "UpdateGlyphs")
@@ -306,7 +301,6 @@ function OvaleData:OnEnable()
 	self:RegisterEvent("GLYPH_REMOVED", "UpdateGlyphs")
 	self:RegisterEvent("GLYPH_UPDATED", "UpdateGlyphs")
 	self:RegisterEvent("PLAYER_ALIVE")
-	self:RegisterEvent("PLAYER_LEVEL_UP")
 	self:RegisterEvent("PLAYER_TALENT_UPDATE", "RemplirListeTalents")
 	self:RegisterEvent("SPELLS_CHANGED", "FillSpellList")
 	self:RegisterEvent("UNIT_PET", "FillPetSpellList")
@@ -320,7 +314,6 @@ function OvaleData:OnDisable()
 	self:UnregisterEvent("GLYPH_REMOVED")
 	self:UnregisterEvent("GLYPH_UPDATED")
 	self:UnregisterEvent("PLAYER_ALIVE")
-	self:UnregisterEvent("PLAYER_LEVEL_UP")
 	self:UnregisterEvent("PLAYER_TALENT_UPDATE")
 	self:UnregisterEvent("SPELLS_CHANGED")
 	self:UnregisterEvent("UNIT_PET")
@@ -333,15 +326,6 @@ function OvaleData:PLAYER_ALIVE(event)
 	self:FillSpellList()
 end

-function OvaleData:PLAYER_LEVEL_UP(event, level, ...)
-	level = tonumber(level)
-	if level then
-		self.level = level
-	else
-		self.level = self.level + 1
-	end
-end
-
 function OvaleData:GetRootSpellList()
 	if rootSpellList then
 		return rootSpellList
@@ -520,13 +504,14 @@ function OvaleData:GetGCD(spellId)
 	end

 	-- Default value
-	if self.className == "ROGUE" or
-		(self.className == "MONK" and
+	local class = OvalePaperDoll.class
+	if class == "ROGUE" or
+		(class == "MONK" and
 			(OvaleStance:IsStance("monk_stance_of_the_sturdy_ox") or OvaleStance:IsStance("monk_stance_of_the_fierce_tiger"))) or
-		(self.className == "DRUID" and OvaleStance:IsStance("druid_cat_form")) then
+		(class == "DRUID" and OvaleStance:IsStance("druid_cat_form")) then
 		return 1.0
-	elseif self.className == "MAGE" or self.className == "WARLOCK" or self.className == "PRIEST" or
-			(self.className == "DRUID" and not OvaleStance:IsStance("druid_bear_form")) then
+	elseif class == "MAGE" or class == "WARLOCK" or class == "PRIEST" or
+			(class == "DRUID" and not OvaleStance:IsStance("druid_bear_form")) then
 		local cd = 1.5 / OvalePaperDoll:GetSpellHasteMultiplier()
 		if (cd<1) then
 			cd = 1
@@ -550,7 +535,7 @@ function OvaleData:GetComputedSpellCD(spellId)
 		-- Les chevaliers de la mort ont des infos fausses sur le CD quand ils n'ont plus les runes
 		-- On force à 1,5s ou 1s en présence impie
 		-- TODO: is it still the case in MoP?
-		if self.className=="DEATHKNIGHT" and actionCooldownDuration==10 and
+		if OvalePaperDoll.class == "DEATHKNIGHT" and actionCooldownDuration == 10 and
 				(not self.spellInfo[spellId] or self.spellInfo[spellId].cd~=10) then
 			local impie = GetSpellInfo(48265)
 			if impie and UnitBuff("player", impie) then
diff --git a/OvaleOptions.lua b/OvaleOptions.lua
index b785d75..a758763 100644
--- a/OvaleOptions.lua
+++ b/OvaleOptions.lua
@@ -306,11 +306,11 @@ local options =
 					end,
 					get = function(info)
 						local source = OvaleOptions.db.profile.source
-						local code = OvaleScripts.script[OvaleData.className][source].code
+						local code = OvaleScripts.script[OvalePaperDoll.class][source].code
 						return strgsub(code, "\t", "    ")
 					end,
 					set = function(info, v)
-						OvaleScripts:RegisterScript(OvaleData.className, "custom", L["Script personnalisé"], v)
+						OvaleScripts:RegisterScript(OvalePaperDoll.class, "custom", L["Script personnalisé"], v)
 						OvaleOptions.db.profile.code = v
 						OvaleOptions:SendMessage("Ovale_ScriptChanged")
 					end,
@@ -327,7 +327,7 @@ local options =
 						return L["Ecraser le Script personnalisé préexistant?"]
 					end,
 					func = function()
-						local class = OvaleData.className
+						local class = OvalePaperDoll.class
 						local source = OvaleOptions.db.profile.source
 						local code = OvaleScripts.script[class][source].code
 						OvaleScripts.script[class]["custom"].code = code
@@ -550,7 +550,7 @@ function OvaleOptions:OnInitialize()
 	self.db.RegisterCallback( self, "OnProfileChanged", "HandleProfileChanges" )
 	self.db.RegisterCallback( self, "OnProfileCopied", "HandleProfileChanges" )

-	OvaleScripts:RegisterScript(OvaleData.className, "custom", L["Script personnalisé"], self.db.profile.code)
+	OvaleScripts:RegisterScript(OvalePaperDoll.class, "custom", L["Script personnalisé"], self.db.profile.code)
 	self:HandleProfileChanges()
 end

diff --git a/OvalePaperDoll.lua b/OvalePaperDoll.lua
index 155561b..741ce56 100644
--- a/OvalePaperDoll.lua
+++ b/OvalePaperDoll.lua
@@ -13,6 +13,9 @@ local _, Ovale = ...
 OvalePaperDoll = Ovale:NewModule("OvalePaperDoll", "AceEvent-3.0")

 --<private-static-properties>
+local select = select
+local tonumber = tonumber
+
 local GetMasteryEffect = GetMasteryEffect
 local GetMeleeHaste = GetMeleeHaste
 local GetRangedHaste = GetRangedHaste
@@ -20,12 +23,18 @@ local GetSpellBonusDamage = GetSpellBonusDamage
 local GetSpellBonusHealing = GetSpellBonusHealing
 local UnitAttackPower = UnitAttackPower
 local UnitClass = UnitClass
+local UnitLevel = UnitLevel
 local UnitRangedAttackPower = UnitRangedAttackPower
 local UnitSpellHaste = UnitSpellHaste
 local UnitStat = UnitStat
 --</private-static-properties>

 --<public-static-properties>
+-- player's class token
+OvalePaperDoll.class = select(2, UnitClass("player"))
+-- player's level
+OvalePaperDoll.level = UnitLevel("player")
+
 -- primary stats
 OvalePaperDoll.agility = 0
 OvalePaperDoll.intellect = 0
@@ -52,8 +61,11 @@ OvalePaperDoll.spellBonusHealing = 0
 --<public-static-methods>
 function OvalePaperDoll:OnEnable()
 	self:RegisterEvent("MASTERY_UPDATE")
-	self:RegisterEvent("PLAYER_ENTERING_WORLD")
+	self:RegisterEvent("PLAYER_ALIVE")
+	self:RegisterEvent("PLAYER_ENTERING_WORLD", "PLAYER_ALIVE")
+	self:RegisterEvent("PLAYER_LEVEL_UP")
 	self:RegisterEvent("UNIT_ATTACK_POWER")
+	self:RegisterEvent("UNIT_LEVEL")
 	self:RegisterEvent("UNIT_RANGEDDAMAGE")
 	self:RegisterEvent("UNIT_RANGED_ATTACK_POWER")
 	self:RegisterEvent("UNIT_SPELL_HASTE")
@@ -63,8 +75,11 @@ end

 function OvalePaperDoll:OnDisable()
 	self:UnregisterEvent("MASTERY_UPDATE")
+	self:UnregisterEvent("PLAYER_ALIVE")
 	self:UnregisterEvent("PLAYER_ENTERING_WORLD")
+	self:UnregisterEvent("PLAYER_LEVEL_UP")
 	self:UnregisterEvent("UNIT_ATTACK_POWER")
+	self:UnregisterEvent("UNIT_LEVEL")
 	self:UnregisterEvent("UNIT_RANGEDDAMAGE")
 	self:UnregisterEvent("UNIT_RANGED_ATTACK_POWER")
 	self:UnregisterEvent("UNIT_SPELL_HASTE")
@@ -73,28 +88,38 @@ function OvalePaperDoll:OnDisable()
 end

 function OvalePaperDoll:MASTERY_UPDATE(event)
-	if OvaleData.level < 80 then
+	if self.level < 80 then
 		self.masteryEffect = 0
 	else
 		self.masteryEffect = GetMasteryEffect()
 	end
 end

-function OvalePaperDoll:PLAYER_ENTERING_WORLD(event)
+function OvalePaperDoll:PLAYER_ALIVE(event)
 	self:MASTERY_UPDATE(event)
 	self:UNIT_ATTACK_POWER(event, "player")
+	self:UNIT_RANGEDDAMAGE(event, "player")
 	self:UNIT_RANGED_ATTACK_POWER(event, "player")
 	self:UNIT_SPELL_HASTE(event, "player")
 	self:UNIT_SPELL_POWER(event, "player")
 	self:UNIT_STATS(event, "player")
 end

+function OvalePaperDoll:PLAYER_LEVEL_UP(event, level, ...)
+	self.level = tonumber(level) or UnitLevel("player")
+end
+
 function OvalePaperDoll:UNIT_ATTACK_POWER(event, unitId)
 	if unitId ~= "player" then return end
 	local base, posBuff, negBuff = UnitAttackPower(unitId)
 	self.attackPower = base + posBuff + negBuff
 end

+function OvalePaperDoll:UNIT_LEVEL(event, unitId)
+	if unitId ~= "player" then return end
+	self.level = UnitLevel(unitId)
+end
+
 function OvalePaperDoll:UNIT_RANGEDDAMAGE(event, unitId)
 	if unitId ~= "player" then return end
 	self.rangedHaste = GetRangedHaste()
@@ -112,7 +137,6 @@ function OvalePaperDoll:UNIT_SPELL_HASTE(event, unitId)
 	self.spellHaste = UnitSpellHaste(unitId)
 end

-local _, className = UnitClass("player")
 local classToSchool = {
 	DEATHKNIGHT = 4, -- Nature
 	DRUID = 4, -- Nature
@@ -136,8 +160,8 @@ local isHealingClass = {

 function OvalePaperDoll:UNIT_SPELL_POWER(event, unitId)
 	if unitId ~= "player" then return end
-	self.spellBonusDamage = GetSpellBonusDamage(classToSchool[className])
-	if isHealingClass[className] then
+	self.spellBonusDamage = GetSpellBonusDamage(classToSchool[self.class])
+	if isHealingClass[self.class] then
 		self.spellBonusHealing = GetSpellBonusHealing()
 	else
 		self.spellBonusHealing = self.spellBonusDamage
diff --git a/OvaleScripts.lua b/OvaleScripts.lua
index 5515190..b13eb1c 100644
--- a/OvaleScripts.lua
+++ b/OvaleScripts.lua
@@ -36,7 +36,7 @@ OvaleScripts.script = {
 -- Return a table of script descriptions indexed by source.
 function OvaleScripts:GetDescriptions()
 	local descriptionsTable = {}
-	for src, tbl in pairs(self.script[OvaleData.className]) do
+	for src, tbl in pairs(self.script[OvalePaperDoll.class]) do
 		descriptionsTable[src] = tbl.desc
 	end
 	return descriptionsTable
diff --git a/OvaleState.lua b/OvaleState.lua
index 1fce431..24faaa6 100644
--- a/OvaleState.lua
+++ b/OvaleState.lua
@@ -63,7 +63,7 @@ function OvaleState:UpdatePowerRates()
 		end
 	end

-	if OvaleData.className == "MONK" then
+	if OvalePaperDoll.class == "MONK" then
 		-- Way of the Monk (monk)
 		if OvaleEquipement:HasTwoHandedWeapon() then
 			-- Strip off 40% melee attack speed bonus for two-handed weapon.
@@ -81,7 +81,7 @@ function OvaleState:UpdatePowerRates()
 		end
 	end

-	if OvaleData.className == "ROGUE" then
+	if OvalePaperDoll.class == "ROGUE" then
 		-- Blade Flurry (combat rogue)
 		if OvaleState:GetAura("player", 13877, true) then
 			self.powerRate.energy = self.powerRate.energy * 0.8
@@ -113,7 +113,7 @@ function OvaleState:Reset()

 	self:UpdatePowerRates()

-	if OvaleData.className == "DEATHKNIGHT" then
+	if OvalePaperDoll.class == "DEATHKNIGHT" then
 		for i=1,6 do
 			self.state.rune[i].type = GetRuneType(i)
 			local start, duration, runeReady = GetRuneCooldown(i)