diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-01-29 23:49:17 -0500 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-01-29 23:49:17 -0500 |
commit | 0e2883105e948ff6f35f954f8184fdd232d17a97 (patch) | |
tree | 7764d8b8aa770ddb836cb8a2dc7c580441437969 /examples | |
parent | df56e8a4c6b05e9f2f5c98c14af3f95a7bc6926c (diff) | |
parent | 1cc310e20dda4a378aac76b49288e6c8e2361e5e (diff) |
Merge branch 'socket'
Made the socket object used by jarvis a singleton MetaSocket object to maintain
clean, modular code standards. This also means that future feature addition
will be easier because all references are to one object, of which the behavior
can be easily changed since most systems rely on EventEmitter or callback-style
structures
Diffstat (limited to 'examples')
-rw-r--r-- | examples/jarvis.js | 72 |
1 files changed, 40 insertions, 32 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js index ca58f8b..7b5c64e 100644 --- a/examples/jarvis.js +++ b/examples/jarvis.js @@ -1,5 +1,9 @@ // A bot which responds to `jarvis` with a box +// BUG: The Scheduler, Search, Claims, ident, AND Write Listener can ALL be +// used as extensions for Socket. This would make the code so much easier to +// understand + const Space = require('../space'); const Socket = require('../socket'); const ms = require('../utils/measurespace'); @@ -14,38 +18,44 @@ const wwrap = require('../utils/writewrap'); const Claims = require('../tools/claim'); //// The queue of all writes to be sent to the server @ 200chars/1000ms -var writes = new sched.Queue(1000, 200, (elems) => main.write(elems)); -var main = new Socket(); -var claims = new Claims(detectPrompt); +class MetaSocket extends Socket{ + constructor(world='', delayms=1000){ + super(world); + let self = this; + sched.Queue.call(self, 1000, 200, (elems) => self.write(elems)); + Claims.call(self); + id.call(self); + wwrap.call(self); + self.sender; + self.on('init',(send)=>{ + self.sender = send; + }); + Search.call(this); + } +} +var main = new MetaSocket(); // See ident.js for further documentation, but this basically sets up init functions -id(main, initOnce, init, deinit); -writeListen = new wwrap(main); - -var sender; -function init(send){ - sender = send; // tileUpdates require knowledge of the sender - setTimeout(()=>{writes.enable()},1000); // Would fail if a write were added because it would be within a second of cursor send - writes.enqueue(genNotif().towrite()); -} -function deinit(){ - writes.disable(); -} +main.on('init', (send)=>{ + setTimeout(()=>{main.enable()},1000); + main.enqueue(genNotif().towrite()); +}); +main.on('close', ()=>main.disable()); -function initOnce(){ +main.on('initOnce', ()=>{ timect(); main.on('tileUpdate', tileHandler); // Should only be active after the "control space" of the notification has been established -} +}); //// Management utilities // When a tile changes, processes data into usable form and runs every function in `funcs` w/ that data. // Note that functions can edit inputs function tileHandler(send, source, tiles){ - if (send == sender) return; + if (send == main.sender) return; let tiledata = maketiles(tiles); - claims.handle(send, tiledata.tilespaces, tiledata.locs); + main.handle(send, tiledata.tilespaces, tiledata.locs); } //// "userspace" functions for responses, like to tileUpdates @@ -77,14 +87,14 @@ function notifRefresh(){ let diff = newnotif.copy(); if (typeof notif !== 'undefined') diff.comb(notif, comb.sub); notif = newnotif; - if (notifClaim) claims.unclaim(notifClaim); - notifClaim = claims.claim(newnotif, protectArea); + if (notifClaim) main.unclaim(notifClaim); + notifClaim = main.claim(newnotif, protectArea); return (diff.towrite()); } function timect(){ minsUp++; - writes.enqueue(notifRefresh()); + main.enqueue(notifRefresh()); setTimeout(timect, 60*1000); } @@ -98,27 +108,25 @@ function protectArea(send, tiles, locs, id, space, ctrl){ diff.comb(tile, comb.sub); writelist.push(...diff.towrite()) } - writes.enqueue(writelist); + main.enqueue(writelist); } 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 read = new Search(); +read.match(search, respond); var expire = {}; -function detectPrompt(send, tiles, locs){ // tries to detect the prompt ('jarvis') and calls respond if found. - for (let i=0; i<locs.length; i++){ - let loc = locs[i]; +main.on('fallback', (send, tiles, locs) => { // tries to detect the prompt ('jarvis') and calls respond if found. + for (let loc of locs){ if (read.has(loc)){ clearTimeout(expire[loc]); - delete expire[loc]; read.del(loc); } - let results = read.add(loc, tiles[loc]); - if (results.length > 0) respond(results, send); + read.add(loc, tiles[loc]); expire[loc] = setTimeout(() => {read.del(loc); delete expire[loc];}, 30000); } -} +}); let response = new Space(); response.adhoc('yes, my liege'); @@ -129,7 +137,7 @@ function respond(coord, send){ console.log('called at', coord); callct += 1; response.loc = coord; - writes.enqueue(response.towrite().concat(notifRefresh())); + main.enqueue(response.towrite().concat(notifRefresh())); read.update(response); setTimeout(() => {limits.splice(limits.indexOf(send))}, 5*1000); limits.push(send); |