This repository was archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (92 loc) · 2.67 KB
/
app.js
File metadata and controls
110 lines (92 loc) · 2.67 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
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
/**
* Artillerye
*
* Copyright (c) 2014 Petar Petrov
*
* This work is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/4.0/.
*/
'use strict';
var express = require('express')
, http = require('http')
, path = require('path')
, SocketIO = require('socket.io')
, colors = require('colors')
, routes = require('./server')
, user = require('./server/user')
, _globals = require('./shared/globals')
, packets = require('./shared/packets')
, GameProc = require('./server/gameproc');
var app = express();
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.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'client')));
app.use(express.static(path.join(path.join(__dirname, 'client'), 'dist')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
// web routes
app.get('/be', routes.index);
app.get('/be/users', user.list);
// start http server
var httpServer = http.createServer(app).listen(app.get('port'), function(){
console.log('❤❤❤ Artillerye ❤❤❤ '.blue.bold);
console.log('Server listening on port ' + app.get('port'));
});
// socket.io
var gamesList = [];
var io = SocketIO(httpServer).of('/loosecannon');
/**
* Send a packet. Compress & serialize, if needed.
*/
var send = function(socket, packet, data) {
if (typeof socket === 'string') {
console.log('emit packet to room', socket, packet);
io.to(socket).emit(packet, data);
} else {
if (packet !== packets.PING) {
console.log('sending packet', packet);
}
socket.emit(packet, data);
}
};
io.on('connection', function(socket) {
_globals.debug('Socket connected.');
// send player some server info
send(socket, packets.CONNECTED, {
server: {
name: 'Mindfields',
ver: '0.0.0', // TODO: this is important!
games: gamesList.length
}
});
// search for free game
var foundGame = false;
for (var i = 0, count = gamesList.length; i < count; i++) {
if (!gamesList[i].isFull()) {
gamesList[i].joinClient(socket);
foundGame = true;
break;
}
}
//TODO: free games with 1 (AI) player
if (!foundGame) {
/**
* Create new game
*/
if (gamesList.length < _globals.MAX_GAMES) {
var game = new GameProc(send);
game.initGame();
game.joinClient(socket);
gamesList.push(game);
}
}
});