aboutsummaryrefslogtreecommitdiff
path: root/examples/jarvis.js
blob: 0dad87ecad017b7dee417dc7f8e9007f2124ae0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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');
const vec = require('../utils/vec');
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');
const maketiles = require('../utils/maketiles');
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);

// 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();
}

function 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;
  let tiledata = maketiles(tiles);
  claims.handle(send, tiledata.tilespaces, tiledata.locs);
}

//// "userspace" functions for responses, like to tileUpdates
const command = 'jarvis'
const sig     = 'feynmansfedora'
const notifsrc = '\
                           \n\
  For a node.js YWOT api:                    \n\
&   git.hrhr.dev/ywot-clean                  \n\
&   Try `COMMAND` today. Uptime: UPTIME.     \n\
&   Called: JARVISx ~SIGNATURE          \n\
                                '.replace('COMMAND', command).replace('SIGNATURE', sig);
var minsUp = 0;
var callct = 0;
var notifClaim;

function genNotif(){
  let newnotif = new Space();
  newnotif.loc = [-6, 20];
  text = notifsrc;
  text = text.replace('UPTIME', Math.floor(minsUp/60) + 'h' + minsUp%60 + 'm');
  text = text.replace('JARVIS', callct);
  newnotif.adhoc(text);
  return newnotif
}

function notifRefresh(){
  let newnotif = genNotif();
  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);
  return (diff.towrite());
}

function timect(){
  minsUp++;
  writes.enqueue(notifRefresh());
  setTimeout(timect, 60*1000);
}

function protectArea(send, tiles, locs, id, space, ctrl){
  for (let loc of locs){
    let tile = tiles[loc];
    // Write diffs from the tile
    let diff = space.copy(); // Acts as a window for tile (only see relevant data)
    diff.comb(tile, comb.flip(comb.unmask));
    diff.comb(tile, comb.sub);
    writes.enqueue(diff.towrite())
  }
}

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<locs.length; i++){
    let loc = locs[i];
    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);
    expire[loc] = setTimeout(() => {read.del(loc); delete expire[loc];}, 30000);
  }
}

let response = new Space();
response.adhoc('yes, my liege');

var limits = []; // an array of senders within the last 5 seconds to act as a 'rate limiter'
function respond(coord, send){
  if (limits.indexOf(send) >= 0) return;
  console.log('called at', coord);
  callct += 1;
  response.loc = coord;
  writes.enqueue(response.towrite().concat(notifRefresh()));
  read.update(response);
  setTimeout(() => {limits.splice(limits.indexOf(send))}, 5*1000);
  limits.push(send);
}