File size: 1,136 Bytes
36ba3ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class Observation {
    constructor(bot) {
        if (new.target === Observation) {
            throw new TypeError(
                "Cannot instantiate abstract class Observation"
            );
        }

        this.bot = bot;
        this.name = "Observation";
    }

    observe() {
        throw new TypeError("Method 'observe()' must be implemented.");
    }

    reset() {}
}

function inject(bot, obs_list) {
    bot.obsList = [];
    bot.cumulativeObs = [];
    bot.eventMemory = {};
    obs_list.forEach((obs) => {
        bot.obsList.push(new obs(bot));
    });
    bot.event = function (event_name) {
        let result = {};
        bot.obsList.forEach((obs) => {
            if (obs.name.startsWith("on") && obs.name !== event_name) {
                return;
            }
            result[obs.name] = obs.observe();
        });
        bot.cumulativeObs.push([event_name, result]);
    };
    bot.observe = function () {
        bot.event("observe");
        const result = bot.cumulativeObs;
        bot.cumulativeObs = [];
        return JSON.stringify(result);
    };
}

module.exports = { Observation, inject };