-
Notifications
You must be signed in to change notification settings - Fork 2
/
masterClient.js
63 lines (52 loc) · 1.79 KB
/
masterClient.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
var http = require("http");
var url = require("url");
http.createServer(function (request, response) {
request.setEncoding("utf-8");
var body = '';
request.on('data', function (data) { body += data; });
request.on('end', function () {
var question = body.replace('question=', '');
handleQuestion(question, response);
});
})
.listen(3002);
var handlers = [];
handleQuestion = function(question, response) {
console.log(question);
handlers.forEach(function(handler){
if (handler.canHandle(question)) {
response.write(handler.getAnswer(question));
response.end();
return;
}
});
response.end();
};
handlers[handlers.length] = { // suma simple
canHandle: function(question) { return /¿Cuánto es \d+ más \d+ ?/.test(question); },
getAnswer: function(question) {
var splitted = question.split(' ');
var result = parseInt(splitted[2]) + parseInt(splitted[4]);
return "" + result;
}
};
handlers[handlers.length] = { // metros a pulgadas
canHandle: function(question) { return /¿Cuántas pulgadas son \d+.\d+ metros?/.test(question); },
getAnswer: function(question) {
var splitted = question.split(' ');
var result = parseFloat(splitted[3]) * 39.3700;
return "" + result;
}
};
handlers[handlers.length] = { // la palabra mágica
canHandle: function(question) { return /¿Cuál es la http:\/\/...\/palabramagica?/.test(question); },
getAnswer: function(question) { return 'abracadabra'; }
};
handlers[handlers.length] = { // la palabra mágica
canHandle: function(question) { return /Enumere al menos 3 tipos de frutos secos/.test(question); },
getAnswer: function(question) { return 'Almendras, Anacardos, Avellanas'; }
};
handlers[handlers.length] = { // default
canHandle: function(question) { return true; },
getAnswer: function(question) { return "default"; }
};