Skip to content

Commit

Permalink
Added non-steam platforms compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpaterakis committed Jul 11, 2019
1 parent 2d7c029 commit 7605870
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 75 deletions.
50 changes: 34 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,28 @@ var gamesJson = Search.searchGamesByNameJSON("cyberpunk");
#### Get a game object without searching:
```java
// Get a Game using a GameId
var game = Game.getGameByGameId(1234);
var game = Game.getGameByGameId("1234");

// Get a Game using a SteamAppId
var game = Game.getGameBySteamAppId(567890);
var game = Game.getGameBySteamAppId("567890");

// Get a Game using an EgsId
var game = Game.getGameByGogId("OFB-EAST:57557");

// Get a Game using an OriginId
var game = Game.getGameByOriginId("OFB-EAST:57557");

// Get a Game using an UplayId
var game = Game.getGameByUplayId("4a1562a4-c4d2-4bc5-a85e-f3db588b0072");

// Get a Game using an GogId
var game = Game.getGameByGogId("2031519202");

// Get a Game using its constructor
var game = new Game(567890, SGDBIdTypes.SteamAppId);
var game = new Game("567890", SGDBIdTypes.SteamAppId);

// Get a JSONObject containing the response from the API
var gameJson = Search.getGameJSONBySteamAppId("567890");
```

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

// Get grids by Steam App Id
var grids = Grid.getGridsBySteamAppId(1234);
var grids = Grid.getGridsBySteamAppId("1234");

// Alternatively, you can do it like this:
var grids = Grid.getGridsById(1234, SGDBIdTypes.GameId);
var grids = Grid.getGridsById("1234", SGDBIdTypes.GameId);

// Get a JSONObject containing the response from the API
var gridJson = Search.getGridJSONBySteamAppId("567890");
```

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

// Same as before, but using the styles filter
var grid = Grid.getGridsByGameId(1234, styles);
var grid = Grid.getGridsByGameId("1234", styles);

var grids = Grid.getGridsBySteamAppId(1234, styles);
var grids = Grid.getGridsBySteamAppId("1234", styles);

var grids = Grid.getGridsById(1234, SGDBIdTypes.GameId, styles);
var grids = Grid.getGridsById("1234", SGDBIdTypes.GameId, styles);
```

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

// Downvote the same grid
var grid = Grid.getGridsByGameId(1234).get(0);
var grid = Grid.getGridsByGameId("1234").get(0);
grid.upvote();

// Alternatively, use the Grid's ID (80 in this case) to vote:
Grid.upvoteById(80);
Grid.downvoteById(80);
Grid.upvoteById("80");
Grid.downvoteById("80");
```

#### Upload a grid:
```java
// Upload a blurred grid to Half-Life 2 (2254)
Grid.upload(2254, SGDBStyles.Blurred, "path/of/image.img");
Grid.upload("2254", SGDBStyles.Blurred, "path/of/image.img");
```

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

// Delete multiple grids by ID
var gameIDs = {123, 456};
var gameIDs = {"123", "456"};
Grid.deleteByGridIDs(gameIDs);

// Delete a Grid object
var grid = Grid.getGridsByGameId(1234).get(0);
var grid = Grid.getGridsByGameId("1234").get(0);
grid.delete();
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.steamgriddb</groupId>
<artifactId>java-steamgriddb</artifactId>
<version>1.0</version>
<version>1.1</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/steamgriddb/Enums/SGDBIdTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
*/
public enum SGDBIdTypes {
SteamAppId,
OriginId,
EgsId,
UplayId,
GogId,
GameId
}
162 changes: 138 additions & 24 deletions src/main/java/com/steamgriddb/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,67 @@

import com.steamgriddb.Enums.SGDBIdTypes;
import com.steamgriddb.Connection.SGDBConnectionManager;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONObject;
import org.json.JSONArray;

/**
* Represents a Game as found on SteamGridDB.com
*
*
* @author mpaterakis
*/
public class Game {

/*
* Fields
*/
private int id = -1;
*/
private String id = "";
private String name = "";
private ArrayList<String> types = new ArrayList<>();

/**
* Constructor for Game.
*
*
* @param id The id of the Game
* @param type The type of the given id
* @param type The type of the given id [OriginId, EgsId, UplayId]
*/
public Game(int id, SGDBIdTypes type) {
public Game(String id, SGDBIdTypes type) {
try {
id = URLEncoder.encode(id, "UTF-8");
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Game.class.getName()).log(Level.SEVERE, null, ex);
}
JSONObject json = new JSONObject();

if (type == SGDBIdTypes.SteamAppId) {
json = SGDBConnectionManager.getJSON("games/steam/" + id);
} else if (type == SGDBIdTypes.GameId) {
json = SGDBConnectionManager.getJSON("games/id/" + id);
switch (type) {
case SteamAppId:
json = getGameJSONBySteamAppId(id);
break;
case GameId:
json = getGameJSONByGameId(id);
break;
case GogId:
json = getGameJSONByGogId(id);
break;
case OriginId:
json = getGameJSONByOriginId(id);
break;
case EgsId:
json = getGameJSONByEgsId(id);
break;
case UplayId:
json = getGameJSONByUplayId(id);
break;
default:
break;
}

if (json.getBoolean("success")) {
this.id = json.getJSONObject("data").getInt("id");
this.id = String.valueOf(json.getJSONObject("data").getInt("id"));
this.name = json.getJSONObject("data").getString("name");
JSONArray typesArray = json.getJSONObject("data").getJSONArray("types");
for (int i = 0; i < typesArray.length(); i++) {
Expand All @@ -47,60 +73,148 @@ public Game(int id, SGDBIdTypes type) {

/**
* Get a Game object from a SteamAppId.
*
*
* @param steamAppId The Game's SteamAppId
* @return A Game object
*/
public static Game getGameBySteamAppId(int steamAppId) {
public static Game getGameBySteamAppId(String steamAppId) {
Game game = new Game(steamAppId, SGDBIdTypes.SteamAppId);
return game;
}

/**
* Get a JSONObject of a Game from a SteamAppId.
*
*
* @param steamAppId The Game's SteamAppId
* @return A JSONObject object with the Game's data
*/
public static JSONObject getGameJSONBySteamAppId(int steamAppId) {
public static JSONObject getGameJSONBySteamAppId(String steamAppId) {
JSONObject json = SGDBConnectionManager.getJSON("games/steam/" + steamAppId);
return json;
}

/**
* Get a Game object from an EgsId.
*
* @param egsId The Game's EgsId
* @return A Game object
*/
public static Game getGameByEgsId(String egsId) {
Game game = new Game(egsId, SGDBIdTypes.EgsId);
return game;
}

/**
* Get a JSONObject of a Game from an EgsId.
*
* @param egsId The Game's EgsId
* @return A JSONObject object with the Game's data
*/
public static JSONObject getGameJSONByEgsId(String egsId) {
JSONObject json = SGDBConnectionManager.getJSON("games/egs/" + egsId);
return json;
}

/**
* Get a Game object from an OriginId.
*
* @param originId The Game's OriginId
* @return A Game object
*/
public static Game getGameByOriginId(String originId) {
Game game = new Game(originId, SGDBIdTypes.OriginId);
return game;
}

/**
* Get a JSONObject of a Game from an OriginId.
*
* @param originId The Game's OriginId
* @return A JSONObject object with the Game's data
*/
public static JSONObject getGameJSONByOriginId(String originId) {
JSONObject json = SGDBConnectionManager.getJSON("games/origin/" + originId);
return json;
}

/**
* Get a Game object from a UplayId.
*
* @param uplayId The Game's UplayId
* @return A Game object
*/
public static Game getGameByUplayId(String uplayId) {
Game game = new Game(uplayId, SGDBIdTypes.UplayId);
return game;
}

/**
* Get a JSONObject of a Game from a UplayId.
*
* @param uplayId The Game's UplayId
* @return A JSONObject object with the Game's data
*/
public static JSONObject getGameJSONByUplayId(String uplayId) {
JSONObject json = SGDBConnectionManager.getJSON("games/uplay/" + uplayId);
return json;
}

/**
* Get a Game object from a GogId.
*
* @param gogId The Game's GogId
* @return A Game object
*/
public static Game getGameByGogId(String gogId) {
Game game = new Game(gogId, SGDBIdTypes.GogId);
return game;
}

/**
* Get a JSONObject of a Game from a GogId.
*
* @param gogId The Game's GogId
* @return A JSONObject object with the Game's data
*/
public static JSONObject getGameJSONByGogId(String gogId) {
JSONObject json = SGDBConnectionManager.getJSON("games/gog/" + gogId);
return json;
}

/**
* Get a Game object from a GameId.
*
*
* @param gameId The Game's GameId
* @return A Game object
*/
public static Game getGameByGameId(int gameId) {
public static Game getGameByGameId(String gameId) {
Game game = new Game(gameId, SGDBIdTypes.GameId);
return game;
}

/**
* Get a JSONObject of a Game from a GameId.
*
*
* @param gameId The Game's GameId
* @return A JSONObject object with the Game's data
*/
public static JSONObject getGameJSONByGameId(int gameId) {
public static JSONObject getGameJSONByGameId(String gameId) {
JSONObject json = SGDBConnectionManager.getJSON("games/id/" + gameId);
return json;
}

/**
* Get the Game's id.
*
*
* @return id of the Game
*/
public int getId() {
public String getId() {
return id;
}

/**
* Get the Game's name.
*
*
* @return name of the Game
*/
public String getName() {
Expand All @@ -109,7 +223,7 @@ public String getName() {

/**
* Get the Game's types.
*
*
* @return types of the Game
*/
public ArrayList<String> getTypes() {
Expand Down
Loading

0 comments on commit 7605870

Please sign in to comment.