mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
#61 start of map store/retrieve, communication channel using websockets
This commit is contained in:
1 parent
2bcacb2513
commit
7ece1af073
9 files changed
+422
-4
No files matched your search
@@ -0,0 +1,72 @@
|
||||
var mkdirp = require("mkdirp-promise");
|
||||
var mkdirpSync = require("mkdirp").sync;
|
||||
var fs = require("fs");
|
||||
|
||||
let allowLogging = true;
|
||||
let log = string => (allowLogging)? console.log(string):""
|
||||
|
||||
module.exports = class chunkStore {
|
||||
constructor(name, chunkSize = 32, path = "./chunkStore/"){
|
||||
this.name = name;
|
||||
this.chunkSize = chunkSize;
|
||||
this.path = (path[path.length] == "/") ? path : path+"/";
|
||||
mkdirpSync(path+name);
|
||||
}
|
||||
setEntity(x, y, newEntity){
|
||||
return new Promise((resolve, reject) => {
|
||||
localX = Math.round(x % this.chunkSize);
|
||||
x = Math.floor(Math.round(x)/this.chunkSize);
|
||||
localY = Math.round(y % this.chunkSize);
|
||||
y = Math.floor(Math.round(y)/this.chunkSize);
|
||||
|
||||
this.getChunk(x,y).then(chunk => {
|
||||
if(!chunk.dataObject[localX]) chunk.dataObject[localX] = {};
|
||||
if(!chunk.dataObject[localX][localY]) chunk.dataObject[localX][localY] = {};
|
||||
|
||||
let entity = chunk.dataObject[localX][localY]
|
||||
|
||||
if(newEntity && typeof newEntity == "object"){
|
||||
log("Overwriting with new entity");
|
||||
entity.name = newEntity.name;
|
||||
} else {
|
||||
log("Deleting entry at "+localX+", "+localY);
|
||||
entity.name = undefined;
|
||||
}
|
||||
this.setChunk(x, y, chunk).then(()=>{
|
||||
log("Successfully saved chunk!");
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
setChunk(x, y, chunk){
|
||||
return new Promise((resolve, reject) => {
|
||||
mkdirp(this.path+this.name+"/"+x).then(() => {
|
||||
fs.writeFile(y, JSON.stringify(chunk), function(){
|
||||
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
getChunk(x, y){
|
||||
return new Promise((resolve, reject) => {
|
||||
// make new chunk in case we can't find an existing one
|
||||
let chunk = {
|
||||
dataObject: {},
|
||||
position:{x,y}, // fancy ES6 feature that generates to {x:x, y:y}
|
||||
chunkSize:this.chunkSize,
|
||||
};
|
||||
try {
|
||||
fs.readdir(this.path+this.name, chunkFiles => {
|
||||
log(chunkFiles);
|
||||
if(chunkFiles == undefined) {
|
||||
resolve(chunk);
|
||||
} else {
|
||||
|
||||
}
|
||||
}); // add x and y values as well and we are done?
|
||||
} catch (e){
|
||||
resolve(chunk);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
var assert = require("assert");
|
||||
|
||||
var chunkStore = require("./chunkStore.js");
|
||||
|
||||
describe("chunkStore.js", () => {
|
||||
let store // make our store elevated
|
||||
it("is a constructor", ()=>{
|
||||
assert.equal(typeof chunkStore, "function");
|
||||
// create a new store and elevate it
|
||||
store = new chunkStore("910239"/* instanceID, use defaults for the rest */);
|
||||
assert.equal(store.name, "910239");
|
||||
});
|
||||
it("returns items located in a chunk", done => {
|
||||
store.getChunk(1,0).then(chunkData => {
|
||||
// console.log(chunkData);
|
||||
assert.equal(typeof chunkData, "object");
|
||||
assert(chunkData.dataArray);
|
||||
assert.equal(chunkData.position.x, 1);
|
||||
assert.equal(chunkData.position.y, 0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user