-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.js
66 lines (56 loc) · 1.63 KB
/
update.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
let processData = require('./ProcessData');
let fs = require('fs');
let https = require('https');
let http = require('http');
let path = require('path');
let shards = require('./shards');
let URLS = {
portals: function(shard) {
return `https://esryok.screepspl.us/servers/main/${shard}/portals.json`;
},
rooms: function(shard) {
return `http://www.leagueofautomatednations.com/map/${shard}/rooms.js`;
},
ALLIANCES: `http://www.leagueofautomatednations.com/alliances.js`
};
function createDirIfNotExists(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath);
}
}
function download(fileUrl, destinationPath, callback) {
let file = fs.createWriteStream(destinationPath);
let secure = fileUrl.includes("https");
let method = secure ? https : http;
let request = method.get(fileUrl, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(callback);
filesDownloaded += 1;
console.log(`Finished downloading ${destinationPath}`);
console.log(filesDownloaded);
if (filesDownloaded == FILES_TO_DOWNLOAD) {
processData();
}
});
});
}
function downloadAlliancesFile() {
download(URLS.ALLIANCES, 'alliances.js')
}
function downloadFilesForShard(shard) {
createDirIfNotExists(`./${shard}`);
download(URLS.portals(shard), `./${shard}/portals.js`);
download(URLS.rooms(shard), `./${shard}/rooms.js`);
}
const FILES_TO_DOWNLOAD = 1 + shards.length * 2;
let filesDownloaded = 0;
module.exports = function() {
filesDownloaded = 0;
downloadAlliancesFile();
console.log(`Shards: ${shards}`);
shards.forEach(shard => {
console.log(shard);
downloadFilesForShard(shard);
});
};