-
Notifications
You must be signed in to change notification settings - Fork 1
/
PlayerData.lua
311 lines (266 loc) · 10.3 KB
/
PlayerData.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
local Private, _, Namespace = {}, ...
Namespace.PlayerData = {}
local ReadyCheckState = Namespace.Utils.ReadyCheckState
local BattlegroundStatus = Namespace.Utils.BattlegroundStatus
local RoleCheckStatus = Namespace.Utils.RoleCheckStatus
local GroupType = Namespace.Utils.GroupType
local GetGroupType = Namespace.Utils.GetGroupType
local GetRealUnitName = Namespace.Utils.GetRealUnitName
local GetTime = GetTime
local UnitIsPlayer = UnitIsPlayer
local UnitExists = UnitExists
local UnitFactionGroup = UnitFactionGroup
local UnitGUID = UnitGUID
local UnitIsConnected = UnitIsConnected
local UnitIsGroupLeader = UnitIsGroupLeader
local UnitIsGroupAssistant = UnitIsGroupAssistant
local UnitIsMercenary = UnitIsMercenary
local UnitClass = UnitClass
local GetClassColor = C_ClassColor.GetClassColor
local UNKNOWNOBJECT = UNKNOWNOBJECT
local pairs = pairs
local unpack = unpack
local sort = table.sort
local strsplit = strsplit
local PlayerDataTargets = {
solo = {'player'},
party = { 'player', 'party1', 'party2', 'party3', 'party4' },
raid = {
'raid1', 'raid2', 'raid3', 'raid4', 'raid5', 'raid6', 'raid7', 'raid8', 'raid9', 'raid10',
'raid11', 'raid12', 'raid13', 'raid14', 'raid15', 'raid16', 'raid17', 'raid18', 'raid19', 'raid20',
'raid21', 'raid22', 'raid23', 'raid24', 'raid25', 'raid26', 'raid27', 'raid28', 'raid29', 'raid30',
'raid31', 'raid32', 'raid33', 'raid34', 'raid35', 'raid36', 'raid37', 'raid38', 'raid39', 'raid40',
'player', 'party1', 'party2', 'party3', 'party4',
},
}
local Role = {
Member = 'Member',
Assist = 'Assist',
Leader = 'Leader',
}
local Faction = {
Horde = 'Horde',
Alliance = 'Alliance',
Neutral = 'Neutral',
None = nil,
}
Namespace.PlayerData.Role = Role
Namespace.PlayerData.Faction = Faction
local Memory = {
lastKnownGroupType = GetGroupType(),
AllPlayerData = {
--[GUID] = {
-- guid = GUID,
-- name = playerName,
-- firstName = playerName without realm,
-- realmName = normalized realm name,
-- units = {primary => first unit, first unit = true, second unit = true},
-- class = 'CLASS',
-- readyState = ReadyCheckState,
-- roleCheckStatus = RoleCheckStatus,
-- deserterExpiry = -1,
-- mercenaryExpiry = -1,
-- addonVersion = 'whatever remote version',
-- autoAcceptRole = boolean,
-- battlegroundStatus = BattlegroundStatus
-- isConnected = boolean,
-- role = Role,
-- class = "CLASS",
-- classColor = ColorMixin,
-- faction = Faction,
-- wantLead = boolean,
--},
},
LeaderData = nil, -- the AllPlayerData table for just the leader
AssistData = {
-- the AllPlayerData table for all assists
},
MembersData = {
-- the AllPlayerData table for all normal members
},
UnitPlayerData = {
-- same as AllPlayerData, but only those with <unit><index> like raid6
},
UnitIndexPlayerData = {
-- same as UnitPlayerData but indexed per unit, which means duplicate tables may exist
},
OnUpdateCallbacks = {
-- [name] => function,
},
OnRoleChangeCallbacks = {
-- [name] => function
},
}
--- per name callback with the argument being Memory.AllPlayerData (generated by RebuildPlayerData())
--- @param callback
function Namespace.PlayerData.RegisterOnUpdate(listenerName, callback)
Memory.OnUpdateCallbacks[listenerName] = callback
end
--- per name callback with the arguments being {PlayerData, oldRole, newRole} (generated by RebuildPlayerData())
--- @param callback
function Namespace.PlayerData.RegisterOnRoleChange(listenerName, callback)
Memory.OnRoleChangeCallbacks[listenerName] = callback
end
function Private.RefreshMissingData(data)
local unit = data.units.primary
if not unit then return end
if data.name == UNKNOWNOBJECT then
data.name = GetRealUnitName(unit)
end
if not data.class or not data.classColor then
local _, class = UnitClass(data.units.primary)
data.class = class
data.classColor = class and GetClassColor(class) or nil
end
if data.name ~= UNKNOWNOBJECT and (data.firstName == UNKNOWNOBJECT or not data.firstName or not data.realmName) then
data.firstName, data.realmName = strsplit('-', data.name, 2)
end
end
Namespace.PlayerData.RefreshMissingData = Private.RefreshMissingData
function Namespace.PlayerData.GetGroupLeaderData()
return Memory.LeaderData
end
function Private.GetUnitListForCurrentGroupType()
local groupType = GetGroupType()
if groupType == GroupType.InstanceRaid or groupType == GroupType.Raid then
return PlayerDataTargets.raid
end
if groupType == GroupType.InstanceParty or groupType == GroupType.Party then
return PlayerDataTargets.party
end
return PlayerDataTargets.solo
end
function Namespace.PlayerData.RebuildRoleData()
local groupType = GetGroupType()
if Memory.lastKnownGroupType ~= groupType then
Memory.LeaderData = nil
Memory.AssistData = {}
Memory.MembersData = {}
end
Memory.lastKnownGroupType = groupType
local roleChangeEvents = {}
local eventIndex = 0
local leader
local assists = {}
local members = {}
for guid, playerData in pairs(Memory.UnitPlayerData) do
local unit = playerData.units.primary
if UnitIsGroupLeader(unit) then
playerData.role = Role.Leader
leader = playerData
elseif UnitIsGroupAssistant(unit) then
playerData.role = Role.Assist
assists[guid] = playerData
else
playerData.role = Role.Member
members[guid] = playerData
end
local oldRole
if Memory.LeaderData == playerData then
oldRole = Role.Leader
elseif Memory.AssistData[guid] then
oldRole = Role.Assist
elseif Memory.MembersData[guid] then
oldRole = Role.Member
end
if oldRole ~= playerData.role then
eventIndex = eventIndex + 1
roleChangeEvents[eventIndex] = {playerData, oldRole, playerData.role}
end
end
for _, eventData in pairs(roleChangeEvents) do
local playerData, oldRole, newRole = unpack(eventData)
for _, callback in pairs(Memory.OnRoleChangeCallbacks) do
callback(playerData, oldRole, newRole)
end
end
Memory.LeaderData = leader
Memory.AssistData = assists
Memory.MembersData = members
end
function Namespace.PlayerData.RebuildPlayerData()
for _, data in pairs(Memory.AllPlayerData) do
data.units = {}
end
local unitIndexedPlayerData = {}
local unitPlayerData = {}
local currentTime = GetTime()
for _, unit in pairs(Private.GetUnitListForCurrentGroupType()) do
if UnitExists(unit) and UnitIsPlayer(unit) then
local dataIndex = UnitGUID(unit)
local data = Memory.AllPlayerData[dataIndex]
if not data then
local _, class = UnitClass(unit)
data = {
guid = dataIndex,
name = GetRealUnitName(unit),
readyState = ReadyCheckState.Nothing,
deserterExpiry = -1,
mercenaryExpiry = UnitIsMercenary(unit) and currentTime + 99999 or -1,
units = { primary = unit, [unit] = true },
battlegroundStatus = BattlegroundStatus.Nothing,
roleCheckStatus = RoleCheckStatus.Nothing,
isConnected = UnitIsConnected(unit),
role = UnitIsGroupLeader(unit) and Role.Leader or UnitIsGroupAssistant(unit) and Role.Assist or Role.Member,
class = class,
classColor = class and GetClassColor(class) or nil,
faction = UnitFactionGroup(unit),
wantLead = false,
}
Memory.AllPlayerData[dataIndex] = data
else
-- add every unit, but only recollect all information once
data.units[unit] = true
if not data.units.primary then
data.units.primary = unit
if (data.mercenaryExpiry == -1 or data.mercenaryExpiry < currentTime) and UnitIsMercenary(unit) then
data.mercenaryExpiry = currentTime + 99999
end
data.role = UnitIsGroupLeader(unit) and Role.Leader or UnitIsGroupAssistant(unit) and Role.Assist or Role.Member
data.isConnected = UnitIsConnected(unit)
if data.name == UNKNOWNOBJECT then data.name = GetRealUnitName(unit) end
if data.faction == Faction.None then data.faction = UnitFactionGroup(unit) end
end
end
unitPlayerData[dataIndex] = data
unitIndexedPlayerData[unit] = data
end
end
sort(unitPlayerData)
Memory.UnitIndexPlayerData = unitIndexedPlayerData
Memory.UnitPlayerData = unitPlayerData
for _, callback in pairs(Memory.OnUpdateCallbacks) do
callback(unitPlayerData)
end
Namespace.PlayerData.RebuildRoleData()
return unitPlayerData
end
function Namespace.PlayerData.GetPlayerDataByUnit(unit)
local data = Memory.UnitIndexPlayerData[unit]
if data then return data end
-- fallback to getting the name of the unit in case of weird scenarios
-- where "target" or "nameplate1" is sent
local name = GetRealUnitName(unit)
if not name then return nil end
return Namespace.PlayerData.GetPlayerDataByName(name)
end
function Namespace.PlayerData.GetPlayerDataByName(name)
local hasHyphen = name:find('-')
for _, data in pairs(Memory.AllPlayerData) do
Private.RefreshMissingData(data)
if not hasHyphen and data.firstName == name or data.name == name then
return data
end
end
return nil
end
function Namespace.PlayerData.ForEachPlayerData(callback)
for guid, playerData in pairs(Memory.AllPlayerData) do
if callback(playerData, guid) == false then return end
end
end
function Namespace.PlayerData.ForEachUnitData(callback)
for guid, playerData in pairs(Memory.UnitPlayerData) do
if callback(playerData, guid) == false then return end
end
end