aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/helloworld.js8
-rw-r--r--examples/jarvis.js43
-rw-r--r--examples/spam.js8
3 files changed, 51 insertions, 8 deletions
diff --git a/examples/helloworld.js b/examples/helloworld.js
index cc43666..4984914 100644
--- a/examples/helloworld.js
+++ b/examples/helloworld.js
@@ -1,9 +1,9 @@
-const ywot = require('../socket'); // Import direct library
-const space = require('../space');
+const Socket = require('../socket'); // Import direct library
+const Space = require('../space');
-writes = new space.Space(); //Generate a place to store potential writes
+writes = new Space(); //Generate a place to store potential writes
writes.adhoc('hello\nworld') // A method to fill internal data with inline code.
-main = new ywot.Socket(''); // Open socket
+main = new Socket('helloworld'); // Open socket at yourworldoftext.com/helloworld
main.on('open',()=>{ // When socket opens
main.write(writes.towrite([8,16])); // Tell the server to write the content of `writes` to
});
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!
+ }
+ });
+});
diff --git a/examples/spam.js b/examples/spam.js
index 8134c76..53ac2af 100644
--- a/examples/spam.js
+++ b/examples/spam.js
@@ -1,8 +1,8 @@
// Sorry about writing this, but I want to maintain a notif about the API
-const socket = require('../socket');
-const space = require('../space');
+const Socket = require('../socket');
+const Space = require('../space');
-let text = new space.Space();
+let text = new Space();
text.adhoc("\
\n\
For a node.js YWOT api: \n\
@@ -10,7 +10,7 @@ text.adhoc("\
");
let out = text.towrite([-6,20]);
-let main = new socket.Socket();
+let main = new Socket();
function write(){
main.write(out.slice());