#61 chunkStore is getting filled/emptied on demand

This commit is contained in:
Danielv123 committed 2017-12-10 14:07:37 +01:00
1 parent b1f35c9b72
commit 8fce9a2cfe
6 files changed
+72 -62

No files matched your search

+1 -2
View File
@@ -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",
+6 -6
View File
@@ -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
+46 -44
View File
@@ -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;