-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk.js
119 lines (106 loc) · 2.98 KB
/
chunk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// use a dirty cache
var dirty = require('dirty');
var dirtycache = dirty('worlddata.db').on('load', function() {console.log("dirty cache is loaded")});
var Anvil=require("prismarine-provider-anvil").Anvil("1.8");
var Vec3 = require("vec3");
var anvil = new Anvil(".");
var cache = function(req, res, next){
var value = dirtycache.get(req.path);
if (value !== undefined){
res.send(value);
return;
}
next()
}
function getChunkBlocks(chunk_x, chunk_z){
var blocks = [];
return new Promise(function(resolve, reject){
var d=anvil.load(chunk_x, chunk_z);
d.then(function(data){
if (data == null){
console.log("NULL" + chunk_x + " "+ chunk_z)
resolve(null);
return
}
for(var x=0; x<16;x++){
blocks.push([]);
for(var z=0; z<16;z++){
for(var y=255;y>=0;y--){
var blockType = data.getBlockType(new Vec3(x, y, z));
// console.log(" BT" + blockType)
if(blockType !== 0){
blocks[x][z] = blockType;
break;
}
}
}
}
console.log("Got chunk " + chunk_x, chunk_z)
resolve(blocks);
}).catch(function(err){
console.log("anvil load error")
console.log(err)
console.log(err.stack);
reject(err)
});
})
}
function getRegionChunks(chunk_x, chunk_z){
return new Promise(function(resolve, reject){
var chunks = [];
var rx=Math.floor(chunk_x/32)
var rz=Math.floor(chunk_z/32)
var promises = [];
for(var x=rx;x<32;x++){
chunks.push([])
for(var z=rz;z<32;z++){
(function(x, z){
promises.push(getChunkBlocks(x, z))
})(x, z)
}
}
Promise.all(promises).then((results) => {
for(var r in results){
chunks[Math.floor(r/32)][r%32] = results[r]
}
resolve(chunks)
}).catch((err) => {
reject(err)
});
})
}
var addRoutes = function(app){
app.get('/chunks/:chunk_x/:chunk_z', cache, function (req, res) {
var x = parseInt(req.params.chunk_x);
var z = parseInt(req.params.chunk_z);
console.log("GET /chunks/" + req.params.chunk_x + "/" + req.params.chunk_z )
getChunkBlocks(x, z)
.then((result) => {
res.send(result);
dirtycache.set(req.path, result);
console.log("Updating cache for " + req.path);
}).catch((err) => {
console.log("GET error")
console.log(err)
console.log(err.stack)
})
})
app.get('/regions/:chunk_x/:chunk_z', cache, function (req, res) {
var x = parseInt(req.params.chunk_x);
var z = parseInt(req.params.chunk_z);
console.log("GET /regions/" + req.params.chunk_x + "/" + req.params.chunk_z )
getRegionChunks(x, z)
.then((result) => {
res.send(result);
dirtycache.set(req.path, result);
console.log("Updating cache for " + req.path);
}).catch((err) => {
console.log("GET error")
console.log(err)
console.log(err.stack)
})
})
};
module.exports = {
addRoutes: addRoutes
}