Skip to content

Commit

Permalink
feat(*): builder base update (#150)
Browse files Browse the repository at this point in the history
* feat(*): builder base update

* maintain deprecated properties
  • Loading branch information
csuvajit committed Jul 23, 2023
1 parent 9dd4a89 commit 202ebf7
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 94 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clashofclans.js",
"version": "3.0.2",
"version": "3.1.0",
"description": "JavaScript library for interacting with the Clash of Clans API",
"author": "SUVAJIT <suvajit.me@gmail.com>",
"license": "MIT",
Expand Down
29 changes: 16 additions & 13 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import {
ClanMember,
ClanWar,
ClanWarLog,
League,
Location,
Player,
WarLeague,
RankedClan,
RankedPlayer,
Label,
SeasonRankedPlayer,
GoldPassSeason,
ClanWarLeagueGroup
Expand Down Expand Up @@ -270,7 +267,13 @@ export class Client extends EventEmitter {
/** Get a list of Leagues. */
public async getLeagues(options?: SearchOptions) {
const { data } = await this.rest.getLeagues(options);
return data.items.map((entry) => new League(entry));
return data.items;
}

/** Get a list of Leagues. */
public async getBuilderBaseLeagues(options?: SearchOptions) {
const { data } = await this.rest.getBuilderBaseLeagues(options);
return data.items;
}

/** Get a list of Capital Leagues. */
Expand All @@ -294,7 +297,7 @@ export class Client extends EventEmitter {
/** Get list of Clan War Leagues. */
public async getWarLeagues(options?: SearchOptions) {
const { data } = await this.rest.getWarLeagues(options);
return data.items.map((entry) => new WarLeague(entry));
return data.items;
}

/** Get list of Locations. */
Expand Down Expand Up @@ -324,22 +327,22 @@ export class Client extends EventEmitter {
}

/**
* Get clan versus rankings for a specific location.
* Get clan builder base rankings for a specific location.
*
* For global ranking, use `global` as `locationId`.
*/
public async getVersusClanRanks(locationId: number | 'global', options?: SearchOptions) {
const { data } = await this.rest.getVersusClanRanks(locationId, options);
public async getBuilderBaseClanRanks(locationId: number | 'global', options?: SearchOptions) {
const { data } = await this.rest.getBuilderBaseClanRanks(locationId, options);
return data.items.map((entry) => new RankedClan(entry));
}

/**
* Get player versus rankings for a specific location.
* Get player builder base rankings for a specific location.
*
* For global ranking, use `global` as `locationId`.
*/
public async getVersusPlayerRanks(locationId: number | 'global', options?: SearchOptions) {
const { data } = await this.rest.getVersusPlayerRanks(locationId, options);
public async getBuilderBasePlayerRanks(locationId: number | 'global', options?: SearchOptions) {
const { data } = await this.rest.getBuilderBasePlayerRanks(locationId, options);
return data.items.map((entry) => new RankedPlayer(this, entry));
}

Expand All @@ -356,13 +359,13 @@ export class Client extends EventEmitter {
/** Get list of clan labels. */
public async getClanLabels(options?: SearchOptions) {
const { data } = await this.rest.getClanLabels(options);
return data.items.map((entry) => new Label(entry));
return data.items;
}

/** Get list of player labels. */
public async getPlayerLabels(options?: SearchOptions) {
const { data } = await this.rest.getPlayerLabels(options);
return data.items.map((entry) => new Label(entry));
return data.items;
}

/** Get info about gold pass season. */
Expand Down
37 changes: 28 additions & 9 deletions src/rest/RESTManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
APIClanMemberList,
APICapitalRaidSeasons,
APIClanRankingList,
APIClanVersusRankingList,
APIClanBuilderBaseRankingList,
APIClanWar,
APIClanWarLeagueGroup,
APIClanWarLog,
Expand All @@ -20,7 +20,7 @@ import {
APIPlayer,
APIPlayerRankingList,
APIPlayerSeasonRankingList,
APIPlayerVersusRankingList,
APIPlayerBuilderBaseRankingList,
APIVerifyToken,
APIWarLeague,
APIWarLeagueList,
Expand All @@ -31,7 +31,9 @@ import {
LoginOptions,
APICapitalLeagueList,
APICapitalLeague,
APIClanCapitalRankingList
APIClanCapitalRankingList,
APIBuilderBaseLeagueList,
APIBuilderBaseLeague
} from '../types';
import { RestEvents } from '../util/Constants';
import { RequestHandler } from './RequestHandler';
Expand Down Expand Up @@ -182,6 +184,17 @@ export class RESTManager extends EventEmitter {
return this.requestHandler.request<APILeague>(`/leagues/${leagueId}`, options);
}

/** Get a list of builder base leagues. */
public getBuilderBaseLeagues(options?: SearchOptions) {
const query = Util.queryString(options);
return this.requestHandler.request<APIBuilderBaseLeagueList>(`/builderbaseleagues${query}`, options);
}

/** Get a builder base league info. */
public getBuilderBaseLeague(leagueId: string | number, options?: OverrideOptions) {
return this.requestHandler.request<APIBuilderBaseLeague>(`/builderbaseleagues/${leagueId}`, options);
}

/** Get a list of Capital leagues. */
public getCapitalLeagues(options?: SearchOptions) {
const query = Util.queryString(options);
Expand Down Expand Up @@ -239,16 +252,22 @@ export class RESTManager extends EventEmitter {
return this.requestHandler.request<APIPlayerRankingList>(`/locations/${locationId}/rankings/players${query}`, options);
}

/** Get clan versus rankings for a specific location. */
public getVersusClanRanks(locationId: number | string, options?: SearchOptions) {
/** Get clan builder base rankings for a specific location. */
public getBuilderBaseClanRanks(locationId: number | string, options?: SearchOptions) {
const query = Util.queryString(options);
return this.requestHandler.request<APIClanVersusRankingList>(`/locations/${locationId}/rankings/clans-versus${query}`, options);
return this.requestHandler.request<APIClanBuilderBaseRankingList>(
`/locations/${locationId}/rankings/clans-builder-base${query}`,
options
);
}

/** Get player versus rankings for a specific location. */
public getVersusPlayerRanks(locationId: number | string, options?: SearchOptions) {
/** Get player builder base rankings for a specific location. */
public getBuilderBasePlayerRanks(locationId: number | string, options?: SearchOptions) {
const query = Util.queryString(options);
return this.requestHandler.request<APIPlayerVersusRankingList>(`/locations/${locationId}/rankings/players-versus${query}`, options);
return this.requestHandler.request<APIPlayerBuilderBaseRankingList>(
`/locations/${locationId}/rankings/players-builder-base${query}`,
options
);
}

/** Get clan capital rankings for a specific location. */
Expand Down
37 changes: 21 additions & 16 deletions src/struct/Clan.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { APICapitalLeague, APIClan, OverrideOptions } from '../types';
import { APICapitalLeague, APIChatLanguage, APIClan, APILabel, APIWarLeague, OverrideOptions } from '../types';
import { Client } from '../client/Client';
import { ChatLanguage } from './ChatLanguage';
import { ClanMember } from './ClanMember';
import { WarLeague } from './WarLeague';
import type { Player } from './Player';
import { Location } from './Location';
import { Label } from './Label';
import { Badge } from './Badge';
import { ClanCapital } from './ClanCapital';

Expand All @@ -27,7 +24,7 @@ export class Clan {
public location: Location | null;

/** The clan's trophy count. */
public chatLanguage: ChatLanguage | null;
public chatLanguage: APIChatLanguage | null;

/** The clan's Badge. */
public badge: Badge;
Expand All @@ -41,14 +38,20 @@ export class Clan {
/** The clan's capital points. */
public capitalPoints: number;

/** The clan's versus trophy count. */
public versusPoints: number;
/** The clan's builder base trophy count. */
public builderBasePoints: number;

/** @deprecated */
public versusPoints?: number | null;

/** The minimum trophies required to apply to this clan. */
public requiredTrophies: number;

/** The minimum versus trophies required to apply to this clan. */
public requiredVersusTrophies: number | null;
/** The minimum builder base trophies required to apply to this clan. */
public requiredBuilderBaseTrophies: number | null;

/** @deprecated */
public requiredVersusTrophies?: number | null;

/** The minimum hall level required to apply to this clan. */
public requiredTownHallLevel: number | null;
Expand All @@ -72,13 +75,13 @@ export class Clan {
public isWarLogPublic: boolean;

/** The clan's CWL league. */
public warLeague: WarLeague | null;
public warLeague: APIWarLeague | null;

/** The number of members in the clan. */
public memberCount: number;

/** An array of {@link Label} that the clan has. */
public labels: Label[];
public labels: APILabel[];

/** The clan's Clan Capital information */
public clanCapital: ClanCapital | null;
Expand All @@ -101,29 +104,31 @@ export class Clan {
this.type = data.type;
this.description = data.description;
this.location = data.location ? new Location(data.location) : null;
this.chatLanguage = data.chatLanguage ? new ChatLanguage(data.chatLanguage) : null;
this.chatLanguage = data.chatLanguage ?? null;
this.badge = new Badge(data.badgeUrls);
this.level = data.clanLevel;
this.points = data.clanPoints;
this.versusPoints = data.clanVersusPoints;
this.builderBasePoints = data.clanBuilderBasePoints;
this.versusPoints = data.clanVersusPoints ?? null;
this.requiredTrophies = data.requiredTrophies;
this.requiredTownHallLevel = data.requiredTownhallLevel ?? null;
this.requiredBuilderBaseTrophies = data.requiredBuilderBaseTrophies ?? null;
this.requiredVersusTrophies = data.requiredVersusTrophies ?? null;
this.warFrequency = data.warFrequency;
this.warWinStreak = data.warWinStreak;
this.warWins = data.warWins;
this.warTies = data.warTies ?? null;
this.warLosses = data.warLosses ?? null;
this.isWarLogPublic = data.isWarLogPublic;
this.warLeague = data.warLeague ? new WarLeague(data.warLeague) : null;
this.warLeague = data.warLeague ?? null;
this.memberCount = data.members;
this.labels = data.labels.map((label) => new Label(label));
this.labels = data.labels;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.clanCapital = Object.keys(data.clanCapital ?? {}).length > 0 ? new ClanCapital(data.clanCapital) : null;
this.isFamilyFriendly = data.isFamilyFriendly;
this.capitalPoints = data.clanCapitalPoints;
this.capitalLeague = data.capitalLeague;
this.members = data.memberList?.map((mem) => new ClanMember(this.client, mem)) ?? []; // eslint-disable-line
this.members = data.memberList?.map((mem) => new ClanMember(this.client, mem)) ?? []; // eslint-disable-line
}

/** Get {@link Player} info for every Player in the clan. */
Expand Down
8 changes: 6 additions & 2 deletions src/struct/ClanMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ export class ClanMember {
/** The member's trophy count. */
public trophies: number;

/** The member's versus trophy count. */
public versusTrophies: number | null;
/** The member's builder base trophy count. */
public builderBaseTrophies: number | null;

/** @deprecated */
public versusTrophies?: number | null;

/** The member's rank in the clan. */
public clanRank: number;
Expand All @@ -49,6 +52,7 @@ export class ClanMember {
// eslint-disable-next-line
this.league = new League(data.league ?? UnrankedLeagueData);
this.trophies = data.trophies;
this.builderBaseTrophies = data.builderBaseTrophies ?? null;
this.versusTrophies = data.versusTrophies ?? null;
this.clanRank = data.clanRank;
this.previousClanRank = data.previousClanRank;
Expand Down
12 changes: 6 additions & 6 deletions src/struct/LegendStatistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export class LegendStatistics {
/** Legend statistics for this player's best season. */
public bestSeason: Season | null;

/** Versus Legend statistics for previous season. */
public previousVersusSeason: Season | null;
/** Builder base Legend statistics for previous season. */
public previousBuilderBaseSeason: Season | null;

/** Versus Legend statistics for this player's best season. */
public bestVersusSeason: Season | null;
/** Builder base Legend statistics for this player's best season. */
public bestBuilderBaseSeason: Season | null;

public constructor(data: APILegendStatistics) {
this.legendTrophies = data.legendTrophies;
Expand All @@ -29,7 +29,7 @@ export class LegendStatistics {
this.currentSeason = data.currentSeason?.rank ? new Season(data.currentSeason) : null;
this.bestSeason = data.bestSeason ? new Season(data.bestSeason) : null;

this.previousVersusSeason = data.previousVersusSeason ? new Season(data.previousVersusSeason) : null;
this.bestVersusSeason = data.bestVersusSeason ? new Season(data.bestVersusSeason) : null;
this.previousBuilderBaseSeason = data.previousBuilderBaseSeason ? new Season(data.previousBuilderBaseSeason) : null;
this.bestBuilderBaseSeason = data.bestBuilderBaseSeason ? new Season(data.bestBuilderBaseSeason) : null;
}
}
18 changes: 11 additions & 7 deletions src/struct/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ export class Player {
/** The player's builder hall level, or 0 if it hasn't been unlocked. */
public builderHallLevel: number | null;

/** The player's versus trophy count. */
public versusTrophies: number | null;
/** The player's builder base trophy count. */
public builderBaseTrophies: number | null;

/** The player's best versus trophies. */
public bestVersusTrophies: number | null;
/** @deprecated */
public versusTrophies?: number | null;

/** The number of total versus attacks the player has won. */
/** The player's best builder base trophies. */
public bestBuilderBaseTrophies: number | null;

/** @deprecated */
public versusBattleWins: number | null;

/** The player's donation count for this season. */
Expand Down Expand Up @@ -106,9 +109,10 @@ export class Player {
this.attackWins = data.attackWins;
this.defenseWins = data.defenseWins;
this.builderHallLevel = data.builderHallLevel ?? null;
this.versusTrophies = data.versusTrophies ?? null;
this.bestVersusTrophies = data.bestVersusTrophies ?? null;
this.builderBaseTrophies = data.builderBaseTrophies ?? null;
this.bestBuilderBaseTrophies = data.bestBuilderBaseTrophies ?? null;
this.versusBattleWins = data.versusBattleWins ?? null;
this.versusTrophies = data.versusTrophies ?? null;
this.donations = data.donations;
this.received = data.donationsReceived;
this.clanCapitalContributions = data.clanCapitalContributions;
Expand Down

1 comment on commit 202ebf7

@csuvajit
Copy link
Contributor Author

@csuvajit csuvajit commented on 202ebf7 Jul 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for clashofclans-js ready!

✅ Preview
https://clashofclans-otn77svqt-csuvajit.vercel.app

Built with commit 202ebf7.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.