diff options
| author | ilotterytea <iltsu@alright.party> | 2025-01-05 00:14:45 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-01-05 00:58:57 +0500 |
| commit | b0f71c097631f6f9d595865c48f1ffd1990e8794 (patch) | |
| tree | 0c2a12ba73a9610c6d7aaaf912b08c39f221a4c7 /core/src | |
| parent | 47303cced8f6e55aaab2579293da49723ba39c1d (diff) | |
Diffstat (limited to 'core/src')
6 files changed, 130 insertions, 1 deletions
diff --git a/core/src/kz/ilotterytea/maxon/MaxonConstants.java b/core/src/kz/ilotterytea/maxon/MaxonConstants.java index cab66f4..44c7208 100644 --- a/core/src/kz/ilotterytea/maxon/MaxonConstants.java +++ b/core/src/kz/ilotterytea/maxon/MaxonConstants.java @@ -62,6 +62,7 @@ public class MaxonConstants { public static final String IDENTITY_REFRESH_URL = IDENTITY_BASE_URL + "/refresh"; public static final String IDENTITY_AUTHENTICATION_URL = IDENTITY_BASE_URL + "/authenticate"; public static final String IDENTITY_REGISTRATION_URL = IDENTITY_BASE_URL; + public static final String SESSION_WSS_URL = "ws://localhost:31084"; public static final long DISCORD_APPLICATION_ID = 1051092609659052062L; diff --git a/core/src/kz/ilotterytea/maxon/MaxonGame.java b/core/src/kz/ilotterytea/maxon/MaxonGame.java index 8986de9..7381b6d 100644 --- a/core/src/kz/ilotterytea/maxon/MaxonGame.java +++ b/core/src/kz/ilotterytea/maxon/MaxonGame.java @@ -9,6 +9,7 @@ import kz.ilotterytea.maxon.localization.LocalizationManager; import kz.ilotterytea.maxon.pets.PetManager; import kz.ilotterytea.maxon.screens.SplashScreen; import kz.ilotterytea.maxon.session.IdentityClient; +import kz.ilotterytea.maxon.session.SessionClient; import kz.ilotterytea.maxon.utils.GameUpdater; public class MaxonGame extends Game { @@ -20,7 +21,9 @@ public class MaxonGame extends Game { private PetManager petManager; private DiscordActivityClient discordActivityClient; + private IdentityClient identityClient; + private SessionClient sessionClient; private static MaxonGame instance; @@ -43,6 +46,10 @@ public class MaxonGame extends Game { return identityClient; } + public SessionClient getSessionClient() { + return sessionClient; + } + public LocalizationManager getLocale() { return locale; } @@ -57,6 +64,8 @@ public class MaxonGame extends Game { new GameUpdater().checkLatestUpdate(); identityClient = new IdentityClient(Gdx.app.getPreferences("kz.ilotterytea.SigninIdentity")); + sessionClient = new SessionClient(); + batch = new SpriteBatch(); prefs = Gdx.app.getPreferences(MaxonConstants.GAME_APP_PACKAGE); locale = new LocalizationManager(Gdx.files.internal("i18n/" + prefs.getString("lang", "en_us") + ".json")); 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); + } +} diff --git a/core/src/main/resources/logback.xml b/core/src/main/resources/logback.xml index 4b08185..df24417 100644 --- a/core/src/main/resources/logback.xml +++ b/core/src/main/resources/logback.xml @@ -9,7 +9,7 @@ </encoder> </appender> - <root level="info"> + <root level="debug"> <appender-ref ref="STDOUT"/> </root> </configuration> |
