aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--examples/helloworld.js8
-rw-r--r--examples/jarvis.js43
-rw-r--r--examples/spam.js8
-rw-r--r--socket.js5
-rw-r--r--space.js5
-rw-r--r--tests/socket_cursor.js4
-rw-r--r--tests/socket_fetch.js4
-rw-r--r--tests/space_comb.js6
-rw-r--r--tests/space_file.js4
-rw-r--r--tests/space_fromfetch.js4
-rw-r--r--tests/space_search.js6
-rw-r--r--tests/space_subsection.js4
-rw-r--r--utils/getdims.js15
14 files changed, 88 insertions, 30 deletions
diff --git a/README.md b/README.md
index 158aed2..88f1cfe 100644
--- a/README.md
+++ b/README.md
@@ -18,4 +18,4 @@ Currently, `socket.js` provides `Socket` which has a few signals and functions (
`space.js` provides some dense spatial data management with `Space`, which is really good for specific dialogs and tests, but there may be some more work to be done to create a sparse storage device.
-`examples/helloworld.js` can get you started. Everything in that should work pretty well, but the remainder of the repo is probably pretty buggy until I test it.
+`examples/helloworld.js` can get you started. Everything in the core API (`utils`, `socket.js`, `space.js`) should be pretty stable, but please don't hesitate to report a bug.
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..19f9651
--- /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');
+
+var main = new Socket();
+
+function Sparse(){ // A "sparse" data-storage mechanism
+ this.data = [];
+ this.avail = [];
+ this.add = function(data){
+
+ }
+ this.del = function(id){
+ this.avail.push(id);
+ this.data[id] = null;
+ }
+}
+
+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', sender);
+ }
+ }
+})
+
+main.on('identity', (sender) => {
+ main.on('tileUpdate', (send, source, tiles) => {
+ if (send == sender) return;
+ // Get dimensions somehow
+
+ })
+})
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());
diff --git a/socket.js b/socket.js
index 6d725a8..f2502e8 100644
--- a/socket.js
+++ b/socket.js
@@ -27,7 +27,7 @@ class retryws extends EventEmitter{ // a wrapper on ws that retries on failure
class Socket extends retryws {
constructor(world='') { // Takes the name of the world, which can be an empty string.
- let loc = (world == '') ? '' : `/${world}`;
+ let loc = (world == '') ? '' : `${world}/`;
let addr = `wss://www.yourworldoftext.com/${loc}ws/`;
super(addr);
this.on('message', (msg)=>{
@@ -91,5 +91,4 @@ class Socket extends retryws {
}
}
-exports.retryws = retryws;
-exports.Socket = Socket;
+module.exports = Socket;
diff --git a/space.js b/space.js
index 9ffcd18..63b916e 100644
--- a/space.js
+++ b/space.js
@@ -30,7 +30,8 @@ function Space(){ // CLASS
for (let line=0; line<8; line++)
this.data.push([]); // Adds lines
for (let x=dimension[1]; x<=dimension[3]; x++){
- tilein(tiles[[y,x]],y-dimension[0]);
+ if (! tiles[[y,x]]) tilein({"content":' '.repeat(16*8),"properties":{"cell_props":{}}}); // Insert a null tile
+ else tilein(tiles[[y,x]],y-dimension[0]);
}
}
@@ -123,4 +124,4 @@ function Space(){ // CLASS
}
-exports.Space = Space
+module.exports = Space
diff --git a/tests/socket_cursor.js b/tests/socket_cursor.js
index dc0ca7f..e9e4df7 100644
--- a/tests/socket_cursor.js
+++ b/tests/socket_cursor.js
@@ -1,8 +1,8 @@
// Tests Socket.cursor and Socket.on('cursor')
-socket = require('../socket');
+Socket = require('../socket');
-world = new socket.Socket();
+world = new Socket();
world.on('open', ()=>{world.cursor([22,23])});
world.on('cursor', (locs, send)=>{console.log(locs,send)});
diff --git a/tests/socket_fetch.js b/tests/socket_fetch.js
index a613dc6..b558f89 100644
--- a/tests/socket_fetch.js
+++ b/tests/socket_fetch.js
@@ -1,7 +1,7 @@
// Tests Socket.fetch and Socket.on('fetch')
-const socket = require('../socket');
+const Socket = require('../socket');
-let world = new socket.Socket();
+let world = new Socket();
world.on('open', () => {world.fetch([[10,10,10,10]])});
diff --git a/tests/space_comb.js b/tests/space_comb.js
index 3fae916..1cde721 100644
--- a/tests/space_comb.js
+++ b/tests/space_comb.js
@@ -1,15 +1,15 @@
// Tests space.comb()
-space = require('../space');
+const Space = require('../space');
-newspace = new space.Space();
+newspace = new Space();
newspace.adhoc('\
line1\n\
&&&&transparency&&&&\n\
\n\
afterempty\n')
-otherspace = new space.Space();
+otherspace = new Space();
otherspace.adhoc('\
&&&transparency&&&\n\
testline');
diff --git a/tests/space_file.js b/tests/space_file.js
index 84e42a3..572214e 100644
--- a/tests/space_file.js
+++ b/tests/space_file.js
@@ -1,7 +1,7 @@
// Tests Space.{to,from}file
-const space = require('../space')
+const Space = require('../space')
-text = new space.Space();
+text = new Space();
text.adhoc('\
line of text\n\
diff --git a/tests/space_fromfetch.js b/tests/space_fromfetch.js
index 2d8572a..67b41cc 100644
--- a/tests/space_fromfetch.js
+++ b/tests/space_fromfetch.js
@@ -1,7 +1,7 @@
// Tets Space.fromfetch
-const space = require('../space')
+const Space = require('../space')
-newspace = new space.Space()
+newspace = new Space()
newspace.fromfetch({"4,2": {"content": " hhhhhhhh k hone ns ", "properties": {"writability": null, "cell_props": {}}}},[4,2,4,2],conform=false);
// Straight from a real tileUpdate
diff --git a/tests/space_search.js b/tests/space_search.js
index fffdcc7..14ec174 100644
--- a/tests/space_search.js
+++ b/tests/space_search.js
@@ -1,8 +1,8 @@
// Tests space.search
-const space = require('../space')
+const Space = require('../space')
-let text = new space.Space();
+let text = new Space();
text.adhoc('\
\n\
jarvis\n\
@@ -10,7 +10,7 @@ text.adhoc('\
jarvis \
');
-let line = new space.Space();
+let line = new Space();
line.adhoc('jarvis');
console.log(text.search(line))
diff --git a/tests/space_subsection.js b/tests/space_subsection.js
index a2523e5..b30b7e1 100644
--- a/tests/space_subsection.js
+++ b/tests/space_subsection.js
@@ -1,7 +1,7 @@
// Tests Space.subsection()
-const space = require('../space');
+const Space = require('../space');
-newspace = new space.Space();
+newspace = new Space();
newspace.adhoc('\
line\n\
\n\
diff --git a/utils/getdims.js b/utils/getdims.js
new file mode 100644
index 0000000..2c18964
--- /dev/null
+++ b/utils/getdims.js
@@ -0,0 +1,15 @@
+// Takes a list of coordinate pairs (like the keys from a fetch block) and returns a dimension
+
+function dims(coords){
+ ext = [[Infinity, Infinity], [-Infinity, -Infinity]]
+ for (let i=0; i<coords.length; i++){
+ let coord = coords[i];
+ if (coord[0] < ext[0][0]) ext[0][0] = coord[0];
+ if (coord[1] < ext[0][1]) ext[0][1] = coord[1];
+ if (coord[0] > ext[1][0]) ext[1][0] = coord[0];
+ if (coord[1] > ext[1][1]) ext[1][1] = coord[1];
+ }
+ return ext;
+}
+
+exports = dims