mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
#61 chunkStore is getting filled/emptied on demand
This commit is contained in:
1 parent
b1f35c9b72
commit
8fce9a2cfe
6 files changed
+72
-62
No files matched your search
+8
-7
@@ -13,7 +13,7 @@ module.exports = class chunkStore {
|
|||||||
this.path = (path[path.length] == "/") ? path : path+"/";
|
this.path = (path[path.length] == "/") ? path : path+"/";
|
||||||
mkdirpSync(path+name);
|
mkdirpSync(path+name);
|
||||||
}
|
}
|
||||||
setEntity(x, y, newEntity){
|
setEntity(x/**{number}*/, y/**{number}*/, newEntity/**{object}*/){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
var localX = Math.round(x % this.chunkSize);
|
var localX = Math.round(x % this.chunkSize);
|
||||||
x = Math.floor(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);
|
y = Math.floor(Math.round(y)/this.chunkSize);
|
||||||
|
|
||||||
this.getChunk(x,y).then(chunk => {
|
this.getChunk(x,y).then(chunk => {
|
||||||
log("Setentity")
|
log("Setentity");
|
||||||
if(!chunk.dataObject[localX]) chunk.dataObject[localX] = {};
|
if(!chunk.dataObject[localX]) chunk.dataObject[localX] = {};
|
||||||
if(!chunk.dataObject[localX][localY]) chunk.dataObject[localX][localY] = {};
|
if(!chunk.dataObject[localX][localY]) chunk.dataObject[localX][localY] = {};
|
||||||
|
|
||||||
let entity = chunk.dataObject[localX][localY]
|
|
||||||
|
|
||||||
if(newEntity && typeof newEntity == "object"){
|
if(newEntity && typeof newEntity == "object"){
|
||||||
log("Overwriting with new entity");
|
log("Overwriting with new entity at "+localX+", "+localY);
|
||||||
entity.name = newEntity.name;
|
chunk.dataObject[localX][localY] = newEntity;
|
||||||
} else {
|
} else {
|
||||||
log("Deleting entry at "+localX+", "+localY);
|
log("Deleting entry at "+localX+", "+localY);
|
||||||
chunk.dataObject[localX][localY] = undefined;
|
chunk.dataObject[localX][localY] = undefined;
|
||||||
@@ -44,8 +43,10 @@ module.exports = class chunkStore {
|
|||||||
setChunk(x, y, chunk){
|
setChunk(x, y, chunk){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
mkdirp(this.path+this.name+"/"+x).then(() => {
|
mkdirp(this.path+this.name+"/"+x).then(() => {
|
||||||
fs.writeFile(this.path+this.name+"/"+x+"/"+y, JSON.stringify(chunk), function(){
|
fs.writeFile(this.path+this.name+"/"+x+"/"+y, JSON.stringify(chunk), function(err){
|
||||||
resolve();
|
if(err) {
|
||||||
|
reject(err);
|
||||||
|
} else resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -610,13 +610,20 @@ server.listen(config.masterPort || 8080, function () {
|
|||||||
/* Websockets for remoteMap */
|
/* Websockets for remoteMap */
|
||||||
var io = require("socket.io")(server);
|
var io = require("socket.io")(server);
|
||||||
var slaveMappers = {};
|
var slaveMappers = {};
|
||||||
|
class slaveMapper {
|
||||||
|
constructor(instanceID, socket) {
|
||||||
|
this.instanceID = instanceID;
|
||||||
|
this.socket = socket;
|
||||||
|
}
|
||||||
|
getChunk(x,y){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
io.on('connection', function (socket) {
|
io.on('connection', function (socket) {
|
||||||
socket.emit('hello', { hello: 'world' });
|
socket.emit('hello', { hello: 'world' });
|
||||||
socket.on('registerSlaveMapper', function (data) {
|
socket.on('registerSlaveMapper', function (data) {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
slaveMappers[data.instanceID].getChunk = function getChunk(x,y){
|
slaveMappers[data.instanceID] = new slaveMapper(data.instanceID, socket);
|
||||||
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -53,6 +53,7 @@
|
|||||||
"semver": "^5.4.1",
|
"semver": "^5.4.1",
|
||||||
"simple-rcon": "^0.3.1",
|
"simple-rcon": "^0.3.1",
|
||||||
"socket.io": "^2.0.4",
|
"socket.io": "^2.0.4",
|
||||||
|
"socket.io-client": "^2.0.4",
|
||||||
"sync-request": "^4.0.1",
|
"sync-request": "^4.0.1",
|
||||||
"underscore": "^1.8.3"
|
"underscore": "^1.8.3"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ module.exports = {
|
|||||||
version: modInfo.version,
|
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)
|
// 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.
|
// Make sure its cross platform somehow.
|
||||||
binary: "node",
|
binary: "nodePackage",
|
||||||
arguments: ["index.js"],
|
|
||||||
description: modInfo.description,
|
description: modInfo.description,
|
||||||
// We'll send everything in this file to your stdin. Beware.
|
// We'll send everything in this file to your stdin. Beware.
|
||||||
scriptOutputFileSubscription: "remoteMap.txt",
|
scriptOutputFileSubscription: "remoteMap.txt",
|
||||||
|
|||||||
@@ -63,8 +63,8 @@ function writeout_objects(surface, area)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.insert(lines,line)
|
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
|
line=nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -74,8 +74,8 @@ function on_some_entity_created(event)
|
|||||||
complain("wtf, on_some_entity_created has nil entity")
|
complain("wtf, on_some_entity_created has nil entity")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
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}})
|
--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)
|
complain("on_some_entity_created: "..ent.name.." at "..ent.position.x..","..ent.position.y)
|
||||||
end
|
end
|
||||||
@@ -91,8 +91,8 @@ function on_some_entity_deleted(event)
|
|||||||
local surface = ent.surface
|
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}}
|
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)
|
complain("on_some_entity_deleted: "..ent.name.." at "..ent.position.x..","..ent.position.y)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,57 +1,59 @@
|
|||||||
const needle = require("needle");
|
const needle = require("needle");
|
||||||
const io = require("socket.io");
|
const ioClient = require("socket.io-client");
|
||||||
|
|
||||||
const chunkStore = require("./../../lib/chunkStore.js");
|
const chunkStore = require("./../../lib/chunkStore.js");
|
||||||
|
|
||||||
const moduleConfig = require("./config"); // not to be confused with clusterio config. This config is private to this plugin.
|
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
|
class remoteMap {
|
||||||
const chunkMap = new chunkStore(config.unique, 64, "./chunkStore/"+config.unique);
|
constructor(slaveConfig, messageInterface){
|
||||||
|
this.config = slaveConfig;
|
||||||
/*setInterval(function(){
|
this.messageInterface = messageInterface;
|
||||||
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
|
|
||||||
|
|
||||||
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 {
|
// initialize chunk database
|
||||||
// config is the same config as client.js and master.js uses
|
this.chunkMap = new chunkStore(this.config.unique, 64, "./database/chunkStore/");
|
||||||
config = JSON.parse(chunk);
|
|
||||||
|
|
||||||
// set up websocket communication and handle requests from web interface users (which are going through master)
|
// set up websocket communication and handle requests from web interface users (which are going through master)
|
||||||
// socket should be a global
|
// socket should be a global
|
||||||
socket = io.connect("http://"+config.masterIP+":"+config.masterPort);
|
this.socket = ioClient("http://"+this.config.masterIP+":"+this.config.masterPort);
|
||||||
socket.on("hello", data => {
|
this.socket.on("hello", data => {
|
||||||
socket.emit("registerSlaveMapper", {instanceID: config.unique});
|
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')");
|
||||||
}
|
}
|
||||||
});
|
scriptOutput(data){
|
||||||
process.stdin.on('end', () => {
|
if(data){
|
||||||
process.stdout.write('end');
|
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;
|
||||||
Reference in new issue
Block a user