Files
blitzkit/packages/core/src/blitz/searchPlayersAcrossRegions/index.ts
T

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-09-08 10:17:40 -05:00
import { fetchBlitz, Region } from '@blitzkit/core';
2024-03-12 23:36:56 -05:00
import { usernamePattern } from './constants';
2023-05-16 02:02:50 +00:00
2023-11-08 12:17:08 -06:00
export interface AccountListItem {
2023-09-18 23:05:19 +00:00
nickname: string;
account_id: number;
}
2023-11-08 12:17:08 -06:00
export type AccountList = AccountListItem[];
export type AccountListWithServer = (AccountListItem & {
2023-09-18 23:05:19 +00:00
region: Region;
2023-05-16 02:02:50 +00:00
})[];
2024-09-08 10:11:24 -05:00
export async function searchPlayersAcrossRegions(search: string, limit = 25) {
2024-12-14 13:27:45 -06:00
const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) return [];
2023-11-01 11:45:19 -05:00
const normalizedLimit = Math.floor(limit / 3);
2023-06-02 00:28:34 +00:00
2024-12-14 13:27:45 -06:00
if (usernamePattern.test(trimmed)) {
2023-05-28 16:56:15 +00:00
return (
await Promise.all([
2024-04-15 13:19:25 -05:00
fetchBlitz<AccountList>('com', 'account/list', {
search,
limit: normalizedLimit,
}).then(
2023-05-28 16:56:15 +00:00
(value) =>
value &&
value.map((account) => ({
...account,
2023-09-18 23:05:19 +00:00
region: 'com' as Region,
2023-05-28 16:56:15 +00:00
})),
),
2024-04-15 13:19:25 -05:00
fetchBlitz<AccountList>('eu', 'account/list', {
search,
limit: normalizedLimit,
}).then(
2023-05-28 16:56:15 +00:00
(value) =>
value &&
value.map((account) => ({
...account,
2023-09-18 23:05:19 +00:00
region: 'eu' as Region,
2023-05-28 16:56:15 +00:00
})),
),
2024-04-15 13:19:25 -05:00
fetchBlitz<AccountList>('asia', 'account/list', {
search,
limit: normalizedLimit,
}).then(
2023-05-28 16:56:15 +00:00
(value) =>
value &&
value.map((account) => ({
...account,
2023-09-18 23:05:19 +00:00
region: 'asia' as Region,
2023-05-28 16:56:15 +00:00
})),
),
])
)
.filter(Boolean)
2023-09-18 23:05:19 +00:00
.flat() as AccountListWithServer;
2023-05-28 16:56:15 +00:00
} else {
2023-09-18 23:05:19 +00:00
return [] as AccountListWithServer;
2023-05-28 16:56:15 +00:00
}
2023-05-16 02:02:50 +00:00
}
2024-09-08 10:21:08 -05:00
export * from './constants';