aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-01-18 17:26:33 -0500
committerHolden Rohrer <hr@hrhr.dev>2020-01-18 17:26:33 -0500
commit71f281b208e71014fa61c23a943b1b326272556e (patch)
treecb9887d33416208574c96e5126c93e766e82bc6b
parentc2b847bb35c14feb811b5ae07eea7e069a017f3a (diff)
jarvis externalized identify()
-rw-r--r--examples/jarvis.js27
-rw-r--r--utils/ident.js29
2 files changed, 31 insertions, 25 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js
index 2c39385..a8373ce 100644
--- a/examples/jarvis.js
+++ b/examples/jarvis.js
@@ -9,11 +9,12 @@ const comb = require('../utils/comb');
const Search = require('../tools/search');
const sched = require('../tools/schedule');
const ri = require('../utils/rectintersect');
+const id = require('../utils/ident');
//// Basic handling on open and close of the socket
var main = new Socket();
-main.on('open', open);
+id(main, initOnce, init);
main.on('close', deinit);
//// tileUpdates require knowledge of the prog's identity (sender)
@@ -37,30 +38,6 @@ function initOnce(){
var funcs = [protectArea, detectPrompt];
//// Management utilities
-// "Pings" the server with a cursor location which is then detected
-function open(){
- 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);
- }
- }
-}
-
-// identity records the identity discovered from the cursor and calls configurable init and initOnce functions
-var initialized = false; //"Initialize once" strategy
-function identity(send){
- console.log('identity activated');
- if (! initialized){
- initialized = true;
- initOnce();
- }
- init(send);
-}
// When a tile changes, processes data into usable form and runs every function in `funcs` w/ that data.
// Note that functions can edit inputs
diff --git a/utils/ident.js b/utils/ident.js
new file mode 100644
index 0000000..6d18b88
--- /dev/null
+++ b/utils/ident.js
@@ -0,0 +1,29 @@
+// A portable script to find the sender when the server websocket opens (by cursor movement)
+
+module.exports = function identify(sock, initOnce, init){
+ // `sock` should be Socket instance.
+ // initOnce and init should be functions that act like they describe; initOnce runs once per program execution and init whenever the socket turns back on
+
+ sock.on('open', ()=>{
+ // "Pings" the server with a cursor location which is detected and saved by identity()
+ let coords = [Math.floor(Math.random()*100000+16), Math.floor(Math.random()*1000000+16)];
+ sock.cursor(coords);
+ sock.on('cursor', detect);
+ function detect(pos, send){
+ if (vec.equals(pos[0],coords)){
+ sock.off('cursor', detect);
+ identity(send);
+ }
+ }
+ }
+
+ // Calls initOnce and init on successful identification.
+ let initialized = false;
+ function identity(send){
+ if (! initialized){
+ initialized = true;
+ initOnce();
+ }
+ init(send);
+ }
+}