-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCore.lua
More file actions
575 lines (497 loc) · 18.1 KB
/
Core.lua
File metadata and controls
575 lines (497 loc) · 18.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
--[[
------------------------------------------------------------------------
Project: GuildTithe
File: Core rev. 120
Date: 2014-10-17T03:32:15Z
Purpose: Core Addon Code
Credits: Code written by Vandesdelca32
Copyright (C) 2011 Vandesdelca32
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
------------------------------------------------------------------------
]]
-- Import engine and the localization table.
local addonName, _ = ...
local E, L = unpack(select(2, ...))
--debugArgs: Returns literal "nil" or the tostring of all of the arguments passed to it.
function E:debugArgs(...)
local tmp = {}
for i = 1, select("#", ...) do
tmp[i] = tostring(select(i, ...)) or "nil"
end
return table.concat(tmp, ", ")
end
-- Get a string for the current version of the addon.
function E:GetVerString()
local v, rev = (GetAddOnMetadata(addonName, "VERSION") or "???"), (tonumber('120') or "???")
--[===[@debug@
-- If this code is run, it's an unpackaged version, show this:
if v == "release_v2.5.15" then v = "DEV_VERSION"; end
--@end-debug@]===]
if short then
-- Try to discern what release stage:
if strfind(v, "release") then
return "r" .. rev
elseif strfind(v, "beta") then
return "b" .. rev
else
return "a" .. rev
end
end
return v .. "." .. rev
end
local SettingsDefaults = {
SettingsVer = 2,
CollectSource = {
Quest = -1,
Merchant = -1,
Mail = -1,
Loot = -1,
Trade = -1
},
AutoDeposit = true,
Spammy = false,
TotalTithe = 0,
CurrentTithe = 0,
MiniFrameShown = true,
MiniFrameLocked = false,
SkinElvUI = true,
LDBDisplayTotal = false,
}
-- Get the coin string for the databroker icon, because it needs to be shorter.
local function GetLDBCoinString()
local text = ""
local ct = GuildTithe_SavedDB.CurrentTithe
local tt = GuildTithe_SavedDB.TotalTithe
if GuildTithe_SavedDB.LDBDisplayTotal then
local gold = floor(tt / COPPER_PER_GOLD)
local silver = floor(tt / SILVER_PER_GOLD) % COPPER_PER_SILVER
local copper = floor(tt % COPPER_PER_SILVER)
if gold ~= 0 then
text = format("%s|cffffd700g|r %s|cffc7c7cfs|r %s|cffeda55fc|r", gold, silver, copper)
elseif silver ~= 0 then
text = format("%s|cffc7c7cfs|r %s|cffeda55fc|r", silver, copper)
else
text = format("%s|cffeda55fc|r", copper)
end
else
local gold = floor(ct / COPPER_PER_GOLD)
local silver = floor(ct / SILVER_PER_GOLD) % COPPER_PER_SILVER
local copper = floor(ct % COPPER_PER_SILVER)
if gold ~= 0 then
text = format("%s|cffffd700g|r %s|cffc7c7cfs|r %s|cffeda55fc|r", gold, silver, copper)
elseif silver ~= 0 then
text = format("%s|cffc7c7cfs|r %s|cffeda55fc|r", silver, copper)
else
text = format("%s|cffeda55fc|r", copper)
end
end
return text
end
function E:Init()
--[===[@alpha@
E._DebugMode = true
--@end-alpha@]===]
E.Info = {}
-- Set up the LDB datastream
local t = {
type = "data source",
text = "",
icon = "Interface\\ICONS\\inv_misc_coin_17.blp",
label = "Tithe",
OnClick = function(frame, button)
GameTooltip:Hide()
if button == "LeftButton" then
E.FrameScript_MiniTitheFrameOnClick()
elseif button == "RightButton" then
if not GuildTithe_SavedDB.LDBDisplayTotal then
GuildTithe_SavedDB.LDBDisplayTotal = true
else
GuildTithe_SavedDB.LDBDisplayTotal = false
end
end
end,
OnTooltipShow = function(tooltip)
if tooltip and tooltip.AddLine then
tooltip:SetText("GuildTithe")
if GuildTithe_SavedDB.LDBDisplayTotal then
tooltip:AddLine(L.TooltipLDBDescriptionTotal, 0.8, 0.8, 0.8, 1)
else
tooltip:AddLine(L.TooltipLDBDescriptionCurrent, 0.8, 0.8, 0.8, 1)
end
tooltip:AddLine("\nHint: Left-Click with the guild bank or a letter open to deposit your tithe! Right-Click to toggle between total and current tithes.", 0, 1, 0, 1)
tooltip:Show()
end
end,
}
E.Info.LDBData = LibStub:GetLibrary("LibDataBroker-1.1"):NewDataObject("GuildTithe", t)
-- Check the settings:
if not GuildTithe_SavedDB or GuildTithe_SavedDB.SettingsVer < SettingsDefaults.SettingsVer then
GuildTithe_SavedDB = SettingsDefaults
end
-- Load the frames
GT_MiniTitheFrame:EnableMouse(not GuildTithe_SavedDB.MiniFrameLocked)
if GuildTithe_SavedDB.MiniFrameShown then
GT_MiniTitheFrame:Show()
end
end
function E:ResetWindowSettings()
GT_OptionsFrame:SetUserPlaced(false)
GT_MiniTitheFrame:SetUserPlaced(false)
ReloadUI()
end
function E:ResetCurrentTithe()
GuildTithe_SavedDB.CurrentTithe = 0
end
-- Reset default settings
function E:ResetConfig()
GuildTithe_SavedDB = SettingsDefaults
if GT_OptionsFrame:IsShown() then
self.UpdateOptions(GT_OptionsFrame)
end
return self:ResetWindowSettings()
end
-- This is used to check the special tithe amounts for merchant/mail/trade.
-- They all need the same function, but have slightly different methods. THis way is faster.
local function CheckSpecialTitheAmounts(source, startGold, finishGold)
local titheAmount
-- Get the 'difference' in money
local diff
diff = finishGold - startGold
-- Check for profit
if diff <= 0 then
diff = 0
end
-- Update
titheAmount = diff
return titheAmount or 0
end
-- Quest, Merchant, Mail, Loot, Trade
-- Updates the current tithe, passing update will update the totals.
function E:UpdateOutstandingTithe(source, update, ...)
self:PrintDebug("UpdateOutstandingTithe(" .. self:debugArgs(source, update, ...) .. ")")
if not update then
self.Info.CurrentGold = GetMoney()
return
else
self.Info.FinishGold = GetMoney()
end
self:PrintDebug(" CurrentGold = " .. self:debugArgs(self.Info.CurrentGold) .. ", FinishGold = " .. self:debugArgs(self.Info.FinishGold))
-- Try to extract what GOLD, SILVER, and COPPER are in the client language
local GOLD = strmatch(format(GOLD_AMOUNT, 20), "%d+%s(.+)") -- This will return what's in the brackets, which on enUS would be "Gold"
local SILVER = strmatch(format(SILVER_AMOUNT, 20), "%d+%s(.+)")
local COPPER = strmatch(format(COPPER_AMOUNT, 20), "%d+%s(.+)")
---self:PrintDebug(self:debugArgs(GOLD, SILVER, COPPER))
-- Make some storage for the tithe amount
local titheAmount
-- Checking the source of the money event
-- From Loot, Quest
if source == "Loot" or source == "Quest" then
local arg1 = ...
--Try and parse the current amount from the string.
local g = tonumber(arg1:match("(%d+)%s" .. GOLD)) or 0
local s = tonumber(arg1:match("(%d+)%s" .. SILVER)) or 0
local c = tonumber(arg1:match("(%d+)%s" .. COPPER)) or 0
titheAmount = (g * COPPER_PER_GOLD) + (s * SILVER_PER_GOLD) + c
-- From Merchants, or Mail, or Trade
elseif source == "Merchant" or source == "Mail" or source == "Trade" then
-- Check and make a table for cooldown info storage
if not E.Info.checkDelays then
E.Info.checkDelays = {}
end
--Check the cooldown timer, we're gonna hardcode 2s
if (not E.Info.checkDelays[source]) or (GetTime() >= E.Info.checkDelays[source] + 2) then
E.Info.checkDelays[source] = GetTime()
-- Sanity checks.
if not (self.Info.FinishGold and self.Info.CurrentGold) then
E:PrintDebug("Invalid Type, proper information not available", true)
return
end
-- Is this a trade? It needs special conditions
if source == "Trade" then
-- Trade is a bit laggy, so we pass the guessed gold amount over through this
-- and assume that the finishgold is == currentgold+ trade amount.
local arg1 = ...
if tonumber(arg1) and tonumber(arg1) >= 0 then
self:PrintDebug(" tradeGold = " .. tonumber(arg1))
self.Info.FinishGold = self.Info.CurrentGold + tonumber(arg1)
end
end
titheAmount = CheckSpecialTitheAmounts(source, self.Info.CurrentGold, self.Info.FinishGold)
else
self:PrintDebug("ON ".. source .." CD", true)
return
end
end
self:PrintDebug(" titheAmount = " .. tostring(titheAmount))
-- Do some maths
if (GuildTithe_SavedDB.CollectSource[source]
and GuildTithe_SavedDB.CollectSource[source] >= 1) then
-- Collecting from this source, get the actual amount we're taking from this
local actualTithe = floor((titheAmount * (GuildTithe_SavedDB.CollectSource[source] / 100)))
self:PrintDebug(" actualTithe = " .. tostring(actualTithe))
GuildTithe_SavedDB.CurrentTithe = GuildTithe_SavedDB.CurrentTithe + actualTithe
-- Clear the info table for the next guy
self.Info.CurrentGold = nil
self.Info.FinishGold = nil
-- Tell the user what we collected if they want to know
if GuildTithe_SavedDB.Spammy and actualTithe > 0 then
self:PrintMessage(format(L.ChatSpammyCollectedAmount, GetCoinTextureString(actualTithe), strlower(source)))
end
else
self:PrintDebug(" " .. source .. " not collecting", true)
if GuildTithe_SavedDB.Spammy then
self:PrintMessage(format(L.ChatSpammyNotCollectingSource, source))
end
end
end
-- Handles depositing the tithe
function E:DepositTithe(clicked, isMail)
self:PrintDebug("DepositTithe(".. self:debugArgs(clicked, isMail) .. ") -- ACTUAL DEPOSIT IS DISABLED IN DEBUG MODE")
if not clicked and not GuildTithe_SavedDB.AutoDeposit then
if GuildTithe_SavedDB.Spammy then
self:PrintMessage(L.ChatAutoDepositDisabled)
end
self:PrintDebug(" AutoDeposit Disabled")
return
end
-- Sanity Check, (stop the error speech when trying to deposit 0c)
if GuildTithe_SavedDB.CurrentTithe == 0 then
if GuildTithe_SavedDB.Spammy then
self:PrintMessage(L.ChatNothingToDeposit, true)
end
return
end
-- Make sure the player has enough money first, then spam them out if they don't!
if GetMoney() < GuildTithe_SavedDB.CurrentTithe then
self:PrintMessage(L["ChatNotEnoughFunds"], true)
return
end
-- Deposit the money, then reset CurrentTithe and update TotalTithe
if not self.DebugMode then
if isMail then
-- postal fix
local goldAmount = floor(GuildTithe_SavedDB.CurrentTithe / COPPER_PER_GOLD)
local silverAmount = floor((GuildTithe_SavedDB.CurrentTithe % COPPER_PER_GOLD) / COPPER_PER_SILVER)
local copperAmount = floor(GuildTithe_SavedDB.CurrentTithe % COPPER_PER_SILVER)
SendMailMoneyGold:SetText(goldAmount)
SendMailMoneySilver:SetText(silverAmount)
SendMailMoneyCopper:SetText(copperAmount)
else
DepositGuildBankMoney(GuildTithe_SavedDB.CurrentTithe)
end
end
if GuildTithe_SavedDB.Spammy then
self:PrintMessage(format(L.ChatDepositTitheAmount, GetCoinTextureString(GuildTithe_SavedDB.CurrentTithe)))
end
self:PrintDebug(" Deposit amount = " .. GuildTithe_SavedDB.CurrentTithe)
GuildTithe_SavedDB.TotalTithe = GuildTithe_SavedDB.TotalTithe + GuildTithe_SavedDB.CurrentTithe
if not GuildTithe_SavedDB.LDBDisplayTotal or not E.ShowTotalTimer then
GuildTithe_SavedDB.LDBDisplayTotal = true
E.ShowTotalTimer = E:SetTimer(10, function() GuildTithe_SavedDB.LDBDisplayTotal = false; end)
end
GuildTithe_SavedDB.CurrentTithe = 0
end
local numHelpLines = 10
-- Print Help
function E:PrintHelpMessages()
for i = 1, numHelpLines do
if i == 1 then
self:PrintMessage(format(L["ChatHelpLine1"], self:GetVerString()))
else
self:PrintMessage(L["ChatHelpLine" .. i])
end
end
end
-- Handles slash commands
function E:OnChatCommand(msg)
self:PrintDebug("OnChatCommand(" .. self:debugArgs(msg) .. ")")
local cmd, args = strsplit(" ", strlower(msg))
self:PrintDebug("cmd = " .. tostring(cmd) .. ", args = " .. tostring(args))
if msg == "" or cmd == "help" then
-- Print Help
self:PrintHelpMessages()
-- want to reset settings?
elseif cmd == "reset" then
if not args or args == "tithe" then
StaticPopup_Show("GUILDTITHE_RESETTITHE")
elseif args == "pos" then
self:ResetWindowSettings()
elseif args == "config" then
StaticPopup_Show("GUILDTITHE_RESETCONFIG")
else
self:PrintMessage(format(L.ChatArgNotFound, args or "", cmd), true)
end
-- Show the options frame, we don't save this
elseif cmd == "options" or cmd == "config" then
GT_OptionsFrame:Show()
-- Get the current tithe
elseif cmd == "current" or cmd == "tithe" then
self:PrintMessage(L.ChatOutstandingTithe, GetCoinTextureString(GuildTithe_SavedDB.CurrentTithe))
-- Show/hide or toggle the mini frame.
elseif cmd == "mini" then
-- Show or hide the mini frame, this can be forced, or toggled when passed with no args
if args == "show" then
GT_MiniTitheFrame:Show()
GuildTithe_SavedDB.MiniFrameShown = true
elseif args == "hide" then
GT_MiniTitheFrame:Hide()
GuildTithe_SavedDB.MiniFrameShown = false
-- Lock the mini-frame
elseif args == "lock" then -- This is a toggle
if GuildTithe_SavedDB.MiniFrameLocked then
self:PrintMessage(L.ChatMiniFrameUnlock)
GT_MiniTitheFrame:EnableMouse(true)
GuildTithe_SavedDB.MiniFrameLocked = false
else
self:PrintMessage(L.ChatMiniFrameLock)
GT_MiniTitheFrame:EnableMouse(false)
GuildTithe_SavedDB.MiniFrameLocked = true
end
else -- No args clause, toggle.
if GuildTithe_SavedDB.MiniFrameShown then
GT_MiniTitheFrame:Hide()
GuildTithe_SavedDB.MiniFrameShown = false
else
GT_MiniTitheFrame:Show()
GuildTithe_SavedDB.MiniFrameShown = true
end
end
elseif cmd == "total" then
self:PrintMessage(format(L.OptionsTotalTitheText, GetCoinTextureString(GuildTithe_SavedDB.TotalTithe)))
else
-- This is where we're going to actually print the help info... Later though.
self:PrintMessage(format(L.ChatCommandNotFound, msg), true)
end
end
function GuildTithe_OnLoad(self)
-- Register events
self:RegisterEvent("ADDON_LOADED")
-- The money events.
self:RegisterEvent("CHAT_MSG_MONEY")
self:RegisterEvent("GUILDBANKFRAME_OPENED")
self:RegisterEvent("MAIL_SHOW")
self:RegisterEvent("MAIL_CLOSED")
self:RegisterEvent("MERCHANT_SHOW")
self:RegisterEvent("MERCHANT_CLOSED")
self:RegisterEvent("TRADE_SHOW") -- Trade Opened
self:RegisterEvent("TRADE_CLOSED") -- Trade closed
self:RegisterEvent("TRADE_MONEY_CHANGED") -- Player's money was updated (used after a trade)
self:RegisterEvent("TRADE_REQUEST_CANCEL") -- Cancelled trade.
--Other events
self:RegisterEvent("CHAT_MSG_SYSTEM") -- Quest rewards
self:RegisterEvent("PLAYER_LEAVING_WORLD") -- For saving options when we log and the form is closed.
self:RegisterEvent("PLAYER_ENTERING_WORLD") -- For updating the frames and output.
self:SetScript("OnEvent", E.EventHandler)
-- Register /commands
SLASH_GUILDTITHE_MAIN1="/gt"
SLASH_GUILDTITHE_MAIN2="/tithe"
SLASH_GUILDTITHE_MAIN3="/guildtithe"
-- We set up the slash command like this, so we can call the function as a method, instead of a table index.
SlashCmdList["GUILDTITHE_MAIN"] = function(msg, editBox)
E:OnChatCommand(msg)
end
-- Register dialogs
StaticPopupDialogs["GUILDTITHE_RESETTITHE"] = {
text = L.DialogResetTitheText,
button1 = YES,
button2 = NO,
OnAccept = function()
E:ResetCurrentTithe()
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
}
StaticPopupDialogs["GUILDTITHE_RESETCONFIG"] = {
text = E:ParseColorCodedString(L.DialogResetConfigText),
button1 = YES,
button2 = NO,
OnAccept = function()
E:ResetConfig()
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
showAlert = true,
cancels = "GUILDTITHE_RESETTITHE",
}
StaticPopupDialogs["GUILDTITHE_SKINRELOADWARNING"] = {
text = L.DialogSkinRequiresReload,
button1 = OKAY,
button2 = NO,
OnAccept = function()
ReloadUI()
end,
OnCancel = function()
GT_OptionsFrame_Seperator_ExtraOption4:SetChecked(GuildTithe_SavedDB.SkinElvUI)
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
}
end
function E.EventHandler(self, event, ...)
local arg1, arg2, arg3, arg4 = ...
if event == "ADDON_LOADED" and arg1 == "GuildTithe" then
return E:Init()
elseif event == "PLAYER_ENTERING_WORLD" then
-- Hide the options frame
GT_OptionsFrame:Hide()
-- Skin the frames
E:SkinFrames()
if not E.Loaded then
E:PrintMessage(format(L.Loaded, E:GetVerString()))
E:PrintDebug("Loaded in §bDebug Mode§r! This will print a lot of extra, mostly useless information to your chat. You can disable debug mode by unchecking the box marked \"Debug mode\" in the options.")
E:SetTimer(2, function()
GT_MiniTitheFrame.CurrentTithe:SetText(GetCoinTextureString(GuildTithe_SavedDB.CurrentTithe));
E.Info.LDBData.text = GetLDBCoinString();
end, true, "GT_CTUPDATE")
E.Loaded = true
end
-- GUILDBANKFRAME_OPENED: The GB was opened, deposit the outstanding tithe.
elseif event == "GUILDBANKFRAME_OPENED" then
-- WAIT 3 SECONDS FOR GB TO FULLY OPEN
C_Timer.After(3, function() E:DepositTithe() end)
-- Mail_*: Update outstanding tithe from Mail soruces
elseif event == "MAIL_SHOW" then
return E:UpdateOutstandingTithe("Mail")
elseif event == "MAIL_CLOSED" then
return E:UpdateOutstandingTithe("Mail", true)
-- Merchant_*: Update outstanding tithe from merchants
elseif event == "MERCHANT_SHOW" then
return E:UpdateOutstandingTithe("Merchant")
elseif event == "MERCHANT_CLOSED" then
return E:UpdateOutstandingTithe("Merchant", true)
-- TRADE_*: Update trade amounts;
elseif event == "TRADE_SHOW" then
return E:UpdateOutstandingTithe("Trade")
elseif event == "TRADE_REQUEST_CANCEL" then
E.Info.TotalTradeAmount = nil
E:UpdateOutstandingTithe("Trade", true, 0)
elseif event == "TRADE_CLOSED" then
E:UpdateOutstandingTithe("Trade", true, E.Info.TotalTradeAmount or 0)
E.Info.TotalTradeAmount = nil
elseif event == "TRADE_MONEY_CHANGED" then
E.Info.TotalTradeAmount = TradeRecipientMoneyFrame.staticMoney
-- CHAT_MSG_SYSTEM: Update tithe from Quest rewards
elseif event == "CHAT_MSG_SYSTEM" then
return E:UpdateOutstandingTithe("Quest", true, arg1)
-- CHAT_MSG_MONEY: Update tithe from Loot
elseif event == "CHAT_MSG_MONEY" then
return E:UpdateOutstandingTithe("Loot", true, arg1)
-- PLAYER_LEAVING_WORLD
elseif event == "PLAYER_LEAVING_WORLD" then
GT_OptionsFrame:Hide()
-- The options frame doesn't need to remember where it was.
GT_OptionsFrame:SetUserPlaced(false)
end
end