This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsearch.go
1168 lines (1023 loc) · 29.7 KB
/
search.go
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
package main
import (
"encoding/csv"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/mtgban/go-mtgban/mtgban"
"github.com/mtgban/go-mtgban/mtgmatcher"
"github.com/mtgban/go-mtgban/mtgmatcher/mtgjson"
"golang.org/x/exp/slices"
)
const (
MaxSearchQueryLen = 200
MaxSearchResults = 100
TooLongMessage = "Your query planeswalked away, try a shorter one"
TooManyMessage = "More results available, try adjusting your filters"
NoResultsMessage = "No results found"
NoCardsMessage = "No cards found"
defaultSellerPriorityOpt = TCG_MARKET
defaultVendorPriorityOpt = "CK"
)
type SearchEntry struct {
ScraperName string
Shorthand string
Price float64
Credit float64
Ratio float64
Quantity int
URL string
NoQuantity bool
BundleIcon string
Country string
IndexCombined bool
Secondary float64
}
var AllConditions = []string{"INDEX", "NM", "SP", "MP", "HP", "PO"}
var AllNormalConditions = []string{"NM", "SP", "MP", "HP", "PO"}
func Search(w http.ResponseWriter, r *http.Request) {
sig := getSignatureFromCookies(r)
pageVars := genPageNav("Search", sig)
blocklistRetail, blocklistBuylist := getDefaultBlocklists(sig)
query := r.FormValue("q")
pageVars.IsSets = r.URL.Path == "/sets"
pageVars.PromoTags = mtgmatcher.AllPromoTypes()
pageVars.Nav = insertNavBar("Search", pageVars.Nav, []NavElem{
NavElem{
Name: "Sets",
Short: "📦",
Link: "/sets",
Active: pageVars.IsSets,
Class: "selected",
},
})
page := r.FormValue("page")
if page == "options" {
pageVars.Title = "Options"
for _, seller := range Sellers {
if seller == nil ||
seller.Info().SealedMode ||
slices.Contains(blocklistRetail, seller.Info().Shorthand) {
continue
}
pageVars.SellerKeys = append(pageVars.SellerKeys, seller.Info().Shorthand)
}
for _, vendor := range Vendors {
if vendor == nil ||
vendor.Info().SealedMode ||
slices.Contains(blocklistBuylist, vendor.Info().Shorthand) {
continue
}
pageVars.VendorKeys = append(pageVars.VendorKeys, vendor.Info().Shorthand)
}
render(w, "search.html", pageVars)
return
}
skipSellersOpt := readCookie(r, "SearchSellersList")
if skipSellersOpt != "" {
blocklistRetail = append(blocklistRetail, strings.Split(skipSellersOpt, ",")...)
}
skipVendorsOpt := readCookie(r, "SearchVendorsList")
if skipVendorsOpt != "" {
blocklistBuylist = append(blocklistBuylist, strings.Split(skipVendorsOpt, ",")...)
}
pageVars.SearchSort = readCookie(r, "SearchDefaultSort")
defaultSortOpt := r.FormValue("sort")
if defaultSortOpt != "" {
pageVars.SearchSort = defaultSortOpt
}
pageVars.SearchBest = (readCookie(r, "SearchListingPriority") == "prices")
pageVars.IsSealed = r.URL.Path == "/sealed"
canDownloadCSV, _ := strconv.ParseBool(GetParamFromSig(sig, "SearchDownloadCSV"))
canDownloadCSV = canDownloadCSV || (DevMode && !SigCheck)
pageVars.CanDownloadCSV = canDownloadCSV
pageVars.Nav = insertNavBar("Sets", pageVars.Nav, []NavElem{
NavElem{
Name: "Sealed",
Short: "🧱",
Link: "/sealed",
Active: pageVars.IsSealed,
Class: "selected",
},
})
if len(query) > MaxSearchQueryLen {
pageVars.ErrorMessage = TooLongMessage
render(w, "search.html", pageVars)
return
}
chartId := r.FormValue("chart")
// Check if query is a valid ID
co, err := mtgmatcher.GetUUID(chartId)
if err != nil {
chartId = ""
} else {
// Override the query when chart is requested
query = chartId
}
// If query is empty there is nothing to do
if query == "" {
// Hijack sealed list
if pageVars.IsSealed {
pageVars.EditionSort = SealedEditionsSorted
pageVars.EditionList = SealedEditionsList
render(w, "search.html", pageVars)
return
} else if pageVars.IsSets {
pageVars.EditionSort = TreeEditionsKeys
pageVars.EditionList = TreeEditionsMap
pageVars.TotalSets = TotalSets
pageVars.TotalCards = TotalCards
pageVars.TotalUnique = TotalUnique
sortOpt := r.FormValue("sort")
if sortOpt == "name" {
namedSort := make([]string, len(TreeEditionsKeys))
copy(namedSort, TreeEditionsKeys)
sort.Slice(namedSort, func(i, j int) bool {
return TreeEditionsMap[namedSort[i]][0].Name < TreeEditionsMap[namedSort[j]][0].Name
})
pageVars.EditionSort = namedSort
} else if sortOpt == "size" {
sizeSort := make([]string, len(TreeEditionsKeys))
copy(sizeSort, TreeEditionsKeys)
sort.Slice(sizeSort, func(i, j int) bool {
if TreeEditionsMap[sizeSort[i]][0].Size == TreeEditionsMap[sizeSort[j]][0].Size {
return TreeEditionsMap[sizeSort[i]][0].Name < TreeEditionsMap[sizeSort[j]][0].Name
}
return TreeEditionsMap[sizeSort[i]][0].Size > TreeEditionsMap[sizeSort[j]][0].Size
})
pageVars.EditionSort = sizeSort
}
render(w, "editions.html", pageVars)
return
}
render(w, "search.html", pageVars)
return
}
start := time.Now()
// Keep track of what was searched
pageVars.SearchQuery = query
pageVars.CondKeys = AllConditions
pageVars.Metadata = map[string]GenericCard{}
config := parseSearchOptionsNG(query, blocklistRetail, blocklistBuylist)
if pageVars.IsSealed {
config.SearchMode = "sealed"
}
if config.SortMode != "" {
pageVars.SearchSort = config.SortMode
pageVars.NoSort = true
}
var hideSyp bool
miscSearchOpts := readCookie(r, "SearchMiscOpts")
if miscSearchOpts != "" {
for _, optName := range strings.Split(miscSearchOpts, ",") {
switch optName {
// Skip promotional entries (unless specified)
case "hidePromos":
var skipOption bool
for _, filter := range config.CardFilters {
if filter.Name == "is" {
for _, value := range filter.Values {
if value == "promo" && !filter.Negate {
skipOption = true
}
}
}
}
if !skipOption {
config.CardFilters = append(config.CardFilters, FilterElem{
Name: "is",
Negate: true,
Values: []string{"promo"},
})
}
// Skip non-NM buylist prices
case "hideBLconds":
config.EntryFilters = append(config.EntryFilters, FilterEntryElem{
Name: "condition",
Values: []string{"NM"},
OnlyForVendor: true,
})
// Skip results with no prices
case "skipEmpty":
config.SkipEmptyRetail = true
config.SkipEmptyBuylist = true
case "noSyp":
hideSyp = true
}
}
}
// Hijack for csv download
downloadCSV := r.FormValue("downloadCSV")
if canDownloadCSV && downloadCSV != "" {
// Perform the search
selectedUUIDs, err := searchAndFilter(config)
if err != nil {
UserNotify("search", err.Error())
pageVars.InfoMessage = "Unable to download CSV right now"
render(w, "search.html", pageVars)
return
}
// Limit results to be processed
if len(selectedUUIDs) > MaxUploadProEntries {
selectedUUIDs = selectedUUIDs[:MaxUploadProEntries]
}
var enabledStores []string
if downloadCSV == "retail" {
for _, seller := range Sellers {
if seller != nil && !slices.Contains(blocklistRetail, seller.Info().Shorthand) {
enabledStores = append(enabledStores, seller.Info().Shorthand)
}
}
} else if downloadCSV == "buylist" {
for _, vendor := range Vendors {
if vendor != nil && !slices.Contains(blocklistBuylist, vendor.Info().Shorthand) {
enabledStores = append(enabledStores, vendor.Info().Shorthand)
}
}
}
var filename string
var results map[string]map[string]*BanPrice
if downloadCSV == "retail" {
results = getSellerPrices("scryfall", enabledStores, "", selectedUUIDs, "", true, true)
filename = "mtgban_retail_prices.csv"
} else if downloadCSV == "buylist" {
results = getVendorPrices("scryfall", enabledStores, "", selectedUUIDs, "", true, true)
filename = "mtgban_buylist_prices.csv"
} else {
pageVars.InfoMessage = "Unable to download CSV right now"
render(w, "search.html", pageVars)
return
}
w.Header().Set("Content-Type", "text/csv")
w.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
csvWriter := csv.NewWriter(w)
err = BanPrice2CSV(csvWriter, results, true, true, true)
if err != nil {
w.Header().Del("Content-Type")
UserNotify("search", err.Error())
pageVars.InfoMessage = "Unable to download CSV right now"
render(w, "search.html", pageVars)
}
return
}
allKeys, err := searchAndFilter(config)
if err != nil {
pageVars.InfoMessage = NoCardsMessage
render(w, "search.html", pageVars)
return
}
foundSellers, foundVendors := searchParallelNG(allKeys, config)
cleanQuery := config.CleanQuery
canShowAll := (len(config.CardFilters) != 0 || len(config.UUIDs) != 0)
// Only used in hashing searches, fill in data with what is available
if config.FullQuery != "" {
pageVars.SearchQuery = config.FullQuery
}
// If SkipEmptyBuylist or SkipEmptyRetail are set, we need to remove ids from allKeys
if config.SkipEmptyBuylist {
var filteredKeys []string
// Skip if nothing was found in buylist
for _, cardId := range allKeys {
if len(foundVendors[cardId]) == 0 {
continue
}
filteredKeys = append(filteredKeys, cardId)
}
allKeys = filteredKeys
}
if config.SkipEmptyRetail {
var filteredKeys []string
// Skip if nothing was found in retail or only INDEX entries were found
for _, cardId := range allKeys {
if len(foundSellers[cardId]) == 0 ||
(len(foundSellers[cardId]) == 1 && len(foundSellers[cardId]["INDEX"]) != 0) {
continue
}
filteredKeys = append(filteredKeys, cardId)
}
allKeys = filteredKeys
}
// Early exit if there no matches are found
if len(allKeys) == 0 {
pageVars.InfoMessage = NoResultsMessage
render(w, "search.html", pageVars)
return
}
// Allow displaying the "search all" link only when something
// was searched and no options were specified for it
pageVars.CanShowAll = cleanQuery != "" && canShowAll
pageVars.CleanSearchQuery = cleanQuery
// Update page title
if cleanQuery != "" {
pageVars.Title += ": " + cleanQuery
}
// Save stats
pageVars.TotalUnique = len(allKeys)
// Needed to load search in Upload
if canDownloadCSV {
pageVars.CardHashes = allKeys
}
// Sort sets as requested, default to chronological
switch pageVars.SearchSort {
case "alpha":
sort.Slice(allKeys, func(i, j int) bool {
return sortSetsAlphabetical(allKeys[i], allKeys[j])
})
case "retail":
retSeller := readCookie(r, "SearchSellersPriority")
if retSeller == "" {
retSeller = defaultSellerPriorityOpt
}
sort.Slice(allKeys, func(i, j int) bool {
return sortSetsByRetail(allKeys[i], allKeys[j], retSeller)
})
case "buylist":
blVendor := readCookie(r, "SearchVendorsPriority")
if blVendor == "" {
blVendor = defaultVendorPriorityOpt
}
sort.Slice(allKeys, func(i, j int) bool {
return sortSetsByBuylist(allKeys[i], allKeys[j], blVendor)
})
default:
sort.Slice(allKeys, func(i, j int) bool {
return sortSets(allKeys[i], allKeys[j])
})
}
// Invert the slice if requested
reverseSort, _ := strconv.ParseBool(r.FormValue("reverse"))
if reverseSort {
for i, j := 0, len(allKeys)-1; i < j; i, j = i+1, j-1 {
allKeys[i], allKeys[j] = allKeys[j], allKeys[i]
}
}
pageVars.ReverseMode = reverseSort
// If results can't fit in one page, chunk response and enable pagination
if len(allKeys) > MaxSearchResults {
pageVars.TotalIndex = len(allKeys)/MaxSearchResults + 1
// Parse the requested input page
pageIndex, _ := strconv.Atoi(r.FormValue("p"))
if pageIndex <= 1 {
pageIndex = 1
} else if pageIndex > pageVars.TotalIndex {
pageIndex = pageVars.TotalIndex
}
// Assign the current page index to enable pagination
pageVars.CurrentIndex = pageIndex
// Initialize previous and next pagination links
if pageVars.CurrentIndex > 0 {
pageVars.PrevIndex = pageVars.CurrentIndex - 1
}
if pageVars.CurrentIndex < pageVars.TotalIndex {
pageVars.NextIndex = pageVars.CurrentIndex + 1
}
// Chop results where needed
head := MaxSearchResults * (pageIndex - 1)
tail := MaxSearchResults * pageIndex
if tail > len(allKeys) {
tail = len(allKeys)
}
allKeys = allKeys[head:tail]
}
// Load up image links and other metadata
for _, cardId := range allKeys {
_, found := pageVars.Metadata[cardId]
if !found {
pageVars.Metadata[cardId] = uuid2card(cardId, false, true)
if hideSyp {
meta := pageVars.Metadata[cardId]
meta.SypList = false
pageVars.Metadata[cardId] = meta
}
}
if pageVars.Metadata[cardId].Reserved {
pageVars.HasReserved = true
}
if pageVars.Metadata[cardId].Stocks {
pageVars.HasStocks = true
}
if pageVars.Metadata[cardId].SypList {
pageVars.HasSypList = true
}
}
// Optionally sort according to price
if pageVars.SearchBest {
for _, cardId := range allKeys {
// This skips INDEX and PO conditions
for _, cond := range mtgban.DefaultGradeTags {
_, found := foundSellers[cardId][cond]
if found {
sort.Slice(foundSellers[cardId][cond], func(i, j int) bool {
return foundSellers[cardId][cond][i].Price < foundSellers[cardId][cond][j].Price
})
}
_, found = foundVendors[cardId][cond]
if found {
sort.Slice(foundVendors[cardId][cond], func(i, j int) bool {
return foundVendors[cardId][cond][i].Price > foundVendors[cardId][cond][j].Price
})
}
}
}
}
// Readjust array of INDEX entires
for _, cardId := range allKeys {
_, found := foundSellers[cardId]
if !found {
continue
}
indexArray := foundSellers[cardId]["INDEX"]
tmp := indexArray[:0]
mkmIndex := -1
tcgIndex := -1
tcgEVIndex := -1
tcgEVDirctIndex := -1
// Iterate on array, always passthrough, except for specific entries
for i := range indexArray {
switch indexArray[i].ScraperName {
case MKM_LOW:
// Save reference to the array
tmp = append(tmp, indexArray[i])
mkmIndex = len(tmp) - 1
case MKM_TREND:
// If the reference is found, add a secondary price
// otherwise just leave it as is
if mkmIndex >= 0 {
tmp[mkmIndex].Secondary = indexArray[i].Price
tmp[mkmIndex].ScraperName = "MKM (Low / Trend)"
tmp[mkmIndex].IndexCombined = true
} else {
tmp = append(tmp, indexArray[i])
}
case TCG_LOW:
// Save reference to the array
tmp = append(tmp, indexArray[i])
tcgIndex = len(tmp) - 1
case TCG_MARKET:
// If the reference is found, add a secondary price
// otherwise just leave it as is
if tcgIndex >= 0 {
tmp[tcgIndex].Secondary = indexArray[i].Price
tmp[tcgIndex].ScraperName = "TCG (Low / Market)"
tmp[tcgIndex].IndexCombined = true
} else {
tmp = append(tmp, indexArray[i])
}
case TCG_DIRECT_LOW:
// Skip this one for search results
continue
case "TCG Low EV Mean":
// Save reference to the array
tmp = append(tmp, indexArray[i])
tcgEVIndex = len(tmp) - 1
tmp[tcgEVIndex].ScraperName = "TCG Low EV"
case "TCG Low EV Median":
// If the reference is found, add a secondary price
// otherwise just leave it as is
if tcgEVIndex >= 0 {
// Skip if prices match
if indexArray[i].Price == tmp[tcgEVIndex].Price {
continue
}
tmp[tcgEVIndex].Secondary = indexArray[i].Price
tmp[tcgEVIndex].ScraperName = "TCG Low EV (Mean / Median)"
tmp[tcgEVIndex].IndexCombined = true
} else {
tmp = append(tmp, indexArray[i])
}
case "TCG Direct (net) EV Mean":
// Save reference to the array
tmp = append(tmp, indexArray[i])
tcgEVDirctIndex = len(tmp) - 1
tmp[tcgEVDirctIndex].ScraperName = "Direct EV"
case "TCG Direct (net) EV Median":
// If the reference is found, add a secondary price
// otherwise just leave it as is
if tcgEVDirctIndex >= 0 {
// Skip if prices match
if indexArray[i].Price == tmp[tcgEVDirctIndex].Price {
continue
}
tmp[tcgEVDirctIndex].Secondary = indexArray[i].Price
tmp[tcgEVDirctIndex].ScraperName = "Direct EV (Mean / Median)"
tmp[tcgEVDirctIndex].IndexCombined = true
} else {
tmp = append(tmp, indexArray[i])
}
default:
tmp = append(tmp, indexArray[i])
}
}
foundSellers[cardId]["INDEX"] = tmp
}
pageVars.FoundSellers = foundSellers
pageVars.FoundVendors = foundVendors
pageVars.AllKeys = allKeys
// CHART ALL THE THINGS
if chartId != "" {
// Rebuild the search query by faking a uuid lookup
cfg := parseSearchOptionsNG(chartId, nil, nil)
pageVars.SearchQuery = cfg.FullQuery
// Retrieve data
labels, err := getDateAxisValues(chartId)
if err != nil {
pageVars.InfoMessage = "No chart data available"
} else {
pageVars.AxisLabels = labels
pageVars.ChartID = chartId
for _, config := range enabledDatasets {
if co.Sealed && !config.HasSealed {
continue
}
if !co.Sealed && config.OnlySealed {
continue
}
dataset, err := getDataset(chartId, labels, config)
if err != nil {
log.Println(err)
continue
}
pageVars.Datasets = append(pageVars.Datasets, dataset)
}
}
altId, err := mtgmatcher.Match(&mtgmatcher.Card{
Id: chartId,
Foil: !co.Foil,
})
if err == nil && altId != chartId {
pageVars.Alternative = altId
}
altId, err = mtgmatcher.Match(&mtgmatcher.Card{
Id: chartId,
Variation: "Etched",
})
if err == nil && altId != chartId {
pageVars.AltEtchedId = altId
}
pageVars.StocksURL = pageVars.Metadata[chartId].StocksURL
}
var source string
notifyTitle := "search"
utm := r.FormValue("utm_source")
if utm == "banbot" {
id := r.FormValue("utm_affiliate")
source = fmt.Sprintf("banbot (%s)", id)
} else if utm == "autocard" {
source = "autocard anywhere"
} else if chartId != "" {
source = "chart page"
notifyTitle = "chart"
} else {
u, err := url.Parse(r.Referer())
if err != nil {
log.Println(err)
source = "n/a"
} else {
if strings.Contains(u.Host, "mtgban") {
source = u.Path
} else {
// Avoid automatic URL expansion in Discord
source = fmt.Sprintf("<%s>", u.String())
}
}
}
user := GetParamFromSig(sig, "UserEmail")
msg := fmt.Sprintf("[%s] from %s by %s (took %v)", query, source, user, time.Since(start))
UserNotify(notifyTitle, msg)
LogPages["Search"].Println(msg)
if DevMode {
log.Println(msg)
}
if DevMode {
start = time.Now()
}
render(w, "search.html", pageVars)
if DevMode {
log.Println("render took", time.Since(start))
}
}
func searchSellersNG(cardIds []string, config SearchConfig) (foundSellers map[string]map[string][]SearchEntry) {
// Allocate memory
foundSellers = map[string]map[string][]SearchEntry{}
storeFilters := config.StoreFilters
priceFilters := config.PriceFilters
entryFilters := config.EntryFilters
// Search sellers
for _, seller := range Sellers {
if shouldSkipStoreNG(seller, storeFilters) {
continue
}
// Get inventory
inventory, err := seller.Inventory()
if err != nil {
continue
}
for _, cardId := range cardIds {
entries, found := inventory[cardId]
if !found {
continue
}
// Loop thorugh available conditions
for _, entry := range entries {
// Skip cards that have not the desired condition
if !seller.Info().MetadataOnly && shouldSkipEntryNG(entry, entryFilters) {
continue
}
// Skip cards that don't match desired pricing
if shouldSkipPriceNG(cardId, entry, priceFilters) {
continue
}
// Check if card already has any entry
_, found := foundSellers[cardId]
if !found {
foundSellers[cardId] = map[string][]SearchEntry{}
}
// Set conditions - handle the special TCG one that appears
// at the top of the results
conditions := entry.Conditions
if seller.Info().MetadataOnly {
conditions = "INDEX"
}
// Only add Poor prices if there are no NM and SP entries
if conditions == "PO" && len(foundSellers[cardId]["NM"]) != 0 && len(foundSellers[cardId]["SP"]) != 0 {
continue
}
icon := ""
name := seller.Info().Name
switch name {
case TCG_MAIN:
name = "TCGplayer"
case TCG_DIRECT:
name = "TCGplayer Direct"
icon = "img/misc/direct.png"
case CT_ZERO:
icon = "img/misc/zero.png"
case CT_STANDARD_SEALED:
name = CT_STANDARD
case CT_ZERO_SEALED:
name = CT_ZERO
icon = "img/misc/zero.png"
}
// Prepare all the deets
res := SearchEntry{
ScraperName: name,
Shorthand: seller.Info().Shorthand,
Price: entry.Price,
Quantity: entry.Quantity,
URL: entry.URL,
NoQuantity: seller.Info().NoQuantityInventory || seller.Info().MetadataOnly,
BundleIcon: icon,
Country: Country2flag[seller.Info().CountryFlag],
}
// Do not add the same data twice
if slices.Contains(foundSellers[cardId][conditions], res) {
continue
}
// Touchdown
foundSellers[cardId][conditions] = append(foundSellers[cardId][conditions], res)
}
}
}
return
}
func searchVendorsNG(cardIds []string, config SearchConfig) (foundVendors map[string]map[string][]SearchEntry) {
foundVendors = map[string]map[string][]SearchEntry{}
storeFilters := config.StoreFilters
priceFilters := config.PriceFilters
entryFilters := config.EntryFilters
for _, vendor := range Vendors {
if shouldSkipStoreNG(vendor, storeFilters) {
continue
}
buylist, err := vendor.Buylist()
if err != nil {
continue
}
for _, cardId := range cardIds {
entries, found := buylist[cardId]
if !found {
continue
}
for _, entry := range entries {
if shouldSkipEntryNG(entry, entryFilters) {
continue
}
if shouldSkipPriceNG(cardId, entry, priceFilters) {
continue
}
_, found = foundVendors[cardId]
if !found {
foundVendors[cardId] = map[string][]SearchEntry{}
}
conditions := entry.Conditions
icon := ""
name := vendor.Info().Name
switch name {
case TCG_DIRECT_NET:
icon = "img/misc/direct.png"
case "TCG Player Market":
name = "TCGplayer Trade-In"
case "Sealed EV Scraper":
name = "CK Buylist for Singles"
}
res := SearchEntry{
ScraperName: name,
Shorthand: vendor.Info().Shorthand,
Price: entry.BuyPrice,
Credit: entry.TradePrice,
Ratio: entry.PriceRatio,
Quantity: entry.Quantity,
URL: entry.URL,
BundleIcon: icon,
Country: Country2flag[vendor.Info().CountryFlag],
}
if slices.Contains(foundVendors[cardId][conditions], res) {
continue
}
foundVendors[cardId][conditions] = append(foundVendors[cardId][conditions], res)
}
}
}
return
}
func searchAndFilter(config SearchConfig) ([]string, error) {
query := config.CleanQuery
filters := config.CardFilters
var uuids []string
var err error
switch config.SearchMode {
case "exact":
uuids, err = mtgmatcher.SearchEquals(query)
case "any":
uuids, err = mtgmatcher.SearchContains(query)
case "prefix":
uuids, err = mtgmatcher.SearchHasPrefix(query)
case "hashing":
uuids = config.UUIDs
case "regexp":
uuids, err = mtgmatcher.SearchRegexp(query)
case "sealed":
uuids, err = mtgmatcher.SearchSealedEquals(query)
if err != nil {
uuids, err = mtgmatcher.SearchSealedContains(query)
}
case "mixed":
uuids, err = mtgmatcher.SearchSealedEquals(query)
if err != nil {
uuids, err = mtgmatcher.SearchSealedContains(query)
}
moreUUIDs, _ := mtgmatcher.SearchEquals(query)
uuids = append(uuids, moreUUIDs...)
default:
uuids, err = mtgmatcher.SearchEquals(query)
if err != nil {
uuids, err = mtgmatcher.SearchHasPrefix(query)
if err != nil {
uuids, err = mtgmatcher.SearchRegexp(query)
}
}
}
if err != nil {
uuids, err = attemptMatch(query)
if err != nil {
return nil, err
}
}
var selectedUUIDs []string
for _, uuid := range uuids {
if shouldSkipCardNG(uuid, filters) {
continue
}
selectedUUIDs = append(selectedUUIDs, uuid)
}
return selectedUUIDs, nil
}
// Try searching for cards usign the Match algorithm
func attemptMatch(query string) ([]string, error) {
var uuids []string
uuid, err := mtgmatcher.Match(&mtgmatcher.Card{
Name: query,
})
if err != nil {
var alias *mtgmatcher.AliasingError
if errors.As(err, &alias) {
uuids = alias.Probe()
} else {
// Unsupported case, give up
return nil, err
}
} else {
uuids = append(uuids, uuid)
}
// Repeat for foil and etched (only add if not previously found)
// Add as needed depending on the previous query result
for _, tag := range []string{"Foil", "Etched"} {
uuid, suberr := mtgmatcher.Match(&mtgmatcher.Card{
Name: query,
Variation: tag,
})
if err != nil && suberr != nil {
var alias *mtgmatcher.AliasingError
if errors.As(suberr, &alias) {
for _, extra := range alias.Probe() {
if !slices.Contains(uuids, extra) {
uuids = append(uuids, extra)
}
}
}
} else if !slices.Contains(uuids, uuid) {
uuids = append(uuids, uuid)
}
}
return uuids, nil
}
func searchParallelNG(cardIds []string, config SearchConfig) (foundSellers map[string]map[string][]SearchEntry, foundVendors map[string]map[string][]SearchEntry) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
if !config.SkipRetail {
foundSellers = searchSellersNG(cardIds, config)
}
wg.Done()
}()
go func() {
if !config.SkipBuylist {
foundVendors = searchVendorsNG(cardIds, config)
}
wg.Done()
}()
wg.Wait()
return
}
type SortingData struct {
co *mtgmatcher.CardObject
releaseDate time.Time
parentCode string
}
func getSortingData(uuid string) (*SortingData, error) {
co, err := mtgmatcher.GetUUID(uuid)
if err != nil {
return nil, err
}
set, err := mtgmatcher.GetSet(co.SetCode)
if err != nil {
return nil, err
}
releaseDate := set.ReleaseDate
if co.OriginalReleaseDate != "" {
releaseDate = co.OriginalReleaseDate
}
setDate, err := time.Parse("2006-01-02", releaseDate)