Skip to content

Commit

Permalink
Remove fetch map leaderboard from Manager and add on TMMap
Browse files Browse the repository at this point in the history
  • Loading branch information
GreepTheSheep committed Mar 27, 2022
1 parent 9acb2c8 commit 48829de
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 47 deletions.
9 changes: 0 additions & 9 deletions src/managers/MapManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
59 changes: 21 additions & 38 deletions src/structures/TMMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<TMMapLeaderboard>}
*/
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];
});
}
}

/**
Expand Down Expand Up @@ -185,40 +183,25 @@ class TMMap {
}

/**
* The map leaderboard.
* @type {?Array<TMMapLeaderboard>}
*/
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<?Array<TMMapLeaderboard>>}
*/
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;
}

/**
Expand Down

0 comments on commit 48829de

Please sign in to comment.