Skip to content

Commit 281df40

Browse files
committed
Fetch game info
1 parent 19bd9e6 commit 281df40

File tree

4 files changed

+120
-8
lines changed

4 files changed

+120
-8
lines changed

pom.xml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@
55
<modelVersion>4.0.0</modelVersion>
66

77
<groupId>org.codegame</groupId>
8-
<artifactId>client</artifactId>
8+
<artifactId>codegame-client</artifactId>
99
<version>0.1.0</version>
1010

1111
<name>CodeGame Client</name>
1212
<url>https://code-game.org</url>
1313

1414
<properties>
15-
<maven.compiler.release>11</maven.compiler.release>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
<maven.compiler.release>17</maven.compiler.release>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1619
</properties>
1720

1821
<dependencies>
22+
<dependency>
23+
<groupId>com.google.code.gson</groupId>
24+
<artifactId>gson</artifactId>
25+
<version>2.9.1</version>
26+
</dependency>
1927
</dependencies>
2028

2129
<build>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.codegame.client;
2+
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
import java.util.Arrays;
8+
9+
import javax.net.ssl.HttpsURLConnection;
10+
11+
import com.google.gson.FieldNamingPolicy;
12+
import com.google.gson.Gson;
13+
import com.google.gson.GsonBuilder;
14+
15+
public class Api {
16+
private String url;
17+
private boolean tls;
18+
private String baseURL;
19+
20+
private static Gson json = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
21+
.create();
22+
23+
public Api(String url) {
24+
this.url = trimURL(url);
25+
this.tls = isTLS(this.url);
26+
this.baseURL = Api.baseURL("http", this.tls, this.url);
27+
HttpURLConnection.setFollowRedirects(true);
28+
}
29+
30+
public GameInfo fetchInfo() throws IOException {
31+
URL obj = new URL(this.baseURL + "/api/info");
32+
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
33+
con.setRequestProperty("Accept", "application/json");
34+
InputStreamReader reader = new InputStreamReader(con.getInputStream());
35+
int responseCode = con.getResponseCode();
36+
if (responseCode != 200)
37+
throw new IOException("Failed to fetch game info: unexpected response code: " + responseCode);
38+
GameInfo info = json.fromJson(reader, GameInfo.class);
39+
return info;
40+
}
41+
42+
static String trimURL(String url) {
43+
if (url.endsWith("/"))
44+
url = url.substring(0, url.length() - 1);
45+
String[] parts = url.split("://");
46+
if (parts.length < 2) {
47+
return url;
48+
}
49+
return String.join("://", Arrays.copyOfRange(parts, 1, parts.length));
50+
}
51+
52+
static String baseURL(String protocol, boolean tls, String trimmedURL) {
53+
if (tls)
54+
return protocol + "s://" + trimmedURL;
55+
return protocol + "://" + trimmedURL;
56+
}
57+
58+
static boolean isTLS(String trimmedURL) {
59+
try {
60+
URL url = new URL(baseURL("http", true, trimmedURL) + "/api/info");
61+
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
62+
connection.getInputStream();
63+
if (connection.getSSLSession().isEmpty())
64+
return false;
65+
return connection.getSSLSession().get().isValid();
66+
} catch (IOException e) {
67+
return false;
68+
}
69+
}
70+
71+
public String getURL() {
72+
return url;
73+
}
74+
75+
public String getBaseURL() {
76+
return baseURL;
77+
}
78+
79+
public boolean isTLS() {
80+
return tls;
81+
}
82+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.codegame.client;
2+
3+
import com.google.gson.annotations.*;
4+
5+
public class GameInfo {
6+
@SerializedName("name")
7+
public String name;
8+
@SerializedName("cg_version")
9+
public String cgVersion;
10+
@SerializedName("display_name")
11+
public String displayName;
12+
@SerializedName("description")
13+
public String description;
14+
@SerializedName("version")
15+
public String version;
16+
@SerializedName("repository_url")
17+
public String repositoryURL;
18+
}
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.codegame.client;
22

3-
public class GameSocket
4-
{
5-
public static void main( String[] args )
6-
{
7-
System.out.println( "Hello World!" );
8-
}
3+
public class GameSocket {
4+
private Api api;
5+
6+
public GameSocket(String url) {
7+
api = new Api(url);
8+
}
9+
10+
public Api getApi() {
11+
return api;
12+
}
913
}

0 commit comments

Comments
 (0)