-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerDB.lua
More file actions
197 lines (152 loc) · 4.17 KB
/
PlayerDB.lua
File metadata and controls
197 lines (152 loc) · 4.17 KB
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
--[[
cKick
Copyright(c) 2016, Tobias 'Chimaine' Rummelt, kontakt(at)rummelt-software.de
All rights reserved
]]
local ADDON_NAME, addon = ...
-- ----------------------------------------------------
function addon:CreatePlayerDB()
local instance = {}
local _players = {}
local _nPending = 0
local _pending = {}
local _callbacks = {}
-- ----------------------------------------------------
local function SetPrimarySpellInfo( info )
local primarySpell = addon.Spells:GetPrimarySpell( info.Class, info.Spec )
if ( primarySpell ) then
addon:Log( "Primary spell for %s,%s : %s", info.Class, info.Spec, primarySpell.ID )
info["PrimarySpell"] = primarySpell
info["PrimaryCooldown"] = primarySpell.DefaultCooldown
else
addon:Log( "No primary spell for %s,%s", info.Class, info.Spec )
info["PrimarySpell"] = nil
info["PrimaryCooldown"] = nil
end
end
local function GetPlayerNameAndClass( guid )
local _, classID, _, _, _, name, realm = GetPlayerInfoByGUID( guid )
if ( realm and ( string.len( realm ) > 0 ) ) then
name = name .. "-" .. realm
end
return name, classID
end
local function RetrievePlayerInfo( guid )
local name, classID = GetPlayerNameAndClass( guid )
local info = {
["GUID"] = guid,
["Name"] = name,
["Class"] = classID,
["Spec"] = 0,
["Role"] = "NONE",
["Inspected"] = false,
}
SetPrimarySpellInfo( info )
return info
end
local function RequestInspect( info, reset )
if ( info.Inspected and ( not reset ) ) then
return end
if ( _pending[info.GUID] ) then
return end
addon:Log( "Requesting inspect for %q", info.GUID )
info.Inspected = false
local doRequest = _nPending == 0
_nPending = _nPending + 1
_pending[info.GUID] = GetTime()
if ( doRequest ) then
NotifyInspect( info.Name )
end
end
function instance:RequestNextPending()
if ( _nPending <= 0 ) then
addon:Log( "No more pending requests" )
return end
local guid, v = next( _pending )
local info = _players[guid]
NotifyInspect( info.Name )
end
function instance:GetPlayerInfo( unitID )
if ( not UnitExists( unitID )
or not UnitIsPlayer( unitID )
or not UnitIsFriend( "player", unitID ) ) then
return end
local guid = UnitGUID( unitID )
if ( not guid ) then
return end
local info = _players[guid]
if ( not info ) then
info = RetrievePlayerInfo( guid )
_players[guid] = info
RequestInspect( info )
end
return info
end
function instance:GetPlayerInfoByGUID( guid )
local info = _players[guid]
if ( info ) then
return info end
local name = GetPlayerNameAndClass( guid )
return instance:GetPlayerInfo( name )
end
function instance:StartPlayerInfoUpdate( unitID, reset )
local guid = UnitGUID( unitID )
if ( not guid ) then
return end
local info = _players[guid]
if ( not info ) then
return end
RequestInspect( info, reset )
end
function instance:StartMissingInfoUpdates()
for _, info in next, _players do
RequestInspect( info )
end
end
function instance:UpdatePlayerInfo( guid )
local info = _players[guid]
if ( not info ) then
return end
if ( info.Inspected ) then
return end
addon:Log( "Inspection request took " .. ( GetTime() - _pending[guid] ) .. " seconds" )
_pending[guid] = nil
_nPending = _nPending - 1
local specID = GetInspectSpecialization( info.Name )
local _, name, _, _, _, role, _ = GetSpecializationInfoByID( specID )
addon:Log( "%q spec: %s (ID %s, Role %s)", guid, name, specID, role )
info.Spec = specID
info.Role = role
SetPrimarySpellInfo( info )
if ( specID == 0 ) then
addon:Log( "GetInspectSpecialization failed for %q:%q", guid, info.Name )
return end
info.Inspected = true
if ( _nPending == 0 ) then
if ( #_callbacks > 0 ) then
repeat
table.remove( _callbacks )()
until ( #_callbacks == 0 )
end
end
return true
end
function instance:RemovePlayer( guid )
_players[guid] = nil
end
function instance:Clear()
_players = {}
end
function instance:GetPlayerInfos()
return _players
end
function instance:RegisterCallback( f )
if ( next( _pending ) ) then
table.insert( _callbacks, f )
else
f()
end
end
instance:GetPlayerInfo( "player" )
return instance
end