-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·234 lines (185 loc) · 5.81 KB
/
app.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var PUBLIC_DIRECTORY_PATH = 'public/';
var DEFAULT_FILE = 'index.html';
var http = require('http'),
io = require('./lib/socket.io'),
url = require('url'),
path = require('path'),
sys = require('sys'),
fs = require('fs');
require('./lib/underscore');
var getWordsList = require('./words');
server = http.createServer(function(req, res){
var uri = url.parse(req.url).pathname;
uri = uri + (uri.substr(-1) === '/' ? DEFAULT_FILE:'');
var filename = path.join(__dirname,PUBLIC_DIRECTORY_PATH, uri);
path.exists(filename, function(exists){
if(!exists) {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('File not found \n');
sys.log('404: ' + filename);
res.end();
return;
}
fs.readFile(filename, function(err, file) {
if(err){
res.writeHead(500, {'Content-Type': 'text/plain'});
res.write(err + '\n');
sys.log('500: ' + filename + err);
res.end();
} else {
res.writeHead(200, {'Content-Type': 'text/' + (uri.substr(-3) === '.js' ? 'javascript' : 'html')});
res.write(file);
sys.log('200: ' + filename);
res.end();
}
});
});
});
server.listen(80);
var io = io.listen(server);
var users = [];
var gameStatus = 'off'; //on,off,ready
var points = [];
var words = {};
words.startups = new getWordsList.getStartUps();
words.movies = new getWordsList.getMovies();
words.animals = new getWordsList.getAnimals();
words.verbs = new getWordsList.getVerbs();
categories = Object.keys(words);
word = words[categories[Math.floor(Math.random()*categories.length)]];
var update = setInterval(function(){
if(gameStatus == 'ready'){
_.each(users, function(user){
if(user.artist) {
sys.log('Send new theme');
categories = Object.keys(words);
categorie = words[categories[Math.floor(Math.random()*categories.length)]];
word = categorie.data[Math.floor(Math.random()*categorie.data.length)];
word = {'type':'receiveWord','data':{
'name':word.name,
'picture':(word.picture)?word.picture:'',
'description':(word.description)?word.description:'',
'theme':categorie.description,
'time':120
}};
io.clients[user.id].send(JSON.stringify(word));
io.clients[user.id].broadcast(JSON.stringify({'type':'receiveWord','data':{
'theme':categorie.description,
'time':120
}}));
}
});
gameStatus = 'on';
} else if(gameStatus == 'on') {
word.data.time--;
if(word.data.time <= 0) {
points = [];
setNewArtist();
}
}
},1000);
var checkIfUserIsArtist = function(userId){
return _.any(users,function(user){
if(user.id == userId && user.artist){
return true;
} else {
return false;
}
},this);
}
var setNewArtist = function(fbId){
_.each(users, function(user){
user.artist = false;
});
if(users.length == 0) {
gameStatus = 'off';
} else if(users.length == 1) {
users[0].artist = true;
io.broadcast(JSON.stringify({'type':'newArtist','data':users[0].fbId}));
gameStatus = 'off';
} else {
if(!fbId){
var user = users[Math.floor(Math.random()*users.length)];
} else {
var user = _.detect(users, function(user){return user.fbId == fbId},this);
}
user.artist = true;
io.broadcast(JSON.stringify({'type':'newArtist','data':user.fbId}));
gameStatus = 'ready';
}
}
io.on('connection', function(client){
client.send(JSON.stringify({'type':'usersList','data':users}));
client.send(JSON.stringify({'type':'drawingData','data':points}));
if(gameStatus == 'on') {
client.send(JSON.stringify(word));
}
client.on('message', function(message){
data = JSON.parse(message);
switch (data.type) {
case('drawingData'):
_.each(data.data,function(point){
points.push(point);
});
client.broadcast(message);
break;
case('deleteDrawingData'):
if(checkIfUserIsArtist(client.sessionId)){
points = [];
client.broadcast(JSON.stringify({'type':'deleteDrawingData','data':true}));
}
break;
case('userConnect'):
if(_.any(users, function(user){ return user.fbId == data.data; })) {
client.send(JSON.stringify({'type':'userAlreadyConnected','data':''}));
} else {
if(!_.any(users, function(user){ return user.artist; })){
users.push({id:client.sessionId,fbId:data.data,artist:true});
client.broadcast(JSON.stringify({'type':'userConnect','data':{'fbId':data.data,artist:true}}));
client.send(JSON.stringify({'type':'userArtist','data':true}))
} else {
if(gameStatus == 'off')
gameStatus = 'ready';
users.push({id:client.sessionId,fbId:data.data,artist:false});
client.broadcast(JSON.stringify({'type':'userConnect','data':{'fbId':data.data,artist:false}}));
}
}
break;
case('answerData'):
if(_.any(users, function(user){ return user.fbId == data.data.fbId; })) {
if(data.data.answer.toLowerCase() == word.data.name.toLowerCase()){
var user = _.detect(users, function(user){return user.artist});
io.broadcast(JSON.stringify({'type':'userWin','data':{'artist':user.fbId,'guesser':data.data.fbId}}));
setNewArtist(data.data.fbId);
} else {
if(data.data.answer != '')
io.broadcast(JSON.stringify({'type':'console','data':data.data.answer.toLowerCase().substr(0,40)}));
};
}
break;
case('skip'):
if(_.any(users, function(user){ return user.fbId == data.data; })) {
setNewArtist();
}
break;
}
});
client.on('disconnect', function(){
//We delete user from users array on disconnect
this.setNewArtist = false;
users = _.reject(users, _.bind(function(user){
if(user.id == client.sessionId) {
client.broadcast(JSON.stringify({'type':'userDisconnect','data':user.fbId}));
if(user.artist) {
this.setNewArtist = true;
}
return true;
} else {
return false;
}
},this));
if(this.setNewArtist) {
setNewArtist();
}
});
});