File size: 1,724 Bytes
309ec79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';
class BotMemory {
  constructor(store) {
    this._store = store;
    this.known = new Map();
    this._greetCD = new Map();
  }

  async load() {
    try {
      const d = await this._store.read('memory.json');
      if (d && d.players) {
        for (const [k, v] of Object.entries(d.players)) this.known.set(k, v);
      }
    } catch (_) {}
  }

  async save() {
    try {
      const obj = { players: {} };
      for (const [k, v] of this.known) obj.players[k] = v;
      await this._store.write('memory.json', obj);
    } catch (_) {}
  }

  see(name) {
    if (this.known.has(name)) {
      this.known.get(name).lastSeen = Date.now();
    } else {
      this.known.set(name, { firstSeen: Date.now(), greetCount: 0, lastSeen: Date.now(), actions: [] });
    }
  }

  isKnown(name) { return this.known.has(name); }

  canGreet(name) {
    const l = this._greetCD.get(name) || 0;
    if (Date.now() - l < 30000) return false;
    this._greetCD.set(name, Date.now());
    return true;
  }

  markGreeted(name) {
    const p = this.known.get(name);
    if (p) p.greetCount++;
  }

  recordAction(name, action) {
    const p = this.known.get(name);
    if (p) {
      p.actions = p.actions || [];
      p.actions.push({ a: action, t: Date.now() });
      if (p.actions.length > 20) p.actions.shift();
    }
  }

  knownCount() { return this.known.size; }

  rememberInteraction(name, type) {
    const p = this.known.get(name);
    if (!p) return;
    p.interactions = p.interactions || [];
    p.interactions.push({ type, time: Date.now() });
    if (p.interactions.length > 50) p.interactions.shift();
  }

  getPlayerProfile(name) { return this.known.get(name) || null; }
}
module.exports = { BotMemory };