-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFarmIt2_Core.lua
1038 lines (869 loc) · 32.1 KB
/
FarmIt2_Core.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--------------------------------------------------------------------------------
-- CORE FUNCTIONS
--[[
'bar' = Bar Number
The dynamic table index of a group's record in: FI_SVPC_DATA["Groups"]
This is a user interface convention to provide the appearance of sequential bar numbers regardless of the actual group ID.
'gid' = Group ID
The numeric primary key of a group as found in its data "record" stored at FI_SVPC_DATA.Groups
'bid' = Button ID
The numeric primary key of a button as found in its data "record" stored at FI_SVPC_DATA.Buttons
'cid' = Currency ID
The numeric primary key of a "watched" currency as found in its data "record" stored at FI_SVPC_DATA.Currencies
'sid' = Session ID
The numeric primary key of a farming session as found in its data "record" stored at FI_SVPC_DATA.Sessions
]]--
function FI_Debug(obj, desc)
if ViragDevTool_AddData then
ViragDevTool_AddData(obj, desc)
end
end
--------------------------------------------------------------------------------
-- STARTUP
--------------------------------------------------------------------------------
-- return uptime information
-- threshold should be given in seconds
function FI_Uptime( threshold )
local uptime = time() - FI_LOAD_TS;
if threshold then
-- check uptime against a given threshold
if (uptime < threshold) then
return false;
else
return true;
end
else
return uptime;
end
end
function FI_Init()
-- determine load status
if (FI_SV_CONFIG == nil)
or (FI_SVPC_CONFIG == nil)
or (FI_SVPC_STYLE == nil)
or (FI_SV_DATA == nil)
or (FI_SVPC_DATA == nil)
or (FI_SVPC_CACHE == nil)
then
-- new install or something wrong with saved vars
FI_STATUS = 1;
elseif (FI_SV_CONFIG.version == nil) then
-- sanity check on previous version number
FI_STATUS = 2;
elseif (FI_VERSION > FI_SV_CONFIG.version) then
-- addon updated since last run
FI_STATUS = 2;
-- check minimum compatible version
if (FI_SV_CONFIG.version < FI_MIN_VERSION) then
FI_REQUIRES_REBUILD = true;
end
elseif (FI_SVPC_CONFIG.version == nil) or (FI_VERSION > FI_SVPC_CONFIG.version) then
-- addon updated since last time this character played
FI_STATUS = 2;
else
-- normal startup
FI_STATUS = 3;
end
-- status responses
if (FI_STATUS == 1) then
FI_DB.rebuild("all");
elseif (FI_STATUS == 2) then
if FI_REQUIRES_REBUILD then
FI_DB.rebuild("all");
elseif FI_REQUIRES_RESET then
FI_Reset();
elseif FI_REBUILD_CONFIG then
FI_DB.rebuild("config");
else
-- if no maintenance is required, just update the version number
FI_SV_CONFIG.version = FI_VERSION;
FI_SVPC_CONFIG.version = FI_VERSION;
end
elseif (FI_STATUS == 3) then
-- all clear
end
-- validate configuration tables
FI_DB.scan("recursive", FI_DEFAULTS.SV.CONFIG, FI_SV_CONFIG, true);
FI_DB.scan("recursive", FI_DEFAULTS.SVPC.CONFIG, FI_SVPC_CONFIG, true);
end
----->> FRAME CREATION <<-----
function FI_Render()
-- misc frames
FI_FRAMES.Currency();
-- loop through groups and build buttons
for i,group in ipairs(FI_SVPC_DATA.Groups) do
FI_FRAMES.Group(group.id);
end
FI_Scale();
----- SKIN -----
FI_Style();
----- SHOW -----
if (FI_SVPC_CONFIG.show == true) then
_G["FI_PARENT"]:Show();
else
_G["FI_PARENT"]:Hide();
end
end
----->> LOAD DATA <<-----
function FI_Load()
-- session start
FI_SESSION.load();
-- internal garbage collection
FI_DB.garbage("logs");
-- currency startup checks
FI_Init_Currency();
-- populate item buttons
for i,button in ipairs(FI_SVPC_DATA.Buttons) do
if button.item then
FI_Set_Button(button.id);
end
end
----- G2G, BUFF AND PULL! -----
local version = "v"..tostring(FI_VERSION);
if (FI_RELEASE ~= "RELEASE") then version = version.." "..FI_RELEASE; end
local load_msg = "|cFF33CCFF".."FarmIt "..version.." "..FI_LOAD_STATES[FI_STATUS]..". |cFFFFFFFF".."Type /farmit for configuration help.";
-- indicate debug state
if FI_SV_CONFIG.debug then
load_msg = load_msg.."\n|cFFFF0000Debugging output enabled!|r";
end
DEFAULT_CHAT_FRAME:AddMessage(load_msg);
end
function FI_Reset()
if FI_SV_CONFIG.debug then print("[FarmIt] RESET CALLED!"); end
FI_DB.rebuild("config");
FI_DB.rebuild("style");
FI_DB.rebuild("frames");
FI_DB.rebuild("currencies");
FI_DB.rebuild("sessions");
-- User initiated reset?
if (FI_LOADING == false) then
ReloadUI();
end
if FI_SV_CONFIG.debug then print("[FarmIt] RESET COMPLETE."); end
end
--------------------------------------------------------------------------------
-- FRAME CREATION
--------------------------------------------------------------------------------
FI_FRAMES = {};
FI_FRAMES.Group = function( gid )
if FI_SV_CONFIG.debug then print("Load routine running for Group ID: "..gid); end
local group = FI_DB.select(FI_SVPC_DATA.Groups, {id = gid}, true);
local f_name = "FI_Group_"..group.id;
-- check if frame already exists
if not _G[f_name] then
-- create the base "anchor" frame for the bar
local f = CreateFrame("Button", f_name, _G["FI_PARENT"], "FI_TPL_Group");
f:SetFrameStrata("LOW");
f:SetPoint("CENTER", UIParent, "CENTER", 0,0);
f:SetScale(group.scale);
-- visibility
if group.show then f:Show(); else f:Hide(); end
-- clicks
f:EnableMouse(true);
f:SetMovable(true);
f:SetClampedToScreen(true);
f:RegisterForClicks("LeftButtonDown","LeftButtonUp","RightButtonUp");
f:SetScript("OnClick", FI_Click_Group);
-- handle orientation related requirements
local a = FI_SVPC_STYLE.anchor;
local size_x,size_y,bg_size_x,bg_size_y;
local vertical = {"U","D"};
if tContains(vertical, group.grow) then
-- vertical
size_x,size_y = a.size[1],a.size[2];
bg_size_x,bg_size_y = a.background.size[1],a.background.size[2];
else
-- horizontal
size_x,size_y = a.size[2],a.size[1];
bg_size_x,bg_size_y = a.background.size[2],a.background.size[1];
end
_G[f_name]:SetSize(size_x, size_y);
_G[f_name.."_Background"]:SetSize(bg_size_x, bg_size_y);
_G[f_name.."_Background"]:SetPoint("CENTER", _G[f_name], "CENTER", 0,-1);
-- bar number
_G[f_name.."_Label"]:SetFont(a.text.font, a.text.size, a.text.flags);
_G[f_name.."_Label"]:SetPoint("CENTER", _G[f_name.."_Background"], "CENTER", 0,-6);
_G[f_name.."_Label"]:SetText( FI_DB.find(FI_SVPC_DATA.Groups, {id = group.id}, true) );
_G[f_name.."_Label"]:SetJustifyH("CENTER");
_G[f_name.."_Label"]:SetJustifyV("MIDDLE");
----- POPULATE THE GROUP ---------------------------------------------
FI_FRAMES.Button(group.id);
------------------------------------------------------------------------
-- apply quicksize
FI_UI_Button(group.id);
else
if FI_SV_CONFIG.debug then print("WARNING: Frame '"..f_name.."' already exists!"); end
end
end
-- button, button, who's got the button...
FI_FRAMES.Button = function( gid )
if FI_SV_CONFIG.debug then print("...loading buttons for Group ID: "..gid); end
-- group frame is the first thing we anchor to
local group = FI_DB.select(FI_SVPC_DATA.Groups, {id = gid}, true);
local parent = _G["FI_Group_"..group.id];
local last_frame = parent;
local a = FI_SVPC_STYLE.anchor;
local b = FI_SVPC_STYLE.button;
for i,button in ipairs(FI_SVPC_DATA.Buttons) do
-- group filter
if (button.group == group.id) then
-- define the frame name
local f_name = "FI_Button_"..button.id;
-- skip creation if frame exists (ie- adding buttons to an existing bar)
if not _G[f_name] then
local f = CreateFrame("Button", f_name, parent, "FI_TPL_Button");
f:SetFrameStrata("LOW");
f:Show();
f:SetClampedToScreen(false);
-- clicks
f:RegisterForClicks("LeftButtonUp","RightButtonUp");
f:SetScript("OnMouseUp", FI_Click);
-- secure template attributes
f:SetAttribute("type2", "macro");
f:SetAttribute("macro", false);
-- no padding for first button so it sits against the anchor
if (last_frame == parent) then pad = a.pad; else pad = b.pad; end
-- determine orientation
local a1,a2,x,y;
if (group.grow == "U") then
-- bar grows up
a1,a2,x,y = "BOTTOM","TOP",0,pad;
elseif (group.grow == "D") then
-- bar grows down
a1,a2,x,y = "TOP","BOTTOM",0,-pad;
elseif (group.grow == "L") then
-- bar grows left
a1,a2,x,y = "RIGHT","LEFT",-pad,0;
elseif (group.grow == "R") then
-- bar grows right
a1,a2,x,y = "LEFT","RIGHT",pad,0;
end
-- position the frame
f:SetPoint(a1, last_frame, a2, x, y);
-- size
_G[f_name.."_Background"]:SetSize(b.background.size[1], b.background.size[2]);
--if FI_SV_CONFIG.debug then print("Button ID: "..button.id.." (Group ID: "..group.id..")"); end
else
if FI_SV_CONFIG.debug then print("Frame '"..f_name.."' already exists... skipping. (Group ID: "..group.id..")"); end
end
-- dynamic relative anchoringggggggggggg
last_frame = f_name;
end
end
end
FI_FRAMES.Currency = function()
if FI_SV_CONFIG.debug then print("FI_FRAMES.Currency called."); end
-- set some script triggers
--BruteForceRemovalForInitialDragonFlightCompatibility--_G["TokenFrameContainer"]:SetScript("OnHide", FI_Update_Currency);
--BruteForceRemovalForInitialDragonFlightCompatibility--for n=1,3 do
--BruteForceRemovalForInitialDragonFlightCompatibility-- _G["BackpackTokenFrameToken"..n]:SetScript("OnMouseUp", FI_Edit_Currency);
--BruteForceRemovalForInitialDragonFlightCompatibility--end
GameTooltip:HookScript("OnShow", FI_Hook_Currency);
-- floating currency bar (added in v2.22)
if (not _G["FI_Currency"]) then
local f_name = "FI_Currency";
local a = FI_SVPC_STYLE.anchor;
-- create the parent frame
local f = CreateFrame("Button", f_name, _G["FI_PARENT"]);
f:SetSize(175,22);
f:SetFrameStrata("LOW");
f:SetPoint("TOP", UIParent, "TOP", 0,-30);
f:SetScale(FI_SVPC_CONFIG.Currency.scale);
f:SetAlpha(FI_SVPC_CONFIG.Currency.alpha);
f:Show();
-- clicks
f:SetMovable(true);
f:SetClampedToScreen(true);
-- background
local bg = f:CreateTexture(f_name.."_Background", "BACKGROUND");
bg:SetAllPoints(f);
bg:SetTexture("Interface/Tooltips/UI-Tooltip-Background");
bg:SetVertexColor(0,0,0);
bg:SetAlpha(0.5);
bg:Hide();
local slots = {};
for cid=1,3 do
local slot_name = f_name.."_"..cid;
-- create child frame
slots[cid] = CreateFrame("Button", slot_name, _G[f_name]);
local s = slots[cid];
s:SetSize(50,18);
s:Show();
-- tooltip
s:SetScript("OnEnter", FI_Tooltip_Currency);
s:SetScript("OnLeave", GameTooltip_Hide);
-- clicks
s:EnableMouse(true);
s:RegisterForClicks("LeftButtonDown","LeftButtonUp","RightButtonUp");
s:SetScript("OnClick", FI_Click_Currency);
-- background
local bg = s:CreateTexture(slot_name.."_Background", "BACKGROUND");
bg:SetAllPoints(s);
bg:SetTexture("Interface/Tooltips/UI-Tooltip-Background");
bg:SetVertexColor(0,0,0);
bg:SetAlpha(0.5);
bg:Hide();
-- icon
local i = s:CreateTexture(slot_name.."_Icon", "ARTWORK");
i:SetSize(18,16);
-- count
local c = s:CreateFontString(slot_name.."_Count", "OVERLAY");
c:SetFont(a.text.font, a.text.size, "OUTLINE");
c:SetAlpha(a.text.alpha);
c:SetJustifyH("RIGHT");
c:SetJustifyV("MIDDLE");
-- objective
--[[
local o = s:CreateFontString(slot_name.."_Objective", "OVERLAY");
o:SetFont(a.text.font, a.text.size, "OUTLINE");
o:SetAlpha(a.text.alpha);
o:SetJustifyH("RIGHT");
o:SetJustifyV("MIDDLE");
]]
-- position
i:SetPoint("RIGHT", _G[slot_name], "RIGHT", 0,0);
c:SetPoint("RIGHT", i, "LEFT", -2,0);
--o:SetPoint("RIGHT", i, "LEFT", -2,16);
end
slots[1]:SetPoint("LEFT", _G[f_name], "LEFT", 2,0);
slots[2]:SetPoint("CENTER", _G[f_name], "CENTER", 0,0);
slots[3]:SetPoint("RIGHT", _G[f_name], "RIGHT", -2,0);
end
end
--------------------------------------------------------------------------------
-- VISUAL STYLES
--------------------------------------------------------------------------------
function FI_Set_Texture( texture, arg )
if (type(arg) == "table") then
_G[texture]:SetTexture("Interface/BUTTONS/WHITE8X8");
_G[texture]:SetVertexColor(arg[1], arg[2], arg[3]);
elseif strlen(arg) then
_G[texture]:SetTexture(arg);
elseif FI_SV_CONFIG.debug then
print("[FI_Set_Texture] Bad texture data."); --debug
end
end
function FI_Style( newstyle ) --wrapper
if newstyle then
-- validate input
if FI_STYLES[newstyle] then
-- update style data
FI_SVPC_STYLE = LIB.table.copy(FI_STYLES[newstyle]);
-- user feedback
if (FI_LOADING == false) then
FI_Message("'"..newstyle.."' visual style applied.");
end
else
-- error message
local list = "";
for key,val in pairs(FI_STYLES) do list = list.." "..key; end
FI_Message("Style '"..newstyle.."' not found. Available options are: "..list);
return;
end
end
-- apply visual theme to all bars
for i,group in ipairs(FI_SVPC_DATA.Groups) do
FI_Group_Style(group.id);
end
-- apply style to currency frames
FI_Currency_Style();
-- master alpha setting
_G["FI_PARENT"]:SetAlpha(FI_SVPC_CONFIG.alpha);
end
-- apply visual theme settings to a bar
function FI_Group_Style( gid )
local group = FI_DB.select(FI_SVPC_DATA.Groups, {id = gid}, true);
-- [ANCHOR] --------------------------------------------------
local a = FI_SVPC_STYLE.anchor;
local f_name = "FI_Group_"..group.id;
_G[f_name]:SetAlpha(group.alpha);
-- background
FI_Set_Texture(f_name.."_Background", a.background.texture);
_G[f_name.."_Background"]:SetAlpha(a.background.alpha);
_G[f_name.."_Background"]:SetSize(a.background.size[1], a.background.size[2]);
-- border
-- label
_G[f_name.."_Label"]:SetFont(a.text.font, a.text.size, a.text.flags);
_G[f_name.."_Label"]:SetVertexColor(a.text.color[1], a.text.color[2], a.text.color[3]);
_G[f_name.."_Label"]:SetAlpha(a.text.alpha);
_G[f_name.."_Label"]:SetJustifyH("CENTER");
_G[f_name.."_Label"]:SetJustifyV("MIDDLE");
-- [BUTTON] --------------------------------------------------
local b = FI_SVPC_STYLE.button;
for i,bid in ipairs(FI_Group_Members(group.id)) do
local button = FI_DB.select(FI_SVPC_DATA.Buttons, {id = bid}, true);
f_name = "FI_Button_"..button.id;
-- background
FI_Set_Texture(f_name.."_Background", b.background.texture);
_G[f_name.."_Background"]:SetAlpha(b.background.alpha);
_G[f_name.."_Background"]:SetSize(b.background.size[1], b.background.size[2]);
-- border
-- glow
FI_Set_Texture(f_name.."_Glow", b.glow.color);
_G[f_name.."_Glow"]:SetAlpha(b.glow.alpha);
-- numbers
_G[f_name.."_Count"]:SetFont(b.number.font, b.number.size, b.number.flags);
_G[f_name.."_Count"]:SetVertexColor(b.number.color[1], b.number.color[2], b.number.color[3]);
_G[f_name.."_Count"]:SetAlpha(b.number.alpha);
_G[f_name.."_Objective"]:SetFont(b.number.font, b.number.size, b.number.flags);
_G[f_name.."_Objective"]:SetVertexColor(b.number.color[1], b.number.color[2], b.number.color[3]);
_G[f_name.."_Objective"]:SetAlpha(b.number.alpha);
-- [text]
end
end
function FI_Currency_Style()
local b = FI_SVPC_STYLE.button;
for cid=1,3 do
local f_name = "FI_Currency_"..cid;
if _G[f_name] then
-- background
FI_Set_Texture(f_name.."_Background", b.background.texture);
_G[f_name.."_Background"]:SetAlpha(b.background.alpha);
-- numbers
_G[f_name.."_Count"]:SetFont(b.number.font, b.number.size, b.number.flags);
_G[f_name.."_Count"]:SetVertexColor(b.number.color[1], b.number.color[2], b.number.color[3]);
_G[f_name.."_Count"]:SetAlpha(b.number.alpha);
--[[
_G[f_name.."_Objective"]:SetFont(b.number.font, b.number.size, b.number.flags);
_G[f_name.."_Objective"]:SetVertexColor(b.number.color[1], b.number.color[2], b.number.color[3]);
_G[f_name.."_Objective"]:SetAlpha(b.number.alpha);
]]
elseif FI_SV_CONFIG.debug then
print("[FI_Currency_Style] Frame '"..f_name.."' does not exist!"); --debug
end
end
end
--------------------------------------------------------------------------------
-- OUTPUT
--------------------------------------------------------------------------------
function FI_Alert( message, color, sound, throttle, item, louder )
-- control when output is allowed
if (FI_LOADING == false) and (FI_MOVING == false) and (FI_Uptime(FI_MIN_UPTIME) == true) then
if FI_SV_CONFIG.debug then print("FI_Alert called."); end
-- spam filter
local filter = false;
if throttle and ((time() - FI_ALERT_TS) < FI_SPAM_LIMIT) then
filter = true;
end
if filter then
if FI_SV_CONFIG.debug then print("[FI_Alert] Spam filter triggered."); end
else
-- audio
if sound and FI_SV_CONFIG.Alerts.sound then
if louder and not FI_LOUD_ACTIVE then
FI_LOUD_ACTIVE = true;
local pre = GetCVar("Sound_MasterVolume");
SetCVar("Sound_MasterVolume", 1.0);
C_Timer.After(6, function()
SetCVar("Sound_MasterVolume", pre);
FI_LOUD_ACTIVE = false;
end);
end
if louder and Rarity and item then
Rarity:ShowFoundAlert(item, 1);
else
PlaySound(sound);
end
end
-- chat frame
if FI_SV_CONFIG.Alerts.chat then
FI_Message(message, color);
end
-- on-screen
if FI_SV_CONFIG.Alerts.screen then
FI_Announce(message, color);
end
end
end
-- update timestamp
FI_ALERT_TS = time();
end
-- wrapper for handling multi-line output
function FI_Message( message, color, msg_type )
if (type(message) == "table") then
for i,msg in pairs(message) do
FI_Msg(msg, color, msg_type);
end
else
FI_Msg(message, color, msg_type);
end
end
-- color = table of R,G,B values and the hex code of the color
function FI_Msg( message, color, msg_type )
local output;
-- format message
local prefix = "|cFF33CCFF[FarmIt]|r ";
if msg_type then
if (msg_type == "debug") then
prefix = prefix.."|cFFFFCC00<DEBUG>|r ";
elseif (msg_type == "error") then
prefix = prefix.."|cFFFF3300ERROR:|r ";
end
end
if color then
output = prefix.."|cFF"..color[4]..message;
else
output = prefix..message;
end
-- display message
DEFAULT_CHAT_FRAME:AddMessage(output);
end
function FI_Announce( message, color )
-- display announcement
if color then
UIErrorsFrame:AddMessage(message, color[1], color[2], color[3]);
else
UIErrorsFrame:AddMessage(message);
end
end
--------------------------------------------------------------------------------
-- PROGRESS TRACKING
--------------------------------------------------------------------------------
function FI_Find_ByBag( bag, item )
-- build list of items to check against
local contents = {};
for slot=1,GetContainerNumSlots(bag) do
local id = GetContainerItemID(bag, slot);
if id then table.insert(contents, id); end
end
if tContains(contents, item) then
if FI_SV_CONFIG.debug then print("[FI_Find_ByBag] Found item ID "..item.." in bag "..bag); end
return true;
else
if FI_SV_CONFIG.debug then print("[FI_Find_ByBag] Item ID "..item.." not found in bag "..bag); end
return false;
end
end
function FI_Contents( bag )
if FI_SV_CONFIG.debug then print("[FI_Contents] Called."); end
local contents = {};
for slot=1,GetContainerNumSlots(bag) do
local id = GetContainerItemID(bag, slot);
if id then table.insert(contents, id); end
end
if FI_SV_CONFIG.debug then table.dump(contents); end
return contents;
end
-- NOTE:
-- Because this function is called as a result of a BAG_UPDATE event, this code is executed AFTER the item has
-- actually been added/removed from your bags. Therefore, the item count wont get updated if you no longer
-- have any on hand because FI_Contents() wont find any in your bags...
function FI_Update( bagID )
-- "smart item update" method bypassed for now, due to the problem described above (fixed in v2.22)
if bagID and false then
-- get list of all item IDs in the container
local items = FI_Contents(bagID);
-- check query results
if (#items > 0) then
if FI_SV_CONFIG.debug then print("[FI_Update] Selective update triggered."); end
for i,button in ipairs(FI_SVPC_DATA.Buttons) do
-- filter empty buttons
if button.item then
-- do we have any of the item in this container?
if tContains(items, button.item) then
-- if one of the items in the container is being tracked, run a proper update on it
FI_Update_Button(nil, button);
end
end
end
else
FI_Update_All();
end
else
FI_Update_All();
end
-- update currencies because only 'BAG_UPDATE' fires when you *spend* points
FI_Update_Currency();
end
-- global item update
function FI_Update_All()
if FI_SV_CONFIG.debug then print("[FI_Update_All] Called."); end
-- update all populated item slots
for i,button in ipairs(FI_SVPC_DATA.Buttons) do
if button.item then
FI_Update_Button(nil, button);
end
end
end
function FI_Progress( data, silent )
-- VERBOSE DEBUG
if FI_SV_CONFIG.debug then print("[FI_Progress] Called."); end
local f_name,database,currencyName,status,info,suffix,color,sound,itemName,itemLink;
if FI_SV_CONFIG.debug and true then
if data then
local item_msg
if data.item then item_msg = data.item; elseif data.name then item_msg = data.name; else item_msg = "?"; end
print("[FI_Progress] Progress tracking triggered. Item: "..item_msg..", Last Count: "..data.lastcount..", Count: "..data.count..", Objective: "..data.objective); --debug
else
print("[FI_Progress] No data record was passed!"); return; --debug
end
end
-- determine whether checking a button frame, or other type of data
if data.item then
f_name = "FI_Button_"..data.id;
database = "Buttons";
itemName,itemLink = GetItemInfo(data.item);
elseif data.name ~= nil then
f_name = "FI_Currency_"..data.id;
database = "Currencies";
-- if nested name
local currencyName = data.name;
if currencyName.name then currencyName = currencyName.name end
currencyName = "|cFF"..FI_SV_CONFIG.Colors.currency[4]..currencyName.."|r";
elseif FI_SV_CONFIG.debug then
print("[FI_Progress] Missing data! f_name = "..f_name); return; --debug
end
-- gained, or lost?
status = "Item update:";
if (data.count > data.lastcount) then
-- increase
suffix = "(|cFF00FF00+"..data.count - data.lastcount.."|r)";
sound = 864; -- igBackPackCoinSelect
elseif (data.count < data.lastcount) then
-- decrease
suffix = "(|cFFFF0000-"..data.lastcount - data.count.."|r)";
else
-- count has not changed
if data.name then
-- enforce objective state
local suc;
if (data.count < data.objective) then
suc = false;
else
suc = true;
end
data = FI_DB.update(FI_SVPC_DATA[database], {id = data.id}, {success = suc});
-- apply visual changes, if any
FI_UI_Currency();
-- prevent notification spam since currency updates are hooked to an onShow event.
return;
end
suffix = "";
end
-- OBJECTIVE PROGRESS
local louder = false;
if (data.objective > 0) then
-- Calculate the previous quotient and the new quotient in order to tell if an alert is needed
local prev_quotient = math.floor(data.lastcount / data.objective)
local new_quotient = math.floor(data.count / data.objective)
if (data.count > data.lastcount) then
status = "Farming progress:";
else
status = "Farming update:";
end
-- find out where things stand in relation to the objective
if (data.count < data.objective) then
color = FI_SV_CONFIG.Colors.objective;
-- reset notification flag
if (data.success == true) then
data = FI_DB.update(FI_SVPC_DATA[database], {id = data.id}, {success = false});
end
elseif (new_quotient > prev_quotient) then
-- Don't use data.success here, since we're using the previous and current quotients, but leave the success as true
-- so the display remains the success color
------------------------------------------------------------
-- OBJECTIVE SUCCESS - 1 or more multiples of data.objective
------------------------------------------------------------
color = FI_SV_CONFIG.Colors.success;
if (new_quotient == 1) then
status = "Objective complete!";
else
status = "Objective complete " .. tostring(new_quotient) .. " times!";
end
sound = 619; -- QUESTCOMPLETED
louder = true;
-- update notification flag
data = FI_DB.update(FI_SVPC_DATA[database], {id = data.id}, {success = true});
else
-- greater than objective, already notified of success
color = FI_SV_CONFIG.Colors.success;
-- check notification flag
if (data.success == false) then
data = FI_DB.update(FI_SVPC_DATA[database], {id = data.id}, {success = true});
end
end
------------------------------------------------------------
-- UPDATE THE INTERFACE
------------------------------------------------------------
if (database == "Currencies") then
_G[f_name.."_Count"]:SetVertexColor(color[1], color[2], color[3]);
else
_G[f_name.."_Objective"]:SetVertexColor(color[1], color[2], color[3]);
end
-- format notification message
local hud = "|cFF"..color[4]..data.count.."/"..data.objective.."|r";
if data.item then
if itemLink then
info = itemLink.." "..hud;
elseif FI_SV_CONFIG.debug then
print("[FI_Progress] Missing itemLink! (obj)"); --debug
end
elseif data.name then
info = currencyName.." "..hud;
elseif FI_SV_CONFIG.debug then
print("[FI_Progress] Objective notification failed!"); --debug
end
-- STANDARD PROGRESS
else
color = FI_SV_CONFIG.Colors.progress;
-- format message
local hud = "|cFF"..color[4].."x "..data.count.."|r";
if data.item then
if itemLink then
info = itemLink.." "..hud;
elseif FI_SV_CONFIG.debug then
print("[FI_Progress] Missing itemLink!"); --debug
end
elseif data.name then
info = currencyName.." "..hud;
elseif FI_SV_CONFIG.debug then
print("[FI_Progress] Progress notification failed!"); --debug
end
end
------------------------------------------------------------
-- NOTIFICATION
------------------------------------------------------------
if info and (not silent) and (not CursorHasItem()) then
local message = status.." "..info.." "..suffix;
FI_Alert(message, nil, sound, false, data.item, louder);
end
------------------------------------------------------------
-- LOGGING
------------------------------------------------------------
local log_entry = {};
log_entry["timestamp"] = time(); --seconds since "epoch"
log_entry["table"] = database;
log_entry["record_id"] = data.id;
if data.item then
log_entry["item"] = data.item;
log_entry["count"] = C_Item.GetItemCount(data.item);
elseif data.name then
log_entry["name"] = data.name;
log_entry["count"] = data.count;
elseif FI_SV_CONFIG.debug then
print("[FI_Progress] Log insert error."); --debug
end
table.insert(FI_SV_DATA.Log, log_entry);
if FI_SV_CONFIG.debug and false then
print("Progress tracking complete. Item ID: "..data.item..", Last Count: "..data.lastcount..", Count: "..data.count..", Objective: "..data.objective); --debug
end
end
-- (string) table name, (num) record id, (num) objective amount
function FI_Set_Objective( tbl_name, rec_id, amount )
local data = FI_DB.select(FI_SVPC_DATA[tbl_name], {id = rec_id}, true);
local f_name;
-- capture input
if amount then
if FI_SV_CONFIG.debug then print("[FI_Set_Objective] Table: "..tbl_name..", ID: "..rec_id..", Amount: "..amount); end
if (amount == data.objective) then
-- do nothing, input matches current objective
if FI_SV_CONFIG.debug then print("[FI_Set_Objective] Duplicate objective. Input: "..amount..", Current Objective: "..data.objective); end
else
-- store new objective
data = FI_DB.update(FI_SVPC_DATA[tbl_name], {id = data.id}, {objective = amount});
end
if data.name then
-- update the cache
FI_DB.cache(FI_SVPC_DATA, "Currencies");
end
end
-- update grapical interface if necessary
if data.item then
f_name = "FI_Button_"..data.id;
if (data.objective > 0) then
_G[f_name.."_Objective"]:Show();
else
_G[f_name.."_Objective"]:Hide();
end
elseif data.name then
--[[
f_name = "FI_CurrencyBar_"..data.id;
if (data.objective > 0) then
_G[f_name]:Show();
else
_G[f_name]:Hide();
end
]]
elseif FI_SV_CONFIG.debug then
print("[FI_Set_Objective] Missing data!"); return;
end
-- update interface
if data.item then
local precision;
if (string.len(data.objective) > 5) then
precision = 0;
else
precision = 1;
end
_G[f_name.."_Objective"]:SetText(LIB.ShortNum(data.objective,precision,4));
end
-- check progress
FI_Progress(data, true);
-- notify user
if amount then
local message,sound;
local color = {1,1,1,"FFFFFF"};
if (data.objective > 0) then
-- build message
if data.item then
-- items
local itemName, itemLink = GetItemInfo(data.item);
if itemLink then
message = "Farming objective set: "..data.objective.." "..itemLink;
elseif FI_SV_CONFIG.debug then
print("[FI_Set_Objective] Notification failed due to missing itemLink! Item ID: "..data.item); --debug
end
elseif data.name then
local nameOut = data.name
if type(data.name) == "table" then
nameOut = data.name.name
end
-- currencies
message = "Farming objective set: "..data.objective.." "..nameOut;
-- update the cache
FI_DB.cache(FI_SVPC_DATA, "Currencies");
else
print("[FI_Set_Objective] Notification failed!"); --exception
end
-- decide which sound to use
if (data.count >= data.objective) then
sound = 619; -- QUESTCOMPLETED
else
sound = 618; -- QUESTADDED
end
else
-- inform user we are erasing an objective
if data.success then
message = "Farming objective removed.";
sound = false;
else