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 pathredirect.go
119 lines (102 loc) · 2.61 KB
/
redirect.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
package main
import (
"math/rand"
"net/http"
"strings"
"github.com/mtgban/go-mtgban/mtgmatcher"
)
func Redirect(w http.ResponseWriter, r *http.Request) {
path := strings.TrimPrefix(r.URL.Path, "/go/")
fields := strings.Split(path, "/")
if len(fields) == 2 || len(fields) == 3 {
kind := fields[0]
store := fields[len(fields)-2]
hash := fields[len(fields)-1]
// Default to retail in short mode
if kind == store {
kind = "r"
}
if kind == "r" || kind == "i" {
for _, seller := range Sellers {
if seller != nil && seller.Info().Shorthand == store {
inv, err := seller.Inventory()
if err != nil {
http.NotFound(w, r)
return
}
// Look up the hash: mtgjson, scryfall, and tcgproductid in order
entries, found := inv[hash]
if !found {
entries, found = inv[mtgmatcher.Scryfall2UUID(hash)]
if !found {
entries = inv[mtgmatcher.Tcg2UUID(hash)]
}
}
for _, entry := range entries {
http.Redirect(w, r, entry.URL, http.StatusFound)
return
}
}
}
} else if kind == "b" {
for _, vendor := range Vendors {
if vendor != nil && vendor.Info().Shorthand == store {
bl, err := vendor.Buylist()
if err != nil {
http.NotFound(w, r)
return
}
// Look up the hash: mtgjson, scryfall, and tcgproductid in order
entries, found := bl[hash]
if !found {
entries, found = bl[mtgmatcher.Scryfall2UUID(hash)]
if !found {
entries = bl[mtgmatcher.Tcg2UUID(hash)]
}
}
for _, entry := range entries {
http.Redirect(w, r, entry.URL, http.StatusFound)
return
}
}
}
}
}
http.NotFound(w, r)
}
func RandomSearch(w http.ResponseWriter, r *http.Request) {
var uuid string
// Reuse the randomness of maps to our advantage
sets := mtgmatcher.GetSets()
for _, set := range sets {
if len(set.Cards) == 0 {
continue
}
index := rand.Intn(len(set.Cards))
uuid = set.Cards[index].UUID
break
}
v := r.URL.Query()
v.Set("q", uuid)
r.URL.RawQuery = v.Encode()
r.URL.Path = "/search"
http.Redirect(w, r, r.URL.String(), http.StatusFound)
}
func RandomSealedSearch(w http.ResponseWriter, r *http.Request) {
var uuid string
// Reuse the randomness of maps to our advantage
sets := mtgmatcher.GetSets()
for _, set := range sets {
if len(set.SealedProduct) == 0 || len(set.Booster) == 0 {
continue
}
index := rand.Intn(len(set.SealedProduct))
uuid = set.SealedProduct[index].UUID
break
}
v := r.URL.Query()
v.Set("q", uuid)
r.URL.RawQuery = v.Encode()
r.URL.Path = "/sealed"
http.Redirect(w, r, r.URL.String(), http.StatusFound)
}