Skip to content

Commit 7605870

Browse files
committed
Added non-steam platforms compatibility.
1 parent 2d7c029 commit 7605870

File tree

6 files changed

+426
-75
lines changed

6 files changed

+426
-75
lines changed

README.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,28 @@ var gamesJson = Search.searchGamesByNameJSON("cyberpunk");
3333
#### Get a game object without searching:
3434
```java
3535
// Get a Game using a GameId
36-
var game = Game.getGameByGameId(1234);
36+
var game = Game.getGameByGameId("1234");
3737

3838
// Get a Game using a SteamAppId
39-
var game = Game.getGameBySteamAppId(567890);
39+
var game = Game.getGameBySteamAppId("567890");
40+
41+
// Get a Game using an EgsId
42+
var game = Game.getGameByGogId("OFB-EAST:57557");
43+
44+
// Get a Game using an OriginId
45+
var game = Game.getGameByOriginId("OFB-EAST:57557");
46+
47+
// Get a Game using an UplayId
48+
var game = Game.getGameByUplayId("4a1562a4-c4d2-4bc5-a85e-f3db588b0072");
49+
50+
// Get a Game using an GogId
51+
var game = Game.getGameByGogId("2031519202");
4052

4153
// Get a Game using its constructor
42-
var game = new Game(567890, SGDBIdTypes.SteamAppId);
54+
var game = new Game("567890", SGDBIdTypes.SteamAppId);
55+
56+
// Get a JSONObject containing the response from the API
57+
var gameJson = Search.getGameJSONBySteamAppId("567890");
4358
```
4459

4560
#### Do something with a game object:
@@ -54,13 +69,16 @@ var stylesArray = game.getStyles();
5469
#### Get some grids:
5570
```java
5671
// Get grids by game ID
57-
var grid = Grid.getGridsByGameId(1234);
72+
var grid = Grid.getGridsByGameId("1234");
5873

5974
// Get grids by Steam App Id
60-
var grids = Grid.getGridsBySteamAppId(1234);
75+
var grids = Grid.getGridsBySteamAppId("1234");
6176

6277
// Alternatively, you can do it like this:
63-
var grids = Grid.getGridsById(1234, SGDBIdTypes.GameId);
78+
var grids = Grid.getGridsById("1234", SGDBIdTypes.GameId);
79+
80+
// Get a JSONObject containing the response from the API
81+
var gridJson = Search.getGridJSONBySteamAppId("567890");
6482
```
6583

6684
#### Filter the styles:
@@ -69,11 +87,11 @@ var grids = Grid.getGridsById(1234, SGDBIdTypes.GameId);
6987
SGDBStyles styles[] = {SGDBStyles.Alternate, SGDBStyles.NoLogo};
7088

7189
// Same as before, but using the styles filter
72-
var grid = Grid.getGridsByGameId(1234, styles);
90+
var grid = Grid.getGridsByGameId("1234", styles);
7391

74-
var grids = Grid.getGridsBySteamAppId(1234, styles);
92+
var grids = Grid.getGridsBySteamAppId("1234", styles);
7593

76-
var grids = Grid.getGridsById(1234, SGDBIdTypes.GameId, styles);
94+
var grids = Grid.getGridsById("1234", SGDBIdTypes.GameId, styles);
7795
```
7896

7997
#### Do something with a grid object:
@@ -89,22 +107,22 @@ var authorName = grids.get(0).getAuthor().getName();
89107
#### Vote on grids:
90108
```java
91109
// Upvote the first grid of the game with ID 1234
92-
var grid = Grid.getGridsByGameId(1234).get(0);
110+
var grid = Grid.getGridsByGameId("1234").get(0);
93111
grid.upvote();
94112

95113
// Downvote the same grid
96-
var grid = Grid.getGridsByGameId(1234).get(0);
114+
var grid = Grid.getGridsByGameId("1234").get(0);
97115
grid.upvote();
98116

99117
// Alternatively, use the Grid's ID (80 in this case) to vote:
100-
Grid.upvoteById(80);
101-
Grid.downvoteById(80);
118+
Grid.upvoteById("80");
119+
Grid.downvoteById("80");
102120
```
103121

104122
#### Upload a grid:
105123
```java
106124
// Upload a blurred grid to Half-Life 2 (2254)
107-
Grid.upload(2254, SGDBStyles.Blurred, "path/of/image.img");
125+
Grid.upload("2254", SGDBStyles.Blurred, "path/of/image.img");
108126
```
109127

110128
#### Delete grids:
@@ -113,10 +131,10 @@ Grid.upload(2254, SGDBStyles.Blurred, "path/of/image.img");
113131
Grid.deleteByGridID(gameID);
114132

115133
// Delete multiple grids by ID
116-
var gameIDs = {123, 456};
134+
var gameIDs = {"123", "456"};
117135
Grid.deleteByGridIDs(gameIDs);
118136

119137
// Delete a Grid object
120-
var grid = Grid.getGridsByGameId(1234).get(0);
138+
var grid = Grid.getGridsByGameId("1234").get(0);
121139
grid.delete();
122140
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.steamgriddb</groupId>
55
<artifactId>java-steamgriddb</artifactId>
6-
<version>1.0</version>
6+
<version>1.1</version>
77
<packaging>jar</packaging>
88
<dependencies>
99
<dependency>

src/main/java/com/steamgriddb/Enums/SGDBIdTypes.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
*/
88
public enum SGDBIdTypes {
99
SteamAppId,
10+
OriginId,
11+
EgsId,
12+
UplayId,
13+
GogId,
1014
GameId
1115
}

src/main/java/com/steamgriddb/Game.java

Lines changed: 138 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,67 @@
22

33
import com.steamgriddb.Enums.SGDBIdTypes;
44
import com.steamgriddb.Connection.SGDBConnectionManager;
5+
import java.io.UnsupportedEncodingException;
6+
import java.net.URLEncoder;
57
import java.util.ArrayList;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
610
import org.json.JSONObject;
711
import org.json.JSONArray;
812

913
/**
1014
* Represents a Game as found on SteamGridDB.com
11-
*
15+
*
1216
* @author mpaterakis
1317
*/
1418
public 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

Comments
 (0)