aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md7
-rw-r--r--examples/helloworld.js7
-rw-r--r--socket.js40
3 files changed, 39 insertions, 15 deletions
diff --git a/README.md b/README.md
index e69de29..34d58c3 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1,7 @@
+# ywot-clean
+
+A reconstructed version of `ywot-bot` built off of clean programming principles and a minimal, but fast API.
+
+# Features
+
+Currently, `socket.js` provides `Socket` which has a few signals and functions (documented in the code, of course) which allow abstracted interfacing with YWOT. However, certain facilities available in the old version like queuing or fetch division were scrapped in favor of more direct limits due to the unmaintainability of the old style.
diff --git a/examples/helloworld.js b/examples/helloworld.js
new file mode 100644
index 0000000..ec8ca3b
--- /dev/null
+++ b/examples/helloworld.js
@@ -0,0 +1,7 @@
+const ywot = require('../socket'); // Import direct library
+
+main = new ywot.Socket(''); // Open socket
+main.on('open',()=>{ // When socket opens
+ main.write([[[1,1],[0,0],'!']]); // Tell the server to put an exclamation mark at the topleft of the tile bottomright of the motd
+});
+
diff --git a/socket.js b/socket.js
index 4a030f5..0e84222 100644
--- a/socket.js
+++ b/socket.js
@@ -1,44 +1,50 @@
/* socket.js, a simple wrapper for a YWOT websocket connection */
+const ws = require('ws')
+const EventEmitter = require('events');
class retryws extends EventEmitter{ // a wrapper on ws that retries on failure
constructor(addr) {
+ super();
let sock;
- this.sockdown = true;
+ let sockdown = true;
this.send = (message) => {
+ console.log(message);
if (sockdown){
throw "Socket is down!";
}
sock.send(message);
}
+ let self = this;
function sockinit(){ // addr is not expected to change, so not handed as input; similarly, sock is not passed in a chain because behavior is expected to be serial (either initializing or retrying)
sock = new ws(addr);
- sock.on('open', () => { this.emit('open'); sockdown = false; });
- sock.on('close', (err) => { setTimeout(sockinit,1000); sockdown = true; this.emit('close'); });
- sock.on('error', (err) => { sock.close(); });
- sock.on('message', (message) => { this.emit('message',message); });
+ sock.on('open', () => { sockdown = false; self.emit('open'); });
+ sock.on('close', (err) => { console.log(err); setTimeout(sockinit,1000); sockdown = true; self.emit('close'); });
+ sock.on('error', (err) => { console.log(err); sock.close(); });
+ sock.on('message', (message) => { self.emit('message',message);});
}
+ sockinit();
}
}
class Socket extends retryws {
constructor(world='') { // Takes the name of the world, which can be an empty string.
- let loc = (name = '') ? '' : `/${name}`;
- let addr = `wss://www.yourworldoftext.com/${loc}ws`;
+ let loc = (world == '') ? '' : `/${world}`;
+ let addr = `wss://www.yourworldoftext.com/${loc}ws/`;
super(addr);
this.on('message', (msg)=>{
msg = JSON.parse(msg);
- switch (message.kind){
+ switch (msg.kind){
case 'write':
- this.emit('write', message.accepted, message.rejected); break; // A confirmation message for writes (accepted/rejected updates)
+ this.emit('write', msg.accepted, msg.rejected); break; // A confirmation message for writes (accepted/rejected updates)
case 'cursor':
- this.emit('cursor', message.positions, message.sender); break; // Must be used for self-identification
+ this.emit('cursor', msg.positions, msg.sender); break; // Must be used for self-identification
case 'tileUpdate':
- this.emit('tileUpdate', message.sender, message.source, message.tiles); break; // Any change by another user or possibly oneself
+ this.emit('tileUpdate', msg.sender, msg.source, msg.tiles); break; // Any change by another user or possibly oneself
case 'fetch':
- this.emit('fetch', message.tiles); break; // The response to a fetch request
+ this.emit('fetch', msg.tiles); break; // The response to a fetch request
}
- }
+ });
this.fetch = function(coords){ //coords is a list of quadruplets, each a min/max pair of y/x coordinate pairs which describes at most 1000 tiles
//Unchecked for speed
@@ -53,15 +59,19 @@ class Socket extends retryws {
throw "Too many characters to write";
}
for (var i=0; i<chars.length; i++){
+ chars[i] = chars[i].flat();
chars[i].splice(4,0,0); //
chars[i].push(i);
}
- this.send(`{"edits":${JSON.stringify(chars)},"kind":"write"}`;
+ 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.send(`"kind":"cursor","positions":[${JSON.stringify(coords)}]}`);
}
}
}
+
+exports.retryws = retryws;
+exports.Socket = Socket;