summaryrefslogtreecommitdiff
path: root/server/src/main/java/kz/ilotterytea/frogartha/server/ServerHandlers.java
blob: 1cc0e572a1a2ae61dee08d6236dfea32edfc8626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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.actions.ChangedDirectionAction;
import kz.ilotterytea.frogartha.domain.actions.PlayerJumpAction;
import kz.ilotterytea.frogartha.domain.events.ChangedDirectionEvent;
import kz.ilotterytea.frogartha.domain.events.PlayerJumpEvent;
import kz.ilotterytea.frogartha.domain.server.Acknowledge;
import kz.ilotterytea.frogartha.exceptions.PlayerKickException;
import kz.ilotterytea.frogartha.utils.Logger;

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) {
        if (server.getPlayers()
            .stream()
            .filter((x) -> x.getIdentity() != null)
            .anyMatch((x) -> x.getIdentity().equals(identity) && x.getId() != player.getId())) {
            server.kickConnection(player, PlayerKickException.loggedIn());
            return;
        }

        player.setIdentity(identity);
        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());
    }

    public static void handleChangedDirectionAction(PlayerConnection player, ChangedDirectionAction action) {
        PlayerState state = player.getState();
        Vector3 direction = action.getDirection();

        state.setDirection(direction.x, state.getPosition().y, direction.z);

        player.send(new ChangedDirectionEvent(player.getId(), state.getDirection()));
    }
}