-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoleManager.lua
242 lines (206 loc) · 5.3 KB
/
RoleManager.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
--[[
* Copyright (c) 2011-2012 by Adam Hellberg.
*
* This file is part of Command.
*
* Command is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Command is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Command. If not, see <http://www.gnu.org/licenses/>.
--]]
-- Upvalues
local type = type
-- API Upvalues
local CreateFrame = CreateFrame
local UnitSetRole = UnitSetRole
local UnitGetAvailableRoles = UnitGetAvailableRoles
local UnitGroupRolesAssigned = UnitGroupRolesAssigned
local C = Command
C.RoleManager = {
Roles = {
TANK = "CRM_TANK",
HEALER = "CRM_HEALER",
DAMAGER = "CRM_DAMAGE",
NONE = "CRM_UNDEFINED",
Tank = "TANK",
Healer = "HEALER",
Damage = "DAMAGER",
Undefined = "NONE"
},
BlizzRoles = {}
}
local L = C.LocaleManager
local RM = C.RoleManager
local GT = C.GroupTools
local CM
local CEN = C.Extensions.Number
local DEFAULT_DELAY = 5
local MAX_DELAY = 60 -- Arbitrary max delay, unknown if there is an existing delay
local AnnouncePending = false
function RM:Init()
CM = C.ChatManager
self:LoadSavedVars()
end
function RM:LoadSavedVars()
if type(C.Global["ROLE_MANAGER"]) ~= "table" then
C.Global["ROLE_MANAGER"] = {}
end
self.Settings = C.Global["ROLE_MANAGER"]
if type(self.Settings.ENABLED) ~= "boolean" then
self.Settings.ENABLED = true
end
if type(self.Settings.ANNOUNCE) ~= "boolean" then
self.Settings.ANNOUNCE = true
end
if type(self.Settings.DELAY) ~= "number" then
self.Settings.DELAY = DEFAULT_DELAY
end
end
function RM:OnRoleCheck()
if AnnouncePending then return end
if self.Settings.DELAY > 0 then
AnnouncePending = true
local frame = CreateFrame("Frame")
frame.Time = 0
frame.Delay = self.Settings.DELAY
frame:SetScript("OnUpdate", function(self, elapsed)
self.Time = self.Time + elapsed
if self.Time >= self.Delay then
self:SetScript("OnUpdate", nil)
AnnouncePending = false
RM:Announce()
end
end)
else
self:Announce()
end
end
function RM:Announce()
if not self:RolePollActive() or not self.Settings.ANNOUNCE then return end
local current = self:GetRole()
local msg
if current ~= self.Roles.Undefined then
msg = "CRM_ANNOUNCE_HASROLE"
else
msg = "CRM_ANNOUNCE_NOROLE"
end
CM:SendMessage(L(msg):format(L(self.Roles[current])), "SMART")
end
function RM:SetRole(role)
if type(role) ~= "string" then
error("RoleManager.SetRole: Argument was of type [" .. type(role) .. "], expected [string].")
return nil
end
role = role:lower()
local result = self.Roles.Damage
if role:match("^t") then
result = self.Roles.Tank
elseif role:match("^h") then
result = self.Roles.Healer
end
if not self:CanBeRole(result) then
return false, "CRM_SET_INVALID", {L(self.Roles[result])}
end
UnitSetRole("player", result)
if self:RolePollActive() then
RolePollPopup:Hide()
end
return "CRM_SET_SUCCESS", {L(self.Roles[result])}
end
function RM:GetRole()
local role = UnitGroupRolesAssigned("player")
if role == "TANK" then
return self.Roles.Tank
elseif role == "HEALER" then
return self.Roles.Healer
elseif role == "DAMAGER" then
return self.Roles.Damage
end
return self.Roles.Undefined
end
function RM:ConfirmRole()
local role = self:GetRole()
if role == self.Roles.Undefined then
return false, "CRM_CONFIRM_NOROLE"
end
return self:SetRole(role)
end
function RM:CanBeRole(role)
if type(role) ~= "string" then
error("RoleManager.CanBeRole: Argument was of type [" .. type(role) .. "], expected [string].")
return nil
end
role = role:lower()
local tank, healer, dps = UnitGetAvailableRoles("player")
if role:match("^t") then
return tank
elseif role:match("^h") then
return healer
end
return dps
end
function RM:RolePollActive()
return RolePollPopup and RolePollPopup:IsShown()
end
function RM:Start()
if self:RolePollActive() then
return false, "CRM_START_ACTIVE"
elseif (GT:IsParty() and GT:IsGroupLeader()) or (GT:IsRaid() and GT:IsRaidLeaderOrAssistant()) then
InitiateRolePoll()
return "CRM_START_SUCCESS"
elseif not GT:IsGroup() then
return false, "CRM_START_NOGROUP"
end
return false, "CRM_START_NOPRIV"
end
function RM:Enable()
self.Settings.ENABLED = true
return "CRM_ENABLED"
end
function RM:Disable()
self.Settings.ENABLED = false
return "CRM_DISABLED"
end
function RM:Toggle()
if self:IsEnabled() then
return self:Disable()
end
return self:Enable()
end
function RM:IsEnabled()
return self.Settings.ENABLED
end
function RM:EnableAnnounce()
end
function RM:DisableAnnounce()
end
function RM:ToggleAnnounce()
end
function RM:IsAnnounceEnabled()
end
function RM:GetDelay()
return CEN:FormatSeconds(self:GetRawDelay())
end
function RM:GetRawDelay()
return self.Settings.DELAY
end
function RM:SetDelay(amount)
if amount < 0 then
amount = 0
elseif amount > MAX_DELAY then
amount = MAX_DELAY
end
self.Settings.DELAY = amount
if amount > 0 then
return "CRM_SETDELAY_SUCCESS", {CEN:FormatSeconds(amount)}
end
return "CRM_SETDELAY_INSTANT"
end