1313import org .fusesource .jansi .AnsiConsole ;
1414import org .fusesource .jansi .Ansi .*;
1515
16+ /**
17+ * Represents a connection to a game server.
18+ */
1619public 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