modules and iterator logic

This commit is contained in:
BuckarooBanzay 2020-07-27 08:28:39 +02:00
parent 60545a68b8
commit f00b789a41
7 changed files with 94 additions and 67 deletions

6
src/checkchunk.js Normal file
View File

@ -0,0 +1,6 @@
module.exports = function(pos){
return new Promise(() => {
});
};

27
src/checkmapblock.js Normal file
View File

@ -0,0 +1,27 @@
const mapblockparser = require("./mapblockparser");
const executor = require("./executor");
module.exports = function(pos){
return executor(`
select data
from blocks
where posx = $1 and posy = $2 and posz = $3
`, [pos.x,pos.y,pos.z], { single_row: true })
.then(block => {
console.log(pos, block);
if (block)
return mapblockparser.parse(block.data);
else
return;
})
.then(mapblock => {
console.log(pos, mapblock);
//TODO: magic!
return {
protected: true,
generated: true
};
});
};

View File

@ -1,7 +1,5 @@
const app = require("./app");
const executor = require("./executor");
const mapblockparser = require("./mapblockparser");
const worker = require("./worker");
const port = process.env.PORT || 8080;
@ -9,67 +7,4 @@ app.listen(+port, () => {
console.log(`Listening on http://127.0.0.1:${port}`);
});
executor(`
select
count(*) as count,
min(posx) as minx,
min(posy) as miny,
min(posz) as minz,
max(posx) as maxx,
max(posy) as maxy,
max(posz) as maxz
from blocks`, [], { single_row: true })
.then(res => {
console.log(res);
let { minx, miny, minz, maxx, maxy, maxz } = res;
const pos = { x: minx, y: miny, z: minz };
function shift(pos){
pos.x++;
if (pos.x > maxx){
pos.x = minx;
pos.z++;
}
if (pos.z > maxz){
pos.z = minz;
pos.y++;
}
if (pos.y > maxy){
// done
return true;
}
return false;
}
function worker(pos){
executor(`
select data
from blocks
where posx = $1 and posy = $2 and posz = $3
`, [pos.x,pos.y,pos.z], { single_row: true })
.then(block => {
console.log(pos, block);
if (block)
return mapblockparser.parse(block.data);
else
return;
})
.then(mapblock => {
console.log(pos, mapblock);
const done = shift(pos);
if (!done){
setTimeout(function(){
worker(pos);
}, 50);
}
});
}
worker(pos);
});
worker();

26
src/iterator.js Normal file
View File

@ -0,0 +1,26 @@
module.exports = function(){
const pos = { x: -2000, y: -2000, z: -2000 };
return function(){
return new Promise(resolve => {
if (pos.z >= 2000){
pos.x++;
pos.z = -2000;
} else {
pos.z++;
}
if (pos.x >= 2000){
pos.y++;
pos.x = -2000;
}
if (pos.y > 2000){
resolve(null);
}
resolve(pos);
});
};
};

4
src/removechunk.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = function(pos){
};

4
src/removemapblock.js Normal file
View File

@ -0,0 +1,4 @@
module.exports = function(pos){
};

25
src/worker.js Normal file
View File

@ -0,0 +1,25 @@
const iterator = require("./iterator");
const checkchunk = require("./checkchunk");
const it = iterator();
function worker() {
it()
.then(pos => {
if (pos){
checkchunk(pos)
.then(result => {
//TODO: check if protected/generated
setTimeout(worker, 10);
});
} else {
//done
console.log("done!");
}
})
.catch(e => {
console.log(e);
});
}
module.exports = worker;