// A bot which responds to `jarvis` with a box const Space = require('../space'); const Socket = require('../socket'); const tilekeys = require('../utils/tilekeys'); const ms = require('../utils/measurespace'); const vec = require('../utils/vec'); const comb = require('../utils/comb'); const Search = require('../tools/search'); const Queue = require('../tools/queue'); const ri = require('../utils/rectintersect'); var main = new Socket(); main.on('open', ()=>{ // Tries to identify itself with a cursor movement console.log('socket opened'); let coords = [Math.floor(Math.random()*100000+16),Math.floor(Math.random()*100000+16)]; main.cursor(coords); main.on('cursor',detect); function detect(pos, send){ if (vec.equals(pos[0],coords)){ main.off('cursor', detect); identity(send); } } }); main.on('close', ()=>{ writes.disable() }); var sender; // Global variable that holds identity()'s `send` for tileHandler var initialized = false; function identity(send){ sender = send; console.log('identity activated'); if (! initialized){ initialized = true; initOnce(); } init(); main.on('tileUpdate', tileHandler); } function init(){ writes.enable(); } function initOnce(){ timect(); } function tileHandler(send, source, tiles){ if (send == sender) return; let locs = tilekeys(tiles); let tilespaces = {}; for (let i=0; i { func(send, tilespaces, locs); }); } notifsrc = '\ \n\ For a node.js YWOT api: \n\ & http://github.com/feynmansfedora/ywot-clean \n\ & Try `jarvis` today. Uptime: UPTIME \n\ ' var minsUp = 0; var ctrl; function notifRefresh(){ let newnotif = new Space(); newnotif.loc = [-6, 20]; newnotif.adhoc(notifsrc.replace('UPTIME', Math.floor(minsUp/60) + 'h' + minsUp%60 + 'm')); let diff = newnotif.copy(); if (typeof notif !== 'undefined') diff.comb(notif, comb.sub); notif = newnotif; writes.enqueue(...diff.towrite()); ctrl = ms(notif); } function timect(){ minsUp++; notifRefresh(); setTimeout(timect, 60*1000); } function protectArea(send, tiles, locs){ let tilesize = [8, 16]; // This may be exportable to an external util/tool for (loc of locs){ let tile = tiles[loc]; let origloc = loc; loc = vec.tileToChar(loc); if (ri(ctrl, [loc, vec.add(loc, tilesize)])){ // Write diffs from the tile let inter = notif.copy(); // Acts as a window for tile (only see relevant data) inter.comb(tile, comb.unmask); let diff = notif.copy(); // Actually diffs notify with the errant text diff.comb(inter, comb.sub); writes.enqueue(...diff.towrite()) // Mask the tile let newtile = notif.copy(); newtile.comb(tile, comb.mask); tiles[origloc] = newtile; } } } var search = new Space(); search.adhoc('jarvis'); // The search space is the word jarvis, so whenever that's caught, a relevant function can be called. var read = new Search(search); var expire = {}; function detectPrompt(send, tiles, locs){ // tries to detect the prompt ('jarvis') and calls respond if found. for (let i=0; i 0) respond(results); expire[loc] = setTimeout(() => {read.del(loc)}, 30000); } } let response = new Space(); response.adhoc('yes, my liege'); let writes = new Queue(1000, 200, (elems) => main.write(elems)); function respond(coord){ console.log('called at', coord); response.loc = coord; writes.enqueue(...response.towrite()); read.update(response); }