Skip to content

Commit

Permalink
Merge pull request #87 from GreepTheSheep/85-tmmap-exchange
Browse files Browse the repository at this point in the history
TMMap.exchange will fetch separately from MapManager
  • Loading branch information
GreepTheSheep authored Mar 25, 2022
2 parents dbfe873 + 905bbc6 commit a873d06
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 40 deletions.
19 changes: 4 additions & 15 deletions src/managers/MapManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MapManager{

/**
* The cache manager
* @type {CacheManager}
* @type {CacheManager}
* @private
*/
this._cache = new CacheManager(this.client, this, TMMap);
Expand All @@ -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<TMMap>} The map
* @example
* @example
* client.maps.get('z28QXoFnpODEGgg8MOederEVl3j').then(map => {
* console.log(map.name);
* });
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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;
Expand Down
33 changes: 17 additions & 16 deletions src/structures/TMMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<?TMExchangeMap>}
*/
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));
}
});
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/map.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion test/rooms.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down
2 changes: 1 addition & 1 deletion test/totd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
9 changes: 2 additions & 7 deletions typings/structures/TMMap.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,9 @@ declare class TMMap {
get exchangeId(): string;
/**
* The map informations on trackmania.exchange.
* @type {?TMExchangeMap}
* @returns {Promise<?TMExchangeMap>}
*/
get exchange(): TMExchangeMap;
/**
* @type {TMExchangeMap}
* @private
* */
private _TMExchange;
exchange(): Promise<TMExchangeMap | null>;
/**
* The map leaderboard.
* @type {?Array<TMMapLeaderboard>}
Expand Down

0 comments on commit a873d06

Please sign in to comment.