-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
166 lines (136 loc) · 5.43 KB
/
server.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const cors = require('cors');
const moment = require('moment');
app.use(cors())
let roomId = 1156789;
const roomsDetails = {}
// const colors = ['red', 'green', 'blue', 'yellow', 'pink', 'orange', 'cyan', 'lightgreen'];
io.on('connection', (socket) => {
console.log('Connected');
socket.on('init',(data, cb)=>{
cb('connected to server');
})
socket.on('message',(data)=>{
socket.broadcast.emit('data',{data:data});
})
socket.on('subscribe', function({roomId:room, name}, cb) {
try{
if(!roomsDetails[room]) {
cb({error:true, message:"Room not created"})
return;
}
console.log('joining room', room);
socket.join(room);
var count = io.sockets.adapter.rooms[room].length;
var error = false;
var userIndex;
// if(!roomsDetails[room]) roomsDetails[room]={
// users: [],
// history: [],
// lastCommunicated:{}
// }
if(roomsDetails[room].users.includes(name)){
userIndex = roomsDetails[room].users.indexOf(name)+1; // Existing user
} else{
userIndex = count;
roomsDetails[room].users.push(name); // Updating Connected user per room
}
if(count !== roomsDetails[room].users.length){
error = true;
}
const roomDetails = {
error:error,
count: count,
users: roomsDetails[room].users,
userIndex: userIndex,
historySequence : roomsDetails[room].history.length
};
console.log('room.length', count, name, roomsDetails)
cb(roomDetails); // Respond back total users in the group
socket.broadcast.to(room).emit('newUserJoined', roomDetails);
} catch(e){
console.log('error: subscribe', e)
}
})
socket.on('unSubscribe', function({roomId, playersName}) {
socket.leave(roomId);
socket.broadcast.to(roomId).emit('someOneLeft', playersName);
})
// socket.on('keepAlive', function({user, room}) {
// console.log( user + ' is alive in room: '+room);
// if(roomsDetails[room])
// roomsDetails[room].lastCommunicated[user] = moment().valueOf();
// })
socket.on('start', function(data) {
socket.broadcast.to(data.room).emit('start', data);
roomsDetails[data.room].gameStarted = true;
});
socket.on('playerClickedOneCell', function(data, ack) {
try{
console.log('sending message');
roomsDetails[data.room].history.push(data.message);
roomsDetails[data.room].lastCommunicated = moment().valueOf();
ack({status:'success', message:'Received in server'})
data.historySequence = roomsDetails[data.room].history.length;
socket.broadcast.to(data.room).emit('playerClickedOneCell', data);
} catch(e){
console.log('error: playerClickedOneCell', e)
}
});
socket.on('getHistoryCount', function({roomId}, response){
response(roomsDetails[roomId]?roomsDetails[roomId].history.length:0);
})
socket.on('getHistory', function({roomId}, response){
response(roomsDetails[roomId]?roomsDetails[roomId].history:[]);
})
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('nextPlayer',function(data){
socket.broadcast.to(data.roomId).emit('nextPlayer', data.playerId);
})
socket.on('gameOver', function({roomId, winner}, ack){
socket.broadcast.to(roomId).emit('gameOver', {winner});
ack("received in server");
delete roomsDetails[roomId];
console.log('gameOver');
socket.leave(roomId);
});
socket.on('onNewChat',function(payload){
socket.broadcast.to(payload.roomId).emit('onNewChat', payload.data);
})
});
const port = process.env.PORT || 4999;
server.listen(port, ()=>{
console.log("App is listening in port: "+port);
})
app.use(express.static('build'))
app.get('/create-room-submit',(req,res)=>{
if(!roomsDetails[roomId]) roomsDetails[roomId+'-'+req.query.players]={
users: [],
history: [],
lastCommunicated:moment().valueOf(),
owner: req.query.name,
}
res.send({roomId: roomId+'-'+req.query.players});
roomId++;
})
app.get('/is-room-exist',(req,res)=>{
const roomExist = !!roomsDetails[req.query.roomId];
const userExist = roomExist ? roomsDetails[req.query.roomId].owner===req.query.name ||roomsDetails[req.query.roomId].users.includes(req.query.name) : false;
let message = "success";
if(userExist)
message = req.query.name+' is already joined the group, use another name';
else if (!roomExist)
message = "Room ID: "+req.query.roomId+' does not exist';
if (roomsDetails[req.query.roomId] && roomsDetails[req.query.roomId].gameStarted){
message="Game already started. Try again"
}
res.send({status: roomExist && !userExist && !roomsDetails[req.query.roomId].gameStarted , message });
})
app.get('*',(req,res)=>{
res.sendFile(__dirname+'/build/index.html');
})