-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleNetTools.lua
209 lines (184 loc) · 5.05 KB
/
BattleNetTools.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
--[[
* 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 select = select
local tinsert = table.insert
local tonumber = tonumber
-- API Upvalues
local BNGetToonInfo = BNGetToonInfo
local BNGetNumFriends = BNGetNumFriends
local BNGetFriendInfo = BNGetFriendInfo
local BNGetFriendInfoByID = BNGetFriendInfoByID
local BNGetFriendToonInfo = BNGetFriendToonInfo
local BNGetNumFriendToons = BNGetNumFriendToons
local C = Command
C.BattleNetTools = {}
local BNT = C.BattleNetTools
local CET = C.Extensions.Table
local CES = C.Extensions.String
local FRIEND_RETURN_ARGC = 16
local TOON_RETURN_ARGC = 16
local FRIENDTOON_RETURN_ARGC = 17
local function ParseBNFriendResult(...)
local argc = select("#", ...)
if argc ~= FRIEND_RETURN_ARGC then
error("ParseBNFriendResult expected " .. FRIEND_RETURN_ARGC .. " arguments, got " .. argc)
return nil
end
local id, pName, bTag, isBTagPresence, tName, tId, client, online, last, afk, dnd, bText, note, friend, bTime, sor = ...
local f = {
PresenceID = id,
PresenceName = pName,
BattleTag = bTag,
IsBattleTagPresence = isBTagPresence,
ToonName = tName,
ToonID = tID,
Client = client,
IsOnline = online,
LastOnline = last,
IsAFK = afk,
IsDND = dnd,
BroadcastText = bText,
NoteText = note,
IsFriend = friend,
BroadcastTime = bTime,
CanSoR = sor
}
return f
end
local function ParseBNToonResult(...)
local argc = select("#", ...)
if argc ~= TOON_RETURN_ARGC then
error("ParseBNToonResult expected " .. TOON_RETURN_ARGC .. " arguments, got " .. argc)
return nil
end
local focus, name, client, realm, realmId, faction, race, class, guild, zone, lvl, text, bText, bTime, online, id = ...
local t = {
HasFocus = focus,
Name = name,
Client = client,
Realm = realm,
RealmID = realmId,
Faction = faction,
Race = race,
Class = class,
Guild = guild,
Zone = zone,
Level = tonumber(lvl),
GameText = text,
BroadcastText = bText,
BroadcastTime = bTime,
IsOnline = online,
PresenceID = id
}
if t.Faction == FACTION_HORDE then
t.FactionID = 0
elseif t.Faction == FACTION_ALLIANCE then
t.FactionID = 1
else
t.FactionID = -1
end
return t
end
local function ParseBNFriendToonResult(...)
local argc = select("#", ...)
if argc ~= FRIENDTOON_RETURN_ARGC then
error("ParseBNFriendToonResult expected " .. FRIENDTOON_RETURN_ARGC .. " arguments, got " .. argc)
return nil
end
local focus, name, client, realm, realmId, faction, race, class, guild, zone, lvl, text, bText, unknown, bTime, canSoR, id = ...
local ft = {
HasFocus = focus,
Name = name,
Client = client,
Realm = realm,
RealmID = realmId,
Faction = faction,
Race = race,
Class = class,
Guild = guild,
Zone = zone,
Level = lvl,
GameText = text,
BroadcastText = bText,
Unknown = unknown,
BroadcastTime = bTime,
CanSoR = canSoR,
ToonID = id
}
if ft.Faction == FACTION_HORDE then
ft.FactionID = 0
elseif ft.Faction == FACTION_ALLIANCE then
ft.FactionID = 1
else
ft.FactionID = -1
end
end
function BNT:GetFriend(index)
if index < 1 or index > BNGetNumFriends() then
return nil
end
return ParseBNFriendResult(BNGetFriendInfo(index))
end
function BNT:GetFriendById(id)
return ParseBNFriendResult(BNGetFriendInfoByID(id))
end
function BNT:GetFriendByName(name, startIndex)
local n = BNGetNumFriends()
if n <= 0 then return nil end
for i = startIndex or 1, n do
local friend = ParseBNFriendResult(BNGetFriendInfo(i))
if (friend.ToonName or ""):lower() == name:lower() then
return friend, i
end
end
return nil
end
function BNT:GetAllFriendsByName(name)
local result = {}
local friend, lastIndex
repeat
friend, lastIndex = self:GetFriendByName(name, (lastIndex or 0) + 1)
if friend then tinsert(result, friend) end
until friend == nil
return result
end
function BNT:GetToon(id)
return ParseBNToonResult(BNGetToonInfo(id))
end
function BNT:GetToonByName(name, startIndex)
local numF = BNGetNumFriends()
if numF <= 0 then return nil end
for i = startIndex or 1, numF do
for t = 1, BNGetNumFriendToons(i) do
local toon = ParseBNToonResult(BNGetFriendToonInfo(i, t))
if not CES:IsNullOrEmpty(toon.Name) and toon.Name:lower() == name:lower() then return toon, i end
end
end
return nil
end
function BNT:GetAllToonsByName(name)
local result = {}
local toon, lastIndex
repeat
toon, lastIndex = self:GetToonByName(name, (lastIndex or 0) + 1)
if toon then tinsert(result, toon) end
until toon == nil
return result
end