From cb3005f1086158bf7abcdd85102d86e7bd418e4e Mon Sep 17 00:00:00 2001 From: Lost-Fly <114507453+Lost-Fly@users.noreply.github.com> Date: Mon, 22 Apr 2024 23:27:27 +0500 Subject: [PATCH] Fully reorganized SERVER LOGIC | ADDED GAME SESSIONS --- .../packandgo/client/AndroidLauncher.java | 35 ++--------- .../packandgo/client/MessageProcessor.java | 27 +++------ .../packandgo/client/dto/InputStateImpl.java | 60 +------------------ .../packandgo/client/ws/WebSocketClient.java | 23 +------ .../client/ws/WebSocketListener.java | 7 ++- .../packandgo/client/ws/WsEvent.java | 6 +- assets/env.properties | 2 +- .../aqwsxlostfly/packandgo/GameSession.java | 20 ++----- .../aqwsxlostfly/packandgo/InputState.java | 12 ---- core/src/com/aqwsxlostfly/packandgo/Main.java | 4 +- .../packandgo/Screens/HomeSc.java | 10 ++-- .../packandgo/SessionStateToSend.java | 4 +- .../packandgo/TouchProcessor.java | 6 +- 13 files changed, 38 insertions(+), 178 deletions(-) diff --git a/android/src/com/aqwsxlostfly/packandgo/client/AndroidLauncher.java b/android/src/com/aqwsxlostfly/packandgo/client/AndroidLauncher.java index 83e85f2..bfdc3fd 100644 --- a/android/src/com/aqwsxlostfly/packandgo/client/AndroidLauncher.java +++ b/android/src/com/aqwsxlostfly/packandgo/client/AndroidLauncher.java @@ -11,9 +11,7 @@ import com.badlogic.gdx.Gdx; import com.badlogic.gdx.backends.android.AndroidApplication; import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; -import com.badlogic.gdx.utils.Json; import com.fasterxml.jackson.databind.ObjectMapper; -import com.badlogic.gdx.utils.Timer; import org.java_websocket.handshake.ServerHandshake; @@ -59,25 +57,6 @@ protected void onCreate(Bundle savedInstanceState) { messageProcessor = new MessageProcessor(main); - -// Timer timer = new Timer(); -// timer.scheduleTask(new Timer.Task() { -// @Override -// public void run() { -// main.handleTimer(); -// } -// }, 0, 1); - - -// connectSocket(webSocketClient); - -// main.setSocketState(webSocketClient.isOpen()); -// Gdx.app.log("WS STATUS", "Status: " + main.getSocketState()); - -// if (webSocketClient.isOpen()){ -// main.setSocketState(true); -// } - main.setMessageSender(message -> { webSocketClient.send(toJson(message)); }); @@ -88,7 +67,7 @@ protected void onCreate(Bundle savedInstanceState) { } - private URI getUri(String strUri){ + private URI getUri(String strUri) { try { return new URI(strUri); } catch (URISyntaxException e) { @@ -97,7 +76,7 @@ private URI getUri(String strUri){ } } - private WebSocketListener getWebsocketListener(EventListenerCallback callback){ + private WebSocketListener getWebsocketListener(EventListenerCallback callback) { WebSocketListener webSocketListener = new WebSocketListener() { @Override public void onMessageReceived(String message) { @@ -109,11 +88,10 @@ public void onMessageReceived(String message) { @Override public void onConnect(ServerHandshake handshake) { - Gdx.app.log("CONNECTION CREATED","HTTP_STATUS: " + handshake.getHttpStatusMessage()); + Gdx.app.log("CONNECTION CREATED", "HTTP_STATUS: " + handshake.getHttpStatusMessage()); WsEvent wsEvent = new WsEvent(); wsEvent.setData("CONNECTION_OPENED"); callback.onEvent(wsEvent); -// timer.start(); } @Override @@ -126,7 +104,7 @@ public void onClose(int code, String reason) { @Override public void onError(Exception ex) { - Gdx.app.error("CONNECTION ERROR","ERROR_MESSAGE: " + ex.getMessage()); + Gdx.app.error("CONNECTION ERROR", "ERROR_MESSAGE: " + ex.getMessage()); WsEvent wsEvent = new WsEvent(); wsEvent.setData("ERROR_OCCURRED"); callback.onEvent(wsEvent); @@ -136,11 +114,6 @@ public void onError(Exception ex) { return webSocketListener; } -// private String toJson(Object object){ -// Json json = new Json(); -// return json.toJson(object); -// } - private String toJson(Object object) { ObjectMapper mapper = new ObjectMapper(); try { diff --git a/android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java b/android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java index a25a6a3..8068653 100644 --- a/android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java +++ b/android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java @@ -41,26 +41,20 @@ private void processArray(JsonValue array) { private void processObject(JsonValue object) { String type = object.getString("class", null); - String classtype = object.getString("classtype", null); - if (type == null){ - type = classtype; - } if (type != null) { switch (type) { case "sessionKey": String meId = object.getString("id"); main.setMeId(meId); break; - case "mysession": - String sesid = object.getString("sesid"); - String password = object.getString("password"); - String playersAmount = object.getString("playersAmount"); - String sessionErrorMsg = object.getString("sessionErrorMsg"); + case "sessionRoom": + String sessionId = object.getString("id"); + String sessionPassword = object.getString("password"); + String sessionMsg = object.getString("msg"); main.gameSession.setConnected(true); - main.gameSession.setId(sesid); - main.gameSession.setPassword(password); - main.gameSession.setPlayersAmount(Integer.valueOf(playersAmount)); - main.gameSession.setSessionErrorMsg(sessionErrorMsg); + main.gameSession.setId(sessionId); + main.gameSession.setPassword(sessionPassword); + main.gameSession.setSessionMsg(sessionMsg); break; case "evict": String idToEvict = object.getString("id"); @@ -70,12 +64,7 @@ private void processObject(JsonValue object) { String id = object.getString("id"); float x = object.getFloat("x"); float y = object.getFloat("y"); - int score = object.getInt("score"); - float health = object.getFloat("health"); - float speed = object.getFloat("speed"); - float radius = object.getFloat("radius"); - boolean ghost = object.getBoolean("ghost"); - main.updatePlayerArray(id, x, y, score, health, speed, radius, ghost); + main.updatePlayerArray(id, x, y); break; default: throw new RuntimeException("Unknown message type " + type); diff --git a/android/src/com/aqwsxlostfly/packandgo/client/dto/InputStateImpl.java b/android/src/com/aqwsxlostfly/packandgo/client/dto/InputStateImpl.java index 94c01dc..3b4f089 100644 --- a/android/src/com/aqwsxlostfly/packandgo/client/dto/InputStateImpl.java +++ b/android/src/com/aqwsxlostfly/packandgo/client/dto/InputStateImpl.java @@ -2,7 +2,6 @@ import com.aqwsxlostfly.packandgo.InputState; -import com.aqwsxlostfly.packandgo.Tools.Point2D; public class InputStateImpl implements InputState { @@ -10,27 +9,11 @@ public class InputStateImpl implements InputState { private float x; private float y; private int score; - private float health; - private boolean ghost; - public float speed; - public float radius; public InputStateImpl() { } -// public InputStateImpl(Point2D position, float x, float y, int score, float health, boolean ghost, float speed, float radius) { -// this(); -// this.position = position; -// this.x = x; -// this.y = y; -// this.score = score; -// this.health = health; -// this.ghost = ghost; -// this.speed = speed; -// this.radius = radius; -// } - @Override public void setType(String type) { this.type = type; @@ -52,34 +35,9 @@ public int getScore() { return this.score; } - @Override - public void setHealth(float health) { - this.health = health; - } - - @Override - public float getHealth() { - return this.health; - } - - @Override - public void setRadius(float radius) { - this.radius = radius; - } - - @Override - public float getRadius() { - return this.radius; - } - - @Override - public float getSpeed() { - return speed; - } - @Override public void setX(float x) { - this.x =x; + this.x = x; } @Override @@ -89,7 +47,7 @@ public float getX() { @Override public void setY(float y) { - this.y =y; + this.y = y; } @Override @@ -97,18 +55,4 @@ public float getY() { return this.y; } - @Override - public void setSpeed(float speed) { - this.speed = speed; - } - - @Override - public boolean getGhost() { - return ghost; - } - - @Override - public void setGhost(boolean ghost) { - this.ghost = ghost; - } } diff --git a/android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketClient.java b/android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketClient.java index 78665e1..c09b87e 100644 --- a/android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketClient.java +++ b/android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketClient.java @@ -5,28 +5,7 @@ import java.net.URI; public class WebSocketClient extends org.java_websocket.client.WebSocketClient { - private WebSocketListener webSocketListener; - -// private static final int MAX_RECONNECT_ATTEMPTS = 3; - - // public void connectSocket() { -// isConnecting = true; -// int reconnectAttempts = 0; -// while (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) { -// try { -// main.webSocketClient.connect(); -// isConnecting = false; -// return; -// } catch (Exception e) { -// Gdx.app.error("ERROR SOCKET CONNECT", "Attempt " + (reconnectAttempts + 1) + " failed: " + e.getMessage()); -// reconnectAttempts++; -// } -// } -// -// isConnecting = false; -// -// Gdx.app.error("ERROR SOCKET CONNECT", "Maximum reconnection attempts reached"); -// } + private final WebSocketListener webSocketListener; public WebSocketClient(URI serverUri, WebSocketListener webSocketListener) { super(serverUri); diff --git a/android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketListener.java b/android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketListener.java index e29eb4a..9423486 100644 --- a/android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketListener.java +++ b/android/src/com/aqwsxlostfly/packandgo/client/ws/WebSocketListener.java @@ -4,8 +4,11 @@ public interface WebSocketListener { void onMessageReceived(String message); - void onConnect(ServerHandshake handshake ); - void onClose(int code, String reason ); + + void onConnect(ServerHandshake handshake); + + void onClose(int code, String reason); + void onError(Exception ex); } diff --git a/android/src/com/aqwsxlostfly/packandgo/client/ws/WsEvent.java b/android/src/com/aqwsxlostfly/packandgo/client/ws/WsEvent.java index 95eff70..65a88d2 100644 --- a/android/src/com/aqwsxlostfly/packandgo/client/ws/WsEvent.java +++ b/android/src/com/aqwsxlostfly/packandgo/client/ws/WsEvent.java @@ -3,9 +3,11 @@ public class WsEvent { private String data; - public WsEvent() {} - public final String getData(){ + public WsEvent() { + } + + public final String getData() { return this.data; } diff --git a/assets/env.properties b/assets/env.properties index 3a4c627..b002158 100644 --- a/assets/env.properties +++ b/assets/env.properties @@ -1 +1 @@ -WS_URI=ws://192.168.170.249:8867/ws +WS_URI=ws://192.168.1.55:8867/ws diff --git a/core/src/com/aqwsxlostfly/packandgo/GameSession.java b/core/src/com/aqwsxlostfly/packandgo/GameSession.java index 11e6d07..f14125b 100644 --- a/core/src/com/aqwsxlostfly/packandgo/GameSession.java +++ b/core/src/com/aqwsxlostfly/packandgo/GameSession.java @@ -4,9 +4,7 @@ public class GameSession { private String id; private String password; private Boolean isConnected; - - private String sessionErrorMsg; - private Integer playersAmount; + private String sessionMsg; public GameSession(Boolean isConnected) { this.isConnected = isConnected; @@ -36,19 +34,11 @@ public void setConnected(Boolean connected) { isConnected = connected; } - public String getSessionErrorMsg() { - return sessionErrorMsg; - } - - public void setSessionErrorMsg(String sessionErrorMsg) { - this.sessionErrorMsg = sessionErrorMsg; - } - - public Integer getPlayersAmount() { - return playersAmount; + public String getSessionMsg() { + return sessionMsg; } - public void setPlayersAmount(Integer playersAmount) { - this.playersAmount = playersAmount; + public void setSessionMsg(String sessionMsg) { + this.sessionMsg = sessionMsg; } } diff --git a/core/src/com/aqwsxlostfly/packandgo/InputState.java b/core/src/com/aqwsxlostfly/packandgo/InputState.java index 865de61..3c20dfa 100644 --- a/core/src/com/aqwsxlostfly/packandgo/InputState.java +++ b/core/src/com/aqwsxlostfly/packandgo/InputState.java @@ -9,18 +9,6 @@ public interface InputState { void setScore(int score); int getScore(); - void setHealth(float health); - float getHealth(); - - void setGhost(boolean isGhost); - boolean getGhost(); - - void setRadius(float radius); - float getRadius(); - - void setSpeed(float speed); - float getSpeed(); - void setX(float x); float getX(); diff --git a/core/src/com/aqwsxlostfly/packandgo/Main.java b/core/src/com/aqwsxlostfly/packandgo/Main.java index 03397a1..58fb151 100644 --- a/core/src/com/aqwsxlostfly/packandgo/Main.java +++ b/core/src/com/aqwsxlostfly/packandgo/Main.java @@ -3,14 +3,12 @@ import com.aqwsxlostfly.packandgo.Heroes.Player; import com.aqwsxlostfly.packandgo.Screens.HomeSc; -import com.aqwsxlostfly.packandgo.Screens.WaitingSc; import com.aqwsxlostfly.packandgo.Tools.Point2D; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.utils.ObjectMap; import java.util.Objects; @@ -117,7 +115,7 @@ public void setSocketState(boolean socketState) { } - public void updatePlayerArray(String id, float x_, float y_, int score, float health, float speed, float radius, boolean ghost) { + public void updatePlayerArray(String id, float x_, float y_) { Player player = Main.players.get(id); if (player == null) { Main.players.put(id, new Player(Main.capibara, new Point2D(Main.screenWidth / 2, Main.screenHeight / 2), 10F, (float) (Main.screenHeight / 5), 5)); diff --git a/core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java b/core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java index 27f365c..66852d7 100644 --- a/core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java +++ b/core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java @@ -3,7 +3,7 @@ import com.aqwsxlostfly.packandgo.Main; import com.aqwsxlostfly.packandgo.SessionState; -import com.aqwsxlostfly.packandgo.SessionStateImpl; +import com.aqwsxlostfly.packandgo.SessionStateToSend; import com.aqwsxlostfly.packandgo.Tools.FlyingObject; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; @@ -11,7 +11,6 @@ import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.TextureRegion; -import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; @@ -77,7 +76,6 @@ public HomeSc(Main main) { flyingObjects = new ArrayList(); - // Создаем объекты for (int i = 0; i < 12; i++) { Texture texture = getRandomTexture(); FlyingObject object = new FlyingObject(new TextureRegion(texture)); @@ -257,11 +255,11 @@ public void clicked(InputEvent event, float x, float y) { private void connectToServer(String password, String roomId, boolean isCreating) { if (isCreating) { Gdx.app.log("CREATE ROOM", "Creating a room with ID: " + roomId + " and password: " + password); - SessionState sessionState = new SessionStateImpl("create_room", roomId, password); + SessionState sessionState = new SessionStateToSend("createRoom", roomId, password); main.messageSender.sendMessage(sessionState); } else { Gdx.app.log("JOIN ROOM", "Joining a room with ID: " + roomId + " and password: " + password); - SessionState sessionState = new SessionStateImpl("join_room", roomId, password); + SessionState sessionState = new SessionStateToSend("joinRoom", roomId, password); main.messageSender.sendMessage(sessionState); } } @@ -283,7 +281,7 @@ public void render(float delta) { public void showState(){ Gdx.app.log("SESSION_STATE", main.gameSession.getId() + " " + main.gameSession.getPassword() + " " + - main.gameSession.getSessionErrorMsg() +" " + main.gameSession.getConnected() + " " + main.gameSession.getPlayersAmount()); + main.gameSession.getSessionMsg() +" " + main.gameSession.getConnected() ); } @Override diff --git a/core/src/com/aqwsxlostfly/packandgo/SessionStateToSend.java b/core/src/com/aqwsxlostfly/packandgo/SessionStateToSend.java index 455194b..82f8775 100644 --- a/core/src/com/aqwsxlostfly/packandgo/SessionStateToSend.java +++ b/core/src/com/aqwsxlostfly/packandgo/SessionStateToSend.java @@ -1,12 +1,12 @@ package com.aqwsxlostfly.packandgo; -public class SessionStateImpl implements SessionState{ +public class SessionStateToSend implements SessionState{ private String type; private String Id; private String password; - public SessionStateImpl(String type, String id, String password) { + public SessionStateToSend(String type, String id, String password) { this.type = type; Id = id; this.password = password; diff --git a/core/src/com/aqwsxlostfly/packandgo/TouchProcessor.java b/core/src/com/aqwsxlostfly/packandgo/TouchProcessor.java index 45ca368..7bce654 100644 --- a/core/src/com/aqwsxlostfly/packandgo/TouchProcessor.java +++ b/core/src/com/aqwsxlostfly/packandgo/TouchProcessor.java @@ -67,14 +67,10 @@ public void multitouch(float x, float y, boolean isDownTouch, int pointer) { } public InputState updateAndGetInputState(Player player) { - inputState.setType("state"); + inputState.setType("playerState"); inputState.setX(player.position.getX()); inputState.setY(player.position.getY()); - inputState.setGhost(player.isGhost()); - inputState.setHealth(player.getHealth()); - inputState.setRadius(player.radius); inputState.setScore(player.getScore()); - inputState.setSpeed(player.speed); return inputState; }