aboutsummaryrefslogtreecommitdiff
path: root/examples/nword.js
blob: 0ada6f803c5de664e2db6e5531854c6a06d8470a (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
80
81
82
83
84
// 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] = [idcall.shift()];
  }
});
main.on('write', (acc)=>{
  for (let id in writeids)
    if (acc.indexOf(parseInt(id)) > -1){
      for (let it of writeids[id])
        it();
      delete writeids[id];
    }
});

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

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, send);
    expire[loc] = setTimeout(() => {search.del(loc); delete expire[loc];},
    300*1000);
  }
});

makeclean('','BL[A\u0410][C\u0421]K N[I\u0406]GG[E\u0415]RS');
makeclean('I am racist', '[Nn]+ *[1Ii]+ *[Gg]+ *[Gg]+ *([3Ee]+ *[Rr]+|[4@Aa]+)');
makeclean('I am ableist', '[Rr]+ *[3Ee]+ *[Tt]+ *[4@Aa]+ *[Rr]+ *[Dd]+');

let dolecture = false;
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 = new Space().adhoc(space.print().replace(regex,
      match=>' '.repeat(match.length)));
      out.loc = space.loc;
      if (dolecture && !lecture[send]){
        response.loc = coord;
        out.comb(response, comb.flip(comb.add));
        lecture[send] = true;
        setTimeout(() => {delete lecture[send];}, 10000);
      }
      out.comb(space, comb.sub);
      ids.push(wq.enqueue(out.towrite()));
      idcall.push(() => {search.update(out);});
    }
    search.match([reg], clean);
}