Quantcast

Refactor spell-scoring to new module OvaleScore.

Johnny C. Lam [10-19-13 - 07:53]
Refactor spell-scoring to new module OvaleScore.

Fix scores not being sent to damage meters in change introduced in r855,
where the wrong module names were being used, so none were ever found
despite proper registration of the damage meter functions.

git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1076 d5049fe3-3747-40f7-a4b5-f36d6801af5f
Filename
Ovale.lua
Ovale.toc
OvaleCompile.lua
OvaleData.lua
OvaleFuture.lua
OvaleRecount.lua
OvaleScore.lua
OvaleSkada.lua
diff --git a/Ovale.lua b/Ovale.lua
index a9f471a..5220106 100644
--- a/Ovale.lua
+++ b/Ovale.lua
@@ -13,7 +13,6 @@ Ovale = LibStub("AceAddon-3.0"):NewAddon(addonNamespace, "Ovale", "AceConsole-3.

 --<private-static-properties>
 local L = LibStub("AceLocale-3.0"):GetLocale("Ovale")
-local OvaleGUID = nil
 local OvaleOptions = nil

 local format = string.format
@@ -22,15 +21,11 @@ local select = select
 local tostring = tostring
 local wipe = table.wipe
 local API_GetTime = GetTime
-local API_RegisterAddonMessagePrefix = RegisterAddonMessagePrefix
-local API_SendAddonMessage = SendAddonMessage
 local API_UnitCanAttack = UnitCanAttack
 local API_UnitExists = UnitExists
 local API_UnitHasVehicleUI = UnitHasVehicleUI
 local API_UnitIsDead = UnitIsDead

-local self_damageMeterMethods = {}
-
 local OVALE_FALSE_STRING = tostring(false)
 local OVALE_NIL_STRING = tostring(nil)
 local OVALE_TRUE_STRING = tostring(true)
@@ -55,10 +50,6 @@ Ovale.traced = false
 Ovale.trace=false
 --in combat?
 Ovale.enCombat = false
---score in current combat
-Ovale.score = 0
---maximal theoric score in current combat
-Ovale.maxScore = 0
 Ovale.refreshNeeded = {}
 Ovale.combatStartTime = nil
 Ovale.listes = {}
@@ -91,11 +82,8 @@ end
 --<public-static-methods>
 function Ovale:OnEnable()
     -- Called when the addon is enabled
-	OvaleGUID = self:GetModule("OvaleGUID")
 	OvaleOptions = self:GetModule("OvaleOptions")

-	API_RegisterAddonMessagePrefix("Ovale")
-	self:RegisterEvent("CHAT_MSG_ADDON")
 	self:RegisterEvent("PLAYER_REGEN_ENABLED")
 	self:RegisterEvent("PLAYER_REGEN_DISABLED")
 	self:RegisterEvent("PLAYER_TARGET_CHANGED")
@@ -106,23 +94,12 @@ end

 function Ovale:OnDisable()
     -- Called when the addon is disabled
-	self:UnregisterEvent("CHAT_MSG_ADDON")
 	self:UnregisterEvent("PLAYER_REGEN_ENABLED")
 	self:UnregisterEvent("PLAYER_REGEN_DISABLED")
 	self:UnregisterEvent("PLAYER_TARGET_CHANGED")
 	self.frame:Hide()
 end

--- Receive scores for damage meters from other Ovale addons in the raid.
-function Ovale:CHAT_MSG_ADDON(event, ...)
-	local prefix, message, channel, sender = ...
-	if prefix ~= "Ovale" then return end
-	if channel ~= "RAID" and channel ~= "PARTY" then return end
-
-	local scored, scoreMax, guid = strsplit(";", message)
-	self:SendScoreToDamageMeter(sender, guid, scored, scoreMax)
-end
-
 --Called when the player target change
 --Used to update the visibility e.g. if the user chose
 --to hide Ovale if a friendly unit is targeted
@@ -134,21 +111,10 @@ end
 function Ovale:PLAYER_REGEN_ENABLED()
 	self.enCombat = false
 	self:UpdateVisibility()
-	-- if self.maxScore and self.maxScore > 0 then
-	-- 	self:Print((self.score/self.maxScore*100).."%")
-	-- end
 end

 function Ovale:PLAYER_REGEN_DISABLED()
-	if self.maxScore > 0 then
-		-- Broadcast the player's own score for damage meters when combat ends.
-		-- Broadcast message is "score;maxScore;playerGUID"
-		local message = self.score .. ";" .. self.maxScore .. ";" .. OvaleGUID:GetGUID("player")
-		API_SendAddonMessage("Ovale", message, "RAID")
-	end
 	self.enCombat = true
-	self.score = 0
-	self.maxScore = 0
 	self.combatStartTime = API_GetTime()
 	self:UpdateVisibility()
 end
@@ -269,47 +235,6 @@ function Ovale:ToggleCheckBox(v)
 	end
 end

---[[
-	Damage meter addons that want to receive Ovale scores should implement
-	and register a function that has the following signature:
-
-		ReceiveScore(name, guid, scored, scoreMax)
-
-		Parameters:
-			name - the name of the unit
-			guid - GUID of the named unit
-			scored - current score
-			scoreMax - current maximum score
-
-		Returns:
-			none
-
-	The function should be registered with Ovale using the RegisterDamageMeter
-	method, which needs a unique name for the meter and either the function itself
-	or a method name for the module with the given name.
-]]--
-
-function Ovale:RegisterDamageMeter(name, method)
-	self_damageMeterMethods[name] = method
-end
-
-function Ovale:UnregisterDamageMeter(name)
-	self_damageMeterMethods[name] = nil
-end
-
-function Ovale:SendScoreToDamageMeter(name, guid, scored, scoreMax)
-	for _, method in pairs(self_damageMeterMethods) do
-		if type(method) == "string" then
-			local module = self:GetModule(name) or LibStub("AceAddon-3.0"):GetAddon(name)
-			if module and type(module[method]) == "function" then
-				return module[method](module, name, guid, scored, scoreMax)
-			end
-		elseif type(method) == "function" then
-			return method(name, guid, scored, scoreMax)
-		end
-	end
-end
-
 -- Debugging methods.
 -- format() wrapper that turns nil arguments into tostring(nil)
 function Ovale:Format(...)
diff --git a/Ovale.toc b/Ovale.toc
index 69042a2..1b1ecfd 100644
--- a/Ovale.toc
+++ b/Ovale.toc
@@ -40,14 +40,16 @@ OvalePool.lua
 OvalePoolGC.lua
 OvalePower.lua
 OvaleQueue.lua
-OvaleRecount.lua
-OvaleSkada.lua
 OvaleSpellBook.lua
 OvaleStance.lua
 OvaleTimeSpan.lua
 #
 OvaleDamageTaken.lua
 OvalePaperDoll.lua
+OvaleScore.lua
+#
+OvaleRecount.lua
+OvaleSkada.lua
 #
 OvaleScripts.lua
 defaut\Chaman.lua
diff --git a/OvaleCompile.lua b/OvaleCompile.lua
index 295979b..4bf920a 100644
--- a/OvaleCompile.lua
+++ b/OvaleCompile.lua
@@ -20,6 +20,7 @@ local OvaleEquipement = Ovale.OvaleEquipement
 local OvaleOptions = Ovale.OvaleOptions
 local OvalePaperDoll = Ovale.OvalePaperDoll
 local OvalePool = Ovale.OvalePool
+local OvaleScore = Ovale.OvaleScore
 local OvaleScripts = Ovale.OvaleScripts
 local OvaleSpellBook = Ovale.OvaleSpellBook
 local OvaleStance = Ovale.OvaleStance
@@ -339,8 +340,7 @@ local function ParseScoreSpells(params)
 	for v in strgmatch(params, "(%d+)") do
 		local spellId = tonumber(v)
 		if spellId then
-			--Ovale:FormatPrint("Add spell to score %d", spellId)
-			OvaleData.scoreSpell[spellId] = true
+			OvaleScore:AddSpell(spellId)
 		else
 			Ovale:FormatPrint("ScoreSpell with unknown spell %s", v)
 		end
diff --git a/OvaleData.lua b/OvaleData.lua
index 054aca1..22fc9ef 100644
--- a/OvaleData.lua
+++ b/OvaleData.lua
@@ -45,8 +45,6 @@ self_buffNoSnapshotSpellList =
 OvaleData.itemList = {}
 --spell info from the current script (by spellId)
 OvaleData.spellInfo = {}
---spells that count for scoring
-OvaleData.scoreSpell = {}

 OvaleData.buffSpellList =
 {
diff --git a/OvaleFuture.lua b/OvaleFuture.lua
index 56ca152..d5c9e2c 100644
--- a/OvaleFuture.lua
+++ b/OvaleFuture.lua
@@ -21,6 +21,7 @@ local OvaleData = Ovale.OvaleData
 local OvaleGUID = Ovale.OvaleGUID
 local OvalePaperDoll = Ovale.OvalePaperDoll
 local OvalePool = Ovale.OvalePool
+local OvaleScore = Ovale.OvaleScore
 local OvaleSpellBook = Ovale.OvaleSpellBook

 local ipairs = ipairs
@@ -86,19 +87,6 @@ local function TracePrintf(spellId, ...)
 	end
 end

-local function ScoreSpell(spellId)
-	local si = OvaleData.spellInfo[spellId]
-	if Ovale.enCombat and not (si and si.toggle) and OvaleData.scoreSpell[spellId] then
-		local scored = Ovale.frame:GetScore(spellId)
-		Ovale:Logf("Scored %s", scored)
-		if scored then
-			Ovale.score = Ovale.score + scored
-			Ovale.maxScore = Ovale.maxScore + 1
-			Ovale:SendScoreToDamageMeter(self_playerName, OvaleGUID:GetGUID("player"), scored, 1)
-		end
-	end
-end
-
 -- Return the spell-specific damage multiplier using the information from SpellDamage{Buff,Debuff} declarations.
 -- This doesn't include the base damage multiplier of the character.
 local function GetDamageMultiplier(spellId)
@@ -192,7 +180,7 @@ local function AddSpellToQueue(spellId, lineId, startTime, endTime, channeled, a
 	end
 	tinsert(self_activeSpellcast, spellcast)

-	ScoreSpell(spellId)
+	OvaleScore:ScoreSpell(spellId)
 	Ovale.refreshNeeded["player"] = true
 end

diff --git a/OvaleRecount.lua b/OvaleRecount.lua
index 39fb5e5..d9ff529 100644
--- a/OvaleRecount.lua
+++ b/OvaleRecount.lua
@@ -13,11 +13,9 @@ local OvaleRecount = Ovale:NewModule("OvaleRecount")
 Ovale.OvaleRecount = OvaleRecount

 --<private-static-properties>
-local Recount = LibStub("AceAddon-3.0"):GetAddon("Recount", true)
 local L = LibStub("AceLocale-3.0"):GetLocale("Recount", true)
-if not L then
-	L = setmetatable({}, { __index = function(t, k) t[k] = k; return k; end })
-end
+local OvaleScore = Ovale.OvaleScore
+local Recount = LibStub("AceAddon-3.0"):GetAddon("Recount", true)
 --</private-static-properties>

 --<private-static-methods>
@@ -50,20 +48,21 @@ end
 --<public-static-methods>
 function OvaleRecount:OnInitialize()
 	if Recount then
+		if not L then
+			L = setmetatable({}, { __index = function(t, k) t[k] = k; return k; end })
+		end
 		Recount:AddModeTooltip("Ovale", DataModes, TooltipFuncs, nil, nil, nil, nil)
 	end
 end

 function OvaleRecount:OnEnable()
 	if Recount then
-		Ovale:RegisterDamageMeter("OvaleRecount", "ReceiveScore")
+		OvaleScore:RegisterDamageMeter("OvaleRecount", self, "ReceiveScore")
 	end
 end

 function OvaleRecount:OnDisable()
-	if Recount then
-		Ovale:UnregisterDamageMeter("OvaleRecount")
-	end
+	OvaleScore:UnregisterDamageMeter("OvaleRecount")
 end

 function OvaleRecount:ReceiveScore(name, guid, scored, scoreMax)
diff --git a/OvaleScore.lua b/OvaleScore.lua
new file mode 100644
index 0000000..4e72bde
--- /dev/null
+++ b/OvaleScore.lua
@@ -0,0 +1,140 @@
+--[[--------------------------------------------------------------------
+    Ovale Spell Priority
+    Copyright (C) 2009, 2010, 2011, 2012 Sidoine
+    Copyright (C) 2012, 2013 Johnny C. Lam
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License in the LICENSE
+    file accompanying this program.
+--]]--------------------------------------------------------------------
+
+--[[
+	Damage meter addons that want to receive Ovale scores should implement
+	and register a function that has the following signature:
+
+		ReceiveScore(name, guid, scored, scoreMax)
+
+		Parameters:
+			name - the name of the unit
+			guid - GUID of the named unit
+			scored - current score
+			scoreMax - current maximum score
+
+		Returns:
+			none
+
+	The function should be registered with Ovale using the RegisterDamageMeter
+	method, which needs a unique name for the meter and either the function itself
+	or a method name for the module with the given name.
+]]--
+
+local _, Ovale = ...
+local OvaleScore = Ovale:NewModule("OvaleScore", "AceEvent-3.0")
+Ovale.OvaleScore = OvaleScore
+
+--<private-static-properties>
+local OvaleData = Ovale.OvaleData
+local OvaleGUID = Ovale.OvaleGUID
+
+local pairs = pairs
+local strsplit = string.split
+local API_RegisterAddonMessagePrefix = RegisterAddonMessagePrefix
+local API_SendAddonMessage = SendAddonMessage
+
+-- self_damageMeter[moduleName] = module
+local self_damageMeter = {}
+-- self_damageMeterMethod[moduleName] = methodName or function
+local self_damageMeterMethod = {}
+-- Score from current combat session.
+local self_score = 0
+-- Maximum possible score from current combat session.
+local self_maxScore = 0
+-- Spells for which a score is computed.
+local self_scoredSpell = {}
+--</private-static-properties>
+
+--<public-static-properties>
+--</public-static-properties>
+
+--<public-static-methods>
+function OvaleScore:OnEnable()
+	API_RegisterAddonMessagePrefix("Ovale")
+	self:RegisterEvent("CHAT_MSG_ADDON")
+	self:RegisterEvent("PLAYER_REGEN_ENABLED")
+	self:RegisterEvent("PLAYER_REGEN_DISABLED")
+end
+
+function OvaleScore:OnDisable()
+	self:UnregisterEvent("CHAT_MSG_ADDON")
+	self:UnregisterEvent("PLAYER_REGEN_ENABLED")
+	self:UnregisterEvent("PLAYER_REGEN_DISABLED")
+end
+
+-- Receive scores for damage meters from other Ovale addons in the raid.
+function OvaleScore:CHAT_MSG_ADDON(event, ...)
+	local prefix, message, channel, sender = ...
+	if prefix ~= "Ovale" then return end
+	if channel ~= "RAID" and channel ~= "PARTY" then return end
+
+	local scored, scoreMax, guid = strsplit(";", message)
+	self:SendScore(sender, guid, scored, scoreMax)
+end
+
+function OvaleScore:PLAYER_REGEN_ENABLED()
+	-- Broadcast the player's own score for damage meters when combat ends.
+	-- Broadcast message is "score;maxScore;playerGUID"
+	if self_maxScore > 0 then
+		local message = self_score .. ";" .. self_maxScore .. ";" .. OvaleGUID:GetGUID("player")
+		API_SendAddonMessage("Ovale", message, "RAID")
+	end
+end
+
+function OvaleScore:PLAYER_REGEN_DISABLED()
+	self_score = 0
+	self_maxScore = 0
+end
+
+-- RegisterDamageMeter(moduleName, function) or
+-- RegisterDamageMeter(moduleName, addonObject, methodName)
+function OvaleScore:RegisterDamageMeter(moduleName, addon, func)
+	if not func then
+		func = addon
+	elseif addon then
+		self_damageMeter[moduleName] = addon
+	end
+	self_damageMeterMethod[moduleName] = func
+end
+
+function OvaleScore:UnregisterDamageMeter(moduleName)
+	self_damageMeter[moduleName] = nil
+	self_damageMeterMethod[moduleName] = nil
+end
+
+function OvaleScore:AddSpell(spellId)
+	self_scoredSpell[spellId] = true
+end
+
+function OvaleScore:ScoreSpell(spellId)
+	local si = OvaleData.spellInfo[spellId]
+	if Ovale.enCombat and self_scoredSpell[spellId] and not (si and si.toggle) then
+		local scored = Ovale.frame:GetScore(spellId)
+		Ovale:Logf("Scored %s", scored)
+		if scored then
+			self_score = self_score + scored
+			self_maxScore = self_maxScore + 1
+			self:SendScore(self_playerName, OvaleGUID:GetGUID("player"), scored, 1)
+		end
+	end
+end
+
+function OvaleScore:SendScore(name, guid, scored, scoreMax)
+	for moduleName, method in pairs(self_damageMeterMethods) do
+		local addon = self_damageMeter[moduleName]
+		if addon then
+			addon[method](addon, name, guid, scored, scoreMax)
+		elseif type(method) == "function" then
+			method(name, guid, scored, scoreMax)
+		end
+	end
+end
+--</public-static-methods>
diff --git a/OvaleSkada.lua b/OvaleSkada.lua
index 509f07f..3423839 100644
--- a/OvaleSkada.lua
+++ b/OvaleSkada.lua
@@ -14,6 +14,8 @@ local OvaleSkada = Skada and Skada:NewModule("Ovale Spell Priority") or {}
 Ovale.OvaleSkada = OvaleSkada

 --<private-static-properties>
+local OvaleScore = Ovale.OvaleScore
+
 local ipairs = ipairs
 local floor = math.floor
 local tostring = tostring
@@ -31,9 +33,23 @@ local function GetValue(set)
 		return nil
 	end
 end
+--</private-static-methods>
+
+--<public-static-methods>
+function OvaleSkada:OnEnable()
+	self.metadata = { showspots = true }
+	if Skada then
+		Skada:AddMode(self)
+		OvaleScore:RegisterDamageMeter("OvaleSkada", self, "ReceiveScore")
+	end
+end
+
+function OvaleSkada:OnDisable()
+	OvaleScore:UnregisterDamageMeter("OvaleSkada")
+	if Skada then Skada:RemoveMode(self) end
+end

-local function ReceiveScore(name, guid, scored, scoreMax)
-	local self = OvaleSkada
+function OvaleSkada:ReceiveScore(name, guid, scored, scoreMax)
 	if guid and Skada and Skada.current and Skada.total then
 		local player = Skada:get_player(Skada.current, guid, nil)
 		if player then
@@ -46,19 +62,6 @@ local function ReceiveScore(name, guid, scored, scoreMax)
 		end
 	end
 end
---</private-static-methods>
-
---<public-static-methods>
-function OvaleSkada:OnEnable()
-	self.metadata = { showspots = true }
-	Skada:AddMode(self)
-	Ovale:RegisterDamageMeter("OvaleSkada", ReceiveScore)
-end
-
-function OvaleSkada:OnDisable()
-	Ovale:UnregisterDamageMeter("OvaleSkada")
-	Skada:RemoveMode(self)
-end

 function OvaleSkada:Update(win, set)
 	local max = 0