-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
152 lines (131 loc) · 4.46 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
142
143
144
145
146
147
148
149
150
151
152
// Setup basic express server
var express = require('express');
var app = express();
var path = require('path');
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var bodyParser = require('body-parser');
var ROOMS = {}, lastRoomId = 1000;
server.listen(port, ip, function () {
console.log('Server listening at port %d', port);
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
// Routing
var roomRouter = express.Router();
roomRouter.route('/rooms')
.get(function(req, res){
res.json(ROOMS);
})
.post(function(req, res){
var room = req.body;
room.id = JSON.stringify(lastRoomId++);
room.users = [];
ROOMS[room.id] = room;
res.json({
name: room.name,
admin:room.admin,
id:room.id
});
})
roomRouter.route('/rooms/:roomId/join')
.get(function(req, res){
var roomId = req.params.roomId;
if(ROOMS[roomId]){
var room = ROOMS[roomId];
res.json({
name: room.name,
admin:room.admin,
id:room.id
});
}else{
res.status(404).json({msg:'Room not found'})
}
})
app.use(express.static(path.join(__dirname, 'build')));
app.post('/room', function (req, res) {
res.send('created');
});
app.use('/api', roomRouter);
// Chatroom
var numUsers = 0;
io.on('connection', function (socket) {
var addedUser = false;
socket.on('ADD_USER', function(user){
socket.userId = user.id;
socket.roomId = user.roomId;
delete user.roomId;
ROOMS[socket.roomId].users.push(user);
socket.join(socket.roomId);
//socket.broadcast.to(socket.roomId).emit('USER_JOINED', ROOMS[socket.roomId]);
//io.to(socket.roomId).emit('message', 'USER_JOINED', ROOMS[socket.roomId].users);
io.sockets.to(socket.roomId).emit('USER_JOINED', ROOMS[socket.roomId].users);
})
socket.on('SET_STORY',function(data){
if(data.room && ROOMS[data.room]){
var room = ROOMS[data.room];
if(room.admin === data.admin){
io.sockets.to(data.room).emit('STORY_SET', data.story);
}else{
socket.to(data.room).emit('ERROR', 'Acess denied.');
}
}else{
socket.to(data.room).emit('ERROR', 'Room does not exist.');
}
})
socket.on('SELECT_CARD',function(data){
if(data.room && ROOMS[data.room]){
io.sockets.to(data.room).emit('CARD_SELECED', {user:data.user, card:data.card});
}else{
socket.to(data.room).emit('ERROR', 'Room does not exist.');
}
})
socket.on('REVEAL_CARDS',function(data){
if(data.roomId && ROOMS[data.roomId]){
io.sockets.to(data.roomId).emit('CARDS_REVEALED', {});
}else{
socket.to(data.room).emit('ERROR', 'Room does not exist.');
}
})
socket.on('disconnect', function () {
var room = ROOMS[socket.roomId], index;
if(room){
//check if its admin, delete room
if(room.admin === socket.userId){
delete ROOMS[socket.roomId];
io.sockets.to(socket.roomId).emit('ADMIN_LEFT', {
id: socket.userId,
roomId: socket.roomId
});
}else{
for(var i=0; i< room.users.length; i++){
if(room.users[i].id === socket.userId){
index = i;
break;
}
}
if(index){
room.users = room.users.splice(0, index);
}
io.sockets.to(socket.roomId).emit('USER_LEFT', {
id: socket.userId,
roomId: socket.roomId
});
}
}else{
socket.to(socket.roomId).emit('ERROR', 'Room does not exist.');
}
});
});
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}