-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgame.go
195 lines (167 loc) · 6.41 KB
/
game.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
package igdb
import (
"github.com/Henry-Sarabia/sliceconv"
"github.com/pkg/errors"
"strconv"
)
//go:generate gomodifytags -file $GOFILE -struct Game -add-tags json -w
// Game contains information on an IGDB entry for a particular video game.
// For more information visit: https://api-docs.igdb.com/#game
type Game struct {
ID int `json:"id"`
AgeRatings []int `json:"age_ratings"`
AggregatedRating float64 `json:"aggregated_rating"`
AggregatedRatingCount int `json:"aggregated_rating_count"`
AlternativeNames []int `json:"alternative_names"`
Artworks []int `json:"artworks"`
Bundles []int `json:"bundles"`
Category GameCategory `json:"category"`
Collection int `json:"collection"`
Cover int `json:"cover"`
CreatedAt int `json:"created_at"`
DLCS []int `json:"dlcs"`
Expansions []int `json:"expansions"`
ExternalGames []int `json:"external_games"`
FirstReleaseDate int `json:"first_release_date"`
Follows int `json:"follows"`
Franchise int `json:"franchise"`
Franchises []int `json:"franchises"`
GameEngines []int `json:"game_engines"`
GameModes []int `json:"game_modes"`
Genres []int `json:"genres"`
Hypes int `json:"hypes"`
InvolvedCompanies []int `json:"involved_companies"`
Keywords []int `json:"keywords"`
MultiplayerModes []int `json:"multiplayer_modes"`
Name string `json:"name"`
ParentGame int `json:"parent_game"`
Platforms []int `json:"platforms"`
PlayerPerspectives []int `json:"player_perspectives"`
Rating float64 `json:"rating"`
RatingCount int `json:"rating_count"`
ReleaseDates []int `json:"release_dates"`
Screenshots []int `json:"screenshots"`
SimilarGames []int `json:"similar_games"`
Slug string `json:"slug"`
StandaloneExpansions []int `json:"standalone_expansions"`
Status GameStatus `json:"status"`
Storyline string `json:"storyline"`
Summary string `json:"summary"`
Tags []Tag `json:"tags"`
Themes []int `json:"themes"`
TotalRating float64 `json:"total_rating"`
TotalRatingCount int `json:"total_rating_count"`
UpdatedAt int `json:"updated_at"`
URL string `json:"url"`
VersionParent int `json:"version_parent"`
VersionTitle string `json:"version_title"`
Videos []int `json:"videos"`
Websites []int `json:"websites"`
}
// GameCategory specifies a type of game content.
type GameCategory int
//go:generate stringer -type=GameCategory,GameStatus
// Expected GameCategory enums from the IGDB.
const (
MainGame GameCategory = iota
DLCAddon
Expansion
Bundle
StandaloneExpansion
Mod
Episode
Season
)
// GameStatus specifies the release status of a specific game.
type GameStatus int
// Expected GameStatus enums from the IGDB.
const (
StatusReleased GameStatus = iota
_
StatusAlpha
StatusBeta
StatusEarlyAccess
StatusOffline
StatusCancelled
)
// GameService handles all the API
// calls for the IGDB Game endpoint.
type GameService service
// Get returns a single Game identified by the provided IGDB ID. Provide
// the SetFields functional option if you need to specify which fields to
// retrieve. If the ID does not match any Games, an error is returned.
func (gs *GameService) Get(id int, opts ...Option) (*Game, error) {
if id < 0 {
return nil, ErrNegativeID
}
var g []*Game
opts = append(opts, SetFilter("id", OpEquals, strconv.Itoa(id)))
err := gs.client.post(gs.end, &g, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get Game with ID %v", id)
}
return g[0], nil
}
// List returns a list of Games identified by the provided list of IGDB IDs.
// Provide functional options to sort, filter, and paginate the results.
// Any ID that does not match a Game is ignored. If none of the IDs
// match a Game, an error is returned.
func (gs *GameService) List(ids []int, opts ...Option) ([]*Game, error) {
for len(ids) < 1 {
return nil, ErrEmptyIDs
}
for _, id := range ids {
if id < 0 {
return nil, ErrNegativeID
}
}
var g []*Game
opts = append(opts, SetFilter("id", OpContainsAtLeast, sliceconv.Itoa(ids)...))
err := gs.client.post(gs.end, &g, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get Games with IDs %v", ids)
}
return g, nil
}
// Index returns an index of Games based solely on the provided functional
// options used to sort, filter, and paginate the results. If no Games can
// be found using the provided options, an error is returned.
func (gs *GameService) Index(opts ...Option) ([]*Game, error) {
var g []*Game
err := gs.client.post(gs.end, &g, opts...)
if err != nil {
return nil, errors.Wrap(err, "cannot get index of Games")
}
return g, nil
}
// Search returns a list of Games found by searching the IGDB using the provided
// query. Provide functional options to sort, filter, and paginate the results. If
// no Games are found using the provided query, an error is returned.
func (gs *GameService) Search(qry string, opts ...Option) ([]*Game, error) {
var g []*Game
opts = append(opts, setSearch(qry))
err := gs.client.post(gs.end, &g, opts...)
if err != nil {
return nil, errors.Wrapf(err, "cannot get Game with query %s", qry)
}
return g, nil
}
// Count returns the number of Games available in the IGDB.
// Provide the SetFilter functional option if you need to filter
// which Games to count.
func (gs *GameService) Count(opts ...Option) (int, error) {
ct, err := gs.client.getCount(gs.end, opts...)
if err != nil {
return 0, errors.Wrap(err, "cannot count Games")
}
return ct, nil
}
// Fields returns the up-to-date list of fields in an
// IGDB Game object.
func (gs *GameService) Fields() ([]string, error) {
f, err := gs.client.getFields(gs.end)
if err != nil {
return nil, errors.Wrap(err, "cannot get Game fields")
}
return f, nil
}