-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
119 lines (97 loc) · 3.58 KB
/
web.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
var express = require('express'),
socketIO = require('socket.io'),
Constants = require('./js/constants'),
Server = require('./js/server');
var servers = {};
var app = express.createServer();
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.static(__dirname + '/public'));
var io = socketIO.listen(app);
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
io.disable("log");
});
/* Endpoints for the Clients */
app.get('/join', function (req, res) {
res.render('./client/register.jade', { layout: false });
});
app.post('/join', function (req, res) {
var name = req.param('char-name');
var serverId = req.param('servers');
var team = req.param('teams');
var server = servers[serverId];
console.log("New player '" + name + "' joined the '" + server.name + "' server.");
res.render('./client/controller.jade', { player: { name: name, team: team, server: serverId }, layout: false });
});
/* Endpoints for the Servers */
app.get('/servers', function (req, res) {
var s = [];
for (var id in servers) {
s.push({
id: id,
name: servers[id].name,
population: servers[id].getPopulation(),
teams: servers[id].getTeams()
});
};
res.send(s);
});
app.post('/servers', function (req, res) {
var serverId = parseInt(req.param('servers'));
var serverName = req.param('server-name');
var server = {};
if (serverId === 0) {
server.name = serverName;
server.id = serverId;
} else {
server.name = servers[serverId].name;
server.id = servers[serverId].id;
}
res.render('server/game.jade', { server: server, layout: false });
});
app.get('/', function (req, res) {
res.render('server/servers.jade', { layout: false });
});
app.listen(Constants.SERVER_PORT);
/* Server Sockets */
var serverSockets = io
.of('/server')
.on('connection', function(socket) {
socket.on('join', function(settings) {
var serverId = parseInt(settings.id);
if (serverId === 0) {
var server = new Server(settings.name, settings.screenSize, socket);
server.run();
servers[server.id] = server;
socket.emit('onJoin', { id: server.id, name: server.name });
var numServers = Object.keys(servers).length;
console.log('Server Created: Total # of servers: ' + numServers);
} else {
var server = servers[serverId];
server.serverJoined(socket);
socket.emit('onJoin', { id: server.id, name: server.name });
}
});
socket.on('disconnect', function() {
console.log("A server disconnected");
for (var serverId in servers) {
var server = servers[serverId];
if (server.hasSocket(socket)) {
server.serverDisconnected(socket);
delete servers[serverId];
}
}
});
});
///* Client Sockets */
var playerSockets = io
.of('/client')
.on('connection', function(socket) {
socket.on('join', function(settings) {
var server = servers[parseInt(settings.server)];
server.playerJoined(settings.name, settings.team, socket);
console.log("Initialized player: " + settings.name + " (" + socket.id + ")");
});
});