1 | var babble = require('babble');
|
2 | var eve = require('../../index');
|
3 |
|
4 | function BabbleAgent(id, props) {
|
5 |
|
6 | eve.Agent.call(this, id);
|
7 |
|
8 | this.props = props;
|
9 |
|
10 |
|
11 | this.extend('babble');
|
12 |
|
13 |
|
14 | this.listen('hi')
|
15 | .listen(function (message, context) {
|
16 | console.log(context.from + ': ' + message);
|
17 | return message;
|
18 | })
|
19 | .decide(function (message, context) {
|
20 | return (message.indexOf('age') != -1) ? 'age' : 'name';
|
21 | }, {
|
22 | 'name': babble.tell('hi, my name is ' + this.id),
|
23 | 'age': babble.tell('hi, my age is ' + this.props.age)
|
24 | });
|
25 |
|
26 |
|
27 | this.connect(eve.system.transports.getAll());
|
28 | }
|
29 |
|
30 |
|
31 | BabbleAgent.prototype = Object.create(eve.Agent.prototype);
|
32 | BabbleAgent.prototype.constructor = BabbleAgent;
|
33 |
|
34 |
|
35 | BabbleAgent.prototype.talk = function (to) {
|
36 | var name = this.id;
|
37 | var age = this.props.age;
|
38 |
|
39 | this.tell(to, 'hi')
|
40 | .tell(function (message, context) {
|
41 | if (Math.random() > 0.5) {
|
42 | return 'my name is ' + name;
|
43 | } else {
|
44 | return 'my age is ' + age;
|
45 | }
|
46 | })
|
47 | .listen(function (message, context) {
|
48 | console.log(context.from + ': ' + message);
|
49 | });
|
50 | };
|
51 |
|
52 | module.exports = BabbleAgent;
|