-
Notifications
You must be signed in to change notification settings - Fork 24
/
achieve.js
161 lines (144 loc) · 4.44 KB
/
achieve.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
let ce = require('cloneextend');
let parser = require("./grammar/grammar").parser;
let bot, tasks, parameterized_alias, alias, master, stringTo, giveUser;
function init(_tasks, _giveUser, _parameterized_alias, _alias, _stringTo, _bot, _master) // ou passer simplement task...
{
master = _master;
bot = _bot;
tasks = _tasks;
parameterized_alias = _parameterized_alias;
alias = _alias;
stringTo = _stringTo;
giveUser = _giveUser;
}
function parsedTaskToString(parsedTask) {
function arrayToString(a) {
return "[" + a.map(taskToString).join() + "]";
}
function taskToString(t) {
if (t.constructor === Array) return arrayToString(t);
if (t.constructor === String) return '"' + t + '"';
}
return (taskToString(parsedTask));
}
function reportEndOfTask(parsedTask, done) {
return function () {
console.log("I achieved task " + parsedTaskToString(parsedTask));
done();
};
}
function reportFailOfTask(parsedTask, done) {
return function () {
console.log("I failed task " + parsedTaskToString(parsedTask));
};
}
function applyAction(task, username, parsedTask, done) {
stringTo.stringTo(task.p, parsedTask[0] === "look at" ? 1 : null, username, function (pars) {
task.f.apply(this, pars.concat(giveUser.indexOf(parsedTask[0]) !== -1 ? [username] : []).concat([function (result) {
if (result != null && !result) {
applyAction(task, username, parsedTask, done);
} else {
if (result) reportFailOfTask(parsedTask)(); else reportEndOfTask(parsedTask, done)()
}
}]));
});
}
function nameToTask(parsedTask, username, done) {
replaceParameterizedAlias(parsedTask, username, function (parsedTask) {
let task;
let pars;
if (parsedTask[0] in tasks) {
pars = ce.clone(parsedTask[1]);
task = {f: ce.clone(tasks[parsedTask[0]]), p: pars};
done(task, parsedTask);
}
else done(null, parsedTask);
});
}
function achieve(parsedTask, username, done) {
try {
nameToTask(parsedTask, username, function (task, parsedTask) {
if (task === null) {
console.log("Cannot find " + parsedTaskToString(parsedTask));
done(true);// think how to use this...
return;
}
console.log("I'm going to achieve task " + parsedTaskToString(parsedTask));
setImmediate(function () {
applyAction(task, username, parsedTask, done)
});
});
}
catch (error) {
console.log(error.stack);
done(true);
}
}
function listAux(taskNameList, i, username, done) {
if (i < taskNameList.length) setImmediate(function () {
achieve(taskNameList[i][1], username, (function (taskNameList, i, username, done) {
return function () {
setImmediate(function () {
listAux(taskNameList, i + 1, username, done);
});
};
})(taskNameList, i, username, done))
});
else setImmediate(done);
}
function achieveList(taskNameList, username, done) {
listAux(taskNameList, 0, username, done);
}
//reecriture (systeme suppose confluent et fortement terminal)
function replaceAlias(message) {
let changed = 1;
while (changed) {
changed = 0;
for (let alia in alias) {
const newM = message.replace(alia, alias[alia]);
if (newM !== message) {
message = newM;
changed = 1;
}
}
}
return message;
}
function replaceParameterizedAlias(parsedMessage, username, done) {
if (parsedMessage[0] in parameterized_alias) {
let pars = ce.clone(parsedMessage[1]); // how can I use stringTo ? (removing parameterized alias ?) seems like I don't want to use it
pars = pars.map(function (par) {
return par[1];
});
pars.push(username);
pars.push(function (replaced) {
replaceParameterizedAlias(parse(replaced), username, done);
});
parameterized_alias[parsedMessage[0]].apply(this, pars);
// console.log(pars);
}
else done(parsedMessage);
}
function parse(message) {
return parser.parse(replaceAlias(message));
}
function processMessage(message, username, done) {
if (username !== bot.username && (username === master || master === undefined)) {
console.log(message);
let parsedMessage;
try {
parsedMessage = parse(message);
}
catch (error) {
console.log(error.stack);
return;
}
if (parsedMessage[0] in tasks || parsedMessage[0] in parameterized_alias) achieve(parsedMessage, username, done);
}
}
module.exports = {
init: init,
processMessage: processMessage,
achieve: achieve,
achieveList: achieveList
};