-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·80 lines (65 loc) · 2.33 KB
/
app.js
File metadata and controls
executable file
·80 lines (65 loc) · 2.33 KB
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
// start monitoring with new relic
require('newrelic');
// creating global parameters and start
// listening to 'process.env.PORT', we are creating an express
// server and then we are binding it with socket.io
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
process.env.PORT = process.env.PORT || 1337;
var path = require('path');
var url = 'http://localhost:' + process.env.PORT + '/';
var db = require('./db/queries');
/* We can access nodejitsu enviroment variables from process.env */
/* Note: the SUBDOMAIN variable will always be defined for a nodejitsu app */
if(process.env.SUBDOMAIN){
url = 'http://' + process.env.SUBDOMAIN + '.jit.su/';
}
// listening to process.env.PORT...
server.listen(process.env.PORT);
console.log("Express server listening on port " + process.env.PORT);
console.log(url);
// log it all
io.set('log level', 2);
// give the client access to all client files
app.use(express.static(path.join(__dirname, '../build/client')));
// serving the main applicaion file (index.html)
// when a client makes a request to the app root
// http://localhost:1337
app.get('/', function (req, res) {
res.sendfile(path.join(__dirname, '../build/client/index.html'));
});
// serves basic room details
app.get('/rooms', function (req, res) {
db.getRooms(function(err, rooms){
if (err) { res.send(403, err); throw err; }
res.json(rooms);
});
});
// serves player stats for in-game menu
app.get('/rooms/:id', function (req, res) {
db.getRoomInfo(req.param('id'), function(err, roomInfo){
if (err) { res.send(403, err); throw err; }
res.json(roomInfo);
});
});
// Holds players
var players = {};
var mapItems = [
{ type: 'Alien Space Station', pos: [0, 1000], rot: 0, hp: 100 },
{ type: 'Human Space Station', pos: [0, -1000], rot: Math.PI*2, hp: 100 }
];
var mapSize = 1600;
var maxSpeed = 200; // units per second
function getRandomCoord() {
return Math.random()*mapSize - mapSize/2;
}
function getRandomPosition() {
return [getRandomCoord(), 140, getRandomCoord()];
}
function getTime() {
return (new Date()).getTime();
}
var socketManager = require('./io/manager.js')(mapItems, io);
io.sockets.on('connection', socketManager);