-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
176 lines (152 loc) · 5.45 KB
/
socket.js
File metadata and controls
176 lines (152 loc) · 5.45 KB
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
167
168
169
170
171
172
173
174
175
176
// Models
var mongoose = require('mongoose'),
User = require('./models/user'),
Game = require('./models/game'),
PlayingCards=require('./public/javascript/playingcards-server');
module.exports = function(io, games){
// Array with some colors in random order
//testing room
// var test = new Game({name: 'test', gameType: 'mWar'});
// test.save();
// games[test._id] = test;
Game.find({active: true}, function(err, dbGames){
dbGames.forEach(function(game){
games[game._id]=game;
})
var history = [];
for(var game_id in games){
if(game_id !='undefined'){
var colors = [ 'red', 'green', 'blue', 'magenta', 'purple', 'plum', 'orange' ];
colors.sort(function(a,b) { return Math.random() > 0.5; } );
games[game_id].chat = {history: history, userColors: colors};
games[game_id].save();
}
}
//console.log(games);
})
console.log(games);
/**
* Helper function for escaping input strings
*/
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"');
}
io.sockets.on('connection', function(socket){
//doesn't work ?
if(typeof socket.handshake.sessionID === 'undefined' || typeof socket.handshake.session === 'undefined'){
socket.disconnect();
}
//initialize some stuff
var userColor = false;
// get user from session
try{
console.log('A socket with sessionID ' + socket.handshake.sessionID + ' connected!');
// allows for socket to do io.sockets.in(req.sessionID).emit();
socket.join(socket.handshake.sessionID);
User.findById(socket.handshake.session.passport.user, function(err, user){
if(err){
console.log('No user found. what the hell, ' + err);
}
console.log(user.username); //works
socket.user = user;
});
}
catch(err){
console.log('No session, ' +err);
}
// if (history.length > 0) {
// socket.emit('history',history);
// }
socket.on('newgame', function(options){
console.log('starting new game');
games[games.length]= new Game(options);
})
socket.on('joinGame', function(game_id){
Game.findOne({_id: game_id}, function(err, game){
if(err || !game){ return err}
else{
//db
console.log(game);
socket.room = games[game._id];
socket.room.users.push(socket.user);
socket.room.usernames.push(socket.user.username);
//ram
socket.join(socket.room.name);
io.sockets.in(socket.room.name).emit('updateUsers', socket.room.usernames);
console.log(socket.room.chat);
socket.userColor = socket.room.chat.userColors.shift();
//events
socket.emit('color', socket.userColor)
socket.emit('gameInfo', socket.user.username);
io.sockets.in(socket.room.name).emit('updatechat', {time: (new Date()).getTime(),
text:'You have connected to ' + socket.room.name, author: 'Server', color: 'black'});
}
})
})
// when the client emits 'adduser', this listens and executes
function setname(name){
if(usernames[name]){
console.log('already exists')
k=1;
uname=name+k
while(usernames[uname]){
uname=name + ++k;
}
}
else{
uname = name;
}
return uname;
}
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
// we want to keep history of all sent messages
var obj = {
time: (new Date()).getTime(),
text: htmlEntities(data),
author: socket.user.username,
color: socket.userColor
};
socket.room.chat.history.push(obj);
socket.room.chat.history = socket.room.chat.history.slice(-100);
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.in(socket.room.name).emit('updatechat', obj);
});
socket.on('switchRoom', function(newroom){
// leave the current room (stored in session)
socket.leave(socket.room);
// join new room, received as function parameter
socket.join(newroom);
socket.emit('updatechat', 'SERVER', 'you have connected to '+ newroom);
// sent message to OLD room
socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username+' has left this room');
// update socket session room title
socket.room = newroom;
socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');
socket.emit('updaterooms', rooms, newroom);
});
socket.on('startGame', function(game, deck1, deck2, player1, player2){
console.log('game started')
if(game==='MWar')
{
io.sockets.in(socket.room.name).emit('startgame', deck1, deck2, player1, player2)
console.log(io.sockets.clients(socket.room))
}
})
// when the user disconnects.. perform this
socket.on('disconnect', function(){
if(typeof socket.room != 'undefined'){
// echo globally that this client has left
// socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username + ' has disconnected');
// socket.room.users.splice(socket.room.users.indexOf(socket.user),1);
// socket.room.usernames.splice(socket.room.usernames.indexOf(socket.username),1);
// // update list of users in chat, client-side
// io.sockets.emit('updateusers', socket.room.usernames);
// socket.broadcast.to(socket.room.name).emit('updateUsers', socket.room.usernames);
// socket.leave(socket.room.name);
// colors.push(userColor);
}
});
});
}