-
Notifications
You must be signed in to change notification settings - Fork 1
/
watcher.js
64 lines (54 loc) · 1.56 KB
/
watcher.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
var region = require('./reader')
var toPng = require('./to-png')
var regionList = require('./regionlist')
var async = require('async')
var Path = require('path')
var glob = require('glob')
var fs = require('fs')
var zlib = require('zlib')
module.exports = function(regionPath) {
var timer
var modifiedTimes = {}
function processRegions() {
regionList(regionPath, function(err, regions) {
async.eachLimit(regions, 4, cacheRegion, function() {
console.log('All Regions Processed')
timer = setTimeout(processRegions, 5*60*1000)
})
})
}
function cacheRegion(coords, callback) {
console.log('cache region', coords)
var x = coords.x
var z = coords.z
var path = Path.join(regionPath, 'r.'+x+'.'+z+'.mca')
fs.stat(path, function(err, stats) {
var mtime = modifiedTimes[x+'-'+z]
if (mtime && stats.mtime <= mtime) {
return callback()
}
modifiedTimes[x+'-'+z] = mtime
region(path, function(err, data) {
async.eachSeries(['topo', 'elevation', 'biome'], function(mode, callback) {
var dest = Path.join(__dirname, 'cache', 'r.'+x+'.'+z+'-'+mode+'.png')
toPng(data, dest, mode, callback)
}, callback)
})
})
}
function removeExistingCache() {
var files = glob.sync(Path.join('cache', '*.png'))
files.forEach(function(file) {
fs.unlinkSync(Path.join(__dirname, file))
})
}
// removeExistingCache()
return {
start: function() {
processRegions()
},
stop: function() {
clearTimeout(timer)
}
}
}