-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
92 lines (82 loc) · 2.97 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
"use strict";
var express=require('express.io'),
app=express().http().io(),
createGame = require(__dirname+'/src/create_game.js'),
cnf = require(__dirname+'/config/levels'),
games = {};
app.use(express.static(__dirname+'/public'));
app.use(express.cookieParser());
app.use(express.session({secret : 'G0t_sOm3_boMb5_1n_h4nd'}))
app.io.route('done', function(req) {
if(typeof req.session.game == "undefined") {
req.session.game = req.socket.id;
games[req.session.game] = "join";
} else if(app.io.rooms['/'+req.session.game].length == 4) {
delete req.session.game;
req.socket.emit('HTMLmessage',{
alert : 'Maximum Player limit reached. Create another game if you want.',
redirect : 'origin'
});
return;
}
req.io.join(req.session.game);
if(req.session.game == req.socket.id){
req.socket.emit('HTMLmessage',{
link : '/join/'+req.session.game
});
req.socket.on('button', function(data){
if(data == "start") {
app.io.room(req.session.game).broadcast("initialData",createGame(cnf['1'],app.io.rooms['/'+req.session.game]));
games[req.session.game] = {};
app.io.room(req.session.game).broadcast('HTMLmessage',{start:true});
setTimeout(function(){
app.io.room(req.session.game).broadcast('start');
}, 5000);
}
});
}
console.log(req.session.game,app.io.rooms['/'+req.session.game]);
app.io.room(req.session.game).broadcast('HTMLmessage',{playerCount:app.io.rooms['/'+req.session.game].length});
});
app.io.route('selfData', function(req) {
if(typeof games[req.session.game][req.data.time] == "undefined")
games[req.session.game][req.data.time] = {};
games[req.session.game][req.data.time][req.socket.id] = req.data.data;
req.io.room(req.session.game).broadcast('data',{
id : req.socket.id,
time : req.data.time,
data : req.data.data
});
});
app.io.route('getImages', function(req){
req.socket.emit('images',cnf['images']);
});
app.io.route('disconnect', function(req){
// console.log("destroying",req.socket.id);
// console.log(app.io.rooms['/'+req.session.game].length,app.io.rooms);
if(typeof req.session.game !== "undefined") {
if(games[req.session.game] == "join")
app.io.room(req.session.game).broadcast('HTMLmessage',{playerCount:app.io.rooms['/'+req.session.game].length-1});
}
req.session.destroy();
})
app.get('/', function(req,res) {
// console.log("GET /",req.session);
if(typeof req.session.game !== "undefined") {
delete req.session.game;
}
res.sendfile(__dirname+'/client.html');
});
app.get('/join/:game', function(req,res){
if('/'+req.params.game in app.io.rooms) {
if(games[req.params.game] == "join"){
res.sendfile(__dirname+'/client.html');
req.session.game = req.params.game;
} else {
res.send('<script>alert("Oh so sad, you did not get a chance to join the game room :("); window.location=window.location.origin;</script>');
}
}
else
res.send('<script>alert("game not available!! You could create new game."); window.location=window.location.origin;</script>');
});
app.listen(process.env.PORT || 8080);