-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_game.go
121 lines (115 loc) · 3.44 KB
/
get_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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/Henry-Sarabia/igdb"
"strings"
)
type FormattedGame struct {
Name string `json:"name"`
Series string `json:"series"`
Genres []string `json:"genres"`
ReleaseDate int64 `json:"releaseDate"`
Summary string `json:"summary"`
Developer string `json:"developer"`
Publisher string `json:"publisher"`
Rating int64 `json:"rating"`
Cover string `json:"cover"`
Screenshots []string `json:"screenshots"`
}
func getGame(gameId int) DbGame {
if db.GameExists(gameId) == false {
game := queryIgdbGame(gameId)
insertGame(game, gameId)
}
game := db.GetGame(gameId)
return game
}
func formatGame(game DbGame) string {
var screenshots []string
if game.Screenshots.Valid {
screenshots = strings.Split(game.Screenshots.String, ";")
}
formattedGame := FormattedGame{
Name: game.Name,
Genres: db.GetGenresNameByGameId(game.Id),
ReleaseDate: validateNumber(game.ReleaseDate),
Summary: validateString(game.Summary),
Rating: validateNumber(game.Rating),
Cover: validateString(game.Cover),
Screenshots: screenshots,
}
if game.SeriesId.Valid {
series := db.GetSeriesById(game.SeriesId.Int64)
formattedGame.Series = series.Name
}
if game.DeveloperId.Valid {
developer := db.GetCompanyById(game.DeveloperId.Int64)
formattedGame.Developer = developer.Name
}
if game.PublisherId.Valid {
publisher := db.GetCompanyById(game.PublisherId.Int64)
formattedGame.Publisher = publisher.Name
}
gameString, err := json.Marshal(formattedGame)
if err != nil {
panic(err.Error())
}
return string(gameString)
}
func insertGame(igdbGame igdb.Game, igdbId int) DbGame {
var screenshots []string
for _, screenshot := range igdbGame.Screenshots {
screenshotUrl := fmt.Sprintf("https:%s", screenshot.URL)
screenshotUrl = strings.Replace(screenshotUrl, "t_thumb", "t_1080p", -1)
screenshots = append(screenshots, screenshotUrl)
}
cover := fmt.Sprintf("https:%s", igdbGame.Cover.URL)
cover = strings.Replace(cover, "t_thumb", "t_cover_big_2x", -1)
dbGame := DbGame{
IgdbId: uint(igdbId),
Name: igdbGame.Name,
Summary: sql.NullString{String: igdbGame.Summary, Valid: true},
Rating: sql.NullInt64{Int64: int64(igdbGame.Rating), Valid: true},
ReleaseDate: sql.NullInt64{Int64: int64(igdbGame.FirstReleaseDate), Valid: true},
Cover: sql.NullString{String: cover, Valid: true},
Screenshots: sql.NullString{String: strings.Join(screenshots, ";"), Valid: true},
}
if igdbGame.Collection != 0 {
series := getSeries(igdbGame.Collection)
dbGame.SeriesId = sql.NullInt64{Int64: int64(series.Id), Valid: true}
}
if len(igdbGame.Developers) != 0 {
developer := getCompany(igdbGame.Developers[0])
dbGame.DeveloperId = sql.NullInt64{Int64: int64(developer.Id), Valid: true}
}
if len(igdbGame.Publishers) != 0 {
publisher := getCompany(igdbGame.Publishers[0])
dbGame.PublisherId = sql.NullInt64{Int64: int64(publisher.Id), Valid: true}
}
id := db.AddGame(dbGame)
for _, genreId := range igdbGame.Genres {
genre := getGenre(genreId)
db.AddGameGenre(id, genre.Id)
}
return dbGame
}
func queryIgdbGame(igdbId int) igdb.Game {
game, err := igdbClient.Games.Get(igdbId, igdb.SetFields(
"name",
"summary",
"collection",
"rating",
"developers",
"publishers",
"genres",
"first_release_date",
"cover",
"screenshots",
))
if err != nil {
panic(err.Error())
}
return *game
}