-
Notifications
You must be signed in to change notification settings - Fork 2
/
connectionServer.js
115 lines (98 loc) · 3.28 KB
/
connectionServer.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
var http = require("http");
var qs = require('querystring');
var url = require('url');
var RD = require('./lib/requestDispatcher');
var requestDispatcher = RD.requestDispatcher;
var resourceProvider = require('./lib/resourceProvider').resourceProvider;
var game = require('./lib/game').game;
var leaderboard = require('./lib/leaderboard').leaderboard;
requestDispatcher.addHandler(
new RD.UrlHandler('/index', function(request, response){
writeStaticAndEnd(response, 'index.html');
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/level', function(request, response){
var level = url.parse(request.url, false, true).path.substr(7);
game.level = require('./levels').levels[level];
console.log('Se cambió el level a ' + level);
writeTextAndEnd(response, level);
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/add', function(request, response){
if (request.method != 'POST') {
writeStaticAndEnd(response, '404.html');
return;
}
var body = '';
request.on('data', function (data) {
body += data;
});
request.on('end', function () {
var post = qs.parse(body);
if (post.url.substr(0, 7) != 'http://') {
post.url = 'http://' + post.url;
}
console.dir({ name: post.nombre, url: post.url });
game.addPlayer({ name: post.nombre, url: post.url });
writeStaticAndEnd(response, 'agregado.html');
});
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/pause', function(request, response){
game.pause();
console.log('Cambio de estado: ' + game.status);
writeTextAndEnd(response, game.status);
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/start', function(request, response){
game.start();
console.log('Cambio de estado: ' + game.status);
writeTextAndEnd(response, game.status);
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/leaderboard', function(request, response){
writeStaticAndEnd(response, 'leaderboard.html');
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/JSONLeaderboard', function(request, response){
writeTextAndEnd(response, JSON.stringify(leaderboard.getPlayers()));
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/admin', function(request, response){
writeStaticAndEnd(response, 'admin.html');
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/palabramagica', function(request, response){
writeTextAndEnd(response, 'La palabra magica es abracadabra.')
})
);
requestDispatcher.addHandler(
new RD.UrlHandler('/static/.*', function(request, response){
var staticResourceName = url.parse(request.url).path.replace('/static/', '');
writeStaticAndEnd(response, staticResourceName);
})
);
http.createServer(function (request, response) {
request.setEncoding('utf8');
requestDispatcher.dispatch(request, response);
})
.listen(3001);
game.level = require('./levels').levels.warmup;
function writeStaticAndEnd(response, resource) {
response.setHeader("Content-Type", "text/html; charset=utf8");
response.write(resourceProvider.getResource(resource));
response.end();
}
function writeTextAndEnd(response, text) {
response.setHeader("Content-Type", "text/html; charset=utf8");
response.write(text);
response.end();
}