diff --git a/server/classes/lib/Game.js b/server/classes/lib/Game.js index 87896c5..ab56b06 100644 --- a/server/classes/lib/Game.js +++ b/server/classes/lib/Game.js @@ -4,15 +4,16 @@ var tools = { string: require(__base + 'server/tools/lib/string') }; -var CACHE_KEYS = 'id sc sid ref img url name size'.split(' '); +var CACHE_KEYS = 'id sc sid ref ori img url name size'.split(' '); /** * Game instance * @param {object} config * @param {HTMLHandler} item + * @param {string} referer * @constructor */ -function Game(config, item, url) { +function Game(config, item, referer) { var self = this; self.id = ''; @@ -78,13 +79,14 @@ function Game(config, item, url) { } -function extractData(config, item, url) { +function extractData(config, item, referer) { var self = this; self.id = tools.string.guid(); self.sc = config.sourceId; self.sid = config.systemId; - self.ref = url; + self.ref = referer; + self.ori = config.url; // classic image tag if (config.pg_games.img) { diff --git a/server/classes/lib/Source.js b/server/classes/lib/Source.js index 8e1b238..2cbcf5a 100644 --- a/server/classes/lib/Source.js +++ b/server/classes/lib/Source.js @@ -63,6 +63,8 @@ function Source(sourcePath) { this.games = new classes.GameList(); this.engine = new classes.Engine(this.config.origin, this.config.headers); + + this.stack = Promise.resolve(); } util.inherits(Source, EventEmitter); @@ -183,6 +185,9 @@ Source.prototype.crawl = function (systemId) { self._saveCache(systemId); }) .catch(function (err) { + self.crawling[systemId] = false; + self.emit('crawling', self.crawling); + self.emit('server-error', {error: err.toString()}); console.log(err); }); @@ -197,20 +202,19 @@ Source.prototype.download = function (jsonGame) { var self = this; var game = self.games.retrieve(jsonGame); - if (!game.download.start()) { - return ; - } - var config = this .getSystemConfigs(game.sid) .filter(function (systemConfig) { - return game.ref.indexOf(engine.completeURL(systemConfig.url)) === 0; + return game.ori === systemConfig.url; }) .pop(); if (!config) { // should never happen - game.download.end(false); + return ; + } + + if (!game.download.start()) { return ; } @@ -225,41 +229,58 @@ Source.prototype.download = function (jsonGame) { engine.on(progressEventName, progress); - var tasks = []; - var files = []; - var filename, tmpfile; + function start() { + var filename, tmpfile; + var tasks = []; + var files = []; + + if (config.pg_game) { + tasks = Array.isArray(config.pg_game) ? config.pg_game.slice() : [config.pg_game]; + } + + self.emit('started', {game: game}); - if (config.pg_game) { - tasks = Array.isArray(config.pg_game) ? config.pg_game.slice() : [config.pg_game]; + return self._download(game, progressEventName, tasks) + .then(function (response) { + engine.removeListener(progressEventName, progress); + if (response) { + filename = response.filename(); + return tools.fs.saveToTmpFile(response.body, filename); + } + }) + .then(function (_tmpfile) { + tmpfile = _tmpfile; + return tools.systems.get(game.sid).handleFile(tmpfile, filename, files); + }) + .then(function (renamed) { + game.download.end(true); + self.emit('complete', {game: game, files: files}); + if (!renamed) { + return tools.fs.unlink(tmpfile); + } + }) + .catch(function (err) { + game.download.end(false); + self.emit('server-error', {error: err.toString()}); + console.log(err); + console.log(err.stack); + }); } - - this._download(game, progressEventName, tasks) - .then(function (response) { - engine.removeListener(progressEventName, progress); - if (response) { - filename = response.filename(); - return tools.fs.saveToTmpFile(response.body, filename); - } - }) - .then(function (_tmpfile) { - tmpfile = _tmpfile; - return tools.systems.get(game.sid).handleFile(tmpfile, filename, files); - }) - .then(function (renamed) { - game.download.end(true); - self.emit('complete', { - game: game, - files: files + // If current web ressource required not to be a pig by downloading only one file in same time, we stack the promise + if (self.config.wait) { + self.stack = self.stack + .then(start) + .then(function () { + return new Promise(function (resolve) { + var duration = typeof self.config.wait === 'function' ? self.config.wait(game) : self.config.wait; + self.emit('pause', {duration: duration}); + setTimeout(resolve, duration); + }); }); - if (!renamed) { - return tools.fs.unlink(tmpfile); - } - }) - .catch(function (err) { - game.download.end(false); - console.log(err); - }); + } else { + start(); + } }; @@ -314,6 +335,9 @@ Source.prototype._download = function (game, progressEventName, tasks) { if (!query.method) { return Promise.reject('Unknown method'); } + if (!query.url) { + return Promise.reject('Download URL is missing'); + } if (tasks.length) { return handle(engine.send(query, {referer: game.ref}));