// A bot which responds to `jarvis` with a box const Space = require('../space'); const Socket = require('../socket'); const getdims = require('../utils/getdims'); const tilekeys = require('../utils/tilekeys'); const Search = require('../tools/search'); var main = new Socket(); function equals(arg1,arg2){ // Just takes the specific case argument of each being an int pair return arg1[0] == arg2[0] && arg1[1] == arg2[1] } 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 (equals(pos[0],coords)){ main.off('cursor', detect); identity(send); } } }) function identity(sender){ console.log('identity activated'); 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 = {}; main.on('tileUpdate', (send, source, tiles) => { // if (send == sender) return; ::: this may lead to issues later, but for now is used to count its own updates let locs = tilekeys(tiles); 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 queue = []; // Must be expanded to handle actual use cases let open = true; function dequeue(){ if (queue.length == 0) open = true; else{ open = false; main.write(queue.shift()) // Make more general (possibly a function pointer or a conf variable) setTimeout(dequeue, 1000); } } function respond(coord){ console.log('called at', coord); response.loc = coord; queue.push(response.towrite()); if (open) dequeue(); }