|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +Array.prototype.forEachCallback = function(callback, finishCallback) { |
| 4 | + var current = 0; |
| 5 | + var self = this; |
| 6 | + |
| 7 | + function next() { |
| 8 | + if (!self) { |
| 9 | + console.log("Something went wrong..."); |
| 10 | + throw('No self!'); |
| 11 | + return; |
| 12 | + } |
| 13 | + if (current >= self.length) { |
| 14 | + if (finishCallback) { |
| 15 | + var cb = finishCallback.bind(self); |
| 16 | + cb(); |
| 17 | + } |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + var currentItem = self[current++]; |
| 22 | + |
| 23 | + var cb = callback.bind(currentItem); |
| 24 | + cb(currentItem, next); |
| 25 | + } |
| 26 | + |
| 27 | + next(); |
| 28 | +}; |
| 29 | + |
| 30 | +(function(exports) { |
| 31 | + var path = require('path'), |
| 32 | + fs = require('fs'), |
| 33 | + tiptoe = require('tiptoe'), |
| 34 | + http = require('http'); |
| 35 | + |
| 36 | + var _allsets = null; |
| 37 | + var MTGJSON_VERSION_URL = 'https://mtgjson.com/json/version.json'; |
| 38 | + |
| 39 | + // Constants |
| 40 | + var C = {}; |
| 41 | + |
| 42 | + C.SETS_NOT_ON_GATHERER = [ 'ATH', 'ITP', 'DKM', 'RQS', 'DPA' ]; |
| 43 | + C.STANDARD_SETS = [ 'KTK', 'FRF', 'DTK', 'ORI', 'BFZ' ]; |
| 44 | + |
| 45 | + // Check if JSON path exists |
| 46 | + fs.stat(path.join(__dirname, 'json'), function(err, stats) { if (err) fs.mkdir(path.join(__dirname, 'json')); }); |
| 47 | + |
| 48 | + function isStandardSet(set) { |
| 49 | + if (C.STANDARD_SETS.indexOf(set.code) >= 0) |
| 50 | + return(true); |
| 51 | + |
| 52 | + return(false); |
| 53 | + } |
| 54 | + |
| 55 | + /** Return server version */ |
| 56 | + function serverVersion(callback) { |
| 57 | + var req = http.request( |
| 58 | + { |
| 59 | + method: 'GET', |
| 60 | + hostname: 'mtgjson.com', |
| 61 | + port: 80, |
| 62 | + path: '/json/version.json' |
| 63 | + }, |
| 64 | + function (res) { |
| 65 | + res.on( |
| 66 | + 'data', |
| 67 | + function(chunk) { |
| 68 | + callback(null, chunk.toString()); |
| 69 | + } |
| 70 | + ); |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + req.on('error', function(e) { callback(e); }); |
| 75 | + |
| 76 | + req.end(); |
| 77 | + } |
| 78 | + |
| 79 | + /** Return local version */ |
| 80 | + function currentVersion(callback) { |
| 81 | + var jsonPath = path.join(__dirname, 'json', 'version.json'); |
| 82 | + fs.stat(jsonPath, function(err, stats) { |
| 83 | + if (err) |
| 84 | + return(setImmediate(function() { callback(null, ''); })); |
| 85 | + |
| 86 | + fs.readFile(jsonPath, 'utf8', callback); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + function grabServerContents(callback) { |
| 91 | + var allFilesPath = path.join(__dirname, 'json', 'AllSets-x.json'); |
| 92 | + var file = fs.createWriteStream(path.join(__dirname, 'json', 'allsets.tmp')); |
| 93 | + var req = http.request( |
| 94 | + { |
| 95 | + method: 'GET', |
| 96 | + hostname: 'mtgjson.com', |
| 97 | + port: 80, |
| 98 | + path: '/json/AllSets-x.json' |
| 99 | + }, |
| 100 | + function (res) { |
| 101 | + res.pipe(file); |
| 102 | + file.on('finish', function(err) { |
| 103 | + if (err) |
| 104 | + return(setImmediate(function() { callback(err); })); |
| 105 | + |
| 106 | + tiptoe( |
| 107 | + function() { |
| 108 | + var self = this; |
| 109 | + fs.unlink( |
| 110 | + allFilesPath, |
| 111 | + function(err) { |
| 112 | + // We don't care about errors. We just want the unlink to perform if the old file is there. |
| 113 | + self(); |
| 114 | + } |
| 115 | + ); |
| 116 | + }, |
| 117 | + function() { |
| 118 | + fs.rename(path.join(__dirname, 'json', 'allsets.tmp'), allFilesPath, this); |
| 119 | + }, |
| 120 | + // Update version info |
| 121 | + function() { |
| 122 | + serverVersion(this); |
| 123 | + }, |
| 124 | + function(newCurrentVersion) { |
| 125 | + fs.writeFile(path.join(__dirname, 'json', 'version.json'), newCurrentVersion, 'utf8', this); |
| 126 | + }, |
| 127 | + // Al done. |
| 128 | + function(err) { |
| 129 | + if (err) |
| 130 | + callback(err); |
| 131 | + |
| 132 | + callback(); |
| 133 | + } |
| 134 | + ); |
| 135 | + }); |
| 136 | + } |
| 137 | + ); |
| 138 | + |
| 139 | + req.on('error', function(e) { callback(e); }); |
| 140 | + req.end(); |
| 141 | + } |
| 142 | + |
| 143 | + function isValidSet(set) { |
| 144 | + if (set.isMCISet) |
| 145 | + return(false); |
| 146 | + |
| 147 | + if (C.SETS_NOT_ON_GATHERER.indexOf(set.code) >= 0) |
| 148 | + return(false); |
| 149 | + |
| 150 | + return(true); |
| 151 | + } |
| 152 | + |
| 153 | + function allsets(callback) { |
| 154 | + if (_allsets != null) { |
| 155 | + return(setImmediate(function() { callback(null, _allsets); })); |
| 156 | + } |
| 157 | + |
| 158 | + var ret = []; |
| 159 | + fs.readFile(path.join(__dirname, 'json', 'AllSets-x.json'), 'utf8', function(err, data) { |
| 160 | + if (err) |
| 161 | + callback(err); |
| 162 | + |
| 163 | + _allsets = JSON.parse(data); |
| 164 | + callback(null, _allsets); |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + function search(parameters, callback) { |
| 169 | + var setValidationFunction = isValidSet; |
| 170 | + var validSets = []; |
| 171 | + var p = []; |
| 172 | + |
| 173 | + // Check if the given set is in "validSets" array. |
| 174 | + function _setInList(set) { |
| 175 | + var ret = false; |
| 176 | + if (validSets.indexOf(set.code) >= 0) |
| 177 | + ret = true; |
| 178 | + |
| 179 | + return(ret); |
| 180 | + } |
| 181 | + |
| 182 | + var fixParameters = function(params) { |
| 183 | + var ret = {}; |
| 184 | + if (typeof(params) === 'string') { |
| 185 | + // We only have the card name. |
| 186 | + ret.name = params.toLowerCase(); |
| 187 | + } |
| 188 | + else if (typeof(params) === 'object') { |
| 189 | + // We may have more complex data. Let's parse it... |
| 190 | + var keys = Object.keys(params); |
| 191 | + var i; |
| 192 | + |
| 193 | + for (i = 0; i < keys.length; i++) { |
| 194 | + // Handle special keys... |
| 195 | + if (keys[i] === 'set') { |
| 196 | + var x = params[keys[i]]; |
| 197 | + if (typeof(x) === 'function') |
| 198 | + setValidationFunction = x; |
| 199 | + else if (typeof(x) === 'object') { |
| 200 | + if (Array.isArray(x)) { |
| 201 | + setValidationFunction = _setInList; |
| 202 | + validSets = x.map(function(k) { return(k.toUpperCase()); }); |
| 203 | + } |
| 204 | + else { |
| 205 | + console.log('ERROR: Dont know what to do with this set parameter: ' + JSON.stringify(x)); |
| 206 | + } |
| 207 | + } |
| 208 | + else if (typeof(x) === 'string') { |
| 209 | + validSets = [ x.toUpperCase() ]; |
| 210 | + setValidationFunction = _setInList; |
| 211 | + } |
| 212 | + else { |
| 213 | + console.log('ERROR: Dont know what to do with this set parameter: ' + JSON.stringify(x)); |
| 214 | + } |
| 215 | + } |
| 216 | + // All other keys goes to the search parameter. |
| 217 | + else { |
| 218 | + ret[keys[i]] = params[keys[i]]; |
| 219 | + if (typeof p[keys[i]] === 'string') |
| 220 | + ret[keys[i]] = ret[keys[i]].toLowerCase(); |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + else { |
| 225 | + // We need either a string or a object. We don't know how to deal with this. |
| 226 | + return(false); |
| 227 | + } |
| 228 | + |
| 229 | + return(ret); |
| 230 | + } |
| 231 | + |
| 232 | + var isMatch = function(card, p) { |
| 233 | + // Check if the given card is a match to the current parameters. |
| 234 | + var keys = Object.keys(p); |
| 235 | + var i; |
| 236 | + |
| 237 | + for (i = 0; i < keys.length; i++) { |
| 238 | + var cKey = keys[i]; |
| 239 | + var cValue = card[cKey]; |
| 240 | + |
| 241 | + |
| 242 | + if (cValue) { |
| 243 | + if (typeof(cValue) === 'number') { |
| 244 | + if (card[cKey] === p[cKey]) |
| 245 | + return(true); |
| 246 | + return(false); |
| 247 | + } |
| 248 | + else if (typeof(cValue) === 'string') { |
| 249 | + var re = new RegExp(p[cKey], 'gi'); |
| 250 | + if (cValue.match(re)) |
| 251 | + return(true); |
| 252 | + } |
| 253 | + else { |
| 254 | + console.error('invalid type of value:' + typeof(cValue)); |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + return(false); |
| 260 | + } |
| 261 | + |
| 262 | + var findCard = function(allsets, criteria) { |
| 263 | + var setKeys = Object.keys(allsets); |
| 264 | + var ret = []; |
| 265 | + var i, j; |
| 266 | + |
| 267 | + //console.log('looking for: ' + JSON.stringify(criteria)); |
| 268 | + |
| 269 | + // We go on each set... |
| 270 | + for (i = 0; i < setKeys.length; i++) { |
| 271 | + var currentSet = allsets[setKeys[i]]; |
| 272 | + |
| 273 | + // Continue only if the current set is valid. |
| 274 | + if (setValidationFunction(currentSet)) { |
| 275 | + |
| 276 | + // ...and each card... |
| 277 | + for (j = 0; j < currentSet.cards.length; j++) { |
| 278 | + var currentCard = currentSet.cards[j]; |
| 279 | + |
| 280 | + // Check criteria |
| 281 | + if (isMatch(currentCard, criteria)) { |
| 282 | + currentCard.set = currentSet.code; |
| 283 | + ret.push(currentCard); |
| 284 | + } |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + return(ret); |
| 290 | + } |
| 291 | + |
| 292 | + function executeSearch(err, allsets) { |
| 293 | + if (err) { |
| 294 | + console.log("Error fetching all cards!"); |
| 295 | + console.log(err); |
| 296 | + throw(err); |
| 297 | + } |
| 298 | + var i; |
| 299 | + var ret = []; |
| 300 | + var setKeys = Object.keys(allsets); |
| 301 | + |
| 302 | + // In case there is only one parameter... |
| 303 | + if (p.length === 1) { |
| 304 | + return(setImmediate(function() { |
| 305 | + callback(null, findCard(allsets, p[0])); |
| 306 | + })); |
| 307 | + } |
| 308 | + |
| 309 | + for (i = 0; i < p.length; i++) { |
| 310 | + var obj = {}; |
| 311 | + obj.query = p[i]; |
| 312 | + obj.results = findCard(allsets, p[i]); |
| 313 | + |
| 314 | + ret.push(obj); |
| 315 | + } |
| 316 | + |
| 317 | + callback(null, ret); |
| 318 | + } |
| 319 | + |
| 320 | + // Make sure parameters is an array |
| 321 | + if (Array.isArray(parameters) === false) { |
| 322 | + parameters = [ parameters ]; |
| 323 | + } |
| 324 | + |
| 325 | + // Fix every parameter |
| 326 | + for (var i = 0; i < parameters.length; i++) { |
| 327 | + p.push(fixParameters(parameters[i])); |
| 328 | + } |
| 329 | + |
| 330 | + console.log("Searching for: '" + JSON.stringify(p) + "'."); |
| 331 | + allsets(executeSearch); |
| 332 | + } |
| 333 | + |
| 334 | + exports.allsets = allsets; |
| 335 | + exports.search = search; |
| 336 | + exports.grabServerContents = grabServerContents; |
| 337 | + exports.currentVersion = currentVersion; |
| 338 | + exports.serverVersion = serverVersion; |
| 339 | +})(typeof exports === "undefined" ? window.mtgdb = {} : exports); |
0 commit comments