-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBFAInvasionTimer.lua
557 lines (523 loc) · 17 KB
/
BFAInvasionTimer.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
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
local addonName, mod = ...
local L = mod.L
local candy = LibStub("LibCandyBar-3.0")
local media = LibStub("LibSharedMedia-3.0")
local Timer = C_Timer.After
local faction = UnitFactionGroup("player")
if faction == "Neutral" then return end
local frame = CreateFrame("Frame", addonName, UIParent)
frame:SetPoint("CENTER", UIParent, "CENTER")
frame:SetWidth(180)
frame:SetHeight(15)
frame:SetMovable(true)
frame:EnableMouse(true)
frame:RegisterForDrag("LeftButton")
frame:SetClampedToScreen(true)
frame:Show()
frame:SetScript("OnDragStart", function(f) f:StartMoving() end)
frame:SetScript("OnDragStop", function(f)
f:StopMovingOrSizing()
local a, _, b, c, d = f:GetPoint()
f.db.profile.position[1] = a
f.db.profile.position[2] = b
f.db.profile.position[3] = c
f.db.profile.position[4] = d
end)
do
local function openOpts()
local EnableAddOn = C_AddOns.EnableAddOn or EnableAddOn
local LoadAddOn = C_AddOns.LoadAddOn or LoadAddOn
EnableAddOn("BFAInvasionTimer_Options") -- Make sure it wasn't left disabled for whatever reason
LoadAddOn("BFAInvasionTimer_Options")
LibStub("AceConfigDialog-3.0"):Open(addonName)
end
SlashCmdList[addonName] = openOpts
SLASH_BFAInvasionTimer1 = "/bit"
SLASH_BFAInvasionTimer2 = "/bfainvasiontimer"
frame:SetScript("OnMouseUp", function(_, btn)
if btn == "RightButton" then
openOpts()
end
end)
frame:RegisterEvent("PLAYER_LOGIN")
end
local zoneNames = {
C_Map.GetMapInfo(864).name, -- Vol'dun
C_Map.GetMapInfo(896).name, -- Drustvar
C_Map.GetMapInfo(862).name, -- Zuldazar
C_Map.GetMapInfo(895).name, -- Tiragarde Sound
C_Map.GetMapInfo(863).name, -- Nazmir
C_Map.GetMapInfo(942).name, -- Stormsong Valley
}
local OnEnter, ShowTip, HideTip
do
local idNormal = faction == "Horde" and 13284 or 13283 -- Frontline Warrior
local idWarMode = faction == "Horde" and 13388 or 13387 -- Frontline Veteran
local achievementPlacement = faction == "Horde" and {
4, -- Tiragarde Sound
5, -- Nazmir
1, -- Vol'dun
3, -- Zuldazar
2, -- Drustvar
6, -- Stormsong Valley
} or {
4, -- Tiragarde Sound
6, -- Stormsong Valley
2, -- Drustvar
1, -- Vol'dun
5, -- Nazmir
3, -- Zuldazar
}
--13317 pvp achiev
local tt = CreateFrame("GameTooltip", "BFAITtooltip", UIParent, "GameTooltipTemplate")
local FormatShortDate = FormatShortDate
local IsWarModeDesired = C_PvP.IsWarModeDesired
ShowTip = function(tip)
local coloredZones = {}
local id = IsWarModeDesired() and idWarMode or idNormal
local _, name, _, _, month, day, year, description, _, _, _, _, wasEarnedByMe = GetAchievementInfo(id)
if not wasEarnedByMe or not frame.db.profile.tooltipHideAchiev then
if wasEarnedByMe then
tip:AddDoubleLine(name, FormatShortDate(day, month, year), nil, nil, nil, .5, .5, .5)
else
tip:AddLine(name, nil, nil, nil, .5, .5, .5)
end
tip:AddLine(description, 1, 1, 1, true)
for i = 1, GetAchievementNumCriteria(id) do
local criteriaString, _, completed = GetAchievementCriteriaInfo(id, i)
if completed == false then
criteriaString = "|CFF808080 - " .. criteriaString .. "|r"
coloredZones[achievementPlacement[i]] = "|CFF808080" .. zoneNames[achievementPlacement[i]] .. "|r "
else
criteriaString = "|CFF00FF00 - " .. criteriaString .. "|r"
coloredZones[achievementPlacement[i]] = "|CFF00FF00" .. zoneNames[achievementPlacement[i]] .. "|r "
end
tip:AddLine(criteriaString)
end
tip:AddLine(" ")
else
for i = 1, GetAchievementNumCriteria(id) do
local _, _, completed = GetAchievementCriteriaInfo(id, i)
if completed == false then
coloredZones[achievementPlacement[i]] = "|CFF808080" .. zoneNames[achievementPlacement[i]] .. "|r "
else
coloredZones[achievementPlacement[i]] = "|CFF00FF00" .. zoneNames[achievementPlacement[i]] .. "|r "
end
end
end
local splitLine = false
if not frame.db.profile.tooltipHideMedals then
splitLine = true
-- Honorbound Service Medal / 7th Legion Service Medal
local tbl = C_CurrencyInfo.GetCurrencyInfo(faction == "Horde" and 1716 or 1717)
tip:AddDoubleLine(tbl.name, ("|T%d:15:15:0:0:64:64:4:60:4:60|t %d"):format(tbl.iconFileID, tbl.quantity), 1, 1, 1, 1, 1, 1)
end
if splitLine then
tip:AddLine(" ")
end
tip:AddLine(L.nextInvasions)
if BFAInvasionData[1] ~= 0 then -- Have we seen our first invasion?
-- 19hrs * 60min = 1,140min = *60sec = 68,400sec
local elapsed = time() - BFAInvasionData[1]
local lastKnownInvasionZone = BFAInvasionData[2]
while elapsed > 68400 do
elapsed = elapsed - 68400
lastKnownInvasionZone = lastKnownInvasionZone + 1
if lastKnownInvasionZone == 7 then lastKnownInvasionZone = 1 end
end
local nextAvailableZone = lastKnownInvasionZone == 6 and 1 or lastKnownInvasionZone+1
local t = 68400-elapsed
t = t+time()
local upper, date = string.upper, date
local check = date("%M", t)
if check == "29" then
t = t + 60 -- Round up to 00min if we're at 59min
end
if frame.db.profile.tooltip12hr then
for i = 1, 4 do
tip:AddDoubleLine(
coloredZones[nextAvailableZone] .. _G["WEEKDAY_"..upper(date("%A", t))].." "..date("%I:%M", t) .. " " .. _G["TIMEMANAGER_"..upper(date("%p", t))],
coloredZones[nextAvailableZone == 6 and 1 or nextAvailableZone+1] .. _G["WEEKDAY_"..upper(date("%A", t+68400))].." "..date("%I:%M", t+68400) .. " " .. _G["TIMEMANAGER_"..upper(date("%p", t+68400))],
1, 1, 1, 1, 1, 1
)
t = t + 68400 + 68400
nextAvailableZone = nextAvailableZone + 2
if nextAvailableZone > 6 then
if nextAvailableZone == 8 then
nextAvailableZone = 2
else
nextAvailableZone = 1
end
end
end
else
for i = 1, 4 do
tip:AddDoubleLine(
coloredZones[nextAvailableZone] .._G["WEEKDAY_"..upper(date("%A", t))].." "..date("%H:%M", t),
coloredZones[nextAvailableZone == 6 and 1 or nextAvailableZone+1] .. _G["WEEKDAY_"..upper(date("%A", t+68400))].." "..date("%H:%M", t+68400),
1, 1, 1, 1, 1, 1
)
t = t + 68400 + 68400
nextAvailableZone = nextAvailableZone + 2
if nextAvailableZone > 6 then
if nextAvailableZone == 8 then
nextAvailableZone = 2
else
nextAvailableZone = 1
end
end
end
end
else
tip:AddLine(L.waiting, 1, 1, 1)
end
end
HideTip = function()
tt:Hide()
end
OnEnter = function(f)
tt:SetOwner(f, "ANCHOR_NONE")
tt:SetPoint("BOTTOM", f, "TOP")
ShowTip(tt)
tt:Show()
end
end
local function RearrangeBar()
frame.Bar:ClearAllPoints()
if frame.db.profile.growUp then
frame.Bar:SetPoint("BOTTOM", frame, "TOP")
else
frame.Bar:SetPoint("TOP", frame, "BOTTOM")
end
end
frame.RearrangeBar = RearrangeBar
local ChangeBarColor
do
local quests = faction == "Horde" and {
[54137] = true, -- Drustvar
[53883] = true, -- Zuldazar
[53939] = true, -- Tiragarde Sound
[53885] = true, -- Vol'dun
[54135] = true, -- Nazmir
[54132] = true, -- Stormsong Valley
} or {
[53701] = true, -- Drustvar
[54138] = true, -- Zuldazar
[53711] = true, -- Tiragarde Sound
[54134] = true, -- Vol'dun
[54136] = true, -- Nazmir
[51982] = true, -- Stormsong Valley
}
ChangeBarColor = function(id)
if quests[id] then
frame.Bar:Set("BFAInvasionTimer:complete", 1)
frame.Bar:SetColor(unpack(frame.db.profile.colorComplete))
end
end
end
local StartBar
local hiddenBars = false
do
local IsQuestFlaggedCompleted = C_QuestLog.IsQuestFlaggedCompleted
StartBar = function(text, timeLeft, rewardQuestID, icon, paused)
if frame.Bar then frame.Bar:Stop() end
local bar = candy:New(media:Fetch("statusbar", frame.db.profile.barTexture), frame.db.profile.width, frame.db.profile.height)
frame.Bar = bar
bar:SetScript("OnEnter", OnEnter)
bar:SetScript("OnLeave", HideTip)
bar:SetParent(frame)
bar:SetLabel(text)
bar.candyBarLabel:SetJustifyH(frame.db.profile.alignText)
bar.candyBarDuration:SetJustifyH(frame.db.profile.alignTime)
bar:SetDuration(timeLeft)
bar:Set("BFAInvasionTimer:icon", icon)
if rewardQuestID > 0 then
if IsQuestFlaggedCompleted(rewardQuestID) then
bar:SetColor(unpack(frame.db.profile.colorComplete))
bar:Set("BFAInvasionTimer:complete", 1)
else
bar:SetColor(unpack(frame.db.profile.colorIncomplete))
bar:Set("BFAInvasionTimer:complete", 0)
end
else
bar:SetColor(unpack(frame.db.profile.colorNext))
end
bar.candyBarBackground:SetVertexColor(unpack(frame.db.profile.colorBarBackground))
bar:SetTextColor(unpack(frame.db.profile.colorText))
if frame.db.profile.icon then
bar:SetIcon(icon)
bar:SetIconPosition(frame.db.profile.alignIcon)
end
bar:SetTimeVisibility(frame.db.profile.timeText)
bar:SetLabelVisibility(frame.db.profile.labelText)
bar:SetFill(frame.db.profile.fill)
local flags = nil
if frame.db.profile.monochrome and frame.db.profile.outline ~= "NONE" then
flags = "MONOCHROME," .. frame.db.profile.outline
elseif frame.db.profile.monochrome then
flags = "MONOCHROME"
elseif frame.db.profile.outline ~= "NONE" then
flags = frame.db.profile.outline
end
bar.candyBarLabel:SetFont(media:Fetch("font", frame.db.profile.font), frame.db.profile.fontSize, flags)
bar.candyBarDuration:SetFont(media:Fetch("font", frame.db.profile.font), frame.db.profile.fontSize, flags)
if paused then -- Searching bars
bar:Start()
bar:Pause()
bar:SetTimeVisibility(false)
elseif rewardQuestID > 0 then -- Invasion duration bars
bar:Start(25200) -- 7hrs = 60*6 = 420min = 420*60 = 25,200sec
else -- Next invasion bars
bar:Start(43200) -- 12hrs = 60*12 = 720min = 720*60 = 43,200sec
end
RearrangeBar()
if hiddenBars then
bar:Hide()
end
end
end
local StartBroker
do
local obj
local prevTime, label, repeater = 0, "", false
local function update()
prevTime = prevTime - 60
obj.text = label..": ".. SecondsToTime(prevTime, true)
end
StartBroker = function(text, timeLeft, icon)
if not obj then
obj = LibStub("LibDataBroker-1.1"):NewDataObject("BFAInvasionTimer", {
type = "data source",
icon = icon,
text = text..": ".. SecondsToTime(timeLeft, true),
OnTooltipShow = function(tooltip)
if not tooltip or not tooltip.AddLine or not tooltip.AddDoubleLine then return end
ShowTip(tooltip)
end
})
end
if obj then
obj.icon = icon
obj.text = text..": ".. SecondsToTime(timeLeft, true)
prevTime = timeLeft
label = text
if repeater then repeater:Cancel() end
repeater = C_Timer.NewTicker(60, update)
end
end
end
local FindInvasion
local justLoggedIn = true
do
local GetAreaPOISecondsLeft = C_AreaPoiInfo.GetAreaPOISecondsLeft
local isWaiting = false
-- Vol'dun > Drustvar > Zuldazar > Tiragarde Sound > Nazmir > Stormsong Valley
local zonePOIIds = {
5970, -- Vol'dun
5964, -- Drustvar
5973, -- Zuldazar
5896, -- Tiragarde Sound
5969, -- Nazmir
5966, -- Stormsong Valley
}
local icons = {
236645, -- Interface/Icons/Achievement_PVP_O_A
236594, -- Interface/Icons/Achievement_PVP_A_H
236645, -- Interface/Icons/Achievement_PVP_O_A
236594, -- Interface/Icons/Achievement_PVP_A_H
236645, -- Interface/Icons/Achievement_PVP_O_A
236594, -- Interface/Icons/Achievement_PVP_A_H
}
local questIds = faction == "Horde" and {
53885, -- Vol'dun
54137, -- Drustvar
53883, -- Zuldazar
53939, -- Tiragarde Sound
54135, -- Nazmir
54132, -- Stormsong Valley
} or {
54134, -- Vol'dun
53701, -- Drustvar
54138, -- Zuldazar
53711, -- Tiragarde Sound
54136, -- Nazmir
51982, -- Stormsong Valley
}
FindInvasion = function()
local mode = frame.db.profile.mode
local found = false
for i = 1, #zonePOIIds do
local timeLeftSeconds = GetAreaPOISecondsLeft(zonePOIIds[i])
-- On some realms timeLeftSeconds can return massive values during the initialization of a new event
if timeLeftSeconds and timeLeftSeconds > 60 and timeLeftSeconds < 25201 then -- 7 hours: (7*60)*60 = 25200
if mode == 2 then
StartBroker(zoneNames[i], timeLeftSeconds, icons[i])
else
StartBar(zoneNames[i], timeLeftSeconds, questIds[i], icons[i])
frame:RegisterEvent("QUEST_TURNED_IN")
end
Timer(timeLeftSeconds+60, FindInvasion)
found = true
-- Not fighting a boss, didn't just log in, assault has just spawned (7hrs - 10min = 24600), feature is enabled
if not IsEncounterInProgress() and not justLoggedIn and timeLeftSeconds > 24600 and frame.db.profile.zoneWarnings then
FlashClientIcon()
local text = L.underAttack:format(icons[i], zoneNames[i])
print("|cFF33FF99BFAInvasionTimer|r:", text)
RaidNotice_AddMessage(RaidBossEmoteFrame, text, {r=1, g=1, b=1})
PlaySound(8959, "Master", false) -- SOUNDKIT.RAID_WARNING
end
justLoggedIn = false
local curTime = time()
local elapsed = 25200-timeLeftSeconds
local latestInvasionTime = curTime - elapsed
BFAInvasionData[1] = latestInvasionTime
BFAInvasionData[2] = i
break
end
end
if not found then
if BFAInvasionData[1] ~= 0 then
local elapsed = time() - BFAInvasionData[1]
local lastKnownInvasionZone = BFAInvasionData[2]
-- 19hrs * 60min = 1,140min = *60sec = 68,400sec
while elapsed > 68400 do
elapsed = elapsed - 68400
lastKnownInvasionZone = lastKnownInvasionZone + 1
if lastKnownInvasionZone == 7 then lastKnownInvasionZone = 1 end
end
local t = 68400-elapsed
local nextAvailableZone = lastKnownInvasionZone == 6 and 1 or lastKnownInvasionZone+1
if t > 43200 then -- 12hrs * 60min = 720min = *60sec = 43,200sec
-- If it's longer than 43k then an invasion is currently active.
-- Loop every second until the API call responds with valid results.
Timer(1, FindInvasion)
if not isWaiting then
isWaiting = true
if mode == 2 then
StartBroker(L.waiting, 0, 1044517) -- 1044517 = Interface/Icons/Achievement_Garrison_Invasion
else
StartBar(L.waiting, t, 0, 1044517, true) -- 1044517 = Interface/Icons/Achievement_Garrison_Invasion
frame:UnregisterEvent("QUEST_TURNED_IN")
end
end
return
end
if mode == 2 then
StartBroker(L.next:format(zoneNames[nextAvailableZone]), t, 1044517) -- 1044517 = Interface/Icons/Achievement_Garrison_Invasion
else
StartBar(L.next:format(zoneNames[nextAvailableZone]), t, 0, 1044517) -- 1044517 = Interface/Icons/Achievement_Garrison_Invasion
frame:UnregisterEvent("QUEST_TURNED_IN")
end
Timer(t + 5, FindInvasion)
else
Timer(60, FindInvasion)
if not isWaiting then
isWaiting = true
if mode == 2 then
StartBroker(L.waiting, 0, 1044517) -- 1044517 = Interface/Icons/Achievement_Garrison_Invasion
else
StartBar(L.waiting, 1000, 0, 1044517, true) -- 1044517 = Interface/Icons/Achievement_Garrison_Invasion
end
end
end
end
if isWaiting then
isWaiting = false
end
end
end
local function CheckIfInRaid()
if frame.db.profile.hideInRaid then
local _, iType = GetInstanceInfo()
if iType == "raid" then
hiddenBars = true
frame.Bar:Hide()
elseif hiddenBars then
hiddenBars = false
frame.Bar:Show()
end
end
end
frame:SetScript("OnEvent", function(f)
f:UnregisterEvent("PLAYER_LOGIN")
if type(BFAInvasionData) ~= "table" then
BFAInvasionData = {0, 0}
end
-- saved variables database setup
local defaults = {
profile = {
lock = false,
position = {"CENTER", "CENTER", 0, 0},
fontSize = 10,
barTexture = "Blizzard Raid Bar",
outline = "NONE",
monochrome = false,
font = media:GetDefault("font"),
width = 200,
height = 20,
icon = true,
timeText = true,
labelText = true,
fill = false,
growUp = false,
alignText = "LEFT",
alignTime = "RIGHT",
alignIcon = "LEFT",
colorText = {1,1,1,1},
colorComplete = {0,1,0,1},
colorIncomplete = {1,0,0,1},
colorNext = {0.25,0.33,0.68,1},
colorBarBackground = {0,0,0,0.75},
tooltip12hr = true,
tooltipHideAchiev = false,
tooltipHideMedals = false,
zoneWarnings = false,
hideInRaid = false,
mode = 1,
},
}
f.db = LibStub("AceDB-3.0"):New("BFAInvasionTimerDB", defaults, true)
f:ClearAllPoints()
f:SetPoint(f.db.profile.position[1], UIParent, f.db.profile.position[2], f.db.profile.position[3], f.db.profile.position[4])
local bg = f:CreateTexture()
bg:SetAllPoints(f)
bg:SetColorTexture(0, 1, 0, 0.3)
f.bg = bg
local header = f:CreateFontString(nil, "OVERLAY", "TextStatusBarText")
header:SetAllPoints(f)
header:SetText(addonName)
f.header = header
if f.db.profile.lock then
f:EnableMouse(false)
f:SetMovable(false)
f.bg:Hide()
f.header:Hide()
end
if f.db.profile.mode == 3 then
f:SetParent(WorldMapFrame)
f:SetFrameStrata("FULLSCREEN")
f:SetFrameLevel(10)
end
FindInvasion()
Timer(15, function()
justLoggedIn = false
if BFAInvasionData[1] == 0 then
print("|cFF33FF99BFAInvasionTimer|r:", L.firstRunWarning)
end
local x = GetLocale()
if x == "koKR" then -- XXX temp, Options/Locales needs updated
print("|cFF33FF99BFAInvasionTimer|r is missing locale for", x, "and needs your help! Please visit the project page on GitHub for more info.")
end
end)
if f.db.profile.mode == 1 then
CheckIfInRaid()
f:RegisterEvent("ZONE_CHANGED_NEW_AREA")
end
f:SetScript("OnEvent", function(_, event, id)
if event == "QUEST_TURNED_IN" then
ChangeBarColor(id)
else
CheckIfInRaid()
end
end)
end)