-
Notifications
You must be signed in to change notification settings - Fork 2
/
OrderPage.lua
2398 lines (2017 loc) · 84.4 KB
/
OrderPage.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
local CraftScan = select(2, ...)
CraftScanCrafterOrderListElementMixin = CreateFromMixins(TableBuilderRowMixin);
CraftScan.LIVE = {}
CraftScan.LIVE.customers = {}
local LID = CraftScan.CONST.TEXT;
local function L(id)
return CraftScan.LOCAL:GetText(id);
end
local function GetSortedProfessions()
local result = {}
for parentProfID, color in pairs(CraftScan.CONST.PROFESSION_COLORS) do
local profInfo = C_TradeSkillUI.GetProfessionInfoBySkillLineID(parentProfID);
table.insert(result, {
key = profInfo.professionName,
name = CraftScan.Utils.ColorizeText(profInfo.professionName, color),
ppID = parentProfID,
});
end
table.sort(result, function(lhs, rhs) return lhs.key < rhs.key; end)
for _, profession in ipairs(result) do
profession.key = nil;
end
return result;
end
function CraftScanCrafterOrderListElementMixin:Init(elementData)
self.order = elementData.order
-- self.browseType = elementData.browseType;
self.pageFrame = elementData.pageFrame;
self.contextMenu = elementData.contextMenu;
end
local function removeOrder(orders, order)
local customerInfo = CraftScan.OrderToCustomerInfo(order)
local response = customerInfo.responses[order.responseID]
local orderID = CraftScan.OrderToOrderID(order)
if CraftScan.State.activeOrder == orders[orderID] then
CraftScan.State.activeOrder = nil;
end
-- Wipe out any less granular reponses related to this one
local response = customerInfo.responses[order.responseID];
if not response then
-- A less_granular child that was already wiped out. Nothing left to do.
return
end
if response.less_granular then
for _, child in ipairs(response.less_granular) do
customerInfo.responses[child] = nil
end
end
customerInfo.responses[order.responseID] = nil
-- I somehow managed to get a random empty entry in the responses table.
-- Clear that out in case there's a bug somewhere creating them. No idea how
-- it happened. Hopefully just something during development.
for key, value in pairs(customerInfo.responses) do
if not next(value) then
customerInfo.responses[key] = nil;
end
end
if not next(customerInfo.responses) then
-- No more orders with this customer, so close out the frames related to them.
local liveCustomerInfo = CraftScan.LIVE.customers[order.customerName]
if liveCustomerInfo then
if liveCustomerInfo.chatFrame then
FCF_Close(liveCustomerInfo.chatFrame)
liveCustomerInfo.chatFrame = nil
end
CraftScan.LIVE.customers[order.customerName] = nil
end
CraftScan.DB.customers[order.customerName] = nil
end
-- Remove ourselves from the global list of displayed orders
orders[orderID] = nil
end
function CraftScan.GreetCustomer(button, order)
CraftScanScannerMenu:ClearAlert(order)
local response = CraftScan.OrderToResponse(order)
if button == "LeftButton" then
if not response.greeting_sent then
CraftScan.Utils.SendResponses(response.message, order.customerName)
response.greeting_sent = true
-- TODO: More efficient way to update the display?
CraftScanCraftingOrderPage:ShowGeneric()
else
-- After sending the initial greeting, subsequent clicks open a chat with the customer.
ChatFrame_SendTell(order.customerName, DEFAULT_CHAT_FRAME)
end
elseif button == "MiddleButton" then
-- Middle button to begin chat without the generated greeting
response.greeting_sent = true
ChatFrame_SendTell(order.customerName, DEFAULT_CHAT_FRAME)
elseif button == "RightButton" then
CraftScan.DismissOrder(order);
end
end
function CraftScan.DismissOrder(order)
removeOrder(CraftScan.DB.listed_orders, order)
CraftScanCraftingOrderPage:ShowGeneric()
end
function CraftScanCrafterOrderListElementMixin:OnClick(button)
CraftScan.GreetCustomer(button, self.order)
end
local chatTooltip = CraftScan.Utils.ChatHistoryTooltip:new();
function CraftScanCrafterOrderListElementMixin:OnLineEnter()
self.HighlightTexture:Show();
-- If the request has a specific item, toss up the item tooltip just like
-- the real crafting order page.
local response = CraftScan.OrderToResponse(self.order)
if response.recipeID then
local reagents = {};
local qualityIDs = C_TradeSkillUI.GetQualitiesForRecipe(response.recipeID);
local qualityIdx = qualityIDs and #qualityIDs or 0;
GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
GameTooltip:SetRecipeResultItem(response.recipeID, reagents, nil, nil, qualityIDs and qualityIDs[qualityIdx]);
end
-- In addition, pop up a tooltip that looks like the chat window. We copy
-- the chat window width (up to a limit that fits over the crafting window),
-- and the primary chat window's font settings. This seems to make the text
-- wrapping match and overall it looks pretty close to the real chat window
-- to give an easy refresher on prior interactions without popping out the
-- dedicated chat frame.
chatTooltip:Show("CraftScanChatHistoryTooltip", self, self.order,
string.format(L("Chat History"), CraftScan.NameAndRealmToName(self.order.customerName)));
end
function CraftScanCrafterOrderListElementMixin:OnLineLeave()
self.HighlightTexture:Hide();
GameTooltip:Hide();
chatTooltip:Hide();
ResetCursor();
end
local function CreateAcceptLinkedAccountDialog()
if not CraftScanComm:HavePendingPeerRequest() then
return;
end
local OnAccept = function(nickname)
CraftScanComm:AcceptPeerRequest(nickname);
end
local OnReject = function()
CraftScanComm:RejectPeerRequest();
end
CraftScan.Dialog.Show({
key = "accept_linked_account",
title = L("Accept Linked Account"),
submit = L("Accept Linked Account"),
OnAccept = OnAccept,
OnReject = OnReject,
elements = {
{
type = CraftScan.Dialog.Element.Text,
text = string.format(L(LID.ACCOUNT_LINK_ACCEPT_DST_INFO),
CraftScanComm:GetPendingPeerRequestCharacter(),
CraftScanComm:GetPendingPeerRequestPermissions()),
},
{
type = CraftScan.Dialog.Element.EditBox,
},
},
})
end
CraftScanCraftingOrderPageMixin = {} --CreateFromMixins(ProfessionsRecipeListPanelMixin);
function CraftScanCraftingOrderPageMixin:InitOrderListTable()
local orderList = self.BrowseFrame.OrderList;
orderList:SetHeight(CraftScan.DB.settings.order_list_height or 200);
local pad = 5;
local spacing = 1;
local view = CreateScrollBoxListLinearView(pad, pad, pad, pad, spacing);
view:SetElementInitializer("CraftScanCrafterOrderListElementTemplate", function(button, elementData)
button:Init(elementData);
end);
ScrollUtil.InitScrollBoxListWithScrollBar(orderList.ScrollBox, orderList.ScrollBar, view);
end
function CraftScanCraftingOrderPageMixin:UpdateFilterResetVisibility()
self.BrowseFrame.LeftPanel.CrafterList.FilterButton.ResetButton:SetShown(
not CraftScan.IsUsingDefaultFilters(ignoreSkillLine));
end
function CraftScanCraftingOrderPageMixin:GetDesiredPageWidth()
return 1105;
end
local function UpdateAnalyticsEnabled()
local enabled = CraftScan.DB.analytics.enabled;
if enabled then
CraftScanCraftingOrderPage:EnableAnalytics()
else
CraftScanCraftingOrderPage:DisableAnalytics()
end
end
function CraftScanCraftingOrderPageMixin:OnLoad()
CraftScan.Utils.onLoad(function()
self:ResetSortOrder() -- self.InitButtons()
self:InitOrderListTable()
self:SetupOrderListTable()
UpdateAnalyticsEnabled();
end);
end
function CraftScanCraftingOrderPageMixin:OnShow()
CraftScanScannerMenu:ClearPulses()
self:ShowGeneric()
local icon = CraftScan.Utils.GetCurrentProfessionIcon();
self:SetPortraitToAsset(icon or 4620670);
-- Since we are a UIPanel, Bliz tries to close all other windows, including
-- the dialog we might try to open, so wait a sec then open any incoming
-- requests.
C_Timer.After(1, CreateAcceptLinkedAccountDialog);
end
function CraftScanCraftingOrderPageMixin:OnHide()
end
local function getOrderName(response)
if response.itemID then
local item = Item:CreateFromItemID(response.itemID);
return item:GetItemName()
else
return response.professionName;
end
end
local function ApplySortOrder(sortOrder, lhsOrder, rhsOrder)
-- CraftScan.ChatOrderSortOrder = EnumUtil.MakeEnum("CustomerName", "CrafterName", "ProfessionName", "ItemName", "Interaction"); -- , "Sent");
local lhs = CraftScan.OrderToResponse(lhsOrder);
local rhs = CraftScan.OrderToResponse(rhsOrder);
if sortOrder == CraftScan.ChatOrderSortOrder.ItemName then
local lhsName = getOrderName(lhs)
local rhsName = getOrderName(rhs)
return SortUtil.CompareUtf8i(lhsName, rhsName)
elseif sortOrder == CraftScan.ChatOrderSortOrder.CustomerName then
return SortUtil.CompareUtf8i(lhsOrder.customerName, rhsOrder.customerName)
elseif sortOrder == CraftScan.ChatOrderSortOrder.CrafterName then
return SortUtil.CompareUtf8i(lhs.crafterName, rhs.crafterName)
elseif sortOrder == CraftScan.ChatOrderSortOrder.ProfessionName then
return SortUtil.CompareUtf8i(lhs.professionName, rhs.professionName)
elseif sortOrder == CraftScan.ChatOrderSortOrder.Time then
local now = time()
local lhsAge = math.floor(now - lhs.time)
local rhsAge = math.floor(now - rhs.time)
return SortUtil.CompareNumeric(lhsAge, rhsAge)
end
return 0;
end
local function PurgeOldOrders()
local orders = CraftScan.DB.listed_orders
local now = time()
local old = {}
local timeout = CraftScan.Utils.GetSetting('customer_timeout') * 60;
for orderID, order in pairs(orders) do
local response = CraftScan.OrderToResponse(order)
if not response or now - response.time > timeout then
table.insert(old, order)
end
end
for _, order in ipairs(old) do
removeOrder(orders, order)
end
return #old ~= 0
end
local function UpdateCells()
for customer, customerInfo in pairs(CraftScan.LIVE.customers) do
for _, response in pairs(customerInfo.responses) do
if response.updateAge then
response.updateAge()
end
end
end
if PurgeOldOrders() then
CraftScanCraftingOrderPage:ShowGeneric()
else
C_Timer.After(5, UpdateCells)
end
end
local function SortItemsByComparator(items, keys, comparator)
table.sort(items, function(lhs, rhs)
local cmp = comparator(keys.primarySort.order, lhs, rhs);
if cmp ~= 0 then
if keys.primarySort.ascending then
return cmp < 0
else
return cmp > 0
end
end
if keys.secondarySort then
cmp = comparator(keys.secondarySort.order, lhs, rhs);
if keys.secondarySort.ascending then
return cmp < 0
else
return cmp > 0
end
end
return false;
end);
end
function CraftScanCraftingOrderPageMixin:DisableAnalytics()
self.BrowseFrame.AnalyticsTable:Hide();
self.BrowseFrame.ResizeButton:Hide();
self.BrowseFrame.OrderList:SetHeight(self.BrowseFrame.LeftPanel:GetHeight());
end
function CraftScanCraftingOrderPageMixin:EnableAnalytics()
self.BrowseFrame.AnalyticsTable:Init();
self.BrowseFrame.AnalyticsTable:Show();
self.BrowseFrame.ResizeButton:Show();
self.BrowseFrame.OrderList:SetHeight(CraftScan.DB.settings.order_list_height or 250)
end
function CraftScanCraftingOrderPageMixin:UpdateAnalytics()
if not self:IsShown() then return end
self.BrowseFrame.AnalyticsTable:Refresh();
end
function CraftScanCraftingOrderPageMixin:ShowGeneric()
local scrollBox = self.BrowseFrame.OrderList.ScrollBox;
scrollBox:Show();
local dataProvider = CreateDataProvider();
scrollBox:SetDataProvider(dataProvider);
local orders = {}
for _, order in pairs(CraftScan.DB.listed_orders) do
table.insert(orders, order)
end
SortItemsByComparator(orders, self, ApplySortOrder);
if #orders == 0 then
self.BrowseFrame.OrderList.ResultsText:SetText(PROFESSIONS_CUSTOMER_NO_ORDERS);
self.BrowseFrame.OrderList.ResultsText:Show();
else
self.BrowseFrame.OrderList.ResultsText:Hide();
end
for i, order in ipairs(orders) do
dataProvider:Insert({
order = order,
pageFrame = self,
contextMenu = self.BrowseFrame.OrderList.ContextMenu
});
end
scrollBox:SetDataProvider(dataProvider);
ScrollUtil.AddManagedScrollBarVisibilityBehavior(scrollBox, self.BrowseFrame.OrderList.ScrollBar, nil, nil);
C_Timer.After(5, UpdateCells)
end
function CraftScanCraftingOrderPageMixin:SortOrderIsValid(sortOrder)
return sortOrder == CraftScan.ChatOrderSortOrder.ItemName or sortOrder == CraftScan.ChatOrderSortOrder.CustomerName
end
function CraftScanCraftingOrderPageMixin:ResetSortOrder()
self.primarySort = {
order = CraftScan.ChatOrderSortOrder.Time,
ascending = false
};
self.secondarySort = nil;
if self.tableBuilder then
for frame in self.tableBuilder:EnumerateHeaders() do
frame:UpdateArrow();
end
end
end
function CraftScanCraftingOrderPageMixin:GetSortOrder()
return self.primarySort.order, self.primarySort.ascending;
end
function CraftScanCraftingOrderPageMixin:SetSortOrder(sortOrder)
if self.primarySort.order == sortOrder then
self.primarySort.ascending = not self.primarySort.ascending;
else
self.secondarySort = CopyTable(self.primarySort);
self.primarySort = {
order = sortOrder,
ascending = true
};
end
if self.tableBuilder then
for frame in self.tableBuilder:EnumerateHeaders() do
frame:UpdateArrow();
end
end
self:ShowGeneric()
-- if self.lastRequest then
-- self.lastRequest.offset = 0; -- Get a fresh page of sorted results
-- self:SendOrderRequest(self.lastRequest);
-- end
end
function CraftScanCraftingOrderPageMixin:SetupOrderListTable()
if not self.tableBuilder then
self.tableBuilder = CreateTableBuilder(nil, CraftScanOrderTableBuilderMixin);
local function ElementDataTranslator(elementData)
return elementData;
end
ScrollUtil.RegisterTableBuilder(self.BrowseFrame.OrderList.ScrollBox, self.tableBuilder, ElementDataTranslator);
local function ElementDataProvider(elementData)
return elementData;
end
self.tableBuilder:SetDataProvider(ElementDataProvider);
end
self.tableBuilder:Reset();
self.tableBuilder:SetColumnHeaderOverlap(2);
self.tableBuilder:SetHeaderContainer(self.BrowseFrame.OrderList.HeaderContainer);
self.tableBuilder:SetTableMargins(-3, 5);
self.tableBuilder:SetTableWidth(777);
local PTC = CraftScanTableConstants;
self.tableBuilder:AddFillColumn(self, PTC.NoPadding, 1.0, 8, PTC.ItemName.RightCellPadding,
CraftScan.ChatOrderSortOrder.ItemName, "CraftScanCrafterTableCellItemNameTemplate");
self.tableBuilder:AddFixedWidthColumn(self, PTC.NoPadding, PTC.CustomerName.Width, PTC.CustomerName.LeftCellPadding,
PTC.CustomerName.RightCellPadding, CraftScan.ChatOrderSortOrder.CustomerName,
"CraftScanCrafterTableCellCustomerNameTemplate");
self.tableBuilder:AddFixedWidthColumn(self, PTC.NoPadding, PTC.CrafterName.Width, PTC.CrafterName.LeftCellPadding,
PTC.CrafterName.RightCellPadding, CraftScan.ChatOrderSortOrder.CrafterName,
"CraftScanCrafterTableCellCrafterNameTemplate");
self.tableBuilder:AddFixedWidthColumn(self, PTC.NoPadding, PTC.ProfessionName.Width,
PTC.ProfessionName.LeftCellPadding, PTC.ProfessionName.RightCellPadding,
CraftScan.ChatOrderSortOrder.ProfessionName,
"CraftScanCrafterTableCellProfessionNameTemplate");
self.tableBuilder:AddUnsortableFixedWidthColumn(self, PTC.NoPadding, PTC.Interaction.Width,
PTC.Interaction.LeftCellPadding, PTC.Interaction.RightCellPadding, L("Replies"),
"CraftScanCrafterTableCellInteractionTemplate");
self.tableBuilder:AddFixedWidthColumn(self, PTC.NoPadding, PTC.Time.Width, PTC.Time.LeftCellPadding,
PTC.Time.RightCellPadding, CraftScan.ChatOrderSortOrder.Time, "CraftScanCrafterTableCellTimeTemplate");
-- AddUnsortableFixedWidthColumn
-- self.tableBuilder:AddFixedWidthColumn(self, PTC.NoPadding, PTC.Tip.Width, PTC.Tip.LeftCellPadding,
-- PTC.Tip.RightCellPadding, CraftScanSortOrder.Tip, "CraftScanCrafterTableCellActualCommissionTemplate");
-- self.tableBuilder:AddFixedWidthColumn(self, PTC.NoPadding, PTC.Reagents.Width, PTC.Reagents.LeftCellPadding,
-- PTC.Reagents.RightCellPadding, CraftScanSortOrder.Reagents, "CraftScanCrafterTableCellReagentsTemplate");
-- self.tableBuilder:AddFixedWidthColumn(self, PTC.NoPadding, PTC.Expiration.Width, PTC.Expiration.LeftCellPadding,
-- PTC.Expiration.RightCellPadding, CraftScanSortOrder.Expiration,
-- "CraftScanCrafterTableCellExpirationTemplate");
self.tableBuilder:Arrange();
end
local function ParentProfessionConfig(crafterInfo)
return CraftScan.DB.characters[crafterInfo.name].parent_professions[crafterInfo.parentProfessionID];
end
-- States: 0 - all unchecked
-- 1 - all checked
-- 2 - indeterminate
local function ForEachProfession(op)
local allTrue = true;
local allFalse = true;
for _, crafterConfig in pairs(CraftScan.DB.characters) do
for _, ppConfig in pairs(crafterConfig.parent_professions) do
if not ppConfig.character_disabled then
local result = op(ppConfig);
if result then
allFalse = false;
else
allTrue = false;
end
end
end
end
return allTrue and 1 or allFalse and 0 or 2;
end
local function ForEachCrafterFrame(op)
for _, frame in pairs(CraftScanCraftingOrderPage.BrowseFrame.LeftPanel.CrafterList.ScrollBox:GetFrames()) do
op(frame)
end
end
-- We re-use the same template for the header that we use for the rows. The
-- callbacks check if they are the header and act on the whole list if so.
local crafterListAll = nil
local function IsAll(self)
return self:GetParent() == crafterListAll
end
local function UpdateAllCheckBox(checkbox)
local stateBefore = checkbox.state;
checkbox.state =
ForEachProfession(function(ppConfig)
return ppConfig[checkbox.ppconfig_key];
end);
checkbox:UpdateAllCheckBoxDisplay();
if stateBefore == 2 and checkbox.state ~= 2 then
-- If we're applying the remote state, it has already taken the snapshot
-- for us, so we skip that step here.
if not CraftScanComm.applying_remote_state then
checkbox:RememberAllUserState();
end
end
end
local function InitAllCheckBox(checkbox)
if not checkbox.checked_texture then
checkbox.indeterminate_texture = checkbox:CreateTexture();
checkbox.indeterminate_texture:SetSize(12, 12);
checkbox.indeterminate_texture:SetAllPoints(checkbox);
checkbox.indeterminate_texture:SetAtlas(checkbox.indeterminate_atlas);
checkbox.indeterminate_texture:Hide();
checkbox.checked_texture = checkbox:GetCheckedTexture();
end
UpdateAllCheckBox(checkbox);
end
CraftScan_CrafterToggleMixin = {}
function CraftScan_CrafterToggleMixin:UpdateAllCheckBoxDisplay()
self.indeterminate_texture:Hide();
if self.state == 0 then
self:SetChecked(false);
elseif self.state == 1 then
self:SetCheckedTexture(self.checked_texture);
self:SetChecked(true);
else
self.indeterminate_texture:Show();
self:SetCheckedTexture(self.indeterminate_texture);
self:SetChecked(true);
end
end
function CraftScan_CrafterToggleMixin:RememberAllUserState()
-- Remember the most recent user specified state so we can restore it when
-- clicking through to the indeterminate state.
ForEachProfession(function(ppConfig)
ppConfig[self.ppconfig_key .. "_last"] = ppConfig[self.ppconfig_key];
end)
end
function CraftScan_CrafterToggleMixin:OnClick(button)
if IsAll(self) then
if self.state == 2 then
-- Moving from indeterminate to all disabled. Remember the user
-- configured states of each box so we can click back to
-- indeterminate to restore it.
self:RememberAllUserState();
self.state = 0;
elseif self.state == 1 then
local rememberedState = ForEachProfession(function(ppConfig) return ppConfig[self.ppconfig_key .. "_last"]; end);
if rememberedState == 2 then
self.state = 2;
else
self.state = 0;
end
else
self.state = 1;
end
if self.state ~= 2 then
-- Manually moving everything to all on or all off.
local checked = self.state == 1;
ForEachProfession(function(ppConfig)
ppConfig[self.ppconfig_key] = checked;
end)
ForEachCrafterFrame(function(frame)
frame[self:GetName()]:SetChecked(checked);
end)
-- Push only the ppConfig of all characters across
CraftScanComm:ShareAllPpCharacterModifications();
else
-- When moving to the indeterminate state manually, reapply the last
-- user state.
ForEachProfession(function(ppConfig)
ppConfig[self.ppconfig_key] = ppConfig[self.ppconfig_key .. "_last"];
end)
-- And update the display to match the saved config.
ForEachCrafterFrame(function(frame)
frame[self:GetName()]:InitState();
end)
-- Push only the ppConfig of all characters across
CraftScanComm:ShareAllPpCharacterModifications();
end
self:UpdateAllCheckBoxDisplay();
else
local crafterInfo = self:GetParent().crafterInfo;
ParentProfessionConfig(crafterInfo)[self.ppconfig_key] = self:GetChecked();
UpdateAllCheckBox(crafterListAll[self:GetName()])
local ppChangeOnly = true;
CraftScanComm:ShareCharacterModification(crafterInfo.name, crafterInfo.parentProfessionID, ppChangeOnly);
end
if GameTooltip:IsShown() then
self:SetTooltip();
end
end
function CraftScan_CrafterToggleMixin:InitState()
self:SetChecked(ParentProfessionConfig(self:GetParent().crafterInfo)[self.ppconfig_key])
end
function CraftScan_CrafterToggleMixin:SetTooltip()
GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
if self:GetChecked() then
GameTooltip:SetText(self.enabled_tooltip);
else
GameTooltip:SetText(self.disabled_tooltip);
end
end
function CraftScan_CrafterToggleMixin:OnEnter()
self:GetParent().HoverBackground:Show();
self:SetTooltip();
end
function CraftScan_CrafterToggleMixin:OnLeave()
self:GetParent().HoverBackground:Hide();
GameTooltip:Hide();
end
local function AddClearAnalytics(rootDescription, itemID)
local intervals = {
{ L("1 minute"), 60 },
{ L("15 minutes "), 15 * 60 },
{ L("1 hour"), 60 * 60 },
{ L("1 day"), 24 * 60 * 60 },
{ L("1 week "), 7 * 24 * 60 * 60 },
{ L("30 days"), 30 * 24 * 60 * 60 },
{ L("180 days"), 180 * 24 * 60 * 60 },
{ L("1 year"), 365 * 24 * 60 * 60 },
};
local recentData = rootDescription:CreateButton(L("Clear recent data"));
recentData:CreateTitle(L("Newer than"))
recentData:QueueDivider();
local function ClearItem(clearInfo)
if CraftScan.Analytics:ClearAnalyticsForItem(itemID, clearInfo) then
CraftScanCraftingOrderPage:UpdateAnalytics()
end
end
for _, interval in ipairs(intervals) do
recentData:CreateButton(interval[1], ClearItem, { seconds = interval[2], recent = true });
end
local oldData = rootDescription:CreateButton(L("Clear old data"));
oldData:CreateTitle(L("Older than"))
oldData:QueueDivider();
for i = #intervals, 1, -1 do
local interval = intervals[i];
oldData:CreateButton(interval[1], ClearItem, { seconds = interval[2], recent = false });
end
end
CraftScanAnalyticsTableListElementMixin = CreateFromMixins(TableBuilderRowMixin);
function CraftScanAnalyticsTableListElementMixin:OnClick(button)
if button == "LeftButton" then
CraftScan.Utils.ShowTooltipPlot(self, self.item.itemID, self.item.times, true);
end
if button == "RightButton" then
MenuUtil.CreateContextMenu(self, function(owner, rootDescription)
rootDescription:CreateTitle(owner.item.name);
local professionAssignment = rootDescription:CreateButton("Assign Profession");
local professions = GetSortedProfessions();
for _, profession in ipairs(professions) do
professionAssignment:CreateRadio(profession.name,
function(ppID) return CraftScan.DB.analytics.seen_items[owner.item.itemID].ppID == ppID; end,
function(ppID)
CraftScan.DB.analytics.seen_items[owner.item.itemID].ppID = ppID;
CraftScanCraftingOrderPage:UpdateAnalytics()
end,
profession.ppID);
end
AddClearAnalytics(rootDescription, owner.item.itemID);
end);
end
end
function CraftScanAnalyticsTableListElementMixin:OnLineEnter()
self.HighlightTexture:Show();
GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
GameTooltip:SetItemByID(self.item.itemID);
GameTooltip:Show();
CraftScan.Utils.ShowTooltipPlot(self, self.item.itemID, self.item.times);
end
function CraftScanAnalyticsTableListElementMixin:OnLineLeave()
self.HighlightTexture:Hide();
GameTooltip:Hide();
CraftScan.Utils.HideTooltipPlot();
end
function CraftScanAnalyticsTableListElementMixin:OnHide()
CraftScan.Utils.ForceHideTooltipPlot()
end
function CraftScanAnalyticsTableListElementMixin:Init(elementData)
self.item = elementData.item;
self.pageFrame = elementData.pageFrame;
self.contextMenu = elementData.contextMenu;
end
CraftScanAnalyticsTableMixin = {}
local analyticsInitialized = false
function CraftScanAnalyticsTableMixin:Init()
if not CraftScan.DB.analytics.enabled or analyticsInitialized then return; end
self:ResetSortOrder();
local pad = 5;
local spacing = 1;
local view = CreateScrollBoxListLinearView(pad, pad, pad, pad, spacing);
view:SetElementInitializer("CraftScanAnalyticsTableListElementTemplate", function(button, elementData)
button:Init(elementData);
end);
ScrollUtil.InitScrollBoxListWithScrollBar(self.ScrollBox, self.ScrollBar, view);
if not self.tableBuilder then
self.tableBuilder = CreateTableBuilder(nil, CraftScanAnalyticsTableBuilderMixin);
local function ElementDataTranslator(elementData)
return elementData;
end
ScrollUtil.RegisterTableBuilder(self.ScrollBox, self.tableBuilder, ElementDataTranslator);
local function ElementDataProvider(elementData)
return elementData;
end
self.tableBuilder:SetDataProvider(ElementDataProvider);
end
self.tableBuilder:Reset();
self.tableBuilder:SetColumnHeaderOverlap(2);
self.tableBuilder:SetHeaderContainer(self.HeaderContainer);
self.tableBuilder:SetTableMargins(-3, 5);
self.tableBuilder:SetTableWidth(777);
local ATC = CraftScanAnalyticsTableConstants;
self.tableBuilder:AddFillColumn(self, ATC.NoPadding, 1.0, 8, ATC.ItemName.RightCellPadding,
CraftScan.AnalyticsTableSortOrder.ItemName, "CraftScanAnalyticsCellItemNameTemplate");
self.tableBuilder:AddFixedWidthColumn(self, ATC.NoPadding, ATC.ProfessionName.Width,
ATC.ProfessionName.LeftCellPadding,
ATC.ProfessionName.RightCellPadding, CraftScan.AnalyticsTableSortOrder.ProfessionName,
"CraftScanAnalyticsTableCellProfessionNameTemplate");
self.tableBuilder:AddFixedWidthColumn(self, ATC.NoPadding, ATC.TotalSeen.Width, ATC.TotalSeen.LeftCellPadding,
ATC.TotalSeen.RightCellPadding, CraftScan.AnalyticsTableSortOrder.TotalSeen,
"CraftScanAnalyticsTableCellTotalSeenTemplate");
self.tableBuilder:AddFixedWidthColumn(self, ATC.NoPadding, ATC.TotalSeenFiltered.Width,
ATC.TotalSeenFiltered.LeftCellPadding,
ATC.TotalSeenFiltered.RightCellPadding, CraftScan.AnalyticsTableSortOrder.TotalSeenFiltered,
"CraftScanAnalyticsTableCellTotalSeenFilteredTemplate");
self.tableBuilder:AddFixedWidthColumn(self, ATC.NoPadding, ATC.AveragePerDay.Width, ATC.AveragePerDay
.LeftCellPadding,
ATC.AveragePerDay.RightCellPadding, CraftScan.AnalyticsTableSortOrder.AveragePerDay,
"CraftScanAnalyticsTableCellAveragePerDayTemplate");
self.tableBuilder:AddFixedWidthColumn(self, ATC.NoPadding, ATC.PeakPerHour.Width, ATC.PeakPerHour.LeftCellPadding,
ATC.PeakPerHour.RightCellPadding, CraftScan.AnalyticsTableSortOrder.PeakPerHour,
"CraftScanAnalyticsTableCellPeakPerHourTemplate");
self.tableBuilder:AddFixedWidthColumn(self, ATC.NoPadding, ATC.MedianPerCustomer.Width,
ATC.MedianPerCustomer.LeftCellPadding,
ATC.MedianPerCustomer.RightCellPadding, CraftScan.AnalyticsTableSortOrder.MedianPerCustomer,
"CraftScanAnalyticsTableCellMedianPerCustomerTemplate");
self.tableBuilder:AddFixedWidthColumn(self, ATC.NoPadding, ATC.MedianPerCustomerFiltered.Width,
ATC.MedianPerCustomerFiltered.LeftCellPadding,
ATC.MedianPerCustomerFiltered.RightCellPadding, CraftScan.AnalyticsTableSortOrder.MedianPerCustomerFiltered,
"CraftScanAnalyticsTableCellMedianPerCustomerFilteredTemplate");
self.tableBuilder:Arrange();
analyticsInitialized = true
end
function CraftScanAnalyticsTableMixin:SortOrderIsValid(sortOrder)
return true;
end
function CraftScanAnalyticsTableMixin:ResetSortOrder()
self.primarySort = {
order = CraftScan.AnalyticsTableSortOrder.TotalSeen,
ascending = false
};
self.secondarySort = nil;
if self.tableBuilder then
for frame in self.tableBuilder:EnumerateHeaders() do
frame:UpdateArrow();
end
end
end
function CraftScanAnalyticsTableMixin:GetSortOrder()
return self.primarySort.order, self.primarySort.ascending;
end
function CraftScanAnalyticsTableMixin:SetSortOrder(sortOrder)
if self.primarySort.order == sortOrder then
self.primarySort.ascending = not self.primarySort.ascending;
else
self.secondarySort = CopyTable(self.primarySort);
self.primarySort = {
order = sortOrder,
ascending = true
};
end
if self.tableBuilder then
for frame in self.tableBuilder:EnumerateHeaders() do
frame:UpdateArrow();
end
end
self:Refresh()
end
function CraftScanAnalyticsTableMixin:OnShow()
self:Refresh();
end
local function ApplyAnalyticsSortOrder(sortOrder, lhsItem, rhsItem)
if sortOrder == CraftScan.AnalyticsTableSortOrder.ItemName then
return SortUtil.CompareUtf8i(lhsItem.name, rhsItem.name);
elseif sortOrder == CraftScan.AnalyticsTableSortOrder.ProfessionName then
return SortUtil.CompareUtf8i(lhsItem.profession, rhsItem.profession);
elseif sortOrder == CraftScan.AnalyticsTableSortOrder.TotalSeen then
return SortUtil.CompareNumeric(lhsItem.totalSeen, rhsItem.totalSeen);
elseif sortOrder == CraftScan.AnalyticsTableSortOrder.TotalSeenFiltered then
return SortUtil.CompareNumeric(lhsItem.totalSeenFiltered, rhsItem.totalSeenFiltered);
elseif sortOrder == CraftScan.AnalyticsTableSortOrder.AveragePerDay then
return SortUtil.CompareNumeric(lhsItem.averagePerDay, rhsItem.averagePerDay);
elseif sortOrder == CraftScan.AnalyticsTableSortOrder.PeakPerHour then
return SortUtil.CompareNumeric(lhsItem.peakPerHour, rhsItem.peakPerHour);
elseif sortOrder == CraftScan.AnalyticsTableSortOrder.MedianPerCustomer then
return SortUtil.CompareNumeric(lhsItem.medianPerCustomer, rhsItem.medianPerCustomer);
elseif sortOrder == CraftScan.AnalyticsTableSortOrder.MedianPerCustomerFiltered then
return SortUtil.CompareNumeric(lhsItem.medianPerCustomerFiltered, rhsItem.medianPerCustomerFiltered);
end
return 0;
end
local function GetTimeStamp(timeEntry)
return CraftScan.Analytics.GetTimeStamp(timeEntry);
end
local function CalculateAveragePerDay(times)
local totalTimes = #times;
local timeSpan = GetTimeStamp(times[totalTimes]) - GetTimeStamp(times[1]);
local numberOfDays = math.max(1, timeSpan / 86400);
return totalTimes / numberOfDays;
end
local function CalculateMedianPerCustomer(times)
local indices = {}
local n = #times;
for i = 1, n do
indices[i] = i
end
table.sort(indices, function(lhs, rhs)
local lhsValue = type(times[lhs]) == "table" and times[lhs].c or 1;
local rhsValue = type(times[rhs]) == "table" and times[rhs].c or 1;
return lhsValue < rhsValue;
end)
-- Ignoring full correctness for simplicity, median is half way ignoring odd/even count.
local medianPerCustomer = times[indices[math.ceil(n / 2)]]
-- Count the number of entries that included a repeat count, then get the
-- median of those. We're sorted low-high with raw timestamps first. Walk
-- until we hit the first entry with a count. We likely have far more single
-- requests than repeats, so walk in reverse.
local begin = 1;
for i = #indices, 1, -1 do
if type(times[indices[i]]) ~= "table" then
begin = i + 1;
break;
end
end
local filteredTotal = n - begin;
local medianPerCustomerFiltered = times[indices[math.ceil(filteredTotal / 2) + begin]]
return type(medianPerCustomer) == "table" and medianPerCustomer.c or 1,
type(medianPerCustomerFiltered) == "table" and medianPerCustomerFiltered.c or 1;
end
local function CalculatePeakPerHour(times)
local peakHour = nil;
local peakCount = 0;
local j = 1;
for i = 1, #times do
local windowStart = GetTimeStamp(times[i]);
local windowEnd = windowStart + 3600;
-- Move to the end of the 1 hour window after i.
while j <= #times and GetTimeStamp(times[j]) <= windowEnd do
j = j + 1;
end
local count = j - i;
if count > peakCount then
peakCount = count;
peakHour = windowStart;
end
end
return peakCount, peakHour;
end
function CountDuplicates(array)
local count = 0
for _, value in ipairs(array) do
if type(value) == "table" and value.c and value.c > 1 then
count = count + 1
end
end
return count
end
function TableSize(tbl)
local count = 0
for _, _ in pairs(tbl) do
count = count + 1
end
return count
end
function CraftScanAnalyticsTableMixin:Refresh()
if not CraftScan.DB.analytics.enabled then return; end
self.ScrollBox:Show();
local seenItems = CraftScan.DB.analytics.seen_items;
if not seenItems or not next(seenItems) then
self.ResultsText:SetText(L("No analytics data"));
self.ResultsText:Show();
return;