From eba982836c719487b851053ae1182a42c6768e4a Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 10 Dec 2017 20:55:21 +0100 Subject: [PATCH] #61 Remote map chunk storage and equesting working, waiting for renderer --- master.js | 30 +++++++++++++++++++++++++++--- sharedPlugins/remoteMap/index.js | 5 ++++- static/remoteMap.js | 10 +++++++++- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/master.js b/master.js index 2d117fa5..13b5327b 100644 --- a/master.js +++ b/master.js @@ -614,16 +614,40 @@ class slaveMapper { constructor(instanceID, socket) { this.instanceID = instanceID; this.socket = socket; - } - getChunk(x,y){ + this.socket.on("sendChunk", function(data){ + mapRequesters[data.requesterID].socket.emit("displayChunk", data) + }); + } +} +var mapRequesters = {}; +class mapRequester { + constructor(requesterID, socket, instanceID){ + this.requesterID = requesterID; + this.socket = socket; + this.instanceID = instanceID; + + this.socket.on("requestChunk", loc => { + loc.requesterID = this.requesterID; + let instanceID = loc.instanceID || this.instanceID; + if(slaveMappers[instanceID] && typeof loc.x == "number" && typeof loc.y == "number"){ + slaveMappers[instanceID].socket.emit("getChunk", loc) + } + }); } } io.on('connection', function (socket) { socket.emit('hello', { hello: 'world' }); socket.on('registerSlaveMapper', function (data) { - console.log(data); slaveMappers[data.instanceID] = new slaveMapper(data.instanceID, socket); + console.log("SOCKET registered map provider for "+data.instanceID); + }); + socket.on('registerMapRequester', function(data){ + // data {instanceID:""} + let requesterID = Math.random().toString(); + mapRequesters[requesterID] = new mapRequester(requesterID, socket, data.instanceID); + + console.log("SOCKET registered map requester for "+data.instanceID); }); }); diff --git a/sharedPlugins/remoteMap/index.js b/sharedPlugins/remoteMap/index.js index 1620bec3..50b5b03a 100644 --- a/sharedPlugins/remoteMap/index.js +++ b/sharedPlugins/remoteMap/index.js @@ -21,7 +21,10 @@ class remoteMap { this.socket.emit("registerSlaveMapper", {instanceID: this.config.unique}); }); this.socket.on("getChunk", req => { - + this.chunkMap.getChunk(req.x, req.y).then(chunk => { + chunk.requesterID = req.requesterID; + this.socket.emit("sendChunk", chunk); + }); }); this.messageInterface("/silent-command game.print('"+moduleConfig.name+" version "+moduleConfig.version+" enabled')"); diff --git a/static/remoteMap.js b/static/remoteMap.js index 01649aea..93f8c08b 100644 --- a/static/remoteMap.js +++ b/static/remoteMap.js @@ -1,5 +1,13 @@ var socket = io.connect('http://localhost:8080'); socket.on('hello', function (data) { console.log(data); - socket.emit('registerSlaveMappper', { instanceID: 'data' }); + socket.emit("registerMapRequester", {instanceID: '1611985668'}); + + socket.on("displayChunk", function(chunk){ + console.log(chunk) + }); }); + +function requestChunk(x,y){ + socket.emit('requestChunk', {x:x, y:y, instanceID: '1611985668'}); +} \ No newline at end of file