summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-01-28 22:44:58 +0500
committerilotterytea <iltsu@alright.party>2025-01-28 22:44:58 +0500
commit835c2fdb6c1f1064ebb0eece8816cd4edfee3ec8 (patch)
tree04e0dc3591329010b4bee7720cfe51612a494c72
parent76c0903ea857ff9bf31fe3cf75a3a41d3ee2ec06 (diff)
feat: set the server constants on connect
-rw-r--r--core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java4
-rw-r--r--core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java11
-rw-r--r--server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java2
-rw-r--r--shared/src/main/java/kz/ilotterytea/frogartha/FrogarthaConstants.java10
-rw-r--r--shared/src/main/java/kz/ilotterytea/frogartha/domain/events/ServerOptionsEvent.java83
-rw-r--r--shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java4
6 files changed, 107 insertions, 7 deletions
diff --git a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java
index 681b377..8b3b365 100644
--- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java
+++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionClient.java
@@ -76,7 +76,9 @@ public class SessionClient implements WebSocketListener {
throw new RuntimeException("Deserialized packet is null");
}
- if (obj instanceof IdentifiedEvent) {
+ if (obj instanceof ServerOptionsEvent) {
+ SessionHandlers.handleServerOptionsEvent((ServerOptionsEvent) obj);
+ } else if (obj instanceof IdentifiedEvent) {
IdentifiedEvent x = (IdentifiedEvent) obj;
identity = x.getIdentity();
connectionId = x.getPlayerId();
diff --git a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java
index 946a903..41517b6 100644
--- a/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java
+++ b/core/src/main/java/kz/ilotterytea/frogartha/sessions/SessionHandlers.java
@@ -1,6 +1,7 @@
package kz.ilotterytea.frogartha.sessions;
import com.badlogic.gdx.math.Vector2;
+import kz.ilotterytea.frogartha.FrogarthaConstants;
import kz.ilotterytea.frogartha.FrogarthaGame;
import kz.ilotterytea.frogartha.domain.PlayerData;
import kz.ilotterytea.frogartha.domain.events.*;
@@ -16,6 +17,16 @@ public class SessionHandlers {
private static final FrogarthaGame game = FrogarthaGame.getInstance();
private static final SessionClient client = game.getSessionClient();
+ public static void handleServerOptionsEvent(ServerOptionsEvent event) {
+ FrogarthaConstants.Chat.MAX_MESSAGE_LENGTH = event.getChatMaxMessageLength();
+ FrogarthaConstants.Chat.MESSAGE_PER_MS = event.getChatMessagePerMilliseconds();
+ FrogarthaConstants.Player.MAX_JUMP_STRENGTH = event.getPlayerMaxJumpStrength();
+ FrogarthaConstants.Player.MAX_JUMP_HEIGHT = event.getPlayerMaxJumpHeight();
+ FrogarthaConstants.Player.JUMP_SPEED = event.getPlayerJumpSpeed();
+
+ log.log("Set the server constants: {}", event);
+ }
+
public static void handlePlayerJumpEvent(PlayerJumpEvent event) {
if (!game.getScreen().getClass().equals(GameScreen.class)) {
log.log("The screen is not GameScreen, but the session received PlayerJumpEvent");
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 095fb05..d7f6a44 100644
--- a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java
+++ b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java
@@ -3,6 +3,7 @@ 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.actions.*;
+import kz.ilotterytea.frogartha.domain.events.ServerOptionsEvent;
import kz.ilotterytea.frogartha.exceptions.PlayerKickException;
import kz.ilotterytea.frogartha.utils.Logger;
import kz.ilotterytea.frogartha.utils.SerializerUtils;
@@ -42,6 +43,7 @@ public class FrogarthaServer extends WebSocketServer {
PlayerConnection player = new PlayerConnection(webSocket);
log.log("{} ({}) connected!", player, webSocket.getRemoteSocketAddress().getAddress().getHostAddress());
this.players.add(player);
+ player.send(new ServerOptionsEvent());
}
@Override
diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/FrogarthaConstants.java b/shared/src/main/java/kz/ilotterytea/frogartha/FrogarthaConstants.java
index 06ca3b2..216a9d1 100644
--- a/shared/src/main/java/kz/ilotterytea/frogartha/FrogarthaConstants.java
+++ b/shared/src/main/java/kz/ilotterytea/frogartha/FrogarthaConstants.java
@@ -20,9 +20,9 @@ public class FrogarthaConstants {
}
public static class Player {
- public static final float MAX_JUMP_STRENGTH = 1.5f;
- public static final float MAX_JUMP_HEIGHT = 2f;
- public static final float JUMP_SPEED = 10f;
+ public static float MAX_JUMP_STRENGTH = 1.5f;
+ public static float MAX_JUMP_HEIGHT = 2f;
+ public static float JUMP_SPEED = 10f;
}
public static class Room {
@@ -30,7 +30,7 @@ public class FrogarthaConstants {
}
public static class Chat {
- public static final int MAX_MESSAGE_LENGTH = 100;
- public static final int MESSAGE_PER_MS = 500;
+ public static int MAX_MESSAGE_LENGTH = 100;
+ public static int MESSAGE_PER_MS = 500;
}
}
diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/domain/events/ServerOptionsEvent.java b/shared/src/main/java/kz/ilotterytea/frogartha/domain/events/ServerOptionsEvent.java
new file mode 100644
index 0000000..7393375
--- /dev/null
+++ b/shared/src/main/java/kz/ilotterytea/frogartha/domain/events/ServerOptionsEvent.java
@@ -0,0 +1,83 @@
+package kz.ilotterytea.frogartha.domain.events;
+
+import com.github.czyzby.websocket.serialization.SerializationException;
+import com.github.czyzby.websocket.serialization.Transferable;
+import com.github.czyzby.websocket.serialization.impl.Deserializer;
+import com.github.czyzby.websocket.serialization.impl.Serializer;
+import kz.ilotterytea.frogartha.FrogarthaConstants;
+
+public class ServerOptionsEvent extends Event implements Transferable<ServerOptionsEvent> {
+ private final int chatMaxMessageLength, chatMessagePerMilliseconds;
+ private final float playerMaxJumpStrength, playerMaxJumpHeight, playerJumpSpeed;
+
+ public ServerOptionsEvent() {
+ this.chatMaxMessageLength = FrogarthaConstants.Chat.MAX_MESSAGE_LENGTH;
+ this.chatMessagePerMilliseconds = FrogarthaConstants.Chat.MESSAGE_PER_MS;
+ this.playerMaxJumpStrength = FrogarthaConstants.Player.MAX_JUMP_STRENGTH;
+ this.playerMaxJumpHeight = FrogarthaConstants.Player.MAX_JUMP_HEIGHT;
+ this.playerJumpSpeed = FrogarthaConstants.Player.JUMP_SPEED;
+ }
+
+ public ServerOptionsEvent(
+ int chatMaxMessageLength, int chatMessagePerMilliseconds,
+ float playerMaxJumpStrength, float playerMaxJumpHeight, float playerJumpSpeed
+ ) {
+ this.chatMaxMessageLength = chatMaxMessageLength;
+ this.chatMessagePerMilliseconds = chatMessagePerMilliseconds;
+ this.playerMaxJumpStrength = playerMaxJumpStrength;
+ this.playerMaxJumpHeight = playerMaxJumpHeight;
+ this.playerJumpSpeed = playerJumpSpeed;
+ }
+
+ public int getChatMaxMessageLength() {
+ return chatMaxMessageLength;
+ }
+
+ public int getChatMessagePerMilliseconds() {
+ return chatMessagePerMilliseconds;
+ }
+
+ public float getPlayerMaxJumpStrength() {
+ return playerMaxJumpStrength;
+ }
+
+ public float getPlayerMaxJumpHeight() {
+ return playerMaxJumpHeight;
+ }
+
+ public float getPlayerJumpSpeed() {
+ return playerJumpSpeed;
+ }
+
+ @Override
+ public void serialize(Serializer serializer) throws SerializationException {
+ serializer
+ .serializeInt(chatMaxMessageLength)
+ .serializeInt(chatMessagePerMilliseconds)
+ .serializeFloat(playerMaxJumpStrength)
+ .serializeFloat(playerMaxJumpHeight)
+ .serializeFloat(playerJumpSpeed);
+ }
+
+ @Override
+ public ServerOptionsEvent deserialize(Deserializer deserializer) throws SerializationException {
+ return new ServerOptionsEvent(
+ deserializer.deserializeInt(),
+ deserializer.deserializeInt(),
+ deserializer.deserializeFloat(),
+ deserializer.deserializeFloat(),
+ deserializer.deserializeFloat()
+ );
+ }
+
+ @Override
+ public String toString() {
+ return "ServerOptionsEvent{" +
+ "chatMaxMessageLength=" + chatMaxMessageLength +
+ ", chatMessagePerMilliseconds=" + chatMessagePerMilliseconds +
+ ", playerMaxJumpStrength=" + playerMaxJumpStrength +
+ ", playerMaxJumpHeight=" + playerMaxJumpHeight +
+ ", playerJumpSpeed=" + playerJumpSpeed +
+ '}';
+ }
+}
diff --git a/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java b/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java
index 2a22bcc..b269c2f 100644
--- a/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java
+++ b/shared/src/main/java/kz/ilotterytea/frogartha/utils/SerializerUtils.java
@@ -14,7 +14,7 @@ public class SerializerUtils {
serializer.register(new PlayerJumpEvent());
serializer.register(new ChangedDirectionAction());
serializer.register(new ChangedDirectionEvent());
-
+
serializer.register(new IdentifiedEvent());
serializer.register(new IdentificationAction());
@@ -28,5 +28,7 @@ public class SerializerUtils {
serializer.register(new ChatMessageAction());
serializer.register(new ChatMessageEvent());
+
+ serializer.register(new ServerOptionsEvent());
}
}