diff options
Diffstat (limited to 'server')
3 files changed, 53 insertions, 2 deletions
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 e25dffd..c141bd9 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/FrogarthaServer.java @@ -2,6 +2,7 @@ package kz.ilotterytea.frogartha.server; import com.github.czyzby.websocket.serialization.impl.ManualSerializer; import kz.ilotterytea.frogartha.domain.Identity; +import kz.ilotterytea.frogartha.domain.client.PlayerJumpAction; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; import kz.ilotterytea.frogartha.utils.Logger; import kz.ilotterytea.frogartha.utils.SerializerUtils; @@ -76,8 +77,13 @@ public class FrogarthaServer extends WebSocketServer { throw PlayerKickException.internalServerError(); } - if (obj instanceof Identity) ServerHandlers.handleIdentity(player, (Identity) obj); - else throw PlayerKickException.internalServerError(); + if (obj instanceof Identity) { + ServerHandlers.handleIdentity(player, (Identity) obj); + } else if (obj instanceof PlayerJumpAction) { + ServerHandlers.handlePlayerJumpAction(player, (PlayerJumpAction) obj); + } else { + throw PlayerKickException.internalServerError(); + } } catch (PlayerKickException e) { kickConnection(player, e); } catch (Exception e) { 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 d9326a6..8744e58 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.Identity; +import kz.ilotterytea.frogartha.domain.PlayerState; import org.java_websocket.WebSocket; import java.sql.Timestamp; @@ -11,6 +12,7 @@ public class PlayerConnection { private final int id; private final WebSocket connection; private Identity identity; + private PlayerState state; private final Timestamp connectedTimestamp; @@ -21,6 +23,7 @@ public class PlayerConnection { this.connectedTimestamp = new Timestamp(System.currentTimeMillis()); this.identity = null; + this.state = null; } public void send(Object object) { @@ -50,6 +53,11 @@ public class PlayerConnection { public void setIdentity(Identity identity) { this.identity = identity; + if (state == null) state = new PlayerState(); + } + + public PlayerState getState() { + return state; } @Override 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 b50392f..44162ae 100644 --- a/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java +++ b/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java @@ -1,7 +1,12 @@ package kz.ilotterytea.frogartha.server; +import com.badlogic.gdx.math.Vector3; +import kz.ilotterytea.frogartha.FrogarthaConstants; import kz.ilotterytea.frogartha.domain.Identity; +import kz.ilotterytea.frogartha.domain.PlayerState; +import kz.ilotterytea.frogartha.domain.client.PlayerJumpAction; import kz.ilotterytea.frogartha.domain.server.Acknowledge; +import kz.ilotterytea.frogartha.events.PlayerJumpEvent; import kz.ilotterytea.frogartha.exceptions.PlayerKickException; import kz.ilotterytea.frogartha.utils.Logger; @@ -22,4 +27,36 @@ public class ServerHandlers { player.send(new Acknowledge(Acknowledge.AcknowledgeCode.IDENTIFIED)); log.log("Successfully identified {} for {}", identity, player); } + + public static void handlePlayerJumpAction(PlayerConnection player, PlayerJumpAction action) { + PlayerState state = player.getState(); + + float nowTimestamp = System.currentTimeMillis() / 1000f; + + if (state.getNextJumpTimestamp() > nowTimestamp) { + throw PlayerKickException.jumpSpam(); + } + + state.setNextJumpTimestamp(nowTimestamp + action.getJumpStrength()); + + // calculating start and end positions + Vector3 position = state.getPosition(); + Vector3 startPosition = position.cpy(); + + float jumpDistance = action.getJumpStrength() * FrogarthaConstants.Player.JUMP_SPEED; + Vector3 d = new Vector3(state.getDirection()).sub(startPosition).nor(); + + Vector3 endPosition = new Vector3( + position.x + d.x * jumpDistance, + position.y, + position.z + d.z * jumpDistance + ); + + state.setPosition(endPosition.x, endPosition.y, endPosition.z); + + player.send(new PlayerJumpEvent( + player.getId(), startPosition, endPosition, action.getJumpStrength() + )); + log.log("{} jumped from {} to {} with strength {}", player, startPosition, endPosition, action.getJumpStrength()); + } } |
