aboutsummaryrefslogtreecommitdiff
path: root/examples/jarvis.js
blob: 76e8aa1cd1254e6cf0acf0c2774f06908b04574e (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// A bot which responds to `jarvis` with a box

const Space = require('../space');
const Socket = require('../socket');
const tilekeys = require('../utils/tilekeys');
const ms = require('../utils/measurespace');
const vec = require('../utils/vec');
const comb = require('../utils/comb');
const Search = require('../tools/search');
const Queue = require('../tools/queue');
const ri = require('../utils/rectintersect');

//// Basic handling on open and close of the socket
var main = new Socket();

main.on('open', open);
main.on('close', deinit);

//// tileUpdates require knowledge of the prog's identity (sender)
var sender;
main.on('tileUpdate', tileHandler);

//// Configurable helpers for utilities
function init(send){
  sender = send;
  writes.enable();
}

function deinit(){
  writes.disable();
}

function initOnce(){
  timect();
}

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.once('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
function tileHandler(send, source, tiles){
  if (send == sender) return;
  let locs = tilekeys(tiles);
  let tilespaces = {};
  for (loc of locs){
    tilespaces[loc] = new Space();
    tilespaces[loc].fromfetch(tiles, [loc, loc], conform=false);
  }
  funcs.forEach(func => {
    func(send, tilespaces, locs);
  });
}

//// "userspace" functions for responses, like to tileUpdates
var writes = new Queue(1000, 200, (elems) => main.write(elems));

var command = 'jarvis'
var sig     = 'feynmansfedora'
var notifsrc = '\
                           \n\
  For a node.js YWOT api:                    \n\
&   http://github.com/feynmansfedora/ywot-clean \n\
&   Try `COMMAND` today. Uptime: UPTIME. Called: JARVISx ~SIGNATURE \n\
                                                                    '
notifsrc = notifsrc.replace('COMMAND', command).replace('SIGNATURE', sig);
var minsUp = 0;
var callct = 0;
var ctrl;
function notifRefresh(){
  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);
  let diff = newnotif.copy();
  if (typeof notif !== 'undefined') diff.comb(notif, comb.sub);
  notif = newnotif;
  ctrl = ms(notif);
  return (diff.towrite());
}

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

function protectArea(send, tiles, locs){
  let tilesize = [8, 16]; // This may be exportable to an external util/tool
  for (loc of locs){
    let tile = tiles[loc];
    let origloc = loc;
    loc = vec.tileToChar(loc);
    if (ri(ctrl, [loc, vec.add(loc, tilesize)])){
      // Write diffs from the tile
      let inter = notif.copy(); // Acts as a window for tile (only see relevant data)
      inter.comb(tile, comb.unmask);
      let diff = notif.copy(); // Actually diffs notify with the errant text
      diff.comb(inter, comb.sub);
      writes.enqueue(diff.towrite())
      // Mask the tile
      let newtile = notif.copy();
      newtile.comb(tile, comb.mask);
      tiles[origloc] = newtile;
    }
  }
}

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

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

function respond(coord){
  console.log('called at', coord);
  callct += 1;
  response.loc = coord;
  writes.enqueue(response.towrite().concat(notifRefresh()));
  read.update(response);
}