-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (87 loc) · 2.24 KB
/
index.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
var http = require('http');
var domain = "bot.notenoughmods.com";
var protocol = "http";
var versionList = "?json";
var modList = "%version%.json";
var buildingCache = false
var cacheReady = false
var modCache = {}
function fetchJson( url, callback ) {
var accumulatedData = ""
http.get(url, function(res) {
if(res.statusCode != 200) {
callback({'error': res.statusCode});
}else {
res.setEncoding('utf8');
res.on('error', function(error) {
callback({ 'error': error });
}).on('data', function(chunk) {
accumulatedData += chunk;
}).on('end', function() {
try {
var json = JSON.parse(accumulatedData);
callback(json);
}catch(e) {
callback({'error': e});
}
});
}
}).on('error', function(error) {
callback({ 'error': error });
});
}
function getVersions(callback) {
fetchJson(protocol + "://" + domain + "/" + versionList, callback);
}
function getMods(version, callback) {
fetchJson(protocol + "://" + domain + "/" + modList.replace("%version%", version),
function(data) {
if(Array.isArray(data)) {
modCache[version] = data;
}
callback(data);
});
}
function getModsCached(version, callback, forceSync) {
if(forceSync === true || typeof modCache[version] === "undefined") {
getMods(version, callback);
}else{
callback(modCache[version]);
}
}
function getModByName(version, name, callback, forceSync) {
getModsCached(version, function(data) {
if(Array.isArray(data)) {
var matches = []
for(i = 0; i < data.length; i++) {
if(data[i].name.toUpperCase() === name.toUpperCase()) {
matches.push(data[i]);
}
}
callback(matches);
}else{
callback(data);
}
}, forceSync);
}
function getModByAuthor(version, author, callback, forceSync) {
getModsCached(version, function(data) {
if(Array.isArray(data)) {
var matches = []
for(i = 0; i < data.length; i++) {
if(data[i].author.toUpperCase() === author.toUpperCase()) {
matches.push(data[i]);
}
}
callback(matches);
}else{
callback(data);
}
}, forceSync);
}
module.exports = {
'getVersions': getVersions,
'getMods': getModsCached,
'getModByName' : getModByName,
'getModByAuthor' : getModByAuthor
};