Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
NeutronicMC committed Jan 23, 2021
1 parent 06288a2 commit 9032b77
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
14 changes: 10 additions & 4 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"printWidth": 120,
"trailingComma": "all",
"singleQuote": true
}
"printWidth": 80,
"useTabs": true,
"semi": true,
"singleQuote": false,
"quoteProps": "as-needed",
"trailingComma": "none",
"bracketSpacing": true,
"arrowParens": "avoid",
"endOfLine": "crlf"
}
23 changes: 14 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Axios from "axios";
import NodeCache from "node-cache";

const cache = new NodeCache({ stdTTL: 300, checkperiod: 300 });
const cache = new NodeCache({stdTTL: 300, checkperiod: 300});

const API_URL = "https://api.playhive.com/v0";

Expand Down Expand Up @@ -54,7 +54,7 @@ export async function getMonthlyLeaderboard(game, year, month, amount, skip) {
});
}
} else {
data = await fetchData("/game/monthly/{game}", { game });
data = await fetchData("/game/monthly/{game}", {game});
}
return data.map(player =>
calculateExtraStats(player, game, TIME_SCOPE_MONTHLY)
Expand All @@ -67,7 +67,7 @@ export async function getMonthlyLeaderboard(game, year, month, amount, skip) {
* @returns {Promise<Object>} All-time leaderboard
*/
export async function getAllTimeLeaderboard(game) {
return (await fetchData("/game/all/{game}", { game })).map(player =>
return (await fetchData("/game/all/{game}", {game})).map(player =>
calculateExtraStats(player, game, TIME_SCOPE_ALL)
);
}
Expand Down Expand Up @@ -175,15 +175,15 @@ function calculateLevel(game, xp) {
let flattenLevel = GAME_XP[game][1];
let level =
(-increment + Math.sqrt(Math.pow(increment, 2) - 4 * increment * -xp)) /
(2 * increment) +
(2 * increment) +
1;
if (flattenLevel && level > flattenLevel)
level =
flattenLevel +
(xp -
(increment * Math.pow(flattenLevel - 1, 2) +
(flattenLevel - 1) * increment)) /
((flattenLevel - 1) * increment * 2);
((flattenLevel - 1) * increment * 2);
return level;
}

Expand All @@ -194,11 +194,12 @@ function calculateLevel(game, xp) {
* @returns {Promise<any>} API data
*/
async function fetchData(path, pathParams) {
path = buildUrl(path, pathParams);
let response = cache.get(path);
if (response === undefined) {
response = {};
try {
response = (await Axios.get(buildUrl(path, pathParams))).data;
response = (await Axios.get(path)).data;
} catch (e) {
if (e.response.status === 429) {
response = e.response;
Expand All @@ -207,16 +208,20 @@ async function fetchData(path, pathParams) {
await new Promise(r =>
setTimeout(r, response.headers["retry-after"] * 1000)
);
response = (await Axios.get(buildUrl(path, pathParams))).data;
try {
response = (await Axios.get(path)).data;
} catch (e) {
console.log(e);
}
}
} else {
await new Promise(r => setTimeout(r, 60000));
response = (await Axios.get(buildUrl(path, pathParams))).data;
response = (await Axios.get(path)).data;
}
}
}
cache.set(path, response);
}
cache.set(path, response);
return response;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "hive-tools-wrapper",
"version": "2.0.0",
"version": "2.0.1",
"description": "Advanced Hive Bedrock API wrapper with caching.",
"main": "./index.js",
"dependencies": {
Expand Down
7 changes: 0 additions & 7 deletions src/ts/types/api_types.ts

This file was deleted.

0 comments on commit 9032b77

Please sign in to comment.