aboutsummaryrefslogtreecommitdiff
path: root/examples/clear.js
blob: 68557f503cfa93d45742a9362a7ba0d5dbd1c58b (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
// Demonstrates large-scale changes using tools/queue.js; pastes a character (once) over a large area

// This demo puts a bunch of z's on yourworldoftext.com/zworld (scroll up because it's a big area)
const Socket = require('../socket');
const Space = require('../space');
const Queue = require('../tools/queue');
const comb = require('../utils/comb');
const vec = require('../utils/vec');

let fetchdim = [ [-30,-30], [30,30] ];
let chr = ' ';

let chrdim = [ vec.tileToChar(fetchdim[0]),
            vec.sub(vec.tileToChar(vec.add(fetchdim[1],[1,1])),[1,1]) ];
    // vec.add(,[1,1]) because to the maximum chr in a block is [7,15]
    // higher than the minimun chr in a block.
let size = vec.sub(chrdim[1], chrdim[0]);

console.log('deletion from', chrdim[0], 'to', chrdim[1], 'will occur');

let reqs = [];
for (let i = fetchdim[0][0]; i <= fetchdim[1][0]; i++){
  reqs.push([[i, fetchdim[0][1]], [i, fetchdim[1][1]]]);
}
// This assumes a maximum width of <=1000 by splitting the fetch into
// many rows, each one block tall (this is required by the API).

// Constructs copyspace, what will be written to the page (this can be
// anything, and the repeated chr can be built upon using Space.comb).
// Transparency is also possible but this is also not demonstrated here.
let row = chr.repeat(size[0])+'\n';
let tot = row.repeat(size[1]);
let copyspace = new Space().adhoc(tot);
copyspace.loc = vec.dot(fetchdim[0],[8,16]);

let main = new Socket();
let writect = 0;
let sends;
let writes = new Queue(1000, 200, w => {
  main.write(w);
  writect += 200;
  if (writect > sends){ console.log('done!'); process.exit();}
  console.log(writect + '/' + sends)});

main.on('open',()=>{
  writes.enable(); // disabling on socket close is not handled, but
                   // should be in a longer script
  main.fetch(reqs);
  console.log('enabled');
});

main.on('fetch',(tiles)=>{
  let fetchspace = new Space().fromfetch(tiles,fetchdim);
  copyspace.comb(fetchspace,comb.sub);
  let exit = copyspace.towrite();
  sends = exit.length;
  writes.enqueue(exit);
});