summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-01-23 20:31:23 +0500
committerilotterytea <iltsu@alright.party>2025-01-23 20:31:23 +0500
commitff9d8f584616cc3d9d7000e95f681707fd508497 (patch)
treeb56f83b921f887424e69fffde8c34753a4e09baa /server
parentc94a51d6ab4863e2fa6fd230def08aac3f2bf73a (diff)
feat: server-side sign-in implementation
Diffstat (limited to 'server')
-rw-r--r--server/build.gradle1
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java5
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java11
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java51
4 files changed, 58 insertions, 10 deletions
diff --git a/server/build.gradle b/server/build.gradle
index d4acfbd..9ef7670 100644
--- a/server/build.gradle
+++ b/server/build.gradle
@@ -13,6 +13,7 @@ eclipse.project.name = appName + '-server'
dependencies {
api "org.java-websocket:Java-WebSocket:$javaWebsocketVersion"
+ implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
implementation project(':shared')
}
diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java
index 6e8fbde..095fb05 100644
--- a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java
+++ b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java
@@ -2,7 +2,6 @@ package kz.ilotterytea.frogartha.server;
import com.github.czyzby.websocket.serialization.SerializationException;
import com.github.czyzby.websocket.serialization.impl.ManualSerializer;
-import kz.ilotterytea.frogartha.domain.Identity;
import kz.ilotterytea.frogartha.domain.actions.*;
import kz.ilotterytea.frogartha.exceptions.PlayerKickException;
import kz.ilotterytea.frogartha.utils.Logger;
@@ -88,8 +87,8 @@ public class FrogarthaServer extends WebSocketServer {
throw PlayerKickException.internalServerError();
}
- if (obj instanceof Identity) {
- ServerHandlers.handleIdentity(player, (Identity) obj);
+ if (obj instanceof IdentificationAction) {
+ ServerHandlers.handleIdentification(player, (IdentificationAction) obj);
} else if (obj instanceof PlayerJumpAction) {
ServerHandlers.handlePlayerJumpAction(player, (PlayerJumpAction) obj);
} else if (obj instanceof ChangedDirectionAction) {
diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java b/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java
index f1d0a3b..83f10ec 100644
--- a/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java
+++ b/server/src/main/java/kz/ilotterytea/frogartha/server/PlayerConnection.java
@@ -1,6 +1,7 @@
package kz.ilotterytea.frogartha.server;
import kz.ilotterytea.frogartha.domain.PlayerData;
+import kz.ilotterytea.frogartha.domain.actions.IdentificationAction;
import org.java_websocket.WebSocket;
import java.sql.Timestamp;
@@ -10,6 +11,7 @@ public class PlayerConnection extends PlayerData {
private final WebSocket connection;
private Room room;
+ private IdentificationAction identification;
private final Timestamp connectedTimestamp;
@@ -19,6 +21,7 @@ public class PlayerConnection extends PlayerData {
TOTAL_CONNECTION_IDS++;
this.room = null;
+ this.identification = null;
this.connectedTimestamp = new Timestamp(System.currentTimeMillis());
}
@@ -43,6 +46,14 @@ public class PlayerConnection extends PlayerData {
this.room = room;
}
+ public IdentificationAction getIdentification() {
+ return identification;
+ }
+
+ public void setIdentification(IdentificationAction identification) {
+ this.identification = identification;
+ }
+
public Timestamp getConnectedTimestamp() {
return connectedTimestamp;
}
diff --git a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
index 6891fef..ab5cfd4 100644
--- a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
+++ b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
@@ -2,6 +2,9 @@ package kz.ilotterytea.frogartha.server;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
+import com.badlogic.gdx.utils.JsonReader;
+import com.badlogic.gdx.utils.JsonValue;
+import com.badlogic.gdx.utils.JsonWriter;
import kz.ilotterytea.frogartha.FrogarthaConstants;
import kz.ilotterytea.frogartha.domain.Identity;
import kz.ilotterytea.frogartha.domain.PlayerState;
@@ -12,6 +15,7 @@ import kz.ilotterytea.frogartha.domain.events.IdentifiedEvent;
import kz.ilotterytea.frogartha.domain.events.PlayerJumpEvent;
import kz.ilotterytea.frogartha.exceptions.PlayerKickException;
import kz.ilotterytea.frogartha.utils.Logger;
+import okhttp3.*;
import java.util.Optional;
@@ -19,18 +23,51 @@ public class ServerHandlers {
private static final Logger log = new Logger(ServerHandlers.class);
private static final FrogarthaServer server = FrogarthaServer.getInstance();
- public static void handleIdentity(PlayerConnection player, Identity identity) {
+ public static void handleIdentification(PlayerConnection player, IdentificationAction action) {
+ JsonValue json = new JsonValue(JsonValue.ValueType.object);
+ json.addChild("clientToken", new JsonValue(action.getClientToken()));
+ json.addChild("accessToken", new JsonValue(action.getAccessToken()));
+
+ OkHttpClient client = new OkHttpClient();
+
+ Request httpRequest = new Request.Builder()
+ .post(RequestBody.create(json.toJson(JsonWriter.OutputType.json), MediaType.parse("application/json; charset=utf-8")))
+ .url(FrogarthaConstants.URLS.IDENTITY_IDENTIFY_URL)
+ .build();
+
+ try (Response response = client.newCall(httpRequest).execute()) {
+ if (response.body() == null) {
+ throw new Exception("Response body is null");
+ }
+
+ json = new JsonReader().parse(response.body().string());
+
+ if (response.code() != 200) {
+ throw new Exception(json.get("error").getString("message"));
+ }
+
+ Identity identity = new Identity(
+ json.get("data").getLong("id"),
+ json.get("data").getString("username")
+ );
+
+ player.setIdentification(action);
+ player.setIdentity(identity);
+ player.send(new IdentifiedEvent(player.getId(), identity));
+
+ log.log("Successfully identified {} for {}", identity, player);
+ } catch (Exception e) {
+ log.error("Failed to identify: {}", e);
+ server.kickConnection(player, PlayerKickException.badLogin(e.getMessage()));
+ return;
+ }
+
if (server.getPlayers()
.stream()
.filter((x) -> x.getIdentity() != null)
- .anyMatch((x) -> x.getIdentity().equals(identity) && x.getId() != player.getId())) {
+ .anyMatch((x) -> x.getIdentity().getId() == player.getIdentity().getId() && x.getId() != player.getId())) {
server.kickConnection(player, PlayerKickException.loggedIn());
- return;
}
-
- player.setIdentity(identity);
- player.send(new IdentifiedEvent(player.getId()));
- log.log("Successfully identified {} for {}", identity, player);
}
public static void handlePlayerJumpAction(PlayerConnection player, PlayerJumpAction action) {