22
33import com .steamgriddb .Enums .SGDBIdTypes ;
44import com .steamgriddb .Connection .SGDBConnectionManager ;
5+ import java .io .UnsupportedEncodingException ;
6+ import java .net .URLEncoder ;
57import java .util .ArrayList ;
8+ import java .util .logging .Level ;
9+ import java .util .logging .Logger ;
610import org .json .JSONObject ;
711import org .json .JSONArray ;
812
913/**
1014 * Represents a Game as found on SteamGridDB.com
11- *
15+ *
1216 * @author mpaterakis
1317 */
1418public class Game {
1519
1620 /*
1721 * Fields
18- */
19- private int id = - 1 ;
22+ */
23+ private String id = "" ;
2024 private String name = "" ;
2125 private ArrayList <String > types = new ArrayList <>();
2226
2327 /**
2428 * Constructor for Game.
25- *
29+ *
2630 * @param id The id of the Game
27- * @param type The type of the given id
31+ * @param type The type of the given id [OriginId, EgsId, UplayId]
2832 */
29- public Game (int id , SGDBIdTypes type ) {
33+ public Game (String id , SGDBIdTypes type ) {
34+ try {
35+ id = URLEncoder .encode (id , "UTF-8" );
36+ } catch (UnsupportedEncodingException ex ) {
37+ Logger .getLogger (Game .class .getName ()).log (Level .SEVERE , null , ex );
38+ }
3039 JSONObject json = new JSONObject ();
3140
32- if (type == SGDBIdTypes .SteamAppId ) {
33- json = SGDBConnectionManager .getJSON ("games/steam/" + id );
34- } else if (type == SGDBIdTypes .GameId ) {
35- json = SGDBConnectionManager .getJSON ("games/id/" + id );
41+ switch (type ) {
42+ case SteamAppId :
43+ json = getGameJSONBySteamAppId (id );
44+ break ;
45+ case GameId :
46+ json = getGameJSONByGameId (id );
47+ break ;
48+ case GogId :
49+ json = getGameJSONByGogId (id );
50+ break ;
51+ case OriginId :
52+ json = getGameJSONByOriginId (id );
53+ break ;
54+ case EgsId :
55+ json = getGameJSONByEgsId (id );
56+ break ;
57+ case UplayId :
58+ json = getGameJSONByUplayId (id );
59+ break ;
60+ default :
61+ break ;
3662 }
37-
63+
3864 if (json .getBoolean ("success" )) {
39- this .id = json .getJSONObject ("data" ).getInt ("id" );
65+ this .id = String . valueOf ( json .getJSONObject ("data" ).getInt ("id" ) );
4066 this .name = json .getJSONObject ("data" ).getString ("name" );
4167 JSONArray typesArray = json .getJSONObject ("data" ).getJSONArray ("types" );
4268 for (int i = 0 ; i < typesArray .length (); i ++) {
@@ -47,60 +73,148 @@ public Game(int id, SGDBIdTypes type) {
4773
4874 /**
4975 * Get a Game object from a SteamAppId.
50- *
76+ *
5177 * @param steamAppId The Game's SteamAppId
5278 * @return A Game object
5379 */
54- public static Game getGameBySteamAppId (int steamAppId ) {
80+ public static Game getGameBySteamAppId (String steamAppId ) {
5581 Game game = new Game (steamAppId , SGDBIdTypes .SteamAppId );
5682 return game ;
5783 }
5884
5985 /**
6086 * Get a JSONObject of a Game from a SteamAppId.
61- *
87+ *
6288 * @param steamAppId The Game's SteamAppId
6389 * @return A JSONObject object with the Game's data
6490 */
65- public static JSONObject getGameJSONBySteamAppId (int steamAppId ) {
91+ public static JSONObject getGameJSONBySteamAppId (String steamAppId ) {
6692 JSONObject json = SGDBConnectionManager .getJSON ("games/steam/" + steamAppId );
6793 return json ;
6894 }
6995
96+ /**
97+ * Get a Game object from an EgsId.
98+ *
99+ * @param egsId The Game's EgsId
100+ * @return A Game object
101+ */
102+ public static Game getGameByEgsId (String egsId ) {
103+ Game game = new Game (egsId , SGDBIdTypes .EgsId );
104+ return game ;
105+ }
106+
107+ /**
108+ * Get a JSONObject of a Game from an EgsId.
109+ *
110+ * @param egsId The Game's EgsId
111+ * @return A JSONObject object with the Game's data
112+ */
113+ public static JSONObject getGameJSONByEgsId (String egsId ) {
114+ JSONObject json = SGDBConnectionManager .getJSON ("games/egs/" + egsId );
115+ return json ;
116+ }
117+
118+ /**
119+ * Get a Game object from an OriginId.
120+ *
121+ * @param originId The Game's OriginId
122+ * @return A Game object
123+ */
124+ public static Game getGameByOriginId (String originId ) {
125+ Game game = new Game (originId , SGDBIdTypes .OriginId );
126+ return game ;
127+ }
128+
129+ /**
130+ * Get a JSONObject of a Game from an OriginId.
131+ *
132+ * @param originId The Game's OriginId
133+ * @return A JSONObject object with the Game's data
134+ */
135+ public static JSONObject getGameJSONByOriginId (String originId ) {
136+ JSONObject json = SGDBConnectionManager .getJSON ("games/origin/" + originId );
137+ return json ;
138+ }
139+
140+ /**
141+ * Get a Game object from a UplayId.
142+ *
143+ * @param uplayId The Game's UplayId
144+ * @return A Game object
145+ */
146+ public static Game getGameByUplayId (String uplayId ) {
147+ Game game = new Game (uplayId , SGDBIdTypes .UplayId );
148+ return game ;
149+ }
150+
151+ /**
152+ * Get a JSONObject of a Game from a UplayId.
153+ *
154+ * @param uplayId The Game's UplayId
155+ * @return A JSONObject object with the Game's data
156+ */
157+ public static JSONObject getGameJSONByUplayId (String uplayId ) {
158+ JSONObject json = SGDBConnectionManager .getJSON ("games/uplay/" + uplayId );
159+ return json ;
160+ }
161+
162+ /**
163+ * Get a Game object from a GogId.
164+ *
165+ * @param gogId The Game's GogId
166+ * @return A Game object
167+ */
168+ public static Game getGameByGogId (String gogId ) {
169+ Game game = new Game (gogId , SGDBIdTypes .GogId );
170+ return game ;
171+ }
172+
173+ /**
174+ * Get a JSONObject of a Game from a GogId.
175+ *
176+ * @param gogId The Game's GogId
177+ * @return A JSONObject object with the Game's data
178+ */
179+ public static JSONObject getGameJSONByGogId (String gogId ) {
180+ JSONObject json = SGDBConnectionManager .getJSON ("games/gog/" + gogId );
181+ return json ;
182+ }
183+
70184 /**
71185 * Get a Game object from a GameId.
72- *
186+ *
73187 * @param gameId The Game's GameId
74188 * @return A Game object
75189 */
76- public static Game getGameByGameId (int gameId ) {
190+ public static Game getGameByGameId (String gameId ) {
77191 Game game = new Game (gameId , SGDBIdTypes .GameId );
78192 return game ;
79193 }
80194
81195 /**
82196 * Get a JSONObject of a Game from a GameId.
83- *
197+ *
84198 * @param gameId The Game's GameId
85199 * @return A JSONObject object with the Game's data
86200 */
87- public static JSONObject getGameJSONByGameId (int gameId ) {
201+ public static JSONObject getGameJSONByGameId (String gameId ) {
88202 JSONObject json = SGDBConnectionManager .getJSON ("games/id/" + gameId );
89203 return json ;
90204 }
91205
92206 /**
93207 * Get the Game's id.
94- *
208+ *
95209 * @return id of the Game
96210 */
97- public int getId () {
211+ public String getId () {
98212 return id ;
99213 }
100214
101215 /**
102216 * Get the Game's name.
103- *
217+ *
104218 * @return name of the Game
105219 */
106220 public String getName () {
@@ -109,7 +223,7 @@ public String getName() {
109223
110224 /**
111225 * Get the Game's types.
112- *
226+ *
113227 * @return types of the Game
114228 */
115229 public ArrayList <String > getTypes () {
0 commit comments