forked from TumblrCode/CemeteryMania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (111 loc) · 3.32 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
/**
* Module dependencies.
*
* If Redis server is hosted elsewhere update createClient accordingly!
*
*/
var express = require('express'),
redis = require('redis'),
map = require('map'),
facebookRequest = require('facebook-signed-request'),
httprequest = require('request');
var app = module.exports = express.createServer(),
io = require('socket.io').listen(app),
redis_client = redis.createClient();
// Redis
//redis_client.auth("PASSWORD");
redis_client.on("error", function(err)
{
console.log("RedisError: " + err);
});
// Express Configuration
app.configure(function(){
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "CM Secret!" }));
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
app.dynamicHelpers(
{
session: function(req, res)
{
return req.session;
}
});
});
// Facebook config
var fb_appID = "181403871903302";
var fb_secret = "C";
facebookRequest.secret = fb_secret;
app.post('/', function(req, res)
{
// Handle inital FB post request
var signedRequest = new facebookRequest(req.body.signed_request);
signedRequest.parse(function(error, request)
{
if(request.isValid())
{
if(typeof request.data.oauth_token === 'string')
{
// TODO: rewrite this, but I am lazy right now. Also fix session support
var user_data;
httprequest("https://graph.facebook.com/" + request.data.user_id + "?access_token=" + request.data.oauth_token, function(err, response, body)
{
user_data = JSON.parse(body);
redis_client.set("users." + request.data.user_id + ".name", user_data.first_name + " " + user_data.last_name);
req.session.name = user_data.first_name + " " + user_data.last_name;
});
res.redirect('/');
//res.render('index.jade');
}
else
res.render('auth.jade');
}
else
console.log(error);
});
});
// WebSockets
// Chat Server
var chat = io.of('/chat').on('connection', function(socket)
{
socket.broadcast.emit('chat-player-connect', {from: "server", message: "New player connected :)"});
socket.on('disconnect', function()
{
socket.broadcast.emit('chat-player-disconnect', {from: "server", message: "Player disconnected :("});
});
socket.on('server-send-message', function(data)
{
socket.broadcast.emit('client-send-message', {from: data.from.replace(/(<([^>]+)>)/ig,"") , message: data.message.replace(/(<([^>]+)>)/ig,"") });
});
});
// Game Server
var game = io.of('/game').on('connection', function(socket)
{
socket.on('client-request-player-data', function(data)
{
var authenticated = false;
// Authenticate player
var playerData = {};
if(authenticated)
socket.emit('game-player-data', playerData);
else
socket.emit('game-player-data', { exception : "AuthError"});
});
// Handle game events here
});
// End WebSockets
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
// Map data
map.LoadMap("map/map.cmm", function(MapPacket)
{
console.log("Map loaded; size is (%d, %s).", MapPacket.W, MapPacket.H);
});