summaryrefslogtreecommitdiff
path: root/server/src/kz/ilotterytea/maxon/PlayerConnection.java
blob: 79b46d083bba1ac72e1ad9a443cb3a7ec886f0ef (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
package kz.ilotterytea.maxon;

import kz.ilotterytea.maxon.shared.Identity;
import org.java_websocket.WebSocket;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.sql.Timestamp;

public class PlayerConnection {
    private static int TOTAL_CONNECTION_IDS = 0;

    private final int id;
    private final WebSocket connection;
    private Identity identity;

    private final Timestamp connectedTimestamp;

    public PlayerConnection(WebSocket connection) {
        this.connection = connection;
        this.connectedTimestamp = new Timestamp(System.currentTimeMillis());

        this.id = TOTAL_CONNECTION_IDS;
        TOTAL_CONNECTION_IDS++;
    }

    public int getId() {
        return id;
    }

    public WebSocket getConnection() {
        return connection;
    }

    public Identity getIdentity() {
        return identity;
    }

    public void setIdentity(Identity identity) {
        this.identity = identity;
    }

    public Timestamp getConnectedTimestamp() {
        return connectedTimestamp;
    }

    public void send(Object object) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
            objectOutputStream.writeObject(object);
        } catch (IOException ignored) {

        }
        
        this.connection.send(byteArrayOutputStream.toByteArray());
    }

    @Override
    public String toString() {
        return "PlayerConnection{" +
                "id=" + id +
                '}';
    }
}