-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (82 loc) · 2.32 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
* @fileoverview This is the server app script.
* @author [email protected] (Alvin Lin)
*/
// Constants
var CHAT_TAG = '[Git To The Hub]';
var DEV_MODE = false;
var FRAME_RATE = 1000 / 60;
var IP = process.env.IP || 'localhost';
var PORT_NUMBER = process.env.PORT || 5000;
// Sets the DEV_MODE constant during development if we run 'node server --dev'
process.argv.forEach(function(value, index, array) {
if (value == '--dev' || value == '--development') {
DEV_MODE = true;
}
});
// Dependencies.
var express = require('express');
var http = require('http');
var morgan = require('morgan');
var socketIO = require('socket.io');
var Game = require('./lib/Game');
// Initialization.
var app = express();
var server = http.Server(app);
var io = socketIO(server);
var game = Game.create(io);
app.set('port', PORT_NUMBER);
app.set('view engine', 'jade');
app.use(morgan(':date[web] :method :url :req[header] :remote-addr :status'));
app.use('/public',
express.static(__dirname + '/public'));
app.use('/shared',
express.static(__dirname + '/shared'));
app.get('/', function(request, response) {
response.render('index', {
DEV_MODE: DEV_MODE
});
});
/**
* Server side input handler, modifies the state of the players and the
* game based on the input it receives. Everything runs asynchronously with
* the game loop.
*/
io.on('connection', function(socket) {
/**
* When a new player connects, add them to the game.
*/
socket.on('new-player', function(data, callback) {
game.addNewPlayer(socket.id, data.name);
callback({
success: true
});
});
/**
* Connected players sending will update their player state.
*/
socket.on('player-action', function(data) {
game.updatePlayerOnInput(socket.id, data);
});
/**
* When a player disconnects, remove them from the game.
*/
socket.on('disconnect', function() {
game.removePlayer(socket.id);
});
});
/**
* Server side game loop, runs at 60Hz and sends out update packets to the
* necessary connected clients.
*/
setInterval(function() {
game.update();
game.sendState();
}, FRAME_RATE);
// Starts the server.
server.listen(PORT_NUMBER, function() {
console.log('STARTING SERVER ON PORT ' + PORT_NUMBER);
if (DEV_MODE) {
console.log('DEVELOPMENT MODE ENABLED: SERVING UNCOMPILED JAVASCRIPT!');
}
});