summaryrefslogtreecommitdiff
path: root/core/src/kz/ilotterytea/maxon/session
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-01-05 00:14:45 +0500
committerilotterytea <iltsu@alright.party>2025-01-05 00:58:57 +0500
commitb0f71c097631f6f9d595865c48f1ffd1990e8794 (patch)
tree0c2a12ba73a9610c6d7aaaf912b08c39f221a4c7 /core/src/kz/ilotterytea/maxon/session
parent47303cced8f6e55aaab2579293da49723ba39c1d (diff)
feat: established communication between game and serverHEADmaster
Diffstat (limited to 'core/src/kz/ilotterytea/maxon/session')
-rw-r--r--core/src/kz/ilotterytea/maxon/session/IdentityClient.java9
-rw-r--r--core/src/kz/ilotterytea/maxon/session/SessionClient.java95
-rw-r--r--core/src/kz/ilotterytea/maxon/session/SessionHandlers.java15
3 files changed, 119 insertions, 0 deletions
diff --git a/core/src/kz/ilotterytea/maxon/session/IdentityClient.java b/core/src/kz/ilotterytea/maxon/session/IdentityClient.java
index 570b16b..831589d 100644
--- a/core/src/kz/ilotterytea/maxon/session/IdentityClient.java
+++ b/core/src/kz/ilotterytea/maxon/session/IdentityClient.java
@@ -10,6 +10,7 @@ import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.JsonWriter;
import com.badlogic.gdx.utils.Timer;
import kz.ilotterytea.maxon.MaxonConstants;
+import kz.ilotterytea.maxon.MaxonGame;
import kz.ilotterytea.maxon.utils.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -90,6 +91,8 @@ public class IdentityClient {
IdentityClient.this.userId = String.valueOf(json.get("data").get("user").getInt("id"));
IdentityClient.this.isAuthorised = true;
log.info("Successfully authorised! Welcome back, {}!", IdentityClient.this.username);
+
+ MaxonGame.getInstance().getSessionClient().connect();
} catch (Exception e) {
log.error("An exception was thrown while authorising", e);
}
@@ -141,6 +144,7 @@ public class IdentityClient {
accessToken = null;
userId = null;
isAuthorised = false;
+ MaxonGame.getInstance().getSessionClient().close(5001, "Failed to validate token");
authorize(username, password);
return;
}
@@ -213,6 +217,8 @@ public class IdentityClient {
sessionPreferences.remove("username");
sessionPreferences.remove("password");
sessionPreferences.flush();
+
+ MaxonGame.getInstance().getSessionClient().close(5001, "Invalidated token");
} catch (Exception ignored) {
}
}
@@ -262,11 +268,14 @@ public class IdentityClient {
userId = null;
isAuthorised = false;
log.warn(error);
+ MaxonGame.getInstance().getSessionClient().close(5002, "Failed to refresh token");
return;
}
accessToken = json.get("data").get("accessToken").asString();
log.info("Token has been refreshed!");
+
+ MaxonGame.getInstance().getSessionClient().updateIdentity();
} catch (Exception e) {
log.error("An exception was thrown while refreshing", e);
}
diff --git a/core/src/kz/ilotterytea/maxon/session/SessionClient.java b/core/src/kz/ilotterytea/maxon/session/SessionClient.java
new file mode 100644
index 0000000..78a6c0e
--- /dev/null
+++ b/core/src/kz/ilotterytea/maxon/session/SessionClient.java
@@ -0,0 +1,95 @@
+package kz.ilotterytea.maxon.session;
+
+import kz.ilotterytea.maxon.MaxonConstants;
+import kz.ilotterytea.maxon.MaxonGame;
+import kz.ilotterytea.maxon.screens.MenuScreen;
+import kz.ilotterytea.maxon.shared.Acknowledge;
+import kz.ilotterytea.maxon.shared.Identity;
+import kz.ilotterytea.maxon.shared.exceptions.PlayerKickException;
+import org.java_websocket.client.WebSocketClient;
+import org.java_websocket.handshake.ServerHandshake;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.net.URI;
+import java.nio.ByteBuffer;
+
+public class SessionClient extends WebSocketClient {
+ private final Logger log;
+ private final MaxonGame game;
+
+ public SessionClient() {
+ super(URI.create(MaxonConstants.SESSION_WSS_URL));
+ this.log = LoggerFactory.getLogger(SessionClient.class);
+ this.game = MaxonGame.getInstance();
+ }
+
+ @Override
+ public void onOpen(ServerHandshake handshakedata) {
+ log.info("Connected!");
+ updateIdentity();
+ }
+
+ @Override
+ public void onMessage(String message) {
+ }
+
+ @Override
+ public void onMessage(ByteBuffer bytes) {
+ try {
+ // Deserialize the object
+ ByteArrayInputStream bais = new ByteArrayInputStream(bytes.array());
+ ObjectInputStream ois = new ObjectInputStream(bais);
+ Object obj = ois.readObject();
+
+ if (obj instanceof Acknowledge acknowledge) SessionHandlers.handleAcknowledge(acknowledge);
+ else if (obj instanceof PlayerKickException exception) throw exception;
+ } catch (PlayerKickException e) {
+ log.info("Kicked out!", e);
+ } catch (Exception e) {
+ log.error("An exception was thrown while processing message", e);
+ }
+ }
+
+ @Override
+ public void onClose(int code, String reason, boolean remote) {
+ log.info("Connection closed! Reason: {} {}", code, reason);
+ game.getIdentityClient().invalidateToken();
+ if (!game.getScreen().getClass().equals(MenuScreen.class)) {
+ game.setScreen(new MenuScreen());
+ }
+ }
+
+ @Override
+ public void onError(Exception ex) {
+ log.error("Failed to connect", ex);
+ }
+
+ @Override
+ public void connect() {
+ super.connect();
+ }
+
+ public void updateIdentity() {
+ IdentityClient identityClient = game.getIdentityClient();
+
+ Identity identity = new Identity(identityClient.getClientToken(), identityClient.getAccessToken());
+
+ send(identity);
+ }
+
+ public void send(Object object) {
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(object);
+ send(baos.toByteArray());
+ } catch (Exception e) {
+ log.error("Failed to serialize and send an object", e);
+ }
+ }
+}
diff --git a/core/src/kz/ilotterytea/maxon/session/SessionHandlers.java b/core/src/kz/ilotterytea/maxon/session/SessionHandlers.java
new file mode 100644
index 0000000..7b86916
--- /dev/null
+++ b/core/src/kz/ilotterytea/maxon/session/SessionHandlers.java
@@ -0,0 +1,15 @@
+package kz.ilotterytea.maxon.session;
+
+import kz.ilotterytea.maxon.MaxonGame;
+import kz.ilotterytea.maxon.shared.Acknowledge;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class SessionHandlers {
+ private static final Logger log = LoggerFactory.getLogger(SessionHandlers.class);
+ private static final SessionClient client = MaxonGame.getInstance().getSessionClient();
+
+ public static void handleAcknowledge(Acknowledge acknowledge) {
+ log.info("alright: {}", acknowledge);
+ }
+}