Attempted to fix remoteMap race condition, not working yet

This commit is contained in:
Danielv123 committed 2017-12-16 14:26:20 +01:00
1 parent f9db49734c
commit 853b7d1026
3 files changed
+78 -36

No files matched your search

+65 -25
View File
@@ -3,7 +3,7 @@ var mkdirpSync = require("mkdirp").sync;
var fs = require("fs"); var fs = require("fs");
var objectOps = require("./objectOps"); var objectOps = require("./objectOps");
let allowLogging = false; let allowLogging = true;
let log = string => (allowLogging)? console.log(string):"" let log = string => (allowLogging)? console.log(string):""
module.exports = class chunkStore { module.exports = class chunkStore {
@@ -12,32 +12,72 @@ module.exports = class chunkStore {
this.chunkSize = chunkSize; this.chunkSize = chunkSize;
this.path = (path[path.length] == "/") ? path : path+"/"; this.path = (path[path.length] == "/") ? path : path+"/";
mkdirpSync(path+name); mkdirpSync(path+name);
this.entityWriteQueue = [];
this.chunkWritesInProgress = {};
this.entityWriteInProgress = false;
}
checkChunkWrite(x, y){
if(!this.chunkWritesInProgress[x]) this.chunkWritesInProgress[x] = {};
if(!this.chunkWritesInProgress[x][y]){
return false
} else {
return true
}
}
setChunkWrite(x,y, bool){
if(!this.chunkWritesInProgress[x]) this.chunkWritesInProgress[x] = {};
if(!this.chunkWritesInProgress[x][y]) this.chunkWritesInProgress[x][y] = bool;
} }
setEntity(x/**{number}*/, y/**{number}*/, newEntity/**{object}*/){ setEntity(x/**{number}*/, y/**{number}*/, newEntity/**{object}*/){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var localX = x > 0 ? Math.round(x % this.chunkSize) : Math.round(x % this.chunkSize)-64; var localX = x > 0 ? Math.round(x % this.chunkSize) : Math.round(x % this.chunkSize)+64;
x = Math.floor(Math.round(x)/this.chunkSize); x = Math.floor(Math.round(x)/this.chunkSize);
var localY = y > 0 ? Math.round(y % this.chunkSize) : Math.round(x % this.chunkSize)-64; var localY = y > 0 ? Math.round(y % this.chunkSize) : Math.round(y % this.chunkSize)+64;
y = Math.floor(Math.round(y)/this.chunkSize); y = Math.floor(Math.round(y)/this.chunkSize);
console.log("localX"+localX)
this.getChunk(x,y).then(chunk => { console.log("localY"+localY)
log("Setentity"); console.log(x+ " "+y)
if(!chunk.dataObject[localX]) chunk.dataObject[localX] = {}; this.entityWriteQueue.push({x, y, localX, localY, newEntity});
if(!chunk.dataObject[localX][localY]) chunk.dataObject[localX][localY] = {}; if(this.checkChunkWrite(x,y)){
// we don't need to initiate another request for a chunk because there is one running already which should take care of it
} else {
if(newEntity && typeof newEntity == "object"){ this.setChunkWrite(x, y, true);
log("Overwriting with new entity at "+localX+", "+localY); this.getChunk(x,y).then(chunk => {
chunk.dataObject[localX][localY] = newEntity; log("Setting "+this.entityWriteQueue.length+" entities in chunk X: "+x+" Y: "+y);
} else {
log("Deleting entry at "+localX+", "+localY); // add each queued entity to the chunk
chunk.dataObject[localX][localY] = undefined; this.entityWriteQueue.forEach(queuedEntity => {
} if(queuedEntity.x == x && queuedEntity.y == y){
this.setChunk(x, y, chunk).then(()=>{ let localX = queuedEntity.localX;
log("Successfully saved chunk!"); let localY = queuedEntity.localY;
this.getChunk(x,y).then(chunk => resolve(chunk)); let newEntity = queuedEntity.newEntity;
if(!chunk.dataObject[localX]) chunk.dataObject[localX] = {};
if(!chunk.dataObject[localX][localY]) chunk.dataObject[localX][localY] = {};
if(newEntity && typeof newEntity == "object"){
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;
}
}
});
// clear the now processed queue
this.entityWriteQueue = this.entityWriteQueue.filter(entity => {
if(entity.x != x && entity.y != y) return true
});
// save the chunk and resolve the promise
this.setChunk(x, y, chunk).then(()=>{
log("Successfully saved chunk!");
//this.entityWriteInProgress = false;
this.setChunkWrite(x,y,false);
resolve(chunk);
});
}); });
}); }
}); });
} }
setChunk(x, y, chunk){ setChunk(x, y, chunk){
@@ -58,13 +98,13 @@ module.exports = class chunkStore {
dataObject: {}, dataObject: {},
position:{x,y}, // fancy ES6 feature that generates to {x:x, y:y} position:{x,y}, // fancy ES6 feature that generates to {x:x, y:y}
chunkSize:this.chunkSize, chunkSize:this.chunkSize,
get: function(x, y){ /*get: function(x, y){
if(this.dataObject[Math.round(x)] && this.dataObject[Math.round(x)][Math.round(y)]){ if(this.dataObject[Math.round(x)] && this.dataObject[Math.round(x)][Math.round(y)]){
return this.dataObject[Math.round(x)][Math.round(y)]; return this.dataObject[Math.round(x)][Math.round(y)];
} else { } else {
return undefined; return undefined;
} }
}, },*/
}; };
fs.readFile(this.path+this.name+"/"+x+"/"+y, (err, chunkFile) => { fs.readFile(this.path+this.name+"/"+x+"/"+y, (err, chunkFile) => {
@@ -75,13 +115,13 @@ module.exports = class chunkStore {
if(objectOps.isJSON(chunkFile)) { if(objectOps.isJSON(chunkFile)) {
log("returned from file") log("returned from file")
let chunk = JSON.parse(chunkFile); let chunk = JSON.parse(chunkFile);
chunk.get = function(x, y){ /*chunk.get = function(x, y){
if(this.dataObject[Math.round(x)] && this.dataObject[Math.round(x)][Math.round(y)]){ if(this.dataObject[Math.round(x)] && this.dataObject[Math.round(x)][Math.round(y)]){
return this.dataObject[Math.round(x)][Math.round(y)]; return this.dataObject[Math.round(x)][Math.round(y)];
} else { } else {
return undefined; return undefined;
} }
} }*/
resolve(chunk); resolve(chunk);
} else { } else {
log("file failed") log("file failed")
+11 -9
View File
@@ -4,8 +4,8 @@ var chunkStore = require("./chunkStore.js");
describe("chunkStore.js", () => { describe("chunkStore.js", () => {
// define some values for our test // define some values for our test
const inserterX = 14.5; const inserterX = 14.5; // rounds to 15
const inserterY = -7.5; const inserterY = -17.5; // rounds to -17, +64 would be 47
const inserterName = "inserter"; const inserterName = "inserter";
let store // make our store elevated let store // make our store elevated
@@ -20,21 +20,23 @@ describe("chunkStore.js", () => {
// nothing to do here really // nothing to do here really
}); });
}); });
it("returns items located in a chunk", () => { it("returns items located in a chunk", (done) => {
// We return the promise to mocha as it supports promises natively, works a lot better than calling done() manually. // We return the promise to mocha as it supports promises natively, works a lot better than calling done() manually.
let x = 0; let x = 0;
let y = -1 let y = -1
return store.getChunk(x,y).then(chunkData => { store.getChunk(x,y).then(chunkData => {
// console.log(chunkData); console.log(chunkData);
assert.equal(typeof chunkData, "object"); assert.equal(typeof chunkData, "object");
assert.equal(chunkData.get(inserterX, inserterY).name, inserterName); assert.equal(chunkData.dataObject[15][47].name, inserterName);
assert.equal(chunkData.position.x, x); assert.equal(chunkData.position.x, x);
assert.equal(chunkData.position.y, y); assert.equal(chunkData.position.y, y);
done();
}); });
}); });
it("deletes entities when not provided with an object as 3rd arg", () => { it("deletes entities when not provided with an object as 3rd arg", done => {
return store.setEntity(inserterX, inserterY).then((chunk)=>{ store.setEntity(inserterX, inserterY).then((chunk)=>{
assert(!chunk.get(inserterX, inserterY)); assert(!chunk.dataObject(15, 47));
done();
}); });
}); });
}); });
+2 -2
View File
@@ -2,10 +2,10 @@
{ {
"name": "remoteMap", "name": "remoteMap",
"title": "Clusterio remote map", "title": "Clusterio remote map",
"version": "1.0.0", "version": "2.0.0",
"date": "18-11-2017", "date": "18-11-2017",
"author": "Danielv123", "author": "Danielv123",
"dependencies": ["base >= 0.15"], "dependencies": ["base >= 0.15"],
"factorio_version": "0.15", "factorio_version": "0.16",
"description": "Clusterio plugin for external mapping and possibly building?" "description": "Clusterio plugin for external mapping and possibly building?"
} }