aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--socket.js9
-rw-r--r--tests/socket_cursor.js8
-rw-r--r--tests/socket_fetch.js8
3 files changed, 21 insertions, 4 deletions
diff --git a/socket.js b/socket.js
index 114949b..b690151 100644
--- a/socket.js
+++ b/socket.js
@@ -52,21 +52,22 @@ class Socket extends retryws {
this.send(`{"fetchRectangles":${JSON.stringify(coords)},"kind":"fetch","v":"3"}`);
}
- this.write = function(chars){ //chars is an list of triplets [ tile coordinate (y/x), pixel coordinate (y/x < 16), char ]
+ this.write = function(chars){ //chars is an list of pairs [ pixel coordinate (y/x < 16), char ]
if (chars.length > 200){
throw "Too many characters to write";
}
for (var i=0; i<chars.length; i++){
let char = chars[i]
- chars[i] = [char[0][0],char[0][1],char[1][0],char[1][1],char[2]];
+ let coord = char[0];
+ chars[i] = [Math.floor(coord[0]/8),Math.floor(coord[1]/16),(coord[0] % 8),(coord[1] % 16),char[1]];
chars[i].splice(4,0,0);
chars[i].push(i);
}
this.send(`{"edits":${JSON.stringify(chars)},"kind":"write"}`);
}
- this.cursor = function(coords){ //coords is just one quadruplet analagous to fetch; I think the api could handle more, but it's unnecessary for now.
- this.send(`"kind":"cursor","positions":[${JSON.stringify(coords)}]}`);
+ this.cursor = function(coords){ //coords is just one pair of char coords; I think the api could handle more, but it's unnecessary for now.
+ this.send(`{"kind":"cursor","positions":[{"tileY":${Math.floor(coords[0]/8)},"tileX":${Math.floor(coords[1]/16)},"charY":${coords[0] % 8},"charX":${coords[1] % 16}}]}`);
}
}
diff --git a/tests/socket_cursor.js b/tests/socket_cursor.js
new file mode 100644
index 0000000..dc0ca7f
--- /dev/null
+++ b/tests/socket_cursor.js
@@ -0,0 +1,8 @@
+// Tests Socket.cursor and Socket.on('cursor')
+
+socket = require('../socket');
+
+world = new socket.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
new file mode 100644
index 0000000..a613dc6
--- /dev/null
+++ b/tests/socket_fetch.js
@@ -0,0 +1,8 @@
+// Tests Socket.fetch and Socket.on('fetch')
+const socket = require('../socket');
+
+let world = new socket.Socket();
+
+world.on('open', () => {world.fetch([[10,10,10,10]])});
+
+world.on('fetch', (tiles) => {console.log(tiles);})