From 853b7d1026215000026ed7f23144e3fbcb525a0a Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 16 Dec 2017 14:26:20 +0100 Subject: [PATCH] Attempted to fix remoteMap race condition, not working yet --- lib/chunkStore.js | 90 ++++++++++++++++++++++--------- lib/chunkStore.spec.js | 20 +++---- sharedPlugins/remoteMap/info.json | 4 +- 3 files changed, 78 insertions(+), 36 deletions(-) diff --git a/lib/chunkStore.js b/lib/chunkStore.js index add6ed2e..d53eae87 100644 --- a/lib/chunkStore.js +++ b/lib/chunkStore.js @@ -3,7 +3,7 @@ var mkdirpSync = require("mkdirp").sync; var fs = require("fs"); var objectOps = require("./objectOps"); -let allowLogging = false; +let allowLogging = true; let log = string => (allowLogging)? console.log(string):"" module.exports = class chunkStore { @@ -12,32 +12,72 @@ module.exports = class chunkStore { this.chunkSize = chunkSize; this.path = (path[path.length] == "/") ? path : path+"/"; 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}*/){ 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); - 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); - - this.getChunk(x,y).then(chunk => { - log("Setentity"); - 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; - } - this.setChunk(x, y, chunk).then(()=>{ - log("Successfully saved chunk!"); - this.getChunk(x,y).then(chunk => resolve(chunk)); + console.log("localX"+localX) + console.log("localY"+localY) + console.log(x+ " "+y) + this.entityWriteQueue.push({x, y, localX, localY, newEntity}); + 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 { + this.setChunkWrite(x, y, true); + this.getChunk(x,y).then(chunk => { + log("Setting "+this.entityWriteQueue.length+" entities in chunk X: "+x+" Y: "+y); + + // add each queued entity to the chunk + this.entityWriteQueue.forEach(queuedEntity => { + if(queuedEntity.x == x && queuedEntity.y == y){ + let localX = queuedEntity.localX; + let localY = queuedEntity.localY; + 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){ @@ -58,13 +98,13 @@ module.exports = class chunkStore { dataObject: {}, position:{x,y}, // fancy ES6 feature that generates to {x:x, y:y} 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)]){ return this.dataObject[Math.round(x)][Math.round(y)]; } else { return undefined; } - }, + },*/ }; fs.readFile(this.path+this.name+"/"+x+"/"+y, (err, chunkFile) => { @@ -75,13 +115,13 @@ module.exports = class chunkStore { if(objectOps.isJSON(chunkFile)) { log("returned from file") 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)]){ return this.dataObject[Math.round(x)][Math.round(y)]; } else { return undefined; } - } + }*/ resolve(chunk); } else { log("file failed") diff --git a/lib/chunkStore.spec.js b/lib/chunkStore.spec.js index 75ca332b..00747b8c 100644 --- a/lib/chunkStore.spec.js +++ b/lib/chunkStore.spec.js @@ -4,8 +4,8 @@ var chunkStore = require("./chunkStore.js"); describe("chunkStore.js", () => { // define some values for our test - const inserterX = 14.5; - const inserterY = -7.5; + const inserterX = 14.5; // rounds to 15 + const inserterY = -17.5; // rounds to -17, +64 would be 47 const inserterName = "inserter"; let store // make our store elevated @@ -20,21 +20,23 @@ describe("chunkStore.js", () => { // 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. let x = 0; let y = -1 - return store.getChunk(x,y).then(chunkData => { - // console.log(chunkData); + store.getChunk(x,y).then(chunkData => { + console.log(chunkData); 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.y, y); + done(); }); }); - it("deletes entities when not provided with an object as 3rd arg", () => { - return store.setEntity(inserterX, inserterY).then((chunk)=>{ - assert(!chunk.get(inserterX, inserterY)); + it("deletes entities when not provided with an object as 3rd arg", done => { + store.setEntity(inserterX, inserterY).then((chunk)=>{ + assert(!chunk.dataObject(15, 47)); + done(); }); }); }); \ No newline at end of file diff --git a/sharedPlugins/remoteMap/info.json b/sharedPlugins/remoteMap/info.json index 04d30151..ce55594f 100644 --- a/sharedPlugins/remoteMap/info.json +++ b/sharedPlugins/remoteMap/info.json @@ -2,10 +2,10 @@ { "name": "remoteMap", "title": "Clusterio remote map", -"version": "1.0.0", +"version": "2.0.0", "date": "18-11-2017", "author": "Danielv123", "dependencies": ["base >= 0.15"], -"factorio_version": "0.15", +"factorio_version": "0.16", "description": "Clusterio plugin for external mapping and possibly building?" }