-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (38 loc) · 1.19 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
const express = require('express');
const socketIO = require('socket.io');
const PORT = process.env.PORT || 8080;
const app = express();
app.use(express.static('www/'));
let server = app.listen(PORT, () => console.log(`Listening on port ${PORT}`));
const Room = require('./room');
let rooms = [
new Room(0, 'Mistnost jedna'),
new Room(1, 'Mistnost dva'),
new Room(2, 'Mistnost tri'),
new Room(3, 'Mistnost ctyri')
];
const io = socketIO(server);
setInterval(() => console.log(rooms), 1000);
io.on('connection', (socket) => {
console.log('We got a new boi');
socket.emit('roomlist', rooms);
let room;
socket.on('joinreq', data => {
console.log('joinreq', data);
socket.join(data, () => {
room = rooms[data];
room.newBoi(socket.id);
io.emit('roomlist', rooms);
socket.emit('joinres', data);
io.to(data).emit('room-status', room.players);
});
});
socket.on('disconnect', () => {
console.log('Bye bye');
if (room) {
room.boiLeft(socket.id);
io.to(room.id).emit('room-status', room.players);
room = null;
}
});
});