-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.js
48 lines (32 loc) · 1.2 KB
/
agent.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
examiner = require('./examiner.js');
enigmaFactory = require('./enigma.js');
enigmas = [enigmaFactory.createEnigma(6), enigmaFactory.createEnigma(9,3), enigmaFactory.createEnigma(4,7,0)];
var self = exports;
var extractName = function(url) {
return url.substring(url.lastIndexOf("/"), url.indexOf("."));
}
var missionComplete = function(question) {
return question.code !== undefined;
}
var noMoreEnigmas = function(enigmas) {
return enigmas[0] === undefined;
}
exports.getAnswers = function(question, callback) {
self.answerQuestions(question, examiner, enigmas, callback);
}
exports.answerQuestions = function(question, examiner, enigmas, callback) {
console.log("question: "+ question["question"]);
console.log("url: "+ question["reference-url"]);
console.log("result: " + question["result"]);
if (noMoreEnigmas(enigmas) || missionComplete(question)) {
callback(examiner);
return;
}
enigma = enigmas.shift();
enigma.encrypt(question["question"], function(cipher) {
console.log("cipher:" + cipher);
examiner.putAnswer(extractName(question["reference-url"]),'{"answer":"'+ cipher +'"}', function(result) {
self.answerQuestions(result,examiner, enigmas, callback);
});
});
}