From b98648c7904c458dde5fd609af677536e6a4c57c Mon Sep 17 00:00:00 2001 From: Matthieu Dehon <42576124+GreepTheSheep@users.noreply.github.com> Date: Fri, 25 Mar 2022 09:03:11 +0100 Subject: [PATCH 1/4] Make TMMap.exchange a async method (closes #85) --- src/managers/MapManager.js | 19 ++++--------------- src/structures/TMMap.js | 33 +++++++++++++++++---------------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/managers/MapManager.js b/src/managers/MapManager.js index 31c0f38..2142891 100644 --- a/src/managers/MapManager.js +++ b/src/managers/MapManager.js @@ -17,7 +17,7 @@ class MapManager{ /** * The cache manager - * @type {CacheManager} + * @type {CacheManager} * @private */ this._cache = new CacheManager(this.client, this, TMMap); @@ -28,7 +28,7 @@ class MapManager{ * @param {string} mapUid The map UID * @param {boolean} [cache=this.client.options.cache.enabled] Whether to get the map from cache or not * @returns {Promise} The map - * @example + * @example * client.maps.get('z28QXoFnpODEGgg8MOederEVl3j').then(map => { * console.log(map.name); * }); @@ -40,7 +40,7 @@ class MapManager{ return await this._fetch(mapUid, cache); } } - + /** * Fetches a map and returns its data * @param {string} mapUid The map UID @@ -52,17 +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}`); - // Check if the map exists on tmx - if (res.exchangeid !== 0) { - try { - const tmxurl = this.client.options.api.paths.tmx, - tmxres = await this.client._apiReq(`${tmxurl.protocol}://${tmxurl.host}/${tmxurl.api}/${tmxurl.tabs.mapInfo}/${res.exchangeid}`); - res['exchange'] = tmxres[0]; - } catch (e) { - this.client.emit('error', e); - } - } - // Get map leaderboard try { const leaderboard = this.client.options.api.paths.tmio.tabs.leaderboard, @@ -75,7 +64,7 @@ class MapManager{ const theMap = new TMMap(this.client, res); if (cache) { res._cachedTimestamp = Date.now(); - + this._cache.set(res.mapUid, theMap); } return theMap; diff --git a/src/structures/TMMap.js b/src/structures/TMMap.js index c1d4b4d..dca3a22 100644 --- a/src/structures/TMMap.js +++ b/src/structures/TMMap.js @@ -165,22 +165,23 @@ class TMMap { /** * The map informations on trackmania.exchange. - * @type {?TMExchangeMap} - */ - get exchange() { - if (this.exchangeId == null) return null; - else { - if (this._data.exchange) { - if (!this._TMExchange || this._TMExchange.id !== this.exchangeId) { - /** - * @type {TMExchangeMap} - * @private - * */ - this._TMExchange = new TMExchangeMap(this, this._data.exchange); - } - return this._TMExchange; - } else throw "No exchange data found for this map"; - } + * @returns {Promise} + */ + async exchange() { + return new Promise((resolve, reject) => { + if (!this.exchangeId) return resolve(null); + const tmxurl = this.client.options.api.paths.tmx; + if (!this._data.exchange) { + this.client._apiReq(`${tmxurl.protocol}://${tmxurl.host}/${tmxurl.api}/${tmxurl.tabs.mapInfo}/${this.exchangeId}`).then(data => { + this._data.exchange = data[0]; + return resolve(new TMExchangeMap(this.client, data[0])); + }).catch(err => { + return reject(err); + }); + } else { + return resolve(new TMExchangeMap(this.client, this._data.exchange)); + } + }); } /** From 6780d18017203d8bb369a543cd07bd98387b5776 Mon Sep 17 00:00:00 2001 From: Matthieu Dehon <42576124+GreepTheSheep@users.noreply.github.com> Date: Fri, 25 Mar 2022 09:03:15 +0100 Subject: [PATCH 2/4] Create map.test.js --- test/map.test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 test/map.test.js diff --git a/test/map.test.js b/test/map.test.js new file mode 100644 index 0000000..dbcc777 --- /dev/null +++ b/test/map.test.js @@ -0,0 +1,22 @@ +require('dotenv').config(); +const assert = require('assert'), + TMIO = require('../'), + tmioClient = new TMIO.Client({dev: true}); + +describe("Maps", function(){ + this.timeout(15*1000); + + it("Map Info", async function(){ + const map = await tmioClient.maps.get('1jEeZQTADlb9wIY2YzCjZ5Lpmxh'); + const author = await map.author(); + const exchange = await map.exchange(); + + assert.equal(author.login, "Jtmn3kBnSSadky_mLNhp_A"); + assert.equal(map.medalTimes.author, 11216); + assert.equal(map.medalTimes.gold, 12000); + assert.equal(map.medalTimes.silver, 14000); + assert.equal(map.medalTimes.bronze, 17000); + + assert.equal(exchange.id, 41427); + }); +}); \ No newline at end of file From c47494ea723eee69df2dbef3ae3220d730a62e55 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 25 Mar 2022 08:03:57 +0000 Subject: [PATCH 3/4] :children_crossing: Build DefinitelyTyped - 6780d18017203d8bb369a543cd07bd98387b5776 --- typings/structures/TMMap.d.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/typings/structures/TMMap.d.ts b/typings/structures/TMMap.d.ts index f9f4911..4fcd1a7 100644 --- a/typings/structures/TMMap.d.ts +++ b/typings/structures/TMMap.d.ts @@ -102,14 +102,9 @@ declare class TMMap { get exchangeId(): string; /** * The map informations on trackmania.exchange. - * @type {?TMExchangeMap} + * @returns {Promise} */ - get exchange(): TMExchangeMap; - /** - * @type {TMExchangeMap} - * @private - * */ - private _TMExchange; + exchange(): Promise; /** * The map leaderboard. * @type {?Array} From 117463917b99666cfe3487e0b40704a0e55fcd16 Mon Sep 17 00:00:00 2001 From: Matthieu Dehon <42576124+GreepTheSheep@users.noreply.github.com> Date: Fri, 25 Mar 2022 09:06:57 +0100 Subject: [PATCH 4/4] Updated unit tests --- test/rooms.test.js | 2 +- test/totd.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/rooms.test.js b/test/rooms.test.js index 007bea8..ae8f718 100644 --- a/test/rooms.test.js +++ b/test/rooms.test.js @@ -11,7 +11,7 @@ describe("Rooms", function(){ assert.equal(room.isCloud, false); assert.equal(room.region, null); - assert.equal(room.login, "evoice"); + assert.equal(room.login, "evoiceb"); }); it("Hosted Room", async function(){ diff --git a/test/totd.test.js b/test/totd.test.js index b712944..be98057 100644 --- a/test/totd.test.js +++ b/test/totd.test.js @@ -15,7 +15,7 @@ describe("TOTD", function(){ assert.equal(totd.leaderboardId, "bf597bc1-a8c9-4dfa-9ae9-40a3a5e4a0bf"); assert.equal(totd.campaignId, 8383); assert.equal(map.uid, "17B5XtQBJ_nukdrylfT7Pj1q1C1"); - assert.equal(map.exchange.id, 23641); + assert.equal(map.exchangeId, 23641); assert.equal(author.id, "62c59cd2-4981-43cc-a6d2-7feaf96ceeb1"); }); }); \ No newline at end of file