-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
396 lines (312 loc) · 11.7 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// https://github.com/nko4/website/blob/master/module/README.md#nodejs-knockout-deploy-check-ins
require('nko')('WfsRuxlWWBoW0L63');
'use strict';
var isProduction = (process.env.NODE_ENV === 'production');
var http = require('http');
var port = (isProduction ? 80 : 8000);
var mongojs = require('mongojs');
var db = mongojs("mongodb://nko:nko@ds053428.mongolab.com:53428/nko13", ["statistics", "players"]);
var ejs = require('ejs');
var express = require('express');
var _ = require('underscore');
var _s = require('underscore.string');
var app = express();
console.log('Node app is running at localhost:' + port);
function incStats(name) {
var _inc = {};
_inc[name] = 1;
db.statistics.update({ _id: "main" }, { $inc: _inc }, { upsert: true })
}
incStats('server_starts');
//app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
// 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(__dirname + '/public'));
// app.use(require('connect-assets')());
var Game = (function () {
Game.SPACE = 0;
Game.WALL = 1;
Game.BRICK = 2;
Game.MIXTURE = 3;
Game.REVIVAL_BRICK = 5000; // 5s
Game.REVIVAL_MIXTURE = 10000; // 10s
Game.RAND_NEW_BRICK_TIME = 35 * 1000; // 35s
function Game(max_x, max_y) {
var self = this;
this.map = [];
var MAP_X = this.MAP_X = max_x;
var MAP_Y = this.MAP_Y = max_y;
this.brickCount = 0;
this.wallCount = 0;
this.powerCount = 0;
this.maxCount = MAP_X * MAP_Y;
this.players = {};
this.uuids = {};
for (var x = MAP_X; x >= 0; x--) {
this.map[x] = [];
for (var y = MAP_Y; y >= 0; y--) {
this.map[x].push(0);
}
}
_.times(MAP_X, function (x) {
self.map[x][0] = Game.WALL;
self.wallCount += 1;
});
_.times(MAP_X, function (x) {
self.map[x][MAP_Y] = Game.WALL;
self.wallCount += 1;
});
_.times(MAP_Y, function (y) {
self.map[0][y] = Game.WALL;
self.wallCount += 1;
});
_.times(MAP_Y, function (y) {
self.map[MAP_X][y] = Game.WALL;
self.wallCount += 1;
});
_.times(MAP_X, function (n) {
if (!n) return;
if (n % 2) return;
if (n === MAP_X - 1) return;
_.times(MAP_Y, function (m) {
if (!m) return;
if (m % 2) return;
if (m === MAP_Y - 1) return;
self.map[n][m] = Game.WALL;
self.wallCount += 1;
});
});
this.map[MAP_X][MAP_Y] = 1; // hack :) - brawo Kamil!
this.wallCount += 1;
this.maxCount -= this.wallCount;
_.times(parseInt(MAP_X * MAP_Y * 0.30), function () {
var x = Math.floor(Math.random() * MAP_X - 1) + 1;
var y = Math.floor(Math.random() * MAP_Y - 1) + 1;
if (self.map[x][y] == 0) {
self.map[x][y] = Game.BRICK;
self.brickCount += 1;
}
});
}
Game.prototype.randNewBrick = function () {
if (this.brickCount < (this.maxCount / 3)) {
var x = Math.floor(Math.random() * this.MAP_X - 1) + 1;
var y = Math.floor(Math.random() * this.MAP_Y - 1) + 1;
if (this.map[x][y] == 0) {
this.map[x][y] = Game.BRICK;
this.brickCount += 1;
return [x, y];
}
}
return false;
};
Game.prototype.getPlayer = function (uuid, cb) {
db.players.findOne({
uuid: uuid
}, function (err, doc) {
cb(doc)
});
};
Game.prototype.randNewMixture = function () {
if (this.powerCount < 50) {
var x = Math.floor(Math.random() * this.MAP_X - 1) + 1;
var y = Math.floor(Math.random() * this.MAP_Y - 1) + 1;
if (this.map[x][y] == Game.SPACE) {
this.map[x][y] = Game.MIXTURE;
this.powerCount += 1;
return [x, y];
}
}
return false;
};
Game.prototype.set = function (x, y, value) {
if (this.map[x][y] == Game.BRICK) {
this.brickCount += 1;
}
if (this.map[x][y] == Game.MIXTURE) {
this.powerCount += 1;
}
this.map[x][y] = value;
};
Game.prototype.setId = function (uuid, socket_id) {
this.uuids[uuid] = socket_id;
};
Game.prototype.getSocketIdBy = function (socket_id) {
return _.invert(this.uuids)[socket_id];
};
Game.prototype.getEmptyTile = function () {
var emptyTiles = [];
_.each(this.map, function (column, x) {
_.each(column, function (tile, y) {
if (!tile) {
emptyTiles.push({ x: x, y: y });
}
});
});
return emptyTiles[_.random(0, emptyTiles.length - 1)];
};
return Game;
})();
var game = new Game(44, 30);
var server = app.listen(port);
var io = require('socket.io').listen(server);
if (isProduction) {
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.set('log level', 1); // reduce logging
// enable all transports (optional if you want flashsocket support, please note that some hosting
// providers do not allow you to create servers that listen on a port different than 80 or their
// default port)
io.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
}
app.get('/', function (reseq, res) {
res.render('index', { isProduction: isProduction })
});
function updatePlayer(uuid, settings) {
db.players.update(
{ uuid: uuid }, // first
{ $set: settings },
{ upsert: true }
);
}
io.sockets.on('connection', function (socket) {
var ip = socket.handshake.headers['x-forwarded-for'] || socket.handshake.address.address;
socket.on('update', function (uuid, settings) {
updatePlayer(uuid, settings)
});
socket.on('say', function (uuid, message) {
game.getPlayer(uuid, function (player) {
io.sockets.emit('warn', '<em>' + player.name + '</em>: ' + _s.stripTags(message));
});
});
socket.on('play', function (uuid, settings) {
if (_.isEmpty(settings)) {
game.getPlayer(uuid, function (player) {
socket.broadcast.emit('join', { id: socket.id, ip: ip, name: player.name });
socket.emit('info', 'Welcome again <em>' + player.name + '</em>');
socket.broadcast.emit('info', 'Player <em>' + player.name + '</em> joined from <img src="http://www.geojoe.co.uk/api/flag/?ip=' + ip + '" alt="-" />');
game.players[socket.id] = { n: player.name };
var emptyTile = game.getEmptyTile();
socket.emit('play', socket.id, emptyTile.x, emptyTile.y, player.name);
if (!game.players[socket.id]) {
game.players[socket.id] = {};
}
game.players[socket.id] = _.extend(game.players[socket.id], { x: emptyTile.x, y: emptyTile.y })
});
} else {
socket.broadcast.emit('join', { id: socket.id, ip: ip, name: settings.name });
socket.emit('info', 'Welcome <em>' + settings.name + '</em>');
socket.broadcast.emit('info', 'Player <em>' + settings.name + '</em> joined from <img src="http://www.geojoe.co.uk/api/flag/?ip=' + ip + '" alt="-" />')
updatePlayer(uuid, _.extend({
ip: ip,
joined: new Date()
}, settings));
game.players[socket.id] = { n: settings.name };
var emptyTile = game.getEmptyTile();
socket.emit('play', socket.id, emptyTile.x, emptyTile.y, settings.name);
if (!game.players[socket.id]) {
game.players[socket.id] = {};
}
game.players[socket.id] = _.extend(game.players[socket.id], { x: emptyTile.x, y: emptyTile.y });
}
socket.emit('map', game.map);
incStats('players_joins');
game.setId(uuid, socket.id);
});
socket.on('mc', function (x, y, type) {
if (game.map[x][y] === 2 && type === 0) {
game.getPlayer(game.getSocketIdBy(socket.id), function (player) {
game.brickCount -= 1;
// db.players.update(
// { uuid: player.uuid },
// { $inc: { points: 1 } } ,
// { upsert: true }
// );
});
}
game.set(x, y, type);
socket.broadcast.emit('mc', x, y, type);
});
socket.on('kill', function (id) {
delete game.players[id];
socket.broadcast.emit('killed', { id: id, by_id: socket.id });
incStats('players_kills');
if (id != socket.id) {
game.getPlayer(game.getSocketIdBy(socket.id), function (player) {
if (player && player.uuid) {
db.players.update(
{ uuid: player.uuid },
{ $inc: { points: 100 } },
{ upsert: true }
);
}
});
}
});
socket.on('pm', function (x, y) {
if (!game.players[socket.id]) {
game.players[socket.id] = {};
}
game.players[socket.id] = _.extend(game.players[socket.id], { x: x, y: y });
var temp_players = [];
_.each(game.players, function (v, k) {
temp_players.push(_.extend({ id: k }, v));
});
socket.broadcast.emit('pm', temp_players);
}); // player move
socket.on('disconnect', function () {
delete game.players[socket.id];
game.getPlayer(game.getSocketIdBy(socket.id), function (player) {
socket.broadcast.emit('leave', { id: socket.id });
if (player && player.name) {
socket.broadcast.emit('info', 'Player <em>' + player.name + '</em> leave');
}
});
});
});
/******************************************************************************/
setInterval(function () {
db.players.find({ points: { $gt: 1 }, name: { $exists: true } }, {
name: 1,
points: 1
}).sort({ points: -1 }).limit(10).toArray(function (err, ranking) {
if (!_.isEmpty(ranking)) {
io.sockets.emit('ranking', ranking);
}
});
}, 1000 * 5);
/******************************************************************************/
// setInterval(function () {
// var new_brick = game.randNewBrick();
// if (new_brick) {
// io.sockets.emit('mc', new_brick[0], new_brick[1], Game.BRICK);
// //incStats('bricks');
// }
// }, Game.REVIVAL_BRICK);
/******************************************************************************/
setInterval(function () {
var new_mixture = game.randNewMixture();
// build ne brick
if (new_mixture) {
io.sockets.emit('mc', new_mixture[0], new_mixture[1], Game.MIXTURE);
setTimeout(function () {
if (game.map[new_mixture[0]][new_mixture[1]] == Game.MIXTURE) {
io.sockets.emit('mc', new_mixture[0], new_mixture[1], Game.SPACE);
game.powerCount -= 1;
}
}, Game.RAND_NEW_BRICK_TIME);
incStats('powerups');
}
}, Game.REVIVAL_MIXTURE);