-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
94 lines (78 loc) · 2.74 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
var express = require('express')
, http = require('http')
, faye = require('faye');
var Player = require('./public/js/player').Player
, World = require('./public/js/world').World
, Game = require('./models/game').Game
, Utils = require('./public/js/utils').Utils;
var app = express();
app.configure(function() {
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session({secret:'asdfsdf'}));
app.use(express.bodyParser());
});
/** Redirects to Game Page **/
app.get('/', function(req, res){
var data = {}
if(req.session.name === undefined) {
data.logged_in = false;
req.session.token = Utils.makeId(7);
data.token = req.session.token;
data.name = Utils.makeId(7);
} else {
data.logged_in = true;
data.token = req.session.token;
data.name = req.session.name;
}
res.render('index.ejs', data);
});
app.post('/join', function(req, res){
var player = {},
name = '';
if(req.session.name === undefined && req.body.token === req.session.token ) {
name = req.session.name = req.body.name;
player = new Player({name: req.body.name, token: req.body.token});
player.teleport(world);
world.addPlayer(player);
faye_server.getClient().publish('/update_players', world.getPlayers());
}
res.send({status: 'success'});
});
/** returns current player positions **/
app.get('/players/get', function(req, res){
res.send(world.getPlayers());
});
/** returns current map **/
app.get('/map/get', function(req, res){
res.send(world.getMap());
});
/** returns current world **/
app.get('/world/get', function(req, res){
res.send(world.getWorld());
});
var server = http.createServer(app);
var faye_server = new faye.NodeAdapter({mount: '/faye'});
var extension = {
incoming : function(message, callback) {
/* if(message.channel === '/join') {
var player = new Player({clientId:message.clientId, name: message.data.name});
player.teleport(world);
world.addPlayer(player);
server.getClient().publish('/new/player', {playername:message.data.name});
}*/
if(message.channel === '/move') {
world.movePlayer(message.data.name, message.data.direction, message.data.token);
//faye_server.getClient().publish('/moves', {name:message.data.name, direction:message.data.direction});
}
callback(message);
}
};
faye_server.addExtension(extension);
server.listen(3000);
faye_server.attach(server);
process.faye_server = faye_server;
world = new World();
world.init();
world.lifeCycle();
console.log("Express server running at\n => http://localhost:3000/\nCTRL + C to shutdown");