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
/
product.go
604 lines (531 loc) · 14.1 KB
/
product.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
package main
import (
"log"
"sort"
"strings"
"time"
"github.com/mtgban/go-mtgban/mtgban"
"github.com/mtgban/go-mtgban/mtgmatcher"
"github.com/mtgban/go-mtgban/mtgmatcher/mtgjson"
)
type EditionEntry struct {
Name string
Code string
Date time.Time
Keyrune string
Size int
FmtDate string
Special bool
ShowFin bool
HasReg bool
HasFoil bool
HasRaritySpecial bool
HasRarityMythic bool
}
var categoryEdition = map[string]string{
"archenemy": "Boxed Sets",
"arsenal": "Commander Supplements",
"box": "Boxed Sets",
"commander": "Commander Decks",
"core": "Core Sets",
"draft_innovation": "Draft Experiments",
"duel_deck": "Deck Series",
"expansion": "Expansions",
"from_the_vault": "From the Vault Sets",
"funny": "Funny Sets",
"game": "Standalone Game",
"masterpiece": "Boxed Sets",
"masters": "Reprint Sets",
"memorabilia": "Boxed Sets",
"planechase": "Boxed Sets",
"premium_deck": "Deck Series",
"promo": "Boxed Sets",
"starter": "Starter Sets",
"vanguard": "Boxed Sets",
}
var categoryOverrides = map[string]string{
"CMB1": "masters",
"CMB2": "masters",
"PTG": "box",
"SS1": "arsenal",
"SS2": "arsenal",
"SS3": "arsenal",
"HOP": "game",
"PC2": "game",
"PCA": "game",
"E02": "game",
"ARC": "game",
}
var editionRenames = map[string]string{
"Duel Decks Anthology: Elves vs. Goblins": "Duel Decks Anthology",
"Media Inserts": "San Diego Comic-Con",
"Modern Horizons 1 Timeshifts": "Modern Horizons",
"Mystery Booster Playtest Cards 2019": "Mystery Booster Convention Edition 2019",
"Mystery Booster Playtest Cards 2021": "Mystery Booster Convention Edition 2021",
"Mystery Booster Retail Edition Foils": "Mystery Booster Retail Edition",
"World Championship Decks 1997": "World Championship Decks",
"Judge Gift Cards 2014": "Judge Gift Cards",
}
var sealedEditionSkips = map[string]string{
"Chronicles Japanese": "",
"Legends Italian": "",
"The Dark Italian": "",
"Rivals Quick Start Set": "",
"Modern Horizons": "",
}
func makeEditionEntry(set *mtgjson.Set, names ...string) EditionEntry {
date, _ := time.Parse("2006-01-02", set.ReleaseDate)
dateAfterMythic, _ := time.Parse("2006-01-02", "2008-10-01")
name := set.Name
if len(names) > 0 && names[0] != "" {
name = names[0]
}
special := false
var specialRarity bool
switch set.Code {
case "H1R",
"SCD":
special = true
case "PLIST",
"CLB",
"CMR",
"TSR",
"TSB":
specialRarity = true
}
return EditionEntry{
Name: name,
Code: set.Code,
Date: date,
Keyrune: strings.ToLower(set.KeyruneCode),
Size: len(set.Cards),
FmtDate: set.ReleaseDate,
Special: special,
ShowFin: !set.IsNonFoilOnly && !set.IsFoilOnly,
HasReg: !set.IsFoilOnly,
HasFoil: !set.IsNonFoilOnly,
HasRaritySpecial: specialRarity,
HasRarityMythic: date.After(dateAfterMythic),
}
}
func getAllEditions() ([]string, map[string]EditionEntry) {
sets := mtgmatcher.GetSets()
sortedEditions := make([]string, 0, len(sets))
listEditions := map[string]EditionEntry{}
for _, set := range sets {
sortedEditions = append(sortedEditions, set.Code)
listEditions[set.Code] = makeEditionEntry(set)
}
sort.Slice(sortedEditions, func(i, j int) bool {
return listEditions[sortedEditions[i]].Date.After(listEditions[sortedEditions[j]].Date)
})
return sortedEditions, listEditions
}
func getTreeEditions() ([]string, map[string][]EditionEntry) {
sets := mtgmatcher.GetSets()
sortedEditions := make([]string, 0, len(sets))
listEditions := map[string][]EditionEntry{}
for _, set := range sets {
entry := makeEditionEntry(set)
if set.ParentCode == "" {
// Skip if it was already added from the other case
_, found := listEditions[set.Code]
if found {
continue
}
// Create the head, list in the slice to be sorted
listEditions[set.Code] = []EditionEntry{entry}
sortedEditions = append(sortedEditions, set.Code)
} else {
// Find the very fist parent
topParentCode := set.ParentCode
for sets[topParentCode].ParentCode != "" {
topParentCode = sets[topParentCode].ParentCode
}
// Check if the head of the tree is already present
_, found := listEditions[topParentCode]
if !found {
// If not, create it
headEntry := makeEditionEntry(sets[topParentCode])
listEditions[topParentCode] = []EditionEntry{headEntry}
sortedEditions = append(sortedEditions, topParentCode)
}
// Append the new entry
listEditions[topParentCode] = append(listEditions[topParentCode], entry)
}
}
// Sort main list by date
sort.Slice(sortedEditions, func(i, j int) bool {
// Sort by name in case date is the same
if listEditions[sortedEditions[i]][0].Date == listEditions[sortedEditions[j]][0].Date {
return listEditions[sortedEditions[i]][0].Name < listEditions[sortedEditions[j]][0].Name
}
return listEditions[sortedEditions[i]][0].Date.After(listEditions[sortedEditions[j]][0].Date)
})
// Sort sublists by date
for _, key := range sortedEditions {
sort.Slice(listEditions[key], func(i, j int) bool {
// Keep the first element always first
if j == 0 {
return false
}
// Sort by name in case date is the same
if listEditions[key][i].Date == listEditions[key][j].Date {
return listEditions[key][i].Name < listEditions[key][j].Name
}
return listEditions[key][i].Date.After(listEditions[key][j].Date)
})
}
return sortedEditions, listEditions
}
func getSealedEditions() ([]string, map[string][]EditionEntry) {
sets := mtgmatcher.GetSets()
sortedEditions := []string{}
listEditions := map[string][]EditionEntry{}
for _, set := range sets {
if set.SealedProduct == nil {
continue
}
_, found := sealedEditionSkips[set.Name]
if found {
continue
}
// Skip Judge promos as they don't have a real product associated, except for 2014
if strings.HasPrefix(set.Name, "Judge Gift Cards") && !strings.HasSuffix(set.Name, "2014") {
continue
}
setType := set.Type
rename, found := categoryOverrides[set.Code]
if found {
setType = rename
}
category, found := categoryEdition[setType]
if !found {
category = set.Type
}
rename = editionRenames[set.Name]
entry := makeEditionEntry(set, rename)
listEditions[category] = append(listEditions[category], entry)
}
for key := range listEditions {
sort.Slice(listEditions[key], func(i, j int) bool {
return listEditions[key][i].Date.After(listEditions[key][j].Date)
})
sortedEditions = append(sortedEditions, key)
}
sort.Slice(sortedEditions, func(i, j int) bool {
return listEditions[sortedEditions[i]][0].Date.After(listEditions[sortedEditions[j]][0].Date)
})
return sortedEditions, listEditions
}
type ReprintEntry struct {
UUID string
SetCode string
Date time.Time
Price float64
}
const (
YearsBeforeReprint = 2
MinimumReprintPrice = 3.0
)
// Produce a map of card : []ReprintEntry containing array reprints sorted by age
func getReprintsGlobal() ([]string, map[string][]ReprintEntry) {
var tcgLow mtgban.InventoryRecord
var tcgMarket mtgban.InventoryRecord
for _, seller := range Sellers {
if seller == nil {
continue
}
if seller.Info().Shorthand == TCG_LOW {
tcgLow, _ = seller.Inventory()
}
if seller.Info().Shorthand == TCG_MARKET {
tcgMarket, _ = seller.Inventory()
}
}
sets := mtgmatcher.GetSets()
uuids := mtgmatcher.GetUUIDs()
var names []string
listReprints := map[string][]ReprintEntry{}
dupes := map[string]bool{}
for _, uuid := range uuids {
co, _ := mtgmatcher.GetUUID(uuid)
set, found := sets[co.SetCode]
if !found {
continue
}
// Skip very old stuff
switch set.Code {
case "PVAN", "OARC", "PCEL", "PDCI",
"LEG", "DRK", "ATQ", "ARN", "PTK",
"OPCA", "OHOP",
"FBB", "4BB", "DRKITA", "LEGITA", "RIN", "4EDALT", "BCHR":
continue
}
// Skip strange stuff
if co.IsReserved || co.Sealed || co.IsFunny || mtgmatcher.IsToken(co.Name) ||
co.BorderColor == "gold" || co.BorderColor == "silver" ||
co.HasPromoType(mtgjson.PromoTypePromoPack) ||
co.HasPromoType(mtgjson.PromoTypePrerelease) {
continue
}
// Ignore the foil printing of printed cards
if co.Foil && len(co.Finishes) > 1 {
continue
}
// Skip processed cards (using scryfallId to catch foil/nonfoil)
if dupes[co.Identifiers["scryfallId"]] {
continue
}
dupes[co.Identifiers["scryfallId"]] = true
// Load the date for the card
releaseDate := set.ReleaseDate
if co.OriginalReleaseDate != "" {
releaseDate = co.OriginalReleaseDate
}
printDate, err := time.Parse("2006-01-02", releaseDate)
if err != nil {
continue
}
var price float64
entries, found := tcgLow[co.UUID]
if !found {
entries, found = tcgMarket[co.UUID]
}
if found {
price = entries[0].Price
}
// Append to the results
listReprints[co.Name] = append(listReprints[co.Name], ReprintEntry{
UUID: co.UUID,
Price: price,
Date: printDate,
})
}
// Filter results
for name, reprints := range listReprints {
var shouldSkip bool
for i := range reprints {
// Skip cards that are not old enough
if time.Now().Sub(reprints[i].Date).Hours()/24/365 <= YearsBeforeReprint {
shouldSkip = true
break
}
// Skip cards that are too low or that don't have a price
price := reprints[i].Price
if price < MinimumReprintPrice {
shouldSkip = true
break
}
}
if shouldSkip {
delete(listReprints, name)
continue
}
names = append(names, name)
sort.Slice(reprints, func(i, j int) bool {
return reprints[i].Date.After(reprints[j].Date)
})
listReprints[name] = reprints
}
return names, listReprints
}
var ProductKeys = []string{
"TotalValueByTcgLow",
"TotalValueByTcgDirect",
"TotalValueByTcgLowMinusBulk",
"TotalValueBuylist",
"TotalValueDirectNet",
}
var ProductFoilKeys = []string{
"TotalFoilValueByTcgLow",
"TotalFoilValueByTcgDirect",
"TotalFoilValueByTcgLowMinusBulk",
"TotalFoilValueBuylist",
"TotalFoilValueDirectNet",
}
var ProductTitles = []string{
"by TCGLow",
"by TCG Direct",
"by TCGLow without Bulk",
"by CK Buylist",
"by TCG Direct (net)",
}
const (
bulkPrice = 2.99
)
// Check if it makes sense to keep two keep foil and nonfoil separate
func combineFinish(setCode string) bool {
set, err := mtgmatcher.GetSet(setCode)
if err != nil {
return false
}
setType := set.Type
rename, found := categoryOverrides[setCode]
if found {
setType = rename
}
switch setType {
case "commander",
"box",
"duel_deck",
"from_the_vault",
"masterpiece",
"memorabilia",
"promo":
return true
}
return false
}
func bulkBuylist(co *mtgmatcher.CardObject) float64 {
var price float64
switch co.Rarity {
case "mythic":
price = 0.30
if co.Foil {
price = 0.25
}
case "rare":
price = 0.08
if co.Foil {
price = 0.15
}
case "common", "uncommon":
price = 5.0 / 1000
if co.Foil {
price = 0.02
}
default:
if co.IsPromo {
price = 0.05
} else if mtgmatcher.IsBasicLand(co.Name) {
price = 0.01
if co.Foil {
price = 0.10
}
}
}
return price
}
func runSealedAnalysis() {
log.Println("Running set analysis")
var tcgInventory mtgban.InventoryRecord
var tcgDirect mtgban.InventoryRecord
for _, seller := range Sellers {
if seller == nil {
continue
}
if seller.Info().Shorthand == TCG_LOW {
tcgInventory, _ = seller.Inventory()
}
if seller.Info().Shorthand == TCG_DIRECT {
tcgDirect, _ = seller.Inventory()
}
}
var ckBuylist mtgban.BuylistRecord
var directNetBuylist mtgban.BuylistRecord
for _, vendor := range Vendors {
if vendor == nil {
continue
}
switch vendor.Info().Shorthand {
case "CK":
ckBuylist, _ = vendor.Buylist()
case "TCGDirectNet":
directNetBuylist, _ = vendor.Buylist()
}
}
runRawSetValue(tcgInventory, tcgDirect, ckBuylist, directNetBuylist)
}
func runRawSetValue(tcgInventory, tcgDirect mtgban.InventoryRecord, ckBuylist, directNetBuylist mtgban.BuylistRecord) {
inv := map[string]float64{}
invFoil := map[string]float64{}
invDirect := map[string]float64{}
invDirectFoil := map[string]float64{}
invNoBulk := map[string]float64{}
invNoBulkFoil := map[string]float64{}
bl := map[string]float64{}
blFoil := map[string]float64{}
blDirectNet := map[string]float64{}
blDirectNetFoil := map[string]float64{}
uuids := mtgmatcher.GetUUIDs()
for _, uuid := range uuids {
co, _ := mtgmatcher.GetUUID(uuid)
// Skip sets that are not well tracked upstream
if co.SetCode == "PMEI" || co.BorderColor == "gold" {
continue
}
// Determine whether to keep prices separated or combine them
useFoil := co.Foil && !combineFinish(co.SetCode)
var blPrice float64
entriesBl, found := ckBuylist[uuid]
if !found {
blPrice = bulkBuylist(co)
} else {
blPrice = entriesBl[0].BuyPrice
}
if useFoil {
blFoil[co.SetCode] += blPrice
} else {
bl[co.SetCode] += blPrice
}
entriesInv, found := tcgInventory[uuid]
if found {
if useFoil {
invFoil[co.SetCode] += entriesInv[0].Price
} else {
inv[co.SetCode] += entriesInv[0].Price
}
if entriesInv[0].Price > bulkPrice {
if useFoil {
invNoBulkFoil[co.SetCode] += entriesInv[0].Price
} else {
invNoBulk[co.SetCode] += entriesInv[0].Price
}
}
}
entriesInv, found = tcgDirect[uuid]
if found {
if useFoil {
invDirectFoil[co.SetCode] += entriesInv[0].Price
} else {
invDirect[co.SetCode] += entriesInv[0].Price
}
}
entriesBl, found = directNetBuylist[uuid]
if found {
if useFoil {
blDirectNetFoil[co.SetCode] += entriesBl[0].BuyPrice
} else {
blDirectNet[co.SetCode] += entriesBl[0].BuyPrice
}
}
}
for i, records := range []map[string]float64{
inv,
invDirect,
invNoBulk,
bl,
blDirectNet,
invFoil,
invDirectFoil,
invNoBulkFoil,
blFoil,
blDirectNetFoil,
} {
record := mtgban.InventoryRecord{}
for code, price := range records {
record[code] = append(record[code], mtgban.InventoryEntry{
Price: price,
})
}
// Keep the two key sets separate
key := ""
if i >= len(ProductKeys) {
key = ProductFoilKeys[i%len(ProductKeys)]
} else {
key = ProductKeys[i]
}
Infos[key] = record
}
}