Skip to content

Commit

Permalink
Merge pull request #83 from GreepTheSheep/develop
Browse files Browse the repository at this point in the history
3.0.6
  • Loading branch information
GreepTheSheep authored Jan 27, 2022
2 parents 342ee94 + 4427c15 commit 36545cb
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 287 deletions.
66 changes: 59 additions & 7 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.5",
"version": "3.0.6",
"description": "Node.js inplementation of Trackmania Live services (trackmania.io)",
"main": "src/index.js",
"types": "typings/index.d.ts",
Expand All @@ -27,7 +27,7 @@
"dependencies": {
"events": "3.3.0",
"luxon": "^2.3.0",
"node-fetch": "2.6.1"
"node-fetch": "2.6.7"
},
"devDependencies": {
"@discordjs/docgen": "0.10.0",
Expand Down
7 changes: 7 additions & 0 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const RoomManager = require('../managers/RoomManager');
const EventManager = require('../managers/EventManager');
const NewsManager = require('../managers/NewsManager');
const AdsManager = require('../managers/AdsManager');
const MatchesManager = require('../managers/MatchesManager');

/**
* Instantiates a new client. This is the entry point.
Expand Down Expand Up @@ -75,6 +76,12 @@ class Client extends BaseClient {
*/
this.events = new EventManager(this);

/**
* The matches manager
* @type {MatchesManager}
*/
this.matches = new MatchesManager(this);

/**
* The news manager
* @type {NewsManager}
Expand Down
82 changes: 82 additions & 0 deletions src/data/RoyalTeams.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
[
{
"name": "Flamingo",
"img": "https://trackmania.io/img/clans/flamingo.png"
},
{
"name": "Pig",
"img": "https://trackmania.io/img/clans/pig.png"
},
{
"name": "Clown Fish",
"img": "https://trackmania.io/img/clans/clown_fish.png"
},
{
"name": "Fox",
"img": "https://trackmania.io/img/clans/fox.png"
},
{
"name": "Octopus",
"img": "https://trackmania.io/img/clans/octopus.png"
},
{
"name": "Butterfly",
"img": "https://trackmania.io/img/clans/butterfly.png"
},
{
"name": "Crocodile",
"img": "https://trackmania.io/img/clans/crocodile.png"
},
{
"name": "Grasshopper",
"img": "https://trackmania.io/img/clans/grasshopper.png"
},
{
"name": "Ladybug",
"img": "https://trackmania.io/img/clans/ladybug.png"
},
{
"name": "Macaw Parrot",
"img": "https://trackmania.io/img/clans/macaw_parrot.png"
},
{
"name": "Giraffe",
"img": "https://trackmania.io/img/clans/giraffe.png"
},
{
"name": "Bee",
"img": "https://trackmania.io/img/clans/bee.png"
},
{
"name": "Dolphin",
"img": "https://trackmania.io/img/clans/dolphin.png"
},
{
"name": "Peafowl",
"img": "https://trackmania.io/img/clans/peafowl.png"
},
{
"name": "Kangaroo",
"img": "https://trackmania.io/img/clans/kangaroo.png"
},
{
"name": "Monkey",
"img": "https://trackmania.io/img/clans/monkey.png"
},
{
"name": "Panda",
"img": "https://trackmania.io/img/clans/panda.png"
},
{
"name": "Zebra",
"img": "https://trackmania.io/img/clans/zebra.png"
},
{
"name": "Rabbit",
"img": "https://trackmania.io/img/clans/rabbit.png"
},
{
"name": "Polar Bear",
"img": "https://trackmania.io/img/clans/polar_bear.png"
}
]
9 changes: 0 additions & 9 deletions src/managers/MapManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ class MapManager{
}
}

// Get map votes thanks to RoboTec's Voting API
try {
const mapVotes = this.client.options.api.paths.mapVoting.tabs.getVotes,
votes = await this.client._apiReq(`${new ReqUtil(this.client).votingAPIURL}/${mapVotes}?map=${mapUid}`);
res['karma'] = votes;
} catch (e) {
this.client.emit('error', e);
}

// Get map leaderboard
try {
const leaderboard = this.client.options.api.paths.tmio.tabs.leaderboard,
Expand Down
132 changes: 132 additions & 0 deletions src/managers/MatchesManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const ReqUtil = require('../util/ReqUtil');
const CacheManager = require('./CacheManager');
const {MMTypes, MatchmakingGroup, MatchStatus} = require('../util/Constants'); // eslint-disable-line no-unused-vars
const Match = require('../structures/Match');
const Client = require('../client/Client'); // eslint-disable-line no-unused-vars

/**
* Represents the matches manager (3v3 or Royal matches).
*/
class MatchesManager{
constructor(client){
/**
* The client instance.
* @type {Client}
* @readonly
*/
this.client = client;

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

/**
* Get a list of the matches
* @param {MatchmakingGroup} [group='3v3'] The group to get the matches from
* @param {number} [page=0] The page to get
* @param {Boolean} [cache=this.client.options.cache.enabled] Whether to cache the matches or not
* @returns {Promise<Array<MatchResult>>}
*/
async list(group = 2, page = 0, cache = this.client.options.cache.enabled){
if (typeof group == 'string') group = MMTypes[group];
const matches = this.client.options.api.paths.tmio.tabs.matches;
const res = await this.client._apiReq(`${new ReqUtil(this.client).tmioAPIURL}/${matches}/${group}/${page}`);
const array = [];
if (res.matches.length > 0) {
for (let i = 0; i < res.matches.length; i++) {
let match = new MatchResult(this.client, res.matches[i]);
if (cache) {
res.matches[i]._cachedTimestamp = Date.now();
this._cache.set(res.id, match);
}
array.push(match);
}
}
return array;
}

/**
* Fetches a Trackmania Match and returns its data.
* @param {string} liveID The live ID of the match (should start with 'LID-MTCH')
* @param {boolean} [cache=this.client.options.cache.enabled] Whether to get the news from cache or not
* @returns {Promise<Match>} The Match
*/
async get(liveID, cache = this.client.options.cache.enabled){
if (cache && this._cache.has(liveID)) {
return this._cache.get(liveID);
} else {
return await this._fetch(liveID, cache);
}
}

/**
* Fetches a Match and returns its data
* @param {string} liveID Thelive ID of the match (should start with 'LID-MTCH')
* @param {boolean} [cache=this.client.options.cache.enabled] Whether to cache the news or not
* @returns {Promise<Match>} The splashscreen
* @private
*/
async _fetch(liveID, cache = this.client.options.cache.enabled){
if (!liveID.startsWith("LID-MTCH")) throw "The live ID must start with 'LID-MTCH'";
const match = this.client.options.api.paths.tmio.tabs.match;
const res = await this.client._apiReq(`${new ReqUtil(this.client).tmioAPIURL}/${match}/${liveID}`);

const theMatch = new Match(this.client, res);
if (cache) {
res._cachedTimestamp = Date.now();
this._cache.set(res.lid, theMatch);
}
return theMatch;
}
}

/**
* The result of a Match from the list. It is completely different from the {@link Match} object.
*/
class MatchResult {
constructor(client, data){
/**
* The client instance.
* @type {Client}
*/
this.client = client;

/**
* The match ID
* @type {number}
*/
this.id = data.id;

/**
* The match Live ID
* @type {string}
*/
this.liveID = data.lid;

/**
* The match start date
* @type {Date}
*/
this.startDate = new Date(data.starttime);

/**
* The status of the match
* @type {MatchStatus}
*/
this.status = data.status;
}

/**
* Get the match
* @param {boolean} [cache=this.client.options.cache.enabled] Whether to cache the match or not
*/
async match(cache = this.client.options.cache.enabled){
return this.client.matches.get(this.liveID, cache);
}
}

module.exports = MatchesManager;
Loading

0 comments on commit 36545cb

Please sign in to comment.