diff options
Diffstat (limited to 'socket.js')
-rw-r--r-- | socket.js | 38 |
1 files changed, 28 insertions, 10 deletions
@@ -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)=>{ @@ -36,7 +36,7 @@ class Socket extends retryws { case 'write': this.emit('write', msg.accepted, msg.rejected); break; // A confirmation message for writes (accepted/rejected updates) case 'cursor': - this.emit('cursor', msg.positions, msg.sender); break; // Must be used for self-identification + this.emit('cursor', msg.positions.map(pos => [pos.tileY*8+pos.charY,pos.tileX*16+pos.charX]), msg.sender); break; // Must be used for self-identification case 'tileUpdate': this.emit('tileUpdate', msg.sender, msg.source, msg.tiles); break; // Any change by another user or possibly oneself case 'fetch': @@ -49,28 +49,46 @@ class Socket extends retryws { for (var i=0; i<coords.length; i++){ coords[i] = {"minY":coords[i][0], "minX":coords[i][1], "maxY":coords[i][2], "maxX":coords[i][3]}; } - this.send(`{"fetchRectangles":${JSON.stringify(coords)},"kind":"fetch","v":"3"}`); + this.send( JSON.stringify({ + "fetchRectangles": 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.send( JSON.stringify({ + "edits": 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( JSON.stringify({ + "kind": "cursor", + "positions": + [ + { + "tileY": Math.floor(coords[0]/8), + "tileX": Math.floor(coords[1]/16), + "charY": coords[0] % 8, + "charX": coords[1] % 16 + } + ] + })); } } } -exports.retryws = retryws; -exports.Socket = Socket; +module.exports = Socket; |