Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/shared/src/ClanApiSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,35 @@ export const ClanDonationsResponseSchema = z.object({
limit: z.number(),
});
export type ClanDonationsResponse = z.infer<typeof ClanDonationsResponseSchema>;

export const ClanDonationPeriodSchema = z.enum(["all", "week", "month"]);
export type ClanDonationPeriod = z.infer<typeof ClanDonationPeriodSchema>;

// One row of the clan's Top donors ranking (GET /clans/:tag/donations/
// leaderboard): a current member's net donations of one currency in the
// period — refund reversals are subtracted, so it can be less than the sum of
// their rows in the Recent list. Week/month are the last 7/30 days.
export const ClanDonorSchema = z.object({
// 1-based across pages.
rank: z.number(),
// Donor's public ID.
playerId: z.string(),
// Display username; null when never set.
username: z.string().nullable(),
// Positive integer as a decimal bigint string; parse with BigInt.
total: z.string(),
// Donations counted towards `total`.
donations: z.number(),
});
export type ClanDonor = z.infer<typeof ClanDonorSchema>;

export const ClanDonationLeaderboardResponseSchema = z.object({
results: ClanDonorSchema.array(),
// Ranked donors in the period, not just this page.
total: z.number(),
page: z.number(),
limit: z.number(),
});
export type ClanDonationLeaderboardResponse = z.infer<
typeof ClanDonationLeaderboardResponseSchema
>;
7 changes: 7 additions & 0 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,17 @@
"donate_success": "Donated {amount} {currency} to [{tag}]!",
"donate_title": "Donate to [{tag}]",
"donate_your_balance": "You have {balance}",
"donations_count": "{count, plural, one {# donation} other {# donations}}",
"donations_deleted_player": "Deleted player",
"donations_empty": "No donations yet.",
"donations_filter_all": "All",
"donations_members_only": "Donations are visible to clan members only.",
"donations_period_all": "All time",
"donations_period_month": "30 days",
"donations_period_week": "7 days",
"donations_recent": "Recent",
"donations_top": "Top donors",
"donations_top_empty": "No donations in this period.",
"donations_unavailable": "Donations are unavailable.",
"elo_contributed": "{elo} ELO",
"elo_contributed_tooltip": "Clan Wars ELO this player has won or lost the clan",
Expand Down
47 changes: 47 additions & 0 deletions src/client/ClanApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
ClanBrowseResponseSchema,
type ClanBrowseSort,
type ClanDiscord,
type ClanDonationLeaderboardResponse,
ClanDonationLeaderboardResponseSchema,
type ClanDonationPeriod,
type ClanDonationsResponse,
ClanDonationsResponseSchema,
type ClanGameFilter,
Expand Down Expand Up @@ -39,7 +42,10 @@ export type {
ClanBrowseSort,
ClanDiscord,
ClanDonation,
ClanDonationLeaderboardResponse,
ClanDonationPeriod,
ClanDonationsResponse,
ClanDonor,
ClanGame,
ClanGameFilter,
ClanGamePlayer,
Expand Down Expand Up @@ -771,6 +777,47 @@ export async function fetchClanDonations(
}
}

/**
* Members-only ranking of current members by net donations of one currency,
* over all time or the last 7 (`week`) / 30 (`month`) days.
*/
export async function fetchClanDonationLeaderboard(
tag: string,
opts: {
currencyType: ClanCurrencyType;
period?: ClanDonationPeriod;
page?: number;
limit?: number;
},
): Promise<
ClanDonationLeaderboardResponse | { error: ClanDonationsFetchError }
> {
try {
const params = new URLSearchParams();
params.set("currencyType", opts.currencyType);
params.set("period", opts.period ?? "all");
params.set("page", String(opts.page ?? 1));
params.set("limit", String(opts.limit ?? 10));
const res = await clanFetch(
`/clans/${encodeURIComponent(tag)}/donations/leaderboard?${params}`,
);
if (res.status === 403) return { error: "forbidden" };
if (!res.ok) return { error: "failed" };
const json = await res.json();
const parsed = ClanDonationLeaderboardResponseSchema.safeParse(json);
if (!parsed.success) {
console.warn(
"fetchClanDonationLeaderboard: Zod validation failed",
parsed.error,
);
return { error: "failed" };
}
return parsed.data;
} catch {
return { error: "failed" };
}
}

export async function fetchClanBans(
tag: string,
page = 1,
Expand Down
Loading
Loading