diff --git a/lib/chunkStore.js b/lib/chunkStore.js index bcfa9b25..c3d9e9ab 100644 --- a/lib/chunkStore.js +++ b/lib/chunkStore.js @@ -13,7 +13,7 @@ module.exports = class chunkStore { this.path = (path[path.length] == "/") ? path : path+"/"; mkdirpSync(path+name); } - setEntity(x, y, newEntity){ + setEntity(x/**{number}*/, y/**{number}*/, newEntity/**{object}*/){ return new Promise((resolve, reject) => { var localX = Math.round(x % this.chunkSize); x = Math.floor(Math.round(x)/this.chunkSize); @@ -21,15 +21,14 @@ module.exports = class chunkStore { y = Math.floor(Math.round(y)/this.chunkSize); this.getChunk(x,y).then(chunk => { - log("Setentity") + log("Setentity"); if(!chunk.dataObject[localX]) chunk.dataObject[localX] = {}; if(!chunk.dataObject[localX][localY]) chunk.dataObject[localX][localY] = {}; - let entity = chunk.dataObject[localX][localY] if(newEntity && typeof newEntity == "object"){ - log("Overwriting with new entity"); - entity.name = newEntity.name; + log("Overwriting with new entity at "+localX+", "+localY); + chunk.dataObject[localX][localY] = newEntity; } else { log("Deleting entry at "+localX+", "+localY); chunk.dataObject[localX][localY] = undefined; @@ -44,8 +43,10 @@ module.exports = class chunkStore { setChunk(x, y, chunk){ return new Promise((resolve, reject) => { mkdirp(this.path+this.name+"/"+x).then(() => { - fs.writeFile(this.path+this.name+"/"+x+"/"+y, JSON.stringify(chunk), function(){ - resolve(); + fs.writeFile(this.path+this.name+"/"+x+"/"+y, JSON.stringify(chunk), function(err){ + if(err) { + reject(err); + } else resolve(); }); }); }); diff --git a/master.js b/master.js index 991681e1..2d117fa5 100644 --- a/master.js +++ b/master.js @@ -610,13 +610,20 @@ server.listen(config.masterPort || 8080, function () { /* Websockets for remoteMap */ var io = require("socket.io")(server); var slaveMappers = {}; +class slaveMapper { + constructor(instanceID, socket) { + this.instanceID = instanceID; + this.socket = socket; + } + getChunk(x,y){ + + } +} io.on('connection', function (socket) { socket.emit('hello', { hello: 'world' }); socket.on('registerSlaveMapper', function (data) { console.log(data); - slaveMappers[data.instanceID].getChunk = function getChunk(x,y){ - - } + slaveMappers[data.instanceID] = new slaveMapper(data.instanceID, socket); }); }); diff --git a/package.json b/package.json index f9385fca..c9088226 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "semver": "^5.4.1", "simple-rcon": "^0.3.1", "socket.io": "^2.0.4", + "socket.io-client": "^2.0.4", "sync-request": "^4.0.1", "underscore": "^1.8.3" }, diff --git a/sharedPlugins/remoteMap/config.js b/sharedPlugins/remoteMap/config.js index ce545550..f210afc1 100644 --- a/sharedPlugins/remoteMap/config.js +++ b/sharedPlugins/remoteMap/config.js @@ -9,8 +9,7 @@ module.exports = { version: modInfo.version, // Binary entrypoint for plugin. Don't let it crash. Stdout is sent to game as server chat (run /c commands from here for interface) // Make sure its cross platform somehow. - binary: "node", - arguments: ["index.js"], + binary: "nodePackage", description: modInfo.description, // We'll send everything in this file to your stdin. Beware. scriptOutputFileSubscription: "remoteMap.txt", diff --git a/sharedPlugins/remoteMap/control.lua b/sharedPlugins/remoteMap/control.lua index 30574a6b..80465964 100644 --- a/sharedPlugins/remoteMap/control.lua +++ b/sharedPlugins/remoteMap/control.lua @@ -63,8 +63,8 @@ function writeout_objects(surface, area) end end table.insert(lines,line) - write_file(header..table.concat(lines,"").."\n") - + --write_file(header..table.concat(lines,"").."\n") + --write_file(table.concat(lines,"").."\n") line=nil end @@ -74,8 +74,8 @@ function on_some_entity_created(event) complain("wtf, on_some_entity_created has nil entity") return end - - writeout_objects(ent.surface, {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}}) + write_file(ent.name.." "..ent.position.x.." "..ent.position.y) + --writeout_objects(ent.surface, {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}}) complain("on_some_entity_created: "..ent.name.." at "..ent.position.x..","..ent.position.y) end @@ -91,8 +91,8 @@ function on_some_entity_deleted(event) local surface = ent.surface local area = {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}} - table.insert(todo_next_tick, function () writeout_objects(surface, area ) end) - + --table.insert(todo_next_tick, function () writeout_objects(surface, area ) end) + write_file("deleted "..ent.position.x.." "..ent.position.y) complain("on_some_entity_deleted: "..ent.name.." at "..ent.position.x..","..ent.position.y) end diff --git a/sharedPlugins/remoteMap/index.js b/sharedPlugins/remoteMap/index.js index 181c1962..1620bec3 100644 --- a/sharedPlugins/remoteMap/index.js +++ b/sharedPlugins/remoteMap/index.js @@ -1,57 +1,59 @@ const needle = require("needle"); -const io = require("socket.io"); +const ioClient = require("socket.io-client"); const chunkStore = require("./../../lib/chunkStore.js"); const moduleConfig = require("./config"); // not to be confused with clusterio config. This config is private to this plugin. -console.log("/silent-command game.print('"+moduleConfig.name+" version "+moduleConfig.version+" enabled')"); -// initialize chunk database -const chunkMap = new chunkStore(config.unique, 64, "./chunkStore/"+config.unique); - -/*setInterval(function(){ - console.log("/silent-command game.write_file('"+moduleConfig.scriptOutputFileSubscription+"', game.tick, true, 0)"); -},1000);*/ - -var config = {}; -process.stdin.setEncoding('utf8'); -process.stdin.on('readable', () => { - var chunk = process.stdin.read(); - if (chunk !== null && !isNaN(chunk) && !chunk.includes("objects")){ - console.log(chunk); - throw chunk; - // objects -29,-35;-28,-34: ,inserter -28.5 -34.5 +class remoteMap { + constructor(slaveConfig, messageInterface){ + this.config = slaveConfig; + this.messageInterface = messageInterface; - let data = chunk.split(","); - data.shift();data.shift();data.shift(); - if(data && data[0] == undefined){ - // this position is now empty, delete whatever was there from DB - console.log("empty: ") - console.log(chunk) - } - data.forEach(entity => { - let name = entity.split(" ")[0]; - let xPos = entity.split(" ")[1]; - let yPos = entity.split(" ")[2]; - if(xPos && yPos && name){ - chunkMap.setEntity(xPos, yPos, name).then(chunk => { - console.log("Added "+name+" to chunk "+chunk.position.x+", "+chunk.position.y); - }); - } - }); - } else { - // config is the same config as client.js and master.js uses - config = JSON.parse(chunk); + // initialize chunk database + this.chunkMap = new chunkStore(this.config.unique, 64, "./database/chunkStore/"); // set up websocket communication and handle requests from web interface users (which are going through master) // socket should be a global - socket = io.connect("http://"+config.masterIP+":"+config.masterPort); - socket.on("hello", data => { - socket.emit("registerSlaveMapper", {instanceID: config.unique}); + this.socket = ioClient("http://"+this.config.masterIP+":"+this.config.masterPort); + this.socket.on("hello", data => { + this.socket.emit("registerSlaveMapper", {instanceID: this.config.unique}); }); + this.socket.on("getChunk", req => { + + }); + + this.messageInterface("/silent-command game.print('"+moduleConfig.name+" version "+moduleConfig.version+" enabled')"); } -}); -process.stdin.on('end', () => { - process.stdout.write('end'); -}); + scriptOutput(data){ + if(data){ + this.messageInterface(data); + // express-transport-belt -35.5 -14.5 + + data = data.split(" "); + if(data && data[0] == undefined){ + // this position is now empty, delete whatever was there from DB + console.log("empty: "); + console.log(data); + return + } + let name = data[0]; + let xPos = data[1]; + let yPos = data[2]; + if(xPos && yPos && name){ + if(name == "deleted"){ + this.chunkMap.setEntity(xPos, yPos, "delete this entity").then(data => { + this.messageInterface("Deleted data "+data.position.x+", "+data.position.y); + }).catch((err)=>messageInterface(err)); + } else { + this.chunkMap.setEntity(xPos, yPos, {name}).then(data => { + this.messageInterface("Added "+name+" to data "+data.position.x+", "+data.position.y); + }).catch((err)=>messageInterface(err)); + } + } + + } + } +} +module.exports = remoteMap;