-
Notifications
You must be signed in to change notification settings - Fork 14
/
thequest.js
98 lines (80 loc) · 2.77 KB
/
thequest.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
/**
* Module dependencies.
*/
if(process.env.NODE_ENV == "azure")
config = require("./config/azure");
else
config = require("./config/local");
var express = require('express')
, socketio = require("socket.io")
, routes = require('./routes')
, actions = require("./lib/expressActions")
, MongoStore = require('connect-mongo')(express)
, SessionSockets = require('session.socket.io')
, mongoose = require("mongoose")
, browserify = require("browserify")
, requireJade = require("require-jade")
, http = require('http')
, path = require('path');
var app = express();
var cookieParser = express.cookieParser("thequest");
var sessionStore = new MongoStore(config.MongoStore);
var db = mongoose.createConnection(config.mongoose.hostname,
config.mongoose.db,
config.mongoose.port,
config.mongoose.opts);
db.once("open", function(){
require("./models/Backbone").db = db;
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.methodOverride());
app.use(express.session({
secret: "thequest",
store: sessionStore
}));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
var bundle = browserify({
mount: "/game-index.js",
watch: true,
debug: true,
cache: false
})
.use(requireJade)
.addEntry("./client/game-index.js");
app.use(bundle);
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
actions.mount(app, require("./routes/user"));
actions.mount(app, require("./routes/game"));
var Game = require("./models/Game")
, User = require("./models/User")
, Player = require("./models/Player")
, Client = require("./models/Client");
var server = http.createServer(app).listen(app.get('port'), "0.0.0.0",function(){
console.log("Express server listening on port " + app.get('port'));
var io = socketio.listen(server);
io.set("log level", 0);
var sio = new SessionSockets(io, sessionStore, cookieParser);
var game = Game.create();
game.startWorld();
sio.on("connection", function(err, socket, session){
if(err) return console.log(err);
if(!session.userId) return;
User.findById(session.userId, function(err, user){
if(err) return console.log(err);
if(!user) return console.log("user not registered");
game.addClient(Client.create(socket, user));
});
});
});
});