Skip to content

Commit

Permalink
Merge pull request #96 from GreepTheSheep/develop
Browse files Browse the repository at this point in the history
3.1.0
  • Loading branch information
GreepTheSheep authored Mar 31, 2022
2 parents 36545cb + 2a422c3 commit b98219b
Show file tree
Hide file tree
Showing 20 changed files with 542 additions and 213 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Publish dev
on:
workflow_dispatch:
schedule:
- cron: '0 0 */2 * *'
- cron: '0 0 */4 * *'

jobs:
npm:
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trackmania.io",
"version": "3.0.6",
"version": "3.1.0",
"description": "Node.js inplementation of Trackmania Live services (trackmania.io)",
"main": "src/index.js",
"types": "typings/index.d.ts",
Expand All @@ -26,7 +26,7 @@
"homepage": "https://tmio.greep.gq/",
"dependencies": {
"events": "3.3.0",
"luxon": "^2.3.0",
"luxon": "^2.3.1",
"node-fetch": "2.6.7"
},
"devDependencies": {
Expand Down
143 changes: 139 additions & 4 deletions src/managers/COTDManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const ReqUtil = require('../util/ReqUtil');
const CacheManager = require('./CacheManager');
const COTD = require('../structures/COTD');
const Client = require('../client/Client'); // eslint-disable-line no-unused-vars

const Player = require('../structures/Player'); // eslint-disable-line no-unused-vars
const { COTDLeaderboardSortGroup } = require('../util/Constants'); // eslint-disable-line no-unused-vars

/**
* Represents a COTD Manager.
Expand All @@ -17,18 +18,44 @@ class COTDManager{

/**
* The cache manager
* @type {CacheManager}
* @type {CacheManager}
* @private
*/
this._cache = new CacheManager(this.client, this, COTD);
}

/**
* Get the COTD leaderboard by category
* @param {COTDLeaderboardSortGroup} [sort="wins"] The leaderboard sorting
* @param {boolean} [includeReruns=false] Whether to include reruns when sorting or not
* @param {number} [page=0] The page number
* @param {boolean} [cache=this.client.options.cache.enabled] Whether to get the list from cache or not
* @returns {Promise<Array<COTDLeaderboard>>}
*/
async leaderboard(sort = "wins", includeReruns = false, page = 0, cache = this.client.options.cache.enabled) {
const cacheKey = `leaderboard_${sort}${includeReruns ? 'reruns' : ''}_${page}`;
if (cache && this._cache.has(cacheKey)) {
return this._cache.get(cacheKey);
} else {
const cotd = this.client.options.api.paths.tmio.tabs.cotd,
players = this.client.options.api.paths.tmio.tabs.players,
res = await this.client._apiReq(`${new ReqUtil(this.client).tmioAPIURL}/${cotd}/${players}/${page}/${sort}${includeReruns ? 'reruns' : ''}`);

let results = [];
for (var i = 0; i < res.players.length; i++) {
results.push(new COTDLeaderboard(this.client, res.players[i]));
}
if (cache) this._cache.set(cacheKey, results);
return results;
}
}

/**
* Fetches the latest COTDs and returns its data
* @param {number} [page=0] The page, each page contains 12 items
* @param {boolean} [cache=this.client.options.cache.enabled] Whether to get the list from cache or not
* @returns {Promise<Array<COTD>>} The COTD list
* @example
* @example
* client.cotd.get().then(event => {
* console.log(event.name);
* });
Expand All @@ -40,7 +67,7 @@ class COTDManager{
return await this._fetch(page, cache);
}
}

/**
* Fetches a COTD and returns its data
* @param {number} [page=0] The page
Expand All @@ -62,4 +89,112 @@ class COTDManager{
}
}

/**
* Represents a position in the COTD Leaderboard
*/
class COTDLeaderboard {
constructor(client, data) {
/**
* The client instance.
* @type {Client}
*/
this.client = client;

/**
* The data of the COTD leaderboard.
* @type {Object}
* @private
*/
this._data = data;
}

/**
* The player's name
* @type {string}
*/
get playerName() {
return this._data.player.name;
}

/**
* The player's club tag
* @type {string}
*/
get playerTag() {
return this._data.player.tag;
}

/**
* The player's account ID
* @type {string}
*/
get playerId() {
return this._data.player.id;
}

/**
* The player
* @returns {Promise<Player>}
*/
async player() {
return await this.client.players.get(this.playerId);
}

/**
* The position of the player in the selected category
* @type {number}
*/
get position() {
return this._data.position;
}

/**
* The amount of COTD the player has played
* @type {number}
*/
get played() {
return this._data.totalplayed;
}

/**
* The amount of COTD reruns the player has played
* @type {number}
*/
get rerunsPlayed() {
return this._data.totalplayedreruns;
}

/**
* The amount of COTD the player has won
* @type {number}
*/
get wins() {
return this._data.wins;
}

/**
* The amount of COTD reruns the player has won
* @type {number}
*/
get rerunsWins() {
return this._data.winsreruns;
}

/**
* The amount of win streak the player has
* @type {number}
*/
get winStreak() {
return this._data.winstreak;
}

/**
* The amount of win streak the player has (reruns included)
* @type {number}
*/
get winStreakWithReruns() {
return this._data.winstreakreruns;
}
}

module.exports = COTDManager;
10 changes: 5 additions & 5 deletions src/managers/CampaignManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CampaignManager{

/**
* The cache manager
* @type {CacheManager}
* @type {CacheManager}
* @private
*/
this._cache = new CacheManager(this.client, this, Campaign);
Expand Down Expand Up @@ -83,7 +83,7 @@ class CampaignManager{
* @returns {Promise<Array<CampaignSearchResult>>} The campaigns
* @example
* client.campaigns.search('htimh').then(campaigns => {
* client.campaigns.get(campaigns[0].clubid, campaigns[0].id).then(async campaign => {
* campaigns[0].getCampaign().then(async campaign => {
* const maps = await campaign.maps();
* maps.forEach(map => console.log(map.name));
* });
Expand All @@ -106,7 +106,7 @@ class CampaignManager{
* @param {number} id The campaign Id
* @param {boolean} [cache=this.client.options.cache.enabled] Whether to get the campaign from cache or not
* @returns {Promise<Campaign>} The campaign
* @example
* @example
* client.campaigns.get(54, 10621).then(campaign => {
* console.log(campaign.name);
* });
Expand All @@ -118,7 +118,7 @@ class CampaignManager{
return await this._fetch(clubId, id, cache);
}
}

/**
* Fetches a campaign and returns its data
* @param {number} clubId The club Id that the campaign belongs to
Expand All @@ -142,7 +142,7 @@ class CampaignManager{
const theCampaign = new Campaign(this.client, res);
if (cache) {
res._cachedTimestamp = Date.now();

this._cache.set(res.id, theCampaign);
}
return theCampaign;
Expand Down
28 changes: 4 additions & 24 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,30 +52,10 @@ 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,
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();

this._cache.set(res.mapUid, theMap);
}
return theMap;
Expand Down
Loading

0 comments on commit b98219b

Please sign in to comment.