summaryrefslogtreecommitdiff
path: root/tictactoe/server/index.js
blob: 760a932451f710b2c80573bd48943aecf78c813f (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const WebSocket = require('ws');
const server = new WebSocket.Server({
    port: 25432
});

let games = {};

server.on('connection', function(socket, req) {
    socket.address = req.socket.remoteAddress;
    socket.room = null;
    socket.host = null;

    socket.on('close', () => {
        if (socket.room) {
            try {
                games[socket.room][0].socket.send(JSON.stringify({error:"PEER_LEFT"}));
                games[socket.room][1].socket.send(JSON.stringify({error:"PEER_LEFT"}));
            } catch (e) {}
            delete games[socket.room];
        }
    })

    socket.on('message', function(msg) {
        let data;
        try {
            data = JSON.parse(msg);
        } catch (e) {
            socket.send(JSON.stringify({error:"INVALID_DATA"}));
            return;
        }

        try {
            if (data.action) {
                switch (data.action) {
                    case "ready":
                        games[socket.room][1].socket.send(JSON.stringify({
                            notification: {
                                name: "GAME_READY",
                                message: {
                                    name: games[socket.room][0].name
                                }
                            }
                        }));
                        break;

                    case "reset":
                        games[socket.room][0].socket.send(JSON.stringify({
                            notification: {
                                name: "GAME_RESET",
                                message: null
                            }
                        }));
                        games[socket.room][0].socket.send(JSON.stringify({
                            notification: {
                                name: "YOUR_TURN",
                                message: true
                            }
                        }));

                        games[socket.room][1].socket.send(JSON.stringify({
                            notification: {
                                name: "GAME_RESET",
                                message: null
                            }
                        }));
                        games[socket.room][1].socket.send(JSON.stringify({
                            notification: {
                                name: "YOUR_TURN",
                                message: false
                            }
                        }));

                        break;

                    case "place":
                        games[socket.room][socket.host ? 1 : 0].socket.send(JSON.stringify({
                            notification: {
                                name: "CELL_PLACE",
                                message: {
                                    name: data.position,
                                    host: !socket.host
                                }
                            }
                        }));
                        break;

                    case "init":
                        if (!data.room || !data.player) {
                            socket.send(JSON.stringify({error:"MISSING_OPERAND"}));
                            return;
                        }

                        if (!games[data.room]) {
                            games[data.room] = [
                                {
                                    name: data.player,
                                    address: socket.address,
                                    socket: socket
                                },
                                null
                            ]
                            socket.room = data.room;
                            socket.host = true;

                            socket.send(JSON.stringify({result:{name:"CONNECTION_ESTABLISHED",player:0}}));
                        } else if (!games[data.room][1] || games[data.room][1] === null) {
                            data.player = data.player.replaceAll("<", "-").replaceAll(">", "-").replaceAll("&", "-")

                            games[data.room][1] = {
                                name: data.player,
                                address: socket.address,
                                socket: socket
                            }
                            socket.room = data.room;
                            socket.host = false;

                            games[data.room][0].socket.send(JSON.stringify({notification:{name:"PEER_JOIN",message:{name:data.player,address:socket.address}}}));
                            socket.send(JSON.stringify({result:{player:1}}));

                            break;
                        } else {
                            socket.send(JSON.stringify({error:"ROOM_FULL"}));
                            break;
                        }

                        console.log(games);

                        break;
                    default:
                        socket.send(JSON.stringify({error:"INVALID_ACTION"}));
                        break;
                }
            } else {
                socket.send(JSON.stringify({error:"MISSING_OPERAND"}));
            }
        } catch (e) {
            console.log(e);
            socket.send(JSON.stringify({error:"INTERNAL_ERROR"}));
        }
    });
});