Skip to content

Commit d7d1f3f

Browse files
committed
Add javadocs
1 parent aa66d79 commit d7d1f3f

File tree

6 files changed

+208
-8
lines changed

6 files changed

+208
-8
lines changed

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@
4242
</dependencies>
4343

4444
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-javadoc-plugin</artifactId>
49+
<version>3.4.1</version>
50+
<executions>
51+
<execution>
52+
<id>attach-javadocs</id>
53+
<goals>
54+
<goal>jar</goal>
55+
</goals>
56+
</execution>
57+
</executions>
58+
</plugin>
59+
</plugins>
4560
<pluginManagement>
4661
<plugins>
4762
<plugin>

src/main/java/org/codegame/client/Api.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,27 @@
1919
import com.google.gson.annotations.SerializedName;
2020
import com.google.gson.reflect.TypeToken;
2121

22+
/**
23+
* Common methods for interfacing with CodeGame game servers.
24+
*/
2225
public class Api {
2326
private String url;
2427
private boolean tls;
2528
private String baseURL;
2629

27-
public static Gson json = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
30+
static Gson json = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
2831
.create();
2932

30-
public Api(String url) {
33+
Api(String url) {
3134
HttpURLConnection.setFollowRedirects(true);
3235
this.url = trimURL(url);
3336
this.tls = isTLS(this.url);
3437
this.baseURL = Api.baseURL("http", this.tls, this.url);
3538
}
3639

40+
/**
41+
* Game info from the `/api/info` endpoint.
42+
*/
3743
public class GameInfo {
3844
@SerializedName("name")
3945
public String name;
@@ -49,14 +55,29 @@ public class GameInfo {
4955
public String repositoryURL;
5056
}
5157

58+
/**
59+
* Fetches game info from the `/api/info` endpoint.
60+
*
61+
* @return An instance of the GameInfo class.
62+
* @throws IOException Thrown when the request fails.
63+
*/
5264
public GameInfo fetchInfo() throws IOException {
5365
return fetchJSON("/api/info", GameInfo.class);
5466
}
5567

56-
public static class GameConfigResponse<T> {
68+
private static class GameConfigResponse<T> {
5769
public T config;
5870
}
5971

72+
/**
73+
* Fetches the config of the game.
74+
*
75+
* @param <T> The type of the game config.
76+
* @param gameId The ID of the game.
77+
* @param configClass The class of the game config.
78+
* @return An instance of T.
79+
* @throws IOException Thrown when the request fails.
80+
*/
6081
public <T> T fetchGameConfig(String gameId, Class<T> configClass) throws IOException {
6182
GameConfigResponse<T> response = fetchJSON("/api/games/" + gameId,
6283
TypeToken.getParameterized(GameConfigResponse.class,
@@ -243,14 +264,23 @@ static boolean isTLS(String trimmedURL) {
243264
}
244265
}
245266

267+
/**
268+
* @return The URL of the game server without any protocol.
269+
*/
246270
public String getURL() {
247271
return url;
248272
}
249273

274+
/**
275+
* @return The URL of the game server prepended with http:// or https://.
276+
*/
250277
public String getBaseURL() {
251278
return baseURL;
252279
}
253280

281+
/**
282+
* @return Whether the game server supports TLS.
283+
*/
254284
public boolean isTLS() {
255285
return tls;
256286
}

src/main/java/org/codegame/client/Dirs.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import org.apache.commons.lang3.SystemUtils;
44

5-
public class Dirs {
6-
public static String UserHome() {
5+
class Dirs {
6+
static String UserHome() {
77
return System.getProperty("user.home");
88
}
99

10-
public static String DataHome() {
10+
static String DataHome() {
1111
if (SystemUtils.IS_OS_WINDOWS)
1212
return DataHomeWindows();
1313
else if (SystemUtils.IS_OS_MAC)

src/main/java/org/codegame/client/GameSocket.java

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
import org.fusesource.jansi.AnsiConsole;
1414
import org.fusesource.jansi.Ansi.*;
1515

16+
/**
17+
* Represents a connection to a game server.
18+
*/
1619
public class GameSocket {
17-
private static final String cgVersion = "0.8";
20+
private static final String cgVersion = "0.7";
1821

1922
@FunctionalInterface
2023
public interface EventCallback<T> {
@@ -39,6 +42,13 @@ private class Callbacks<T> {
3942
private HashMap<String, Callbacks> eventListeners = new HashMap<>();
4043
private CountDownLatch exitEvent = new CountDownLatch(1);
4144

45+
/**
46+
* Creates a new game socket.
47+
*
48+
* @param url The URL of the game server. The protocol should be omitted.
49+
* @throws IOException Thrown when the URL does not point to a valid CodeGame
50+
* game server.
51+
*/
4252
public GameSocket(String url) throws IOException {
4353
AnsiConsole.systemInstall();
4454
api = new Api(url);
@@ -50,21 +60,53 @@ public GameSocket(String url) throws IOException {
5060
}
5161
}
5262

63+
/**
64+
* Creates a new game on the server.
65+
*
66+
* @param makePublic Whether to make the created game public.
67+
* @param protect Whether to protect the game with a join secret.
68+
* @param config The game config.
69+
* @return Information about the created game. Including the game ID and the
70+
* join secret when protected == true.
71+
* @throws IOException Thrown when the request fails.
72+
*/
5373
public Api.GameData createGame(boolean makePublic, boolean protect, Object config) throws IOException {
5474
return api.createGame(makePublic, protect, config);
5575
}
5676

77+
/**
78+
* Creates a new player in the game and connects to it.
79+
*
80+
* @param gameId The ID of the game.
81+
* @param username The desired username.
82+
* @throws IOException Thrown when the request fails.
83+
*/
5784
public void join(String gameId, String username) throws IOException {
5885
join(gameId, username, "");
5986
}
6087

88+
/**
89+
* Creates a new player in the protected game and connects to it.
90+
*
91+
* @param gameId The ID of the game.
92+
* @param username The desired username.
93+
* @param joinSecret The join secret of the game.
94+
* @throws IOException Thrown when the request fails.
95+
*/
6196
public void join(String gameId, String username, String joinSecret) throws IOException {
6297
if (session.gameURL != "")
6398
throw new IllegalStateException("This socket is already connected to a game.");
6499
var player = api.createPlayer(gameId, username, joinSecret);
65100
connect(gameId, player.id, player.secret);
66101
}
67102

103+
/**
104+
* Loads the session from disk and reconnects to the game.
105+
*
106+
* @param username The username of the session.
107+
* @throws IOException Thrown when the session doesn't exist or the request
108+
* fails.
109+
*/
68110
public void restoreSession(String username) throws IOException {
69111
if (session.gameURL != "")
70112
throw new IllegalStateException("This socket is already connected to a game.");
@@ -77,6 +119,14 @@ public void restoreSession(String username) throws IOException {
77119
}
78120
}
79121

122+
/**
123+
* Connects to a player on the server.
124+
*
125+
* @param gameId The ID of the game.
126+
* @param playerId The ID of the player.
127+
* @param playerSecret The secret of the player.
128+
* @throws IOException Thrown when the connection fails.
129+
*/
80130
public void connect(String gameId, String playerId, String playerSecret) throws IOException {
81131
if (session.gameURL != "")
82132
throw new IllegalStateException("This socket is already connected to a game.");
@@ -96,6 +146,12 @@ public void connect(String gameId, String playerId, String playerSecret) throws
96146
}
97147
}
98148

149+
/**
150+
* Connects to a game as a spectator.
151+
*
152+
* @param gameId The ID of the game.
153+
* @throws IOException Thrown when the connection fails.
154+
*/
99155
public void spectate(String gameId) throws IOException {
100156
if (session.gameURL != "")
101157
throw new IllegalStateException("This socket is already connected to a game.");
@@ -109,6 +165,9 @@ public void spectate(String gameId) throws IOException {
109165
usernameCache = api.fetchPlayers(gameId);
110166
}
111167

168+
/**
169+
* Blocks until the connection is closed.
170+
*/
112171
public void listen() {
113172
try {
114173
exitEvent.await();
@@ -117,6 +176,9 @@ public void listen() {
117176
}
118177
}
119178

179+
/**
180+
* Close the underlying websocket connection.
181+
*/
120182
public void close() {
121183
websocket.sendClose(WebSocket.NORMAL_CLOSURE, "Normal closure.");
122184
listen();
@@ -137,6 +199,15 @@ public Event(String name, T data) {
137199
}
138200
}
139201

202+
/**
203+
* Registers a callback that is triggered every time the event is received.
204+
*
205+
* @param <T> The type of the event data.
206+
* @param eventName The name of the event.
207+
* @param type The type of the event data.
208+
* @param callback The callback function.
209+
* @return An ID that can be used to remove the callback.
210+
*/
140211
@SuppressWarnings("unchecked")
141212
public <T> String on(String eventName, Class<T> type, EventCallback<T> callback) {
142213
if (!eventListeners.containsKey(eventName))
@@ -151,6 +222,15 @@ public <T> String on(String eventName, Class<T> type, EventCallback<T> callback)
151222
return id;
152223
}
153224

225+
/**
226+
* Registers a callback that is triggered the next time the event is received.
227+
*
228+
* @param <T> The type of the event data.
229+
* @param eventName The name of the event.
230+
* @param type The type of the event data.
231+
* @param callback The callback function.
232+
* @return An ID that can be used to remove the callback.
233+
*/
154234
@SuppressWarnings("unchecked")
155235
public <T> String once(String eventName, Class<T> type, EventCallback<T> callback) {
156236
if (!eventListeners.containsKey(eventName))
@@ -168,6 +248,13 @@ public <T> String once(String eventName, Class<T> type, EventCallback<T> callbac
168248
return id;
169249
}
170250

251+
/**
252+
* Sends the command to the server.
253+
*
254+
* @param <T> The type of the command data.
255+
* @param commandName The name of the command.
256+
* @param data The command data.
257+
*/
171258
public <T> void send(String commandName, T data) {
172259
if (websocket == null || session.getPlayerId().isEmpty())
173260
throw new IllegalStateException("The socket is not connected to a player.");
@@ -176,12 +263,26 @@ public <T> void send(String commandName, T data) {
176263
websocket.sendText(json, true).join();
177264
}
178265

266+
/**
267+
* Removes the event callback.
268+
*
269+
* @param eventName The name of the event.
270+
* @param id The ID of the callback.
271+
*/
179272
public void removeCallback(String eventName, String id) {
180273
if (!eventListeners.containsKey(eventName))
181274
return;
182275
eventListeners.get(eventName).callbacks.remove(id);
183276
}
184277

278+
/**
279+
* Retrieves the username of the player from the player cache or fetches it from
280+
* the server if it is not already there.
281+
*
282+
* @param playerID The ID of the player.
283+
* @return Ther usernamer of the player.
284+
* @throws IOException Thrown when the request fails.
285+
*/
185286
public String username(String playerID) throws IOException {
186287
if (usernameCache.containsKey(playerID))
187288
return usernameCache.get(playerID);
@@ -190,10 +291,17 @@ public String username(String playerID) throws IOException {
190291
return username;
191292
}
192293

294+
/**
295+
* @return An instance of the Api class which can be used to make requests to
296+
* the game server.
297+
*/
193298
public Api getApi() {
194299
return api;
195300
}
196301

302+
/**
303+
* @return The current session object.
304+
*/
197305
public Session getSession() {
198306
return session;
199307
}

0 commit comments

Comments
 (0)