-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
141 lines (108 loc) · 3.34 KB
/
index.js
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
/* This file is part of Litaxx.
Litaxx is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Litaxx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Litaxx. If not, see <https://www.gnu.org/licenses/>.
*/
// External requirements
const express = require('express')
const path = require('path')
const http = require('http')
const socketio = require('socket.io')
// Internal requirements
const createRoom = require('./server/room')
const app = express()
app.set('views', 'client')
app.set('view engine', 'ejs')
app.use(express.static('client'));
const server = http.createServer(app)
const port = process.env.PORT || 3000
let users = new Map();
let rooms = new Map();
exports.users = users
exports.rooms = rooms
const io = socketio(server)
app.get('/', (req, res) => {
res.render('index', {"rooms": rooms});
})
app.get('/game', (req, res) => {
const gameId = req.query.gameId;
if (!gameId) {
// If the gameId was not sent, create a new room and
// redirect the user to it.
const room = createRoom(io);
rooms.set(room.code, room);
res.redirect('/game?gameId=' + room.code);
} else if (!(/^[A-Z]{4}$/.test(gameId))) {
res.send("Room code is not valid")
} else if (!rooms.get(gameId)) {
res.send("Room doesn't exist");
} else {
res.render('mode-online');
}
})
app.get('/computer', (req, res) => {
res.render('mode-computer');
})
io.on('connection', socket => {
socket.on('join_lobby', _ => {
socket.join("lobby");
})
socket.on('join_game', code => {
let room = rooms.get(code)
if (!room) {
socket.emit("room_doesnt_exist", 0);
} else if (users.get(socket.id) != code) {
users.set(socket.id, room.code);
room.join(socket);
}
})
socket.on('move', move => {
const room = getRoom(socket.id);
if (room) {
room.makeMove(move, socket.id);
}
})
// FENs are a way to compress the board's state into a string
// https://www.chessprogramming.org/Forsyth-Edwards_Notation
socket.on('fen', fen => {
const room = getRoom(socket.id);
if (room) {
room.setFen(fen);
}
})
socket.on('offer_draw', _ => {
const room = getRoom(socket.id);
if (room) {
room.offerDraw(socket.id);
}
})
socket.on('resign', _ => {
const room = getRoom(socket.id);
if (room) {
room.resign(socket.id);
}
})
socket.on('disconnect', _ => {
const room = getRoom(socket.id);
if (socket.id in users) {
users.delete(socket.id);
}
if (room) {
room.leave(socket);
}
})
})
server.listen(port, () => {
console.log(`Server running on port: ${port}`)
})
function getRoom(id) {
const code = users.get(id);
return rooms.get(code);
}