diff options
Diffstat (limited to 'examples/jarvis.js')
-rw-r--r-- | examples/jarvis.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js new file mode 100644 index 0000000..ccf1f80 --- /dev/null +++ b/examples/jarvis.js @@ -0,0 +1,43 @@ +// 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'); + +var main = new Socket(); +var read = {}; + +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 + 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); + this.emit('identity', send); + } + } +}) + +main.on('identity', (sender) => { + console.log('identity activated'); + main.on('tileUpdate', (send, source, tiles) => { + if (send == sender) return; + let locs = tilekeys(tiles); + for (let i=0; i<locs.length; i++){ + let loc = locs[i]; + if (read[loc]) clearTimeout(read[loc].timer); + read[loc] = ({ + "timer":setTimeout(()=>{delete read[dim]},30000), + "block":new Space(), + }); + read[loc].block.fromfetch(tiles, [loc[0][0], loc[0][1], loc[1][0], loc[1][1]], conform=false); + // TODO : if any pair or quadruplet of blocks are adjacent, SEARCH THEM! + } + }); +}); |