-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
251 lines (227 loc) · 5.34 KB
/
storage.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
package mtgprice
import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strings"
"sync"
"github.com/broady/mtgprice/gatherer"
"github.com/broady/mtgprice/tcg"
"github.com/cznic/kv"
)
type Opts struct {
Filename, CardData string
}
type Client struct {
db *kv.DB
mu sync.RWMutex
// Immutable.
cards map[string]*CardInfo
}
type CardInfo struct {
Name string `json:"name"`
Names []string `json:"names,omitempty"`
// NOTE: there is one card with .5 mana cost.
CMC float64 `json:"cmc"`
MultiverseID *int `json:"multiverseid,omitempty"`
ManaCost string `json:"manaCost"`
Rarity string `json:"rarity"`
Power string `json:"power,omitempty"`
Toughness string `json:"toughness,omitempty"`
Type string `json:"type"`
Types []string `json:"types"`
Colors []string `json:"colors"`
Text string `json:"text"`
Legalities []Legality `json:"legalities"`
}
type Legality struct {
Format string `json:"format"`
Legality string `json:"legality"`
}
func (c *CardInfo) String() string {
return c.Name
}
func (c *CardInfo) Detail() string {
s := fmt.Sprintf("%v %v", c.Name, c.ManaCost)
for _, t := range c.Types {
if t == "Creature" {
s += fmt.Sprintf(" (%v/%v)", c.Power, c.Toughness)
}
}
s += "\n" + c.Type
if c.Text != "" {
s += "\n" + c.Text
}
legality := map[string][]string{}
for _, l := range c.Legalities {
if l.Legality != "Legal" {
legality[l.Legality] = append(legality[l.Legality], l.Format)
}
}
for l, fs := range legality {
s += fmt.Sprintf("\n%v: %v", l, strings.Join(fs, ", "))
}
return s
}
// entry represents an entry for the database.
type entry struct {
TCGPrice *tcg.Price
GathererInfo *gatherer.CardInfo
}
func Open(opts Opts) (c *Client, err error) {
c = new(Client)
c.cards, err = readCardData(opts.CardData)
if err != nil {
return
}
c.db, err = kv.Open(opts.Filename, &kv.Options{})
if err != nil {
log.Printf("Creating new database... %v", err)
c.db, err = kv.Create(opts.Filename, &kv.Options{})
}
return
}
func readCardData(fn string) (map[string]*CardInfo, error) {
f, err := os.Open(fn)
if err != nil {
return nil, err
}
var cardData map[string]*CardInfo
if err := json.NewDecoder(f).Decode(&cardData); err != nil {
return nil, fmt.Errorf("could not read card json file: %v", err)
}
// Normalize.
out := make(map[string]*CardInfo)
for _, card := range cardData {
if len(card.Names) != 0 {
out[strings.ToLower(strings.Join(card.Names, " & "))] = card
out[strings.ToLower(strings.Join(card.Names, " / "))] = card
out[strings.ToLower(strings.Join(card.Names, " // "))] = card
}
card.Name = normalizeCardName(card.Name)
out[strings.ToLower(card.Name)] = card
}
return out, nil
}
func (c *Client) Close() error {
if c != nil && c.db != nil {
return c.db.Close()
}
return nil
}
func normalizeCardName(s string) string {
s = strings.Replace(s, "Æ", "Ae", -1)
return strings.Replace(s, "’", "'", -1)
}
func (c *Client) CardInfo(cardName string) (ci CardInfo, ok bool) {
card, ok := c.cards[strings.ToLower(normalizeCardName(cardName))]
if ok {
return *card, ok
}
return
}
func (c *Client) getEntry(cardName string) (e entry, err error) {
cardName = normalizeCardName(cardName)
ci, ok := c.CardInfo(cardName)
if !ok {
return e, errors.New("card not found")
}
err = c.get(ci.Name, &e)
if err != nil && err != doesNotExistError {
return e, err
}
if err == nil && e.TCGPrice != nil && e.GathererInfo != nil {
//log.Printf("cache hit: %s", cardName)
return e, nil
}
touch := false
if e.TCGPrice == nil {
prices, err := c.priceForCard(ci)
if err != nil {
log.Println("Fetching price:", err)
} else {
e.TCGPrice = prices
touch = true
}
}
if e.GathererInfo == nil {
name := ci.Name
if len(ci.Names) != 0 {
name = strings.Join(ci.Names, " & ")
}
gInfo, err := gatherer.InfoByName(name)
if err != nil {
log.Println("Fetching gatherer:", err)
} else {
e.GathererInfo = gInfo
touch = true
}
}
if touch {
go c.set(ci.Name, e)
}
return e, nil
}
type info struct {
entry
CardInfo
}
func (c *Client) RichInfo(cardName string) (i info, err error) {
ci, ok := c.CardInfo(cardName)
if !ok {
return i, errors.New("could not find card")
}
i.CardInfo = ci
e, err := c.getEntry(cardName)
if err != nil {
return
}
i.entry = e
return
}
func (c *Client) PriceForCard(cardName string) (prices tcg.Price, err error) {
e, err := c.getEntry(cardName)
if err != nil {
return
}
prices = *e.TCGPrice
return
}
var doesNotExistError = errors.New("val does not exist")
func (c *Client) get(key string, val interface{}) error {
c.mu.RLock()
defer c.mu.RUnlock()
v, err := c.db.Get(nil, []byte(key))
if err != nil {
return err
}
if len(v) == 0 {
return doesNotExistError
}
return gob.NewDecoder(bytes.NewBuffer(v)).Decode(val)
}
func (c *Client) set(key string, val interface{}) error {
c.mu.Lock()
defer c.mu.Unlock()
b := new(bytes.Buffer)
err := gob.NewEncoder(b).Encode(val)
if err != nil {
return err
}
err = c.db.Set([]byte(key), b.Bytes())
if err != nil {
log.Printf("ERROR writing to cache: %v", err)
}
return err
}
func (c *Client) priceForCard(ci CardInfo) (prices *tcg.Price, err error) {
name := ci.Name
if len(ci.Names) != 0 {
name = strings.Join(ci.Names, " // ")
}
return tcg.Get(name)
}