forked from felixrieseberg/Node-Mayhem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
157 lines (135 loc) · 4.49 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Setting up required components
/* ----------------------------------------------------------------- */
var http = require('http'),
path = require('path'),
isProduction = (process.env.NODE_ENV === 'production'),
port = isProduction ? 80 : process.env.PORT || 8000,
express = require('express'),
app = express(),
socketio = require('socket.io'),
server, io, players, highScores;
// Setting up express for routing
app.set('port', port);
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.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// Routing
/* ----------------------------------------------------------------- */
app.get('/', function (req, res) {
res.render('index');
});
app.get('/game', function (req, res) {
res.render('game');
});
// Socket.io: Setting up multiplayer
/* ----------------------------------------------------------------- */
server = http.createServer(app);
io = socketio.listen(server);
players = {};
highScores = {};
io.set('log level', 0);
// Socket.io: Setting up event handlers for all the messages that come
// in from the client (check out /public/js/game.js and /views/game.jade
// for that).
io.on('connection', function (socket) {
socket.on('disconnect', function () {
socket.broadcast.emit('removePlayer', socket.sessionId);
delete players[socket.sessionId];
delete highScores[socket.sessionId];
io.sockets.emit('highScores', highScores);
});
socket.on('gameReady', function (data) {
socket.sessionId = data.id;
socket.playerName = data.name;
if (players[data.id]) {
socket.broadcast.emit('removePlayer', socket.sessionId);
delete players[socket.sessionId];
delete highScores[socket.sessionId];
io.sockets.emit('highScores', highScores);
}
var player = {
id: data.id,
z: 6,
health: 3,
score: 0,
p: {
x: 8 * 48,
y: 2 * 48
},
n: socket.playerName
};
highScores[data.id] = {
name: socket.playerName,
score: 0
};
socket.broadcast.emit('addPlayer', player);
players[data.id] = player;
socket.emit('playerId', data.id);
socket.emit('addMainPlayer', player);
socket.emit('addPlayers', players);
io.sockets.emit('highScores', highScores);
});
socket.on('updatePlayerState', function (position, state) {
if (!players[socket.sessionId]) {
return;
}
players[socket.sessionId].p = position;
socket.broadcast.emit('updatePlayerState', {
id: socket.sessionId,
p: position,
s: state
});
});
socket.on('fireBullet', function (id, source, target) {
socket.broadcast.emit('fireBullet', id, source, target);
});
socket.on('playerHit', function (data) {
socket.broadcast.emit('remotePlayerHit', data);
});
socket.on('scoreHit', function () {
var player = players[socket.sessionId];
if (!player) {
return;
}
player.score = player.score ? player.score + 100 : 100;
socket.emit('score', player.score);
highScores[player.id] = {
name: player.n,
score: player.score
};
io.sockets.emit('highScores', highScores);
});
socket.on('resetPlayer', function () {
var player = {
id: socket.sessionId,
z: 6,
score: 0,
health: 3,
p: {
x: 8 * 48,
y: 2 * 48
},
n: socket.playerName
};
socket.broadcast.emit('removePlayer', player.id);
socket.broadcast.emit('addPlayer', player);
socket.emit('addMainPlayer', player);
});
socket.on('playerHealed', function (data) {
socket.broadcast.emit('remotePlayerHealed', data);
});
});
// ...and actually starting the server!
/* ----------------------------------------------------------------- */
server.listen(app.get('port'), function () {
console.log('\n' +
'-----------------------------\n' +
'| Welcome to Node Mayhem! |\n' +
'-----------------------------\n' +
'Server listening on port ' + app.get('port'));
});