-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
364 lines (340 loc) · 11 KB
/
Copy pathserver.js
File metadata and controls
364 lines (340 loc) · 11 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
This file is part of CSGO Drawing Board.
CSGO Drawing Board is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CSGO Drawing Board is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CSGO Drawing Board. If not, see <http://www.gnu.org/licenses/>.
*/
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
path = require('path');
process.on('uncaughtException', function(err) {
console.log('uncaught exception: ' + err);
})
var port = process.env.PORT || 80;
server.listen(port);
/* io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
}); */
var rooms = {};
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.static(path.join(__dirname, 'public')));
//POSTS
app.post('/test', function(req, res) {
console.log('got make_room post');
console.log(req.body);
var map = req.body.map;
console.log('map is: ' + map);
var roomname;
do{
var crypto = require('crypto'),
shasum = crypto.createHash('sha1');
shasum.update('' + Math.random() + new Date().getTime());
roomname = shasum.digest('hex');
}while(rooms[roomname] !== undefined);
rooms[roomname] = new Room(roomname);
rooms[roomname].map = map;
res.redirect('/room=' + roomname);
});
//GETS
app.get('/', function(req, res) {
console.log('sent index');
res.sendfile('index.html');
});
app.get('/room=:roomId', function(req, res) {
var roomId = req.params.roomId;
console.log('room id is: ' + roomId);
if(rooms[roomId] !== undefined) {
var mapurl = getMap(rooms[roomId].map);
res.render('editor', {imgurl: mapurl});
}
else
res.send('invalid room!');
});
function getMap(map) {
switch(map) {
case 'dust2':
return 'imgs/de_dust2_radar_spectate.png';
break;
case 'inferno':
return 'imgs/de_inferno_radar_spectate.png';
break;
case 'dust':
return 'imgs/de_dust_radar_spectate.png';
break;
case 'train':
return 'imgs/de_train_radar_spectate.png';
break;
case 'vertigo':
return 'imgs/de_vertigo_radar.png';
break;
case 'nuke':
return 'imgs/de_nuke_radar_spectate.png';
break;
case 'mirage':
return 'imgs/de_mirage_radar_spectate.png';
break;
default:
return 'imgs/de_dust2_radar_spectate_default.png';
break;
}
}
function Shape(id, type, x, y, points) {
this.id = id;
this.type = type;
this.user = 'none';
this.xPos = x;
this.yPos = y;
this.points = points;
this.setX = function(newX) {
this.xPos = newX;
}
this.setY = function(newY) {
this.yPos = newY;
}
}
function Player(name, num, img) {
this.num = num;
this.name = name;
this.img = img;
this.color;
this.drawings = [];
}
function Room(name) {
this.name = name;
this.shapesBeingUsed = false;
this.shapes = {};
this.players = [];
this.colors = ["#FF0000", "#0000FF", "#FF00FF", "#00FF00", "#FF6600"];
this.getShapes = function() {
return this.shapes;
}
}
io.on('connection', function(socket) {
socket.on('info', function(data){
try{
console.log('got here');
if(rooms[data.room] === undefined) {
console.log('BAD ROOM!');
socket.disconnect();
return;
}
//if(rooms[data.room] === undefined) rooms[data.room] = new Room(data.room);
var pnum = rooms[data.room].players.length;
var tmpname = 'player ' + pnum;
var player = new Player(tmpname, pnum, 'http://upload.wikimedia.org/wikipedia/en/a/af/Question_mark.png');
if(rooms[data.room].colors.length > 0)
player.color = rooms[data.room].colors.pop();
else player.color = "#FF0000";
rooms[data.room].players.push(player);
var crypto = require('crypto')
, shasum = crypto.createHash('sha1');
var timestamp = (new Date).getTime();
console.log('timestamp is: ' + timestamp);
shasum.update(data.room + player.name + player.img + player.num + timestamp);
var locid = shasum.digest('hex');
console.log('LOCID IS: ' + locid);
rooms[data.room].shapes[locid] = new Shape(locid, 'player', 200, 200);
rooms[data.room].shapes[locid].img = player.img;
player.imgid = locid;
socket.emit('pinfo', player);
socket.emit('all players', rooms[data.room].players);
socket.emit('all shapes', rooms[data.room].getShapes());
socket.join(data.room);
io.sockets.in(data.room).emit('player change', {type: 'new', players: rooms[data.room].players, newPlayer: player});
socket.set('room', {room: data.room, player: player}, function(){});
socket.broadcast.to(data.room).emit('new shape', rooms[data.room].shapes[locid]);
}
catch(err) {
console.log('error in info: ' + err);
}
});
socket.on('disconnect', function(data){
try{
socket.get('room', function(err, room) {
if(room === undefined || room === null) {
console.log('disconnecting bad request...');
return;
}
for(var i=0; i<room.player.drawings.length; i++) {
delete rooms[room.room].shapes[room.player.drawings[i]];
io.sockets.in(room.room).emit('remove shape', room.player.drawings[i]);
}
io.sockets.in(room.room).emit('remove shape', room.player.imgid);
delete rooms[room.room].shapes[room.player.imgid];
rooms[room.room].colors.push(room.player.color);
var index = room.player.num;
rooms[room.room].players.splice(index,1);
if(rooms[room.room].players.length < 1) {
console.log('deleteing room: ' + room.room);
delete rooms[room.room];
return;
}
for(var i=index; i<rooms[room.room].players.length; i++){
rooms[room.room].players[i].num--;
}
io.sockets.in(room.room).emit('player change', {type: 'disconnect', players: rooms[room.room].players, dcid: room.player.imgid});
});
}
catch(err) {
console.log('error in disconnect: ' + err);
}
});
socket.on('draw', function(data){
if(data === null || data === undefined) return;
try{
socket.get('room', function(err, room) {
var i=0;
do {
var crypto = require('crypto')
, shasum = crypto.createHash('sha1');
if(room === null) return;
shasum.update(data.type + data.xPos + data.yPos + data.points + i);
var locid = shasum.digest('hex');
console.log(locid);
i++;
}
while(rooms[room.room].shapes[locid] !== undefined);
rooms[room.room].shapes[locid] = new Shape(locid, data.type, data.xPos, data.yPos, data.points);
rooms[room.room].shapes[locid].color = room.player.color;
if(data.type === 'brush'){
rooms[room.room].players[room.player.num].drawings.push(locid);
console.log("drawings are: " + rooms[room.room].players[room.player.num].drawings);
}
io.sockets.in(room.room).emit('new shape', rooms[room.room].shapes[locid]);
});
}
catch(err) {
console.log('error in draw: ' + err);
}
});
socket.on('taking control', function(data) {
console.log('got into taking control');
try{
socket.get('room', function(err, room) {
if (data === null || data === undefined || rooms[room.room].shapes[data] === undefined) return;
console.log('data is: ' + data);
if(rooms[room.room].shapes[data].user === 'none' ||
rooms[room.room].shapes[data].user === socket) {
rooms[room.room].shapesBeingUsed = true;
rooms[room.room].shapes[data].user = socket;
socket.broadcast.to(room.room).emit('being used', data);
}
});
}
catch(err) {
console.log('error in taking control: ' + err);
}
});
socket.on('relinquish control', function(data) {
if(data === null || data === undefined) return;
try{
socket.get('room', function(err, room) {
rooms[room.room].shapes[data].user = 'none';
rooms[room.room].shapesBeingUsed = false;
io.sockets.in(room.room).emit('not used', {shape: data, color: room.player.color});
});
}
catch(err) {
console.log('error in relinquish control: ' + err);
}
});
socket.on('move obj', function(data) {
if(data === null || data === undefined) return;
try{
socket.get('room', function(err, room) {
console.log('got into here');
if(rooms[room.room].shapes[data.id] === null || rooms[room.room].shapes[data.id] === undefined) return;
if(rooms[room.room].shapes[data.id].user === socket) {
console.log('also, correct user');
rooms[room.room].getShapes()[data.id].setX(data.xPos);
rooms[room.room].getShapes()[data.id].setY(data.yPos);
socket.broadcast.to(room.room).emit('shape move', {id: data.id, xPos: data.xPos, yPos: data.yPos});
}
});
}
catch(err) {
console.log('error in move obj: ' + err);
}
});
socket.on('remove elm', function(data) {
if(data === null || data === undefined) return;
try{
socket.get('room', function(err, room){
delete rooms[room.room].shapes[data];
rooms[room.room].players[room.player.num].drawings.splice(rooms[room.room].players[room.player.num].drawings.indexOf(data), 1);
io.sockets.in(room.room).emit('remove shape', data);
});
}
catch(err) {
console.log('error in remove elm: ' + err);
}
});
socket.on('steam info', function(data) {
console.log('got into steam info befoer html');
try {
socket.get('room', function(err, room) {
var http = require('http');
var options = {
hostname: 'steamcommunity.com',
port: 80,
path: '/id/' + data,
method: 'GET'
};
var req = http.request(options, function(resp){
console.log('got into steam info during html');
var html = '';
resp.on('data', function(chunk) {
html += chunk;
});
resp.on('end', function(){
console.log('got into steam info after html');
var imgregex = new RegExp("img src=\"(.*\_full.jpg)");
var execs = imgregex.exec(html);
if(execs === null) {
console.log('bad login: ' + data);
socket.emit('bad login');
return;
}
else {
var imgurl = imgregex.exec(html)[1];
rooms[room.room].players[room.player.num].name = data;
rooms[room.room].players[room.player.num].img = imgurl;
rooms[room.room].shapes[room.player.imgid].img = imgurl;
io.sockets.in(room.room).emit('player change', {type: 'change', players: rooms[room.room].players});
io.sockets.in(room.room).emit('shape move', {type: 'image change', img: imgurl, id: rooms[room.room].players[room.player.num].imgid});
}
});
});
req.on('error', function(e) {
console.log('error: ' + e.message);
});
req.end();
});
}
catch(err) {
console.log('error in steam info: ' + err);
}
});
socket.on('request drawings', function(data) {
try{
socket.get('room', function(err, room) {
socket.emit('player drawings', room.player.drawings);
});
}
catch(err) {
console.log('error in request drawings: ' + err);
}
});
});