summaryrefslogtreecommitdiff
path: root/webcards/scripts/client.js
blob: ea62e265a94ced0822d10a91b2f88ba27d703a4c (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Client acts as the message hub for the whole game.
// WebSocket messages come into Client and Client redirects them to the lobby or table based on the state of the game.
// Client also performs the handshake for first starting the connection and messages everyone if the connection errors or closes.
function Client(serveraddr, game) {
    this.state = "handshake";
    
    this.soc = new SockWorker(serveraddr, "1", this.cb.bind(this));

    this.lob = new Lobby(document.getElementsByClassName("lobby")[0], this.soc);
    this.tab = new Table(document.getElementsByClassName("table")[0], this.soc);

    this.game = game;
}

Client.prototype = {
    // Initialize the connection
    init: function() {
        this.soc.init();
    },

    // Entry point for a message from the server.
    // If it's a close message, we close the game if it is open and change the lobby to reflect the error/close.
    cb: function(m) {
        console.log(m);

        if(m.type == "error" || m.type == "closed") {
            var t = m.type;
            t = t[0].toUpperCase() + t.slice(1)
            this.lob.setState(t, "#D00", this.soc.server);
            this.tab.handleClose();
            return;
        }

        switch(this.state) {
            case "handshake":
                this.handshake(m);
                break;
            case "lobby":
                this.lobby(m);
                break;
            case "game":
                break;
        }
    },

    // Called when negotiating with the server for the first time and we are determining versions
    handshake: function(m) {
        switch (m.type) {
            case "verr":
                this.soc.close();
                alert(`Error connecting to server: version of client (${this.version}) not accepted.`);
                console.error("Error connecting to server: version of client (${this.version}) not accepted.");
                console.error(m.data);
                return;
            case "lobby":
                this.state = "lobby";
                this.soc.send("ready", "");
                return;
        }
    },

    // Lobby switch, called when in the lobby and a message arrives from the server
    lobby: function (m) {
        switch (m.type) {
            case "plist":
                this.lob.packList(m.data);
                break;
            case "glist":
                this.lob.gameList(m.data, this.game);
                this.game = null;
                break;
            case "players":
                this.lob.players(m.data);
                break;
            case "gdel":
                this.lob.removeGame(m.data);
                break;
            case "gadd":
                this.lob.addGame(m.data);
                break;
            case "pdel":
                this.lob.removePlayer(m.data);
                break;
            case "padd":
                this.lob.addPlayer(m.data);
                break;
            case "pmove":
                this.lob.movePlayer(m.data);
                break;
        }
    },

    // Game switch, called when in game and a message arrives from the server
    game: function (m) {
        switch (m.type) {
            
        }
    },

    // Reset the lobby and table, then attempt to reopen the connection to the server.
    reset: function() {
        this.state = "handshake";

        this.lob.reset();
        this.tab.reset();

        this.soc.init();
    }
};