-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
80 lines (61 loc) · 1.85 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
/*************************************
//
// hungry-llama app
//
**************************************/
// express magic
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var device = require('express-device');
var HungryLlama = require('./modules/hungry-llama.js');
var game = new HungryLlama();
var runningPortNumber = process.env.PORT || 3000;
app.configure(function(){
// I need to access everything in '/public' directly
app.use(express.static(__dirname + '/public'));
//set the view engine
app.set('view engine', 'ejs');
app.set('views', __dirname +'/views');
app.use(device.capture());
});
// logs every request
app.use(function(req, res, next){
// output every request in the array
console.log({method:req.method, url: req.url, device: req.device});
// goes onto the next function in line
next();
});
app.get("/", function(req, res){
res.render('index', {});
});
app.get("/game", function(req, res){
var obj = {};
obj.name = req.query.name;
res.render('game', obj);
});
var clients = [];
game.on(HungryLlama.EVENT_DATA_CHANGED, function(data) {
io.sockets.emit('blast', game.gameData);
});
io.sockets.on('connection', function (socket) {
io.sockets.emit('blast', game.gameData);
clients.push(socket);
socket.on('name', function(data){
game.addPlayer(socket.id, data.name);
});
socket.on('blast', function(data, cb){
io.sockets.emit('blast', {msg:data.msg});
cb();
});
socket.on('answer', function(data, fn) {
game.answered(socket.id, data);
});
socket.on('disconnect', function() {
game.removePlayer(socket.id);
clients.splice(clients.indexOf(socket));
});
});
server.listen(runningPortNumber);
module.exports = app;