mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-20 23:53:13 +09:00
#61 Completed chunkStore for now, time to work on filling it
This commit is contained in:
1 parent
e2a795528f
commit
cdddfe6e30
2 files changed
+59
-21
No files matched your search
+36
-15
@@ -1,8 +1,9 @@
|
|||||||
var mkdirp = require("mkdirp-promise");
|
var mkdirp = require("mkdirp-promise");
|
||||||
var mkdirpSync = require("mkdirp").sync;
|
var mkdirpSync = require("mkdirp").sync;
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
var objectOps = require("./objectOps");
|
||||||
|
|
||||||
let allowLogging = true;
|
let allowLogging = false;
|
||||||
let log = string => (allowLogging)? console.log(string):""
|
let log = string => (allowLogging)? console.log(string):""
|
||||||
|
|
||||||
module.exports = class chunkStore {
|
module.exports = class chunkStore {
|
||||||
@@ -14,12 +15,13 @@ module.exports = class chunkStore {
|
|||||||
}
|
}
|
||||||
setEntity(x, y, newEntity){
|
setEntity(x, y, newEntity){
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
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);
|
||||||
localY = Math.round(y % this.chunkSize);
|
var localY = Math.round(y % this.chunkSize);
|
||||||
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")
|
||||||
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] = {};
|
||||||
|
|
||||||
@@ -30,10 +32,11 @@ module.exports = class chunkStore {
|
|||||||
entity.name = newEntity.name;
|
entity.name = newEntity.name;
|
||||||
} else {
|
} else {
|
||||||
log("Deleting entry at "+localX+", "+localY);
|
log("Deleting entry at "+localX+", "+localY);
|
||||||
entity.name = undefined;
|
chunk.dataObject[localX][localY] = undefined;
|
||||||
}
|
}
|
||||||
this.setChunk(x, y, chunk).then(()=>{
|
this.setChunk(x, y, chunk).then(()=>{
|
||||||
log("Successfully saved chunk!");
|
log("Successfully saved chunk!");
|
||||||
|
this.getChunk(x,y).then(chunk => resolve(chunk));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -41,8 +44,8 @@ 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(y, JSON.stringify(chunk), function(){
|
fs.writeFile(this.path+this.name+"/"+x+"/"+y, JSON.stringify(chunk), function(){
|
||||||
|
resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -54,19 +57,37 @@ 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){
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
try {
|
fs.readFile(this.path+this.name+"/"+x+"/"+y, (err, chunkFile) => {
|
||||||
fs.readdir(this.path+this.name, chunkFiles => {
|
|
||||||
log(chunkFiles);
|
if(err){
|
||||||
if(chunkFiles == undefined) {
|
log(err)
|
||||||
|
resolve(chunk);
|
||||||
|
} else {
|
||||||
|
if(objectOps.isJSON(chunkFile)) {
|
||||||
|
log("returned from file")
|
||||||
|
let chunk = JSON.parse(chunkFile);
|
||||||
|
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);
|
resolve(chunk);
|
||||||
} else {
|
} else {
|
||||||
|
log("file failed")
|
||||||
|
resolve(chunk);
|
||||||
}
|
}
|
||||||
}); // add x and y values as well and we are done?
|
}
|
||||||
} catch (e){
|
}); // add x and y values as well and we are done?
|
||||||
resolve(chunk);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+23
-6
@@ -3,6 +3,11 @@ var assert = require("assert");
|
|||||||
var chunkStore = require("./chunkStore.js");
|
var chunkStore = require("./chunkStore.js");
|
||||||
|
|
||||||
describe("chunkStore.js", () => {
|
describe("chunkStore.js", () => {
|
||||||
|
// define some values for our test
|
||||||
|
const inserterX = 14.5;
|
||||||
|
const inserterY = -7.5;
|
||||||
|
const inserterName = "inserter";
|
||||||
|
|
||||||
let store // make our store elevated
|
let store // make our store elevated
|
||||||
it("is a constructor", ()=>{
|
it("is a constructor", ()=>{
|
||||||
assert.equal(typeof chunkStore, "function");
|
assert.equal(typeof chunkStore, "function");
|
||||||
@@ -10,14 +15,26 @@ describe("chunkStore.js", () => {
|
|||||||
store = new chunkStore("910239"/* instanceID, use defaults for the rest */);
|
store = new chunkStore("910239"/* instanceID, use defaults for the rest */);
|
||||||
assert.equal(store.name, "910239");
|
assert.equal(store.name, "910239");
|
||||||
});
|
});
|
||||||
it("returns items located in a chunk", done => {
|
it("allows you to store the locations of entities in a chunk divided database", () => {
|
||||||
store.getChunk(1,0).then(chunkData => {
|
return store.setEntity(inserterX, inserterY, {name:inserterName}).then(() => {
|
||||||
|
// nothing to do here really
|
||||||
|
});
|
||||||
|
});
|
||||||
|
it("returns items located in a chunk", () => {
|
||||||
|
// 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);
|
// console.log(chunkData);
|
||||||
assert.equal(typeof chunkData, "object");
|
assert.equal(typeof chunkData, "object");
|
||||||
assert(chunkData.dataArray);
|
assert.equal(chunkData.get(inserterX, inserterY).name, inserterName);
|
||||||
assert.equal(chunkData.position.x, 1);
|
assert.equal(chunkData.position.x, x);
|
||||||
assert.equal(chunkData.position.y, 0);
|
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));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user