Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
Rewrite Info
Browse files Browse the repository at this point in the history
  • Loading branch information
Kathund committed Jul 29, 2024
1 parent 1d4a078 commit 56b72b5
Show file tree
Hide file tree
Showing 128 changed files with 11,331 additions and 2 deletions.
85 changes: 85 additions & 0 deletions API/getSkyblockAuctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import AuctionInfo from '../src/structures/SkyBlock/Auctions/AuctionInfo';
import Auction from '../src/structures/SkyBlock/Auctions/Auction';
import Errors from '../src/Errors';
async function getPage(this: any, page: any = 0, options: any = {}) {
// eslint-disable-next-line no-underscore-dangle
const content = await this._makeRequest(`/skyblock/auctions?page=${page}`, false);
const result: any = {};
if (!options.noInfo) result.info = new AuctionInfo(content);
if (options.raw) result.auctions = content.auctions;
else if (options.noAuctions) result.auctions = [];
else result.auctions = content.auctions.map((x: any) => new Auction(x, options.includeItemBytes));
return result;
}
async function noReject(promise: any, args: any = [], retries: any = 3, cooldown: any = 100) {
try {
const result = await promise.call(null, ...args);
return result;
} catch {
if (retries) {
await new Promise((resolve) => setTimeout(resolve, cooldown));
return await noReject(promise, args, retries - 1, cooldown);
}
return null;
}
}

import Endpoint from '../src/Private/Endpoint';
import Client from '../src/Client';
export default class getSkyblockAuctions extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getSkyblockAuctions';
}

async execute(range: any, options: any) {
options.retries ||= 3;
options.cooldown ||= 100;
if (null === range || '*' === range) range = [0, (await getPage(0, { noAuctions: true })).info.totalPages];
if (!Array.isArray(range)) range = [parseInt(range), parseInt(range)];
if (isNaN(range[0])) throw new Error(Errors.PAGE_INDEX_ERROR);
if (parseInt(options.retries) !== options.retries || 10 < options.retries || 0 > options.retries) {
throw new Error(Errors.INVALID_OPTION_VALUE);
}
if (parseInt(options.cooldown) !== options.cooldown || 3000 < options.cooldown || 0 > options.cooldown) {
throw new Error(Errors.INVALID_OPTION_VALUE);
}
range = range.sort();
const result: any = { auctions: [] };
const fetches = [];
const failedPages = [];
if (options.noAuctions) {
return { info: options.noInfo ? null : (await getPage(range[1], { noAuctions: true })).info };
}
for (let i = range[0]; i <= range[1]; i++) {
if (options.race) {
fetches.push(noReject(getPage, [i, options], options.retries, options.cooldown));
} else {
const resp = await noReject(getPage, [i, options], options.retries, options.cooldown);
if (resp) {
result.auctions = result.auctions.concat(resp.auctions);
if (resp.info) result.info = resp.info;
} else {
failedPages.push(i);
}
}
}
if (fetches.length) {
result.auctions = (await Promise.all(fetches)).reduce((pV, cV, index) => {
if (!cV) {
failedPages.push(index + range[0]);
return pV;
}
if (cV.info) result.info = cV.info;
if (cV.auctions.length) return pV.concat(cV.auctions);
return pV;
}, []);
}
// eslint-disable-next-line no-underscore-dangle
result.info = result.info ? result.info._extend('failedPages', failedPages) : { failedPages };
return result;
}
}
18 changes: 18 additions & 0 deletions src/API/getAchievements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Achievements from '../structures/Static/Achievements';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getAchievements extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getAchievements';
}

async execute() {
const res = await this.client.requests.request('/resources/achievements');
if (res.raw) return res;
return new Achievements(res);
}
}
18 changes: 18 additions & 0 deletions src/API/getActiveHouses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Client from '../Client';
export default class getActiveHouses extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getActiveHouses';
}

async execute() {
const res = await this.client.requests.request('/housing/active');
if (res.raw) return res;
return res.length ? res.map((b: any) => new House(b)) : [];
}
}
18 changes: 18 additions & 0 deletions src/API/getBoosters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Booster from '../structures/Boosters/Booster';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getBoosters extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getBoosters';
}

async execute() {
const res = await this.client.requests.request('/boosters');
if (res.raw) return res;
return res.boosters.length ? res.boosters.map((b: any) => new Booster(b)).reverse() : [];
}
}
18 changes: 18 additions & 0 deletions src/API/getChallenges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Challenges from '../structures/Static/Challenges';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getChallenges extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getChallenges';
}

async execute() {
const res = await this.client.requests.request('/resources/challenges');
if (res.raw) return res;
return new Challenges(res);
}
}
18 changes: 18 additions & 0 deletions src/API/getGameCounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import GameCounts from '../structures/GameCounts';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getGameCounts extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getGameCounts';
}

async execute() {
const res = await this.client.requests.request('/counts');
if (res.raw) return res;
return new GameCounts(res);
}
}
30 changes: 30 additions & 0 deletions src/API/getGuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Guild from '../structures/Guild/Guild';
import isGuildID from '../utils/isGuildID';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
import Client from '../Client';
export default class getGuild extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getGuild';
}

async execute(searchParameter: 'id' | 'name' | 'player', query: string) {
if (!query) throw new Error(Errors.NO_GUILD_QUERY);
if ('id' === searchParameter && !isGuildID(query)) throw new Error(Errors.INVALID_GUILD_ID);
const isPlayerQuery = 'player' === searchParameter;
if (isPlayerQuery) query = await toUuid(query);
if (!['id', 'name', 'player'].includes(searchParameter)) throw new Error(Errors.INVALID_GUILD_SEARCH_PARAMETER);
const res = await this.client.requests.request(`/guild?${searchParameter}=${encodeURI(query)}`);
if (res.raw) return res;
if (!res.guild && 'player' !== searchParameter) {
throw new Error(Errors.GUILD_DOES_NOT_EXIST);
}

return res.guild ? new Guild(res.guild, isPlayerQuery ? query : undefined) : null;
}
}
18 changes: 18 additions & 0 deletions src/API/getGuildAchievements.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import GuildAchievements from '../structures/Static/GuildAchievements';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getGuildAchievements extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getGuildAchievements';
}

async execute() {
const res = await this.client.requests.request('/resources/guilds/achievements');
if (res.raw) return res;
return new GuildAchievements(res);
}
}
20 changes: 20 additions & 0 deletions src/API/getHouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import Errors from '../Errors';
import Client from '../Client';
export default class getHouse extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getHouse';
}

async execute(query: string) {
if (!query) throw new Error(Errors.NO_UUID);
const res = await this.client.requests.request(`/housing/house?house=${query}`);
if (res.raw) return res;
return new House(res);
}
}
27 changes: 27 additions & 0 deletions src/API/getLeaderboards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Leaderboard from '../structures/Leaderboard';
import Constants from '../utils/Constants';
import Errors from '../Errors';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getLeaderboards extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getLeaderboards';
}

async execute() {
const res = await this.client.requests.request('/leaderboards');
if (res.raw) return res;
if (!res.leaderboards) throw new Error(Errors.SOMETHING_WENT_WRONG.replace(/{cause}/, 'Try again.'));
const lbnames = Object.create(Constants.leaderboardNames);
for (const name in lbnames) {
lbnames[name] = res.leaderboards[lbnames[name]].length
? res.leaderboards[lbnames[name]].map((lb: any) => new Leaderboard(lb))
: [];
}
return lbnames;
}
}
24 changes: 24 additions & 0 deletions src/API/getPlayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Player from '../structures/Player';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';

import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getPlayer extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getPlayer';
}

async execute(query: string) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
query = await toUuid(query);
const res = await this.client.requests.request(`/player?uuid=${query}`);
if (res.raw) return res;
if (query && !res.player) throw new Error(Errors.PLAYER_HAS_NEVER_LOGGED);
return new Player(res.player);
}
}
22 changes: 22 additions & 0 deletions src/API/getPlayerHouses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Endpoint from '../Private/Endpoint';
import House from '../structures/House';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
import Client from '../Client';
export default class getPlayerHouses extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getPlayerHouses';
}

async execute(query: string) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
query = await toUuid(query);
const res = await this.client.requests.request(`/housing/houses?player=${query}`);
if (res.raw) return res;
return res.length ? res.map((h: any) => new House(h)) : [];
}
}
18 changes: 18 additions & 0 deletions src/API/getQuests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Quests from '../structures/Static/Quests';
import Endpoint from '../Private/Endpoint';
import Client from '../Client';
export default class getQuests extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getQuests';
}

async execute() {
const res = await this.client.requests.request('/resources/quests');
if (res.raw) return res;
return new Quests(res);
}
}
25 changes: 25 additions & 0 deletions src/API/getRecentGames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import RecentGame from '../structures/RecentGame';
import Endpoint from '../Private/Endpoint';
import toUuid from '../utils/toUuid';
import Errors from '../Errors';
import Client from '../Client';
export default class getRecentGames extends Endpoint {
readonly client: Client;
readonly name: string;
constructor(client: Client) {
super(client);
this.client = client;
this.name = 'getRecentGames';
}

async execute(query: string) {
if (!query) throw new Error(Errors.NO_NICKNAME_UUID);
query = await toUuid(query);
const res = await this.client.requests.request(`/recentgames?uuid=${query}`);
if (res.raw) return res;
if (0 === res.games.length) {
return [];
}
return res.games.map((x: any) => new RecentGame(x));
}
}
Loading

0 comments on commit 56b72b5

Please sign in to comment.