This repository has been archived by the owner on Apr 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.go
181 lines (163 loc) · 4.63 KB
/
service.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
package brew
import (
"sort"
"strconv"
"strings"
"golang.org/x/net/context"
)
type Reader interface {
GetCards(context.Context, Search) ([]Card, error)
GetCardsByName(context.Context, string) ([]Card, error)
GetCard(context.Context, string) (Card, error)
GetRandomCardID(context.Context) (string, error)
GetSets(context.Context) ([]Set, error)
GetSet(context.Context, string) (Set, error)
GetColors(context.Context) ([]string, error)
GetSupertypes(context.Context) ([]string, error)
GetSubtypes(context.Context) ([]string, error)
GetTypes(context.Context) ([]string, error)
}
func toUniqueLower(things []string) []string {
seen := map[string]bool{}
sorted := []string{}
for _, thing := range things {
if _, found := seen[thing]; !found {
sorted = append(sorted, strings.ToLower(thing))
seen[thing] = true
}
}
sort.Strings(sorted)
return sorted
}
type Search struct {
Colors []string
Formats []string
IncludeMulticolor bool
Multicolor bool
MultiverseIDs []string
Names []string
Rarities []string
Sets []string
Status []string
Subtypes []string
Supertypes []string
Rules []string
Types []string
Limit int
Offset int
Page int
}
type Card struct {
Name string `json:"name"`
Id string `json:"id"`
Href string `json:"url,omitempty"`
StoreUrl string `json:"store_url"`
Types []string `json:"types,omitempty"`
Supertypes []string `json:"supertypes,omitempty"`
Subtypes []string `json:"subtypes,omitempty"`
Colors []string `json:"colors,omitempty"`
ConvertedCost int `json:"cmc"`
ManaCost string `json:"cost"`
Text string `json:"text"`
Power string `json:"power,omitempty"`
Toughness string `json:"toughness,omitempty"`
Loyalty int `json:"loyalty,omitempty"`
FormatMap map[string]string `json:"formats"`
Editions []Edition `json:"editions,omitempty"`
}
func (c *Card) Sets() []string {
sets := []string{}
for _, e := range c.Editions {
sets = append(sets, e.SetId)
}
return toUniqueLower(sets)
}
func (c *Card) Formats() []string {
v := []string{}
for format, status := range c.FormatMap {
if status == "legal" || status == "restricted" {
v = append(v, format)
}
}
return toUniqueLower(v)
}
func (c *Card) Status() []string {
v := []string{}
for _, status := range c.FormatMap {
v = append(v, status)
}
return toUniqueLower(v)
}
func (c *Card) Rarities() []string {
r := []string{}
for _, e := range c.Editions {
r = append(r, e.Rarity)
}
return toUniqueLower(r)
}
func (c *Card) MultiverseIds() []string {
r := []string{}
for _, e := range c.Editions {
r = append(r, strconv.Itoa(e.MultiverseId))
}
return toUniqueLower(r)
}
func (c *Card) Multicolor() bool {
return len(c.Colors) > 1
}
// Don't expose this
func (c *Card) Fill(r router) {
c.Href = r.CardURL(c.Id)
c.StoreUrl = TCGCardURL(c)
for i, _ := range c.Editions {
e := &c.Editions[i]
e.Href = r.EditionURL(e.MultiverseId)
e.SetUrl = r.SetURL(e.SetId)
e.ImageUrl = r.EditionImageURL(e.MultiverseId)
e.HTMLUrl = r.EditionHtmlURL(e.MultiverseId)
e.StoreUrl = TCGEditionURL(c, e)
e.Price = &Price{
Low: 0,
Average: 0,
High: 0,
}
}
}
type Edition struct {
Set string `json:"set"`
SetId string `json:"set_id"`
CardId string `json:"-"`
Watermark string `json:"watermark,omitempty"`
Rarity string `json:"rarity"`
Border string `json:"-"`
Artist string `json:"artist"`
MultiverseId int `json:"multiverse_id"`
Flavor string `json:"flavor,omitempty"`
Number string `json:"number"`
Layout string `json:"layout"`
Price *Price `json:"price,omitempty"`
Href string `json:"url,omitempty"`
ImageUrl string `json:"image_url,omitempty"`
SetUrl string `json:"set_url,omitempty"`
StoreUrl string `json:"store_url"`
HTMLUrl string `json:"html_url"`
}
type Price struct {
Low int `json:"low"`
Average int `json:"median"`
High int `json:"high"`
}
type Set struct {
Id string `json:"id"`
Name string `json:"name"`
Border string `json:"border"`
Type string `json:"type"`
Href string `json:"url"`
CardsUrl string `json:"cards_url"`
PriceGuide string `json:"-"`
Priced bool `json:"-"`
}
func (s *Set) Fill(r router) {
s.Href = r.SetURL(s.Id)
s.CardsUrl = r.SetCardsURL(s.Id)
}