aboutsummaryrefslogtreecommitdiff
path: root/examples/nword.js
blob: 2a4e2ce0112676572ecf656de70d9d0645c2ec04 (plain)
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
// Replaces the n-word with "n-word"
const Socket = require('../socket');
const Space = require('../space');
const comb = require('../utils/comb');
const Search = require('../tools/search');
const Queue = require('../tools/queue');
const maketiles = require('../utils/maketiles');
const vec = require('../utils/vec');
const id = require('../utils/ident');

var main = new Socket();
id.call(main);

var search = new Search();
var ids = [];
var idcall = [];
var writeids = {};
var wq = new Queue(1000, 200, (w,max)=>{
  let globid = main.write(w);
  let id;
  while (id = ids.shift()) {
    if (id > max) break;
    if (writeids[globid]) writeids[globid].push(idcall.shift());
    else writeids[globid] = [];
  }
});
main.on('write', (acc)=>{
  for (let id in writeids)
    if (acc.indexOf(id) > -1){
      for (let it of writeids[id])
        writeids[id]();
      delete writeids[id];
    }
});

main.on('init',(send)=>{
  main.sender = send;
  wq.enable();
});

var expire = {};
main.on('tileUpdate', (send, source, tiles)=>{
  if (send === main.sender) return;
  let tiledata = maketiles(tiles);
  for (let loc of tiledata.locs) {
    let spc = tiledata.tilespaces[loc];
    if (search.has(loc)){
      clearTimeout(expire[loc]);
      search.del(loc);
    }
    search.add(loc, spc);
    expire[loc] = setTimeout(() => {search.del(loc); delete expire[loc];},
    300*1000);
  }
});

makeclean('I am racist', '[Nn] *[Ii] *[Gg] *[Gg] *([Ee] *[Rr]|[Aa])');
makeclean('I am ableist', '[Rr] *[Ee] *[Tt] *[Aa] *[Rr] *[Dd]');

let lecture = {};
function makeclean(resp, reg){
    let response = new Space().adhoc(resp);
    let regex = new RegExp(reg,'g');
    function clean(coord, send, space){
      let out;
      if (!lecture[send]){
        response.loc = coord;
        out = response;
        lecture[send] = true;
        setTimeout(() => {delete lecture[send];}, 1000);
      }else{
        out = new Space().adhoc(space.print().replace(regex,
        match=>' '.repeat(match.length))).comb(space, comb.sub);
      }
      ids.push(wq.enqueue(out.towrite()));
      idcall.push(() => {search.update(response);});
    }
    search.match([reg], clean);
}