-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMultiBotBankFrame.lua
More file actions
500 lines (415 loc) · 15.1 KB
/
Copy pathMultiBotBankFrame.lua
File metadata and controls
500 lines (415 loc) · 15.1 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
if not MultiBot then
return
end
local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
local BANK_FRAME_WIDTH = 340
local BANK_FRAME_HEIGHT = 420
local BANK_FRAME_X = -360
local BANK_ROW_WIDTH = 300
local BANK_TEXT_WIDTH = 210
local BANK_WITHDRAW_BUTTON_WIDTH = 70
local BANK_WITHDRAW_BUTTON_OFFSET_X = -8
local BANK_REFRESH_DELAY = 0.65
local function L(key, fallback)
if MultiBot and type(MultiBot.L) == "function" then
return MultiBot.L(key, fallback)
end
return fallback or key
end
local function setButtonEnabled(button, enabled)
if not button then
return
end
if enabled then
if button.Enable then button:Enable() end
if button.SetAlpha then button:SetAlpha(1) end
else
if button.Disable then button:Disable() end
if button.SetAlpha then button:SetAlpha(0.45) end
end
end
local function addSimpleBackdrop(frame, bgAlpha)
if not frame or not frame.SetBackdrop then
return
end
frame:SetBackdrop({
bgFile = "Interface\\Buttons\\WHITE8x8",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true,
tileSize = 16,
edgeSize = 14,
insets = { left = 3, right = 3, top = 3, bottom = 3 },
})
if frame.SetBackdropColor then
frame:SetBackdropColor(0.06, 0.06, 0.08, bgAlpha or 0.90)
end
if frame.SetBackdropBorderColor then
frame:SetBackdropBorderColor(0.35, 0.35, 0.35, 0.95)
end
end
local function createWindow(name, title, width, height, pointX)
if AceGUI then
local widget = AceGUI:Create("Window")
widget:SetTitle(title)
widget:SetLayout("Fill")
widget:SetWidth(width)
widget:SetHeight(height)
widget.frame:SetPoint("CENTER", UIParent, "CENTER", pointX or 0, 0)
widget.frame:SetFrameStrata("DIALOG")
widget:EnableResize(false)
if widget.content then
addSimpleBackdrop(widget.content, 0.90)
end
return widget
end
local frame = CreateFrame("Frame", name, UIParent, "BasicFrameTemplateWithInset")
frame:SetSize(width, height)
frame:SetPoint("CENTER", UIParent, "CENTER", pointX or 0, 0)
frame:SetFrameStrata("DIALOG")
frame.title = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
frame.title:SetPoint("TOP", 0, -7)
frame.title:SetText(title)
frame.content = CreateFrame("Frame", nil, frame)
frame.content:SetPoint("TOPLEFT", frame, "TOPLEFT", 10, -30)
frame.content:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -10, 10)
addSimpleBackdrop(frame.content, 0.90)
return {
frame = frame,
content = frame.content,
SetTitle = function(self, value) self.frame.title:SetText(value) end,
Show = function(self) self.frame:Show() end,
Hide = function(self) self.frame:Hide() end,
IsShown = function(self) return self.frame:IsShown() end,
}
end
local function getFrameContent(window)
if window and window.content then
return window.content
end
return window and window.frame or nil
end
local function setWindowTitle(window, title)
if not window then
return
end
if window.SetTitle then
window:SetTitle(title)
elseif window.title then
window.title:SetText(title)
end
end
local function createText(parent, font, point, x, y)
local text = parent:CreateFontString(nil, "OVERLAY", font)
text:SetPoint(point, parent, point, x, y)
text:SetJustifyH("LEFT")
return text
end
local function systemMessage(message)
message = tostring(message or "")
if message == "" then
return
end
if DEFAULT_CHAT_FRAME and DEFAULT_CHAT_FRAME.AddMessage then
DEFAULT_CHAT_FRAME:AddMessage(message)
elseif print then
print(message)
end
end
local function parseBankItemLine(line)
line = tostring(line or "")
local itemId = tonumber(string.match(line, "item:(%d+)") or "0") or 0
if itemId <= 0 then
return nil
end
local name, link, _, _, _, _, _, _, _, icon = GetItemInfo(itemId)
if not icon and GetItemIcon then
icon = GetItemIcon(itemId)
end
local count = tonumber(string.match(line, "rx(%d+)") or string.match(line, "x(%d+)") or "1") or 1
if count < 1 then
count = 1
end
return {
itemId = itemId,
name = name or ("item:" .. itemId),
link = link or line,
icon = icon or "Interface\\Icons\\INV_Misc_QuestionMark",
count = count,
line = line,
}
end
local function getBankReasonText(reason)
reason = tostring(reason or "")
if reason == "" or reason == "OK" then
return ""
end
return L("info.inventory.item_action.reason." .. reason, reason)
end
local function getBankModeTitle(mode)
if mode == "gbank" then
return L("inventory.gbank.title", "Guild Bank")
end
return L("inventory.bank.title", "Bot Bank")
end
local function getBankModeLoadingText(mode)
if mode == "gbank" then
return L("inventory.gbank.loading", "Loading guild bank...")
end
return L("inventory.bank.loading", "Loading bank...")
end
local function getBankModeCountText(mode)
if mode == "gbank" then
return L("inventory.gbank.count", "guild bank item(s)")
end
return L("inventory.bank.count", "bank item(s)")
end
local function getBankModeBridgeRequiredText(mode)
if mode == "gbank" then
return L("inventory.gbank.bridge.required", "Guild bank bridge is not connected.")
end
return L("inventory.bank.bridge.required", "Bank bridge is not connected.")
end
local function getWithdrawActionForMode(mode)
if mode == "gbank" then
return "GBANK_WITHDRAW"
end
return "BANK_WITHDRAW"
end
local function canWithdrawFromFrame(frame)
if not frame then
return false
end
if frame.mode == "gbank" then
return frame.gbankCanWithdraw == true
end
return true
end
local function ensureBankFrame()
if MultiBot.bankFrame then
return MultiBot.bankFrame
end
local frame = createWindow("MultiBotBankFrame", L("inventory.bank.title", "Bot Bank"), BANK_FRAME_WIDTH, BANK_FRAME_HEIGHT, BANK_FRAME_X)
if not frame then
return nil
end
local content = getFrameContent(frame)
frame.status = createText(content, "GameFontHighlightSmall", "TOPLEFT", 18, -10)
frame.rows = {}
frame.items = {}
frame.page = 1
frame.pageSize = 12
for i = 1, frame.pageSize do
local row = CreateFrame("Button", nil, content)
row:SetPoint("TOPLEFT", content, "TOPLEFT", 18, -32 - ((i - 1) * 27))
row:SetWidth(BANK_ROW_WIDTH)
row:SetHeight(24)
row.icon = row:CreateTexture(nil, "ARTWORK")
row.icon:SetPoint("LEFT", row, "LEFT", 0, 0)
row.icon:SetWidth(22)
row.icon:SetHeight(22)
row.text = createText(row, "GameFontHighlightSmall", "LEFT", 28, 0)
row.text:SetWidth(BANK_TEXT_WIDTH)
row.text:SetHeight(22)
row.withdrawButton = CreateFrame("Button", nil, row, "UIPanelButtonTemplate")
row.withdrawButton:SetPoint("RIGHT", row, "RIGHT", BANK_WITHDRAW_BUTTON_OFFSET_X, 0)
row.withdrawButton:SetWidth(BANK_WITHDRAW_BUTTON_WIDTH)
row.withdrawButton:SetHeight(20)
row.withdrawButton:SetText(L("inventory.bank.withdraw", "Withdraw"))
row.withdrawButton:SetScript("OnClick", function()
local item = row.item
local action = getWithdrawActionForMode(frame.mode)
if not item or not frame.botName or not action then
return
end
if not canWithdrawFromFrame(frame) then
frame.status:SetText(getBankReasonText("NO_GUILD_BANK_RIGHTS"))
return
end
if MultiBot.Comm and MultiBot.Comm.RunInventoryItemAction then
local token = MultiBot.Comm.RunInventoryItemAction(frame.botName, action, item.itemId, 0)
if token then
frame.status:SetText(L("inventory.bank.withdraw.pending", "Withdraw requested..."))
setButtonEnabled(row.withdrawButton, false)
return
end
end
frame.status:SetText(L("inventory.bank.withdraw.failed", "Withdraw request failed."))
end)
row:SetScript("OnEnter", function(self)
if not self.item or not GameTooltip then return end
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
if self.item.link and string.sub(self.item.link, 1, 1) == "|" then
GameTooltip:SetHyperlink(self.item.link)
else
GameTooltip:SetText(self.item.name or "")
end
GameTooltip:Show()
end)
row:SetScript("OnLeave", function()
if GameTooltip then GameTooltip:Hide() end
end)
frame.rows[i] = row
end
frame.prev = CreateFrame("Button", nil, content, "UIPanelButtonTemplate")
frame.prev:SetPoint("BOTTOMLEFT", content, "BOTTOMLEFT", 18, 8)
frame.prev:SetWidth(48)
frame.prev:SetHeight(20)
frame.prev:SetText("<")
frame.pageText = createText(content, "GameFontNormalSmall", "BOTTOM", 0, 12)
frame.next = CreateFrame("Button", nil, content, "UIPanelButtonTemplate")
frame.next:SetPoint("BOTTOMRIGHT", content, "BOTTOMRIGHT", -18, 8)
frame.next:SetWidth(48)
frame.next:SetHeight(20)
frame.next:SetText(">")
frame.render = function(self)
local maxPage = math.max(1, math.ceil(#self.items / self.pageSize))
if self.page > maxPage then self.page = maxPage end
if self.page < 1 then self.page = 1 end
self.pageText:SetText(self.page .. "/" .. maxPage)
setButtonEnabled(self.prev, self.page > 1)
setButtonEnabled(self.next, self.page < maxPage)
local from = ((self.page - 1) * self.pageSize) + 1
for i = 1, self.pageSize do
local row = self.rows[i]
local item = self.items[from + i - 1]
row.item = item
if item then
row.icon:SetTexture(MultiBot.SafeTexturePath(item.icon))
row.text:SetText((item.name or ("item:" .. item.itemId)) .. " |cff999999x" .. tostring(item.count or 1) .. "|r")
if getWithdrawActionForMode(self.mode) then
row.withdrawButton:SetText(L("inventory.bank.withdraw", "Withdraw"))
setButtonEnabled(row.withdrawButton, canWithdrawFromFrame(self))
row.withdrawButton:Show()
else
row.withdrawButton:Hide()
end
row:Show()
else
row.withdrawButton:Hide()
row:Hide()
end
end
end
frame.prev:SetScript("OnClick", function()
frame.page = frame.page - 1
frame:render()
end)
frame.next:SetScript("OnClick", function()
frame.page = frame.page + 1
frame:render()
end)
frame.setItems = function(self, botName, lines, errorReason, mode, guildBankRights)
self.botName = botName
self.mode = mode or self.mode or "bank"
self.gbankCanWithdraw = self.mode == "gbank" and type(guildBankRights) == "table" and guildBankRights.canWithdraw == true
self.gbankWithdrawRemaining = self.mode == "gbank" and type(guildBankRights) == "table" and tonumber(guildBankRights.remaining or 0) or 0
self.items = {}
self.page = 1
setWindowTitle(self, getBankModeTitle(self.mode) .. " - " .. tostring(botName or ""))
if errorReason and errorReason ~= "" then
self.status:SetText(getBankReasonText(errorReason))
else
for _, line in ipairs(lines or {}) do
local item = parseBankItemLine(line)
if item then
table.insert(self.items, item)
end
end
self.status:SetText(#self.items .. " " .. getBankModeCountText(self.mode))
end
self:render()
self:Show()
end
frame:Hide()
MultiBot.bankFrame = frame
return frame
end
function MultiBot.OpenBotBank(botName)
botName = tostring(botName or "")
if botName == "" then
return false
end
local frame = ensureBankFrame()
frame.botName = botName
frame.mode = "bank"
frame.gbankCanWithdraw = false
frame.gbankWithdrawRemaining = 0
frame.items = {}
frame.page = 1
setWindowTitle(frame, getBankModeTitle(frame.mode) .. " - " .. botName)
frame.status:SetText(getBankModeLoadingText(frame.mode))
frame:render()
frame:Show()
if MultiBot.Comm and MultiBot.Comm.RequestBank then
return MultiBot.Comm.RequestBank(botName)
end
systemMessage(getBankModeBridgeRequiredText(frame.mode))
return false
end
function MultiBot.OpenBotGuildBank(botName)
botName = tostring(botName or "")
if botName == "" then
return false
end
local frame = ensureBankFrame()
frame.botName = botName
frame.mode = "gbank"
frame.gbankCanWithdraw = false
frame.gbankWithdrawRemaining = 0
frame.items = {}
frame.page = 1
setWindowTitle(frame, getBankModeTitle(frame.mode) .. " - " .. botName)
frame.status:SetText(getBankModeLoadingText(frame.mode))
frame:render()
frame:Show()
if MultiBot.Comm and MultiBot.Comm.RequestGuildBank then
return MultiBot.Comm.RequestGuildBank(botName)
end
systemMessage(getBankModeBridgeRequiredText(frame.mode))
return false
end
function MultiBot.RefreshBotBank(botName, delay)
local frame = MultiBot.bankFrame
botName = tostring(botName or (frame and frame.botName) or "")
if botName == "" or not frame or frame.mode == "gbank" or not frame.IsShown or not frame:IsShown() then
return false
end
local function refresh()
if frame and frame.IsShown and frame:IsShown() and MultiBot.Comm and MultiBot.Comm.RequestBank then
MultiBot.Comm.RequestBank(botName)
end
end
if type(MultiBot.TimerAfter) == "function" then
MultiBot.TimerAfter(delay or BANK_REFRESH_DELAY, refresh)
else
refresh()
end
return true
end
function MultiBot.RefreshBotGuildBank(botName, delay)
local frame = MultiBot.bankFrame
botName = tostring(botName or (frame and frame.botName) or "")
if botName == "" or not frame or frame.mode ~= "gbank" or not frame.IsShown or not frame:IsShown() then
return false
end
local function refresh()
if frame and frame.IsShown and frame:IsShown() and MultiBot.Comm and MultiBot.Comm.RequestGuildBank then
MultiBot.Comm.RequestGuildBank(botName)
end
end
if type(MultiBot.TimerAfter) == "function" then
MultiBot.TimerAfter(delay or BANK_REFRESH_DELAY, refresh)
else
refresh()
end
return true
end
function MultiBot.OnBridgeBankItems(botName, lines, errorReason)
ensureBankFrame():setItems(botName, lines or {}, errorReason, "bank")
end
function MultiBot.OnBridgeGuildBankItems(botName, lines, errorReason, token, guildBankRights)
ensureBankFrame():setItems(botName, lines or {}, errorReason, "gbank", guildBankRights)
end
function MultiBot.InitializeBankFrame()
ensureBankFrame()
end