Skip to content

Commit aa66d79

Browse files
committed
Implement CG Version mismatch warning
1 parent 9c1bd7d commit aa66d79

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<artifactId>commons-io</artifactId>
3535
<version>2.11.0</version>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.fusesource.jansi</groupId>
39+
<artifactId>jansi</artifactId>
40+
<version>2.4.0</version>
41+
</dependency>
3742
</dependencies>
3843

3944
<build>

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
import com.google.gson.annotations.SerializedName;
1010
import com.google.gson.reflect.TypeToken;
1111

12+
import org.fusesource.jansi.Ansi;
13+
import org.fusesource.jansi.AnsiConsole;
14+
import org.fusesource.jansi.Ansi.*;
15+
1216
public class GameSocket {
17+
private static final String cgVersion = "0.8";
18+
1319
@FunctionalInterface
1420
public interface EventCallback<T> {
1521
void cb(T data);
@@ -33,8 +39,15 @@ private class Callbacks<T> {
3339
private HashMap<String, Callbacks> eventListeners = new HashMap<>();
3440
private CountDownLatch exitEvent = new CountDownLatch(1);
3541

36-
public GameSocket(String url) {
42+
public GameSocket(String url) throws IOException {
43+
AnsiConsole.systemInstall();
3744
api = new Api(url);
45+
var info = api.fetchInfo();
46+
if (!isVersionCompatible(info.cgVersion)) {
47+
System.out.println(Ansi.ansi().fg(Color.YELLOW)
48+
.a("WARNING: CodeGame version mismatch. Server: v" + info.cgVersion + ", client: v" + cgVersion)
49+
.reset());
50+
}
3851
}
3952

4053
public Api.GameData createGame(boolean makePublic, boolean protect, Object config) throws IOException {
@@ -156,7 +169,7 @@ public <T> String once(String eventName, Class<T> type, EventCallback<T> callbac
156169
}
157170

158171
public <T> void send(String commandName, T data) {
159-
if (websocket == null || session.getPlayerId() == "")
172+
if (websocket == null || session.getPlayerId().isEmpty())
160173
throw new IllegalStateException("The socket is not connected to a player.");
161174
Event<T> e = new Event<>(commandName, data);
162175
var json = Api.json.toJson(e, TypeToken.getParameterized(Event.class, data.getClass()).getType());
@@ -205,4 +218,27 @@ private void onMessage(String message) {
205218
private void onClose() {
206219
exitEvent.countDown();
207220
}
221+
222+
private static boolean isVersionCompatible(String serverVersion) {
223+
var serverParts = serverVersion.split("\\.");
224+
if (serverParts.length == 1)
225+
serverParts = new String[] { serverParts[0], "0" };
226+
var clientParts = cgVersion.split("\\.");
227+
if (clientParts.length == 1)
228+
clientParts = new String[] { clientParts[0], "0" };
229+
230+
if (!serverParts[0].equals(clientParts[0]))
231+
return false;
232+
233+
if (clientParts[0].equals("0"))
234+
return serverParts[1].equals(clientParts[1]);
235+
236+
try {
237+
var serverMinor = Integer.parseInt(serverParts[1]);
238+
var clientMinor = Integer.parseInt(clientParts[1]);
239+
return clientMinor <= serverMinor;
240+
} catch (NumberFormatException e) {
241+
return false;
242+
}
243+
}
208244
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public static Session load(String gameURL, String username) throws IOException {
4343
FilenameUtils.concat(gamesPath, URLEncoder.encode(gameURL, "UTF-8") + "/" + username + ".json")));
4444
try {
4545
var session = Api.json.fromJson(reader, Session.class);
46-
if (session.gameId == null || session.gameId == "" || session.playerId == null || session.playerId == ""
47-
|| session.playerSecret == null || session.playerSecret == "") {
46+
if (session.gameId == null || session.gameId.isEmpty() || session.playerId == null
47+
|| session.playerId.isEmpty()
48+
|| session.playerSecret == null || session.playerSecret.isEmpty()) {
4849
throw new IOException("Incomplete session file.");
4950
}
5051
session.gameURL = gameURL;
@@ -56,7 +57,8 @@ public static Session load(String gameURL, String username) throws IOException {
5657
}
5758

5859
public void save() throws IOException, Exception {
59-
if (gameURL == "" || username == "" || gameId == "" || playerId == "" || playerSecret == "") {
60+
if (gameURL.isEmpty() || username.isEmpty() || gameId.isEmpty() || playerId.isEmpty()
61+
|| playerSecret.isEmpty()) {
6062
throw new Exception("Incomplete session file.");
6163
}
6264
var dir = new File(FilenameUtils.concat(gamesPath, URLEncoder.encode(gameURL, "UTF-8")));
@@ -73,7 +75,7 @@ public void save() throws IOException, Exception {
7375

7476
public void remove() {
7577
try {
76-
if (gameURL == "")
78+
if (gameURL.isEmpty())
7779
return;
7880

7981
var dir = new File(FilenameUtils.concat(gamesPath, URLEncoder.encode(gameURL, "UTF-8")));

0 commit comments

Comments
 (0)