forked from Sidoine/Ovale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScore.lua
156 lines (133 loc) · 4.64 KB
/
Score.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
--[[--------------------------------------------------------------------
Copyright (C) 2009, 2010, 2011, 2012 Sidoine De Wispelaere.
Copyright (C) 2012, 2013, 2014 Johnny C. Lam.
See the file LICENSE.txt for copying permission.
--]]--------------------------------------------------------------------
--[[
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, Ovale = ...
local OvaleScore = Ovale:NewModule("OvaleScore", "AceEvent-3.0", "AceSerializer-3.0")
Ovale.OvaleScore = OvaleScore
--<private-static-properties>
local L = Ovale.L
local OvaleDebug = Ovale.OvaleDebug
-- Forward declarations for module dependencies.
local OvaleFuture = nil
local pairs = pairs
local type = type
local API_IsInGroup = IsInGroup
local API_SendAddonMessage = SendAddonMessage
local API_UnitName = UnitName
local LE_PARTY_CATEGORY_INSTANCE = LE_PARTY_CATEGORY_INSTANCE
local MSG_PREFIX = Ovale.MSG_PREFIX
-- Player's GUID.
local self_playerGUID = nil
-- Player's name.
local self_name = nil
-- Register for debugging messages.
OvaleDebug:RegisterDebugging(OvaleScore)
--</private-static-properties>
--<public-static-properties>
-- self_damageMeter[moduleName] = module
OvaleScore.damageMeter = {}
-- self_damageMeterMethod[moduleName] = methodName or function
OvaleScore.damageMeterMethod = {}
-- Score from current combat session.
OvaleScore.score = 0
-- Maximum possible score from current combat session.
OvaleScore.maxScore = 0
-- Spells for which a score is computed.
OvaleScore.scoredSpell = {}
--</public-static-properties>
--<public-static-methods>
function OvaleScore:OnInitialize()
-- Resolve module dependencies.
OvaleFuture = Ovale.OvaleFuture
end
function OvaleScore:OnEnable()
self_playerGUID = Ovale.playerGUID
self_name = API_UnitName("player")
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 == MSG_PREFIX then
local ok, msgType, scored, scoreMax, guid = self:Deserialize(message)
if ok and msgType == "S" then
self:SendScore(sender, guid, scored, scoreMax)
end
end
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 and API_IsInGroup() then
local message = self:Serialize("score", self.score, self.maxScore, self_playerGUID)
local channel = API_IsInGroup(LE_PARTY_CATEGORY_INSTANCE) and "INSTANCE_CHAT" or "RAID"
API_SendAddonMessage(MSG_PREFIX, message, channel)
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)
if OvaleFuture.inCombat and self.scoredSpell[spellId] then
local scored = Ovale.frame:GetScore(spellId)
self:DebugTimestamp("Scored %s for %d.", scored, spellId)
if scored then
self.score = self.score + scored
self.maxScore = self.maxScore + 1
self:SendScore(self_name, self_playerGUID, scored, 1)
end
end
end
function OvaleScore:SendScore(name, guid, scored, scoreMax)
for moduleName, method in pairs(self.damageMeterMethod) 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>