-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.lua
364 lines (307 loc) · 9.34 KB
/
Core.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
RGO = LibStub("AceAddon-3.0"):NewAddon("RGO", "AceEvent-3.0", "AceComm-3.0")
RGO_Console = LibStub("AceConsole-3.0")
AddonCommCommands = {
["end_transmission"] = "end_transmission",
["request_preset"] = "request_preset",
["start_transmission"] = "start_transmission"
}
--transmitting preset helper variables
local transmittedGroups = nil
local transmittedPresetName = nil
function RGO:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("RGO_DB")
if(self.db.realm.presetIndexToGroupStructure == nil) then
self.db.realm.presetIndexToGroupStructure = {}
end
if(self.db.realm.presetIndexToPresetName == nil) then
self.db.realm.presetIndexToPresetName = {}
end
self.queuedSorting = nil
RGO:RegisterEvent("GROUP_ROSTER_UPDATE", "UpdateDropdownVisibility")
RGO:RegisterEvent("PLAYER_ROLES_ASSIGNED", "UpdateDropdownVisibility")
RGO:RegisterEvent("PARTY_LEADER_CHANGED", "UpdateDropdownVisibility")
RGO:RegisterEvent("PLAYER_REGEN_ENABLED", "ProcessQueuedSorting")
RGO:RegisterComm("rgo", "HandleAddonMessage")
RGO:CreatePresetDropdownInRaidFrame()
RGO:UpdateDropdownVisibility()
end
function RGO:OnEnable()
end
function RGO:OnDisable()
end
function RGO:TrimText(s)
return s:match "^%s*(.-)%s*$"
end
function RGO:SendMessage(msg, target)
RGO:SendCommMessage("rgo", msg, "WHISPER", target)
end
function RGO:SendPreset(presetName, target, playerNameRequestor)
if not target or target == "" then
return
end
if not presetName or presetName == "" then
presetName = "unnamed"
end
--print("Sending Raid Group Organizer preset "..presetName.." to " .. target)
RGO:SendMessage(format("%s %s", AddonCommCommands["start_transmission"], presetName), target)
for i = 1, 8 do
local message = ""
for j = 1, 5 do
local playerName = playerNameRequestor(i, j)
if(playerName == nil or playerName == "") then
playerName = "[]"
end
message = message .. playerName
if j < 5 then
message = message .. ","
end
end
RGO:SendMessage(message, target)
end
RGO:SendMessage(AddonCommCommands["end_transmission"], target)
--print("done")
end
function RGO:HandleAddonMessage(prefix, msg, distribution, sender)
local command = {}
for part in string.gmatch(msg, "[^%s]+") do
command[#command + 1] = part
end
if command[1] == AddonCommCommands["end_transmission"] then
if not (transmittedPresetName and transmittedGroups) then
return --wrong message order
end
for key,value in pairs(transmittedGroups) do
if value == "" then
transmittedGroups[key] = nil
end
end
RGO:addNewPreset(transmittedPresetName, transmittedGroups)
RgoFrameScrollBar_Update()
transmittedGroups = nil
transmittedPresetName = nil
--print("done.")
elseif command[1] == AddonCommCommands["start_transmission"] then
if not command[2] then
return
end
transmittedPresetName = command[2]
transmittedGroups = {}
--print("Receiving Raid Group Organizer preset: " .. command[2])
elseif command[1] == AddonCommCommands["request_preset"] then
if not command[2] then
return
end
local argParts = {}
for part in string.gmatch(command[2], "[^,]+") do
argParts[#argParts + 1] = part
end
if #argParts ~= 2 then
return
end
local presetName = argParts[1]
local index = argParts[2]
if sender ~= UnitName("player") then
local group = RGO:getPresetGroup(tonumber(index))
RGO:SendPreset(presetName, sender, function(i, j) return group[(i - 1) * 5 + j] end)
else
--print("Requesting own preset")
end
else
for playerName in string.gmatch(msg, "([^,]+)") do
if (playerName == "[]") then
playerName = ""
end
table.insert(transmittedGroups, playerName)
end
end
end
function RGO:HandleAddonLoadedEvent(event, arg1)
if arg1 == "RaidGroupOrganizer" then
RGO:InitDraggableButtons()
end
end
function RGO:CreatePresetDropdownInRaidFrame()
self.presetDropDown = CreateFrame("FRAME", "RGOChoosePreset", RaidFrame, "UIDropDownMenuTemplate")
self.presetDropDown:SetPoint("TOPLEFT", RaidFrame, "TOPLEFT", 102, -22)
RGOChoosePresetRight:SetAlpha(0)
RGOChoosePresetLeft:SetAlpha(0)
RGOChoosePresetMiddle:SetAlpha(0)
UIDropDownMenu_SetWidth(self.presetDropDown, 7)
UIDropDownMenu_Initialize(self.presetDropDown, RgoDropDownMenu_Initialize)
end
function RGO:PrintMissingPlayers(index)
local sortedGRP = self.db.realm.presetIndexToGroupStructure[index]
local indexToName = {}
local nameToIndex = {}
for i = 1, 40 do
indexToName[i] = GetRaidRosterInfo(i)
if(indexToName[i] ~= nil) then
nameToIndex[indexToName[i]] = i
end
end
local missinPlayers = {}
for i = 1, 8 do
for j = 1, 5 do
local currentIndex = (i - 1) * 5 + j
if(nameToIndex[sortedGRP[currentIndex]] == nil) then
local missingPlayer = sortedGRP[currentIndex]
table.insert(missinPlayers, missingPlayer)
end
end
end
if (#missinPlayers > 0) then
DEFAULT_CHAT_FRAME:AddMessage("Missing preset players: " .. table.concat(missinPlayers, ", " ), 1.0, 0.0, 0.0);
end
end
function RgoDropDownMenu_Initialize (frame, level, menuList)
local function presetDropDown_OnClick(frame, index, arg2, checked)
if (not RGO:sortGroup(index)) then
UIDropDownMenu_SetSelectedValue(RGO.presetDropDown, frame.value);
else
RGO:PrintMissingPlayers(index)
end
end
local info = UIDropDownMenu_CreateInfo()
for i = 1, #RGO.db.realm.presetIndexToPresetName do
info.func = presetDropDown_OnClick
info.text = RGO.db.realm.presetIndexToPresetName[i]
info.arg1 = i
UIDropDownMenu_AddButton(info)
info = UIDropDownMenu_CreateInfo()
end
end
function RGO:UpdateDropdownVisibility()
if UnitInRaid("player") and ((UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) and RGO:getPresetCount() > 0) then
self.presetDropDown:Show()
else
self.presetDropDown:Hide()
end
end
function RGO:ProcessQueuedSorting()
if(queuedSorting ~= nil) then
RGO:sortGroup(queuedSorting)
UIDropDownMenu_SetSelectedValue(RGO.presetDropDown, nil);
RGO:PrintMissingPlayers(queuedSorting)
queuedSorting = nil
end
end
function RGO:deletePreset(index)
if(index ~= nil) then
table.remove(self.db.realm.presetIndexToPresetName, index)
table.remove(self.db.realm.presetIndexToGroupStructure, index)
end
if(RGO:getPresetCount() == 0) then
RGO:UpdateDropdownVisibility()
end
end
function RGO:addNewPreset(name, group)
table.insert(self.db.realm.presetIndexToPresetName, name)
table.insert(self.db.realm.presetIndexToGroupStructure, group)
if(RGO:getPresetCount() == 1) then
RGO:UpdateDropdownVisibility()
end
end
function RGO:updatePreset(index, name, group)
self.db.realm.presetIndexToPresetName[index] = name
self.db.realm.presetIndexToGroupStructure[index] = group
end
function RGO:getPresetCount()
return #self.db.realm.presetIndexToPresetName
end
function RGO:getPresetName(index)
return self.db.realm.presetIndexToPresetName[index]
end
function RGO:getPresetGroup(index)
return self.db.realm.presetIndexToGroupStructure[index]
end
function RGO:getCurrentRaidSetup()
local raidGroupStructure = {}
local raidGroup = {}
for i = 1, 40 do
name, _, subGrp = GetRaidRosterInfo(i)
if(name ~= nil) then
if(raidGroupStructure[subGrp] == nil) then
raidGroupStructure[subGrp] = {}
end
table.insert(raidGroupStructure[subGrp], name)
end
end
for i = 1, 8 do
for j = 1, 5 do
local index = (i - 1) * 5 + j
if(raidGroupStructure[i] == nil) then
raidGroup[index] = nil
else
raidGroup[index] = raidGroupStructure[i][j]
end
end
end
return raidGroup
end
function RGO:sortGroup(index)
if(InCombatLockdown()) then
queuedSorting = index
return false
end
local idToName = {}
local idToSubGrp = {}
local nameToId = {}
local sortedGRP = self.db.realm.presetIndexToGroupStructure[index]
for i = 1, 40 do
idToName[i], a, idToSubGrp[i] = GetRaidRosterInfo(i)
if(idToName[i] ~= nil) then
nameToId[idToName[i]] = i
else
idToSubGrp[i] = nil
end
end
local subGrpSize = {0,0,0,0,0,0,0,0}
for i = 1, 40 do
if idToSubGrp[i] ~= nil then
subGrpSize[idToSubGrp[i]] = subGrpSize[idToSubGrp[i]] + 1
end
end
local idToSortedSubGrp = {}
for i = 1, 8 do
for j = 1, 5 do
local index = (i - 1) * 5 + j
if(nameToId[sortedGRP[index]] ~= nil) then
idToSortedSubGrp[nameToId[sortedGRP[index]]] = i
end
end
end
for i = 1, 8 do
for j = 1, 5 do
local indexInSortedGroup = (i - 1) * 5 + j
if(nameToId[sortedGRP[indexInSortedGroup]] ~= nil) then
local idOfPlayerToSort = nameToId[sortedGRP[indexInSortedGroup]]
if(idToSubGrp[idOfPlayerToSort] == i) then
idToSubGrp[idOfPlayerToSort] = 0
elseif(subGrpSize[i] < 5) then
subGrpSize[idToSubGrp[idOfPlayerToSort]] = subGrpSize[idToSubGrp[idOfPlayerToSort]] -1
SetRaidSubgroup(idOfPlayerToSort, i)
idToSubGrp[idOfPlayerToSort] = 0
subGrpSize[i] = subGrpSize[i] + 1
else
for i2 = 1, 40 do
if(idToSubGrp[i2] == i and idToSubGrp[i2] ~= idToSortedSubGrp[i2]) then
SwapRaidSubgroup(i2, idOfPlayerToSort)
idToSubGrp[i2] = idToSubGrp[idOfPlayerToSort]
idToSubGrp[idOfPlayerToSort] = 0
break
end
end
end
end
end
end
return true
end
RGO_Console:RegisterChatCommand("rgo", "CreateRGOWindow")
function RGO_Console:CreateRGOWindow(input)
if not RgoFrame:IsVisible() then
RgoFrame:Show()
end
end
--this outside of OnInitialize now because it will trigger for the own addon otherwise
RGO:RegisterEvent("ADDON_LOADED", "HandleAddonLoadedEvent")