-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
326 lines (288 loc) · 7.33 KB
/
common.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
package gobgg
import (
"encoding/xml"
"fmt"
"io"
"log"
"strconv"
"strings"
"time"
"golang.org/x/net/html"
)
// ItemType is the item type for the search api
type ItemType string
const (
// RPGItemType for rpg
RPGItemType ItemType = "rpgitem"
// VideGameType for video game
VideGameType ItemType = "videogame"
// BoardGameType for board game
BoardGameType ItemType = "boardgame"
// BoardGameAccessoryType for accessory
BoardGameAccessoryType ItemType = "boardgameaccessory"
// BoardGameExpansionType for expansion
BoardGameExpansionType ItemType = "boardgameexpansion"
)
// SimpleString is the string
type SimpleString struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
}
func (s *SimpleString) String() string {
if s == nil {
return ""
}
return s.Value
}
// NameStruct is the name from the api
type NameStruct struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
Sortindex string `xml:"sortindex,attr"`
Value string `xml:"value,attr"`
}
// PollStruct is the poll
type PollStruct struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
Title string `xml:"title,attr"`
Totalvotes string `xml:"totalvotes,attr"`
Results []struct {
Text string `xml:",chardata"`
Numplayers string `xml:"numplayers,attr"`
Result []struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
Numvotes int `xml:"numvotes,attr"`
Level string `xml:"level,attr"`
} `xml:"result"`
} `xml:"results"`
}
type Statistics struct {
Text string `xml:",chardata"`
Page string `xml:"page,attr"`
Ratings struct {
Text string `xml:",chardata"`
Usersrated struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"usersrated"`
Average struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"average"`
Bayesaverage struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"bayesaverage"`
Ranks struct {
Text string `xml:",chardata"`
Rank []struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
ID string `xml:"id,attr"`
Name string `xml:"name,attr"`
Friendlyname string `xml:"friendlyname,attr"`
Value string `xml:"value,attr"`
Bayesaverage string `xml:"bayesaverage,attr"`
} `xml:"rank"`
} `xml:"ranks"`
Stddev struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"stddev"`
Median struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"median"`
Owned struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"owned"`
Trading struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"trading"`
Wanting struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"wanting"`
Wishing struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"wishing"`
Numcomments struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"numcomments"`
Numweights struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"numweights"`
Averageweight struct {
Text string `xml:",chardata"`
Value string `xml:"value,attr"`
} `xml:"averageweight"`
} `xml:"ratings"`
}
type Poll struct {
}
// PollItem is a single poll item in a poll
type PollItem struct {
Name string
Title string
TotalVotes int
}
// LinkStruct is for the link for the things
type LinkStruct struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
ID int64 `xml:"id,attr"`
Value string `xml:"value,attr"`
Inbound string `xml:"inbound,attr"`
}
// Link is the link
type Link struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type Plays struct {
Total int64 `json:"total,omitempty"`
Page int64 `json:"page,omitempty"`
UserName string `json:"user_name,omitempty"`
UserID int64 `json:"user_id,omitempty"`
Items []Play `json:"items,omitempty"`
}
type Play struct {
ID int64 `json:"id,omitempty"`
Date time.Time `json:"date,omitempty"`
Quantity int64 `json:"quantity,omitempty"`
Length time.Duration `json:"length,omitempty"`
Incomplete bool `json:"incomplete,omitempty"`
NowInStats bool `json:"now_in_stats,omitempty"`
Location string `json:"location,omitempty"`
Comment string `json:"comment,omitempty"`
Item Item `json:"item,omitempty"`
Players []Player `json:"players,omitempty"`
}
type Item struct {
Name string `json:"name,omitempty"`
Type ItemType `json:"type,omitempty"`
ID int64 `json:"id,omitempty"`
}
type Player struct {
UserName string `json:"user_name,omitempty"`
UserID string `json:"user_id,omitempty"`
Name string `json:"name,omitempty"`
StartPosition string `json:"start_position,omitempty"`
Color string `json:"color,omitempty"`
Score int64 `json:"score,omitempty"`
New bool `json:"new,omitempty"`
Rating string `json:"rating,omitempty"`
Win bool `json:"win,omitempty"`
}
func nameStructToString(args []NameStruct) (string, []string) {
var (
primary string
alternate []string
)
for _, name := range args {
switch {
case name.Type == "primary":
primary = name.Value
case name.Type == "alternate":
alternate = append(alternate, name.Value)
default:
log.Printf("Name type %q is not handled, please report it as an issue", name.Type)
}
}
return primary, alternate
}
func linksMap(links []LinkStruct) map[string][]Link {
m := make(map[string][]Link)
for _, lnk := range links {
ln := append(m[lnk.Type], Link{
ID: lnk.ID,
Name: lnk.Value,
})
m[lnk.Type] = ln
}
return m
}
func safeInt(str string) int64 {
if str == "" {
return 0
}
i64, _ := strconv.ParseInt(str, 10, 0)
return i64
}
func safeIntInterface(in interface{}) int64 {
switch t := in.(type) {
case string:
return safeInt(t)
case int64:
return t
case int:
return int64(t)
case byte:
return int64(t)
default:
return 0
}
}
func safeFloat64(str string) float64 {
if str == "" {
return 0
}
i64, _ := strconv.ParseFloat(str, 64)
return i64
}
func safeDate(str string) time.Time {
ts, err := time.Parse(bggTimeFormat, str)
if err != nil {
return time.Time{}
}
return ts
}
type bggError struct {
XMLName xml.Name `xml:"error"`
Text string `xml:",chardata"`
Message string `xml:"message"`
}
func decode(r io.Reader, in interface{}) error {
buf, err := io.ReadAll(r)
if err != nil {
return fmt.Errorf("error reading data: %w", err)
}
err = xml.Unmarshal(buf, in)
if err == nil {
return nil
}
var errType bggError
if err2 := xml.Unmarshal(buf, &errType); err2 != nil {
return err
}
return fmt.Errorf("error from bgg: %q", errType.Message)
}
func getAttr(attr []html.Attribute, key string) string {
for i := range attr {
if attr[i].Key == key {
return attr[i].Val
}
}
return ""
}
func getID(url string, typ string) int64 {
data := strings.SplitN(strings.Trim(url, "/"), "/", 3)
if len(data) != 3 {
return -1
}
if data[0] != typ {
return -1
}
s, err := strconv.ParseInt(data[1], 10, 0)
if err != nil {
return -1
}
return s
}