@barbarbar338/bhapi
Version:
Brawlhalla API wrapper for NodeJS and web
170 lines • 6.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRankings = exports.getGloryByBHID = exports.getLegendByName = exports.getLegendByID = exports.getAllLegends = exports.getClanByID = exports.getRankedByBHID = exports.getStatsByName = exports.getStatsByBHID = exports.getBHIDFromName = void 0;
const client_1 = require("./client");
const types_1 = require("./types");
const bestRating = (rank) => {
let ratings = [rank, ...rank.legends];
if (rank["2v2"] && rank["2v2"].length > 0)
ratings = [...ratings, ...rank["2v2"]];
const peak = ratings.map((r) => r.peak_rating);
return Math.max(...peak);
};
const gloryFromWins = (totalwins) => {
if (totalwins <= 150)
return 20 * totalwins;
return Math.floor(10 * (45 * Math.pow(Math.log10(totalwins * 2), 2)) + 245);
};
const gloryFromBestRating = (bestrating) => {
let retval = 0;
if (bestrating < 1200)
retval = 250;
if (bestrating >= 1200 && bestrating < 1286)
retval = 10 * (25 + 0.872093023 * (86 - (1286 - bestrating)));
if (bestrating >= 1286 && bestrating < 1390)
retval = 10 * (100 + 0.721153846 * (104 - (1390 - bestrating)));
if (bestrating >= 1390 && bestrating < 1680)
retval = 10 * (187 + 0.389655172 * (290 - (1680 - bestrating)));
if (bestrating >= 1680 && bestrating < 2000)
retval = 10 * (300 + 0.428125 * (320 - (2000 - bestrating)));
if (bestrating >= 2000 && bestrating < 2300)
retval = 10 * (437 + 0.143333333 * (300 - (2300 - bestrating)));
if (bestrating >= 2300)
retval = 10 * (480 + 0.05 * (400 - (2700 - bestrating)));
return Math.floor(retval);
};
const newEloFromOldElo = (elo) => {
if (elo >= 1400)
return Math.floor(1400 + (elo - 1400) / (3 - (3000 - elo) / 800));
return elo;
};
/**
* Get Brawlhalla ID from player name
*
* @param {string} name Player name
* @returns {number} Brawlhalla ID of the player
*/
const getBHIDFromName = async (name) => {
const res = await client_1.request.get("rankings/1v1/all/1", {
params: {
name,
},
});
if (res.data.length === 0)
throw new types_1.BHAPIError(`Player with name "${name}" not found.`, {
code: res.statusText,
status: res.status,
details: res.config.url,
});
return res.data[0].brawlhalla_id;
};
exports.getBHIDFromName = getBHIDFromName;
/**
* Get player stats by Brawlhalla ID
*
* @param {number} brawlhallaId Brawlhalla ID of the player
* @returns {Promise<AxiosResponse<PlayerStats>>} Player stats
*/
const getStatsByBHID = (brawlhallaId) => client_1.request.get(`player/${brawlhallaId}/stats`);
exports.getStatsByBHID = getStatsByBHID;
/**
* Get player stats by name
*
* @param {string} name Player name
* @returns {Promise<AxiosResponse<PlayerStats>>} Player stats
*/
const getStatsByName = async (name) => {
const brawlhallaId = await (0, exports.getBHIDFromName)(name);
return (0, exports.getStatsByBHID)(brawlhallaId);
};
exports.getStatsByName = getStatsByName;
/**
* Get player ranked stats by Brawlhalla ID
*
* @param {number} brawlhallaId Brawlhalla ID of the player
* @returns {Promise<AxiosResponse<PlayerRanked>>} Player ranked stats
*/
const getRankedByBHID = (brawlhallaId) => client_1.request.get(`player/${brawlhallaId}/ranked`);
exports.getRankedByBHID = getRankedByBHID;
/**
* Get clan by ID
*
* @param {number} clanID Brawlhalla clan ID
* @returns {Promise<AxiosResponse<Clan>>} Clan data
*/
const getClanByID = (clanID) => client_1.request.get(`clan/${clanID}`);
exports.getClanByID = getClanByID;
/**
* Get all legends
*
* @returns {Promise<AxiosResponse<StaticAllLegends[]>>} All legends
*/
const getAllLegends = async () => client_1.request.get("legend/all");
exports.getAllLegends = getAllLegends;
/**
* Get legend by ID
*
* @param {number} legendID Brawlhalla legend ID
* @returns {Promise<AxiosResponse<StaticLegend>>} Legend data
*/
const getLegendByID = async (legendID) => client_1.request.get(`legend/${legendID}`);
exports.getLegendByID = getLegendByID;
/**
* Get legend by name
*
* @param {string} name Legend name
* @returns {Promise<AxiosResponse<StaticLegend>>} Legend data
*/
const getLegendByName = async (name) => {
const getAllLegendsResponse = await (0, exports.getAllLegends)();
const legends = getAllLegendsResponse.data;
const legend = legends.find((legend) => legend.legend_name_key.toLowerCase() === name.toLowerCase());
if (!legend)
throw new types_1.BHAPIError(`Legend with name "${name}" not found.`, {
code: "LegendNotFound",
status: 404,
details: `No legend found with name "${name}"`,
});
return (0, exports.getLegendByID)(legend.legend_id);
};
exports.getLegendByName = getLegendByName;
/**
* Get glory data by Brawlhalla ID
*
* @param {number} brawlhallaId Brawlhalla ID of the player
* @returns {Promise<GloryData>} Glory data
*/
const getGloryByBHID = async (brawlhallaId) => {
const res = await (0, exports.getRankedByBHID)(brawlhallaId);
let { games, wins } = res.data;
if (res.data["2v2"] && res.data["2v2"].length > 0)
res.data["2v2"].forEach((data) => {
wins += data.wins;
games += data.games;
});
const bestElo = bestRating(res.data);
const glory = games < 10
? { wins: 0, rating: 0 }
: {
wins: gloryFromWins(wins),
rating: gloryFromBestRating(bestElo),
};
const eloReset = newEloFromOldElo(res.data.rating);
return {
brawlhalla_id: res.data.brawlhalla_id,
name: res.data.name,
bestElo,
eloReset,
glory,
};
};
exports.getGloryByBHID = getGloryByBHID;
/**
* Get player rankings
*
* @param {RankingsOptions<RankingTypes>} rankingOptions Ranking options
* @returns {Promise<AxiosResponse<RankingResponse<RankingTypes>>>} Player rankings
*/
const getRankings = async ({ page, region, type, }) => client_1.request.get(`rankings/${type}/${region}/${page}`);
exports.getRankings = getRankings;
//# sourceMappingURL=brawlhalla.js.map