From 48829de68df9f45786f2cd8e4d48c3500656322b Mon Sep 17 00:00:00 2001 From: Matthieu Dehon <42576124+GreepTheSheep@users.noreply.github.com> Date: Sun, 27 Mar 2022 21:05:31 +0200 Subject: [PATCH] Remove fetch map leaderboard from Manager and add on TMMap --- src/managers/MapManager.js | 9 ------ src/structures/TMMap.js | 59 ++++++++++++++------------------------ 2 files changed, 21 insertions(+), 47 deletions(-) diff --git a/src/managers/MapManager.js b/src/managers/MapManager.js index 2142891..7bf349b 100644 --- a/src/managers/MapManager.js +++ b/src/managers/MapManager.js @@ -52,15 +52,6 @@ class MapManager{ const map = this.client.options.api.paths.tmio.tabs.map; const res = await this.client._apiReq(`${new ReqUtil(this.client).tmioAPIURL}/${map}/${mapUid}`); - // Get map leaderboard - try { - const leaderboard = this.client.options.api.paths.tmio.tabs.leaderboard, - leaderboardRes = await this.client._apiReq(`${new ReqUtil(this.client).tmioAPIURL}/${leaderboard}/${map}/${mapUid}?offset=0&length=100`); - res["leaderboard"] = leaderboardRes; - } catch (e) { - this.client.emit('error', e); - } - const theMap = new TMMap(this.client, res); if (cache) { res._cachedTimestamp = Date.now(); diff --git a/src/structures/TMMap.js b/src/structures/TMMap.js index dca3a22..a67fc70 100644 --- a/src/structures/TMMap.js +++ b/src/structures/TMMap.js @@ -27,14 +27,12 @@ class TMMap { */ this.medalTimes = new TMMapMedalTimes(this); + /** + * The map cached leaderboard data. You should use the leaderboardLoadMore() the first time to load the leaderboard. + * @type {Array} + */ + this.leaderboard = []; - // Check if the exchange data is already fetched - if (!this._data.exchange){ - const tmxurl = this.client.options.api.paths.tmx; - this.client._apiReq(`${tmxurl.protocol}://${tmxurl.host}/${tmxurl.api}/${tmxurl.tabs.mapInfo}/${this.exchangeId}`).then(data => { - this._data.exchange = data[0]; - }); - } } /** @@ -185,40 +183,25 @@ class TMMap { } /** - * The map leaderboard. - * @type {?Array} - */ - get leaderboard() { - if (this._data.leaderboard && this._data.leaderboard.tops.length >= 1) { - const arr = []; - for (let i = 0; i < this._data.leaderboard.tops.length; i++) { - arr.push(new TMMapLeaderboard(this, this._data.leaderboard.tops[i])); - } - return arr; - } else return null; - } - - /** - * Load 100 more results in the leaderboard. + * Load more results in the leaderboard. + * @param {number} [nbOfResults=100] The number of results to load. (max 100) * @returns {Promise>} */ - async leaderboardLoadMore(){ - if (this._data.leaderboard && this._data.leaderboard.tops.length >= 1) { - - const leaderboard = this.client.options.api.paths.tmio.tabs.leaderboard, - map = this.client.options.api.paths.tmio.tabs.map; - const leaderboardRes = await this.client._apiReq(`${new ReqUtil(this.client).tmioAPIURL}/${leaderboard}/${map}/${this.uid}?offset=${this._data.leaderboard.tops.length}&length=100`); - if (leaderboardRes.tops != null){ - for (let i = 0; i < leaderboardRes.tops.length; i++){ - this._data.leaderboard.tops.push(leaderboardRes.tops[i]); - } - } - const arr = []; - for (let i = 0; i < this._data.leaderboard.tops.length; i++) { - arr.push(new TMMapLeaderboard(this, this._data.leaderboard.tops[i])); + async leaderboardLoadMore(nbOfResults = 100) { + if (nbOfResults > 100) nbOfResults = 100; + if (nbOfResults < 1) nbOfResults = 1; + const leaderboard = this.client.options.api.paths.tmio.tabs.leaderboard, + map = this.client.options.api.paths.tmio.tabs.map, + params = new URLSearchParams(); + params.append('offset', this.leaderboard.length); + params.append('length', nbOfResults); + const leaderboardRes = await this.client._apiReq(`${new ReqUtil(this.client).tmioAPIURL}/${leaderboard}/${map}/${this.uid}?${params.toString()}`); + if (leaderboardRes.tops != null){ + for (let i = 0; i < leaderboardRes.tops.length; i++) { + this.leaderboard.push(new TMMapLeaderboard(this, leaderboardRes.tops[i])); } - return arr; - } else return null; + } + return this.leaderboard; } /**