Files
blitzkit/src/core/blitz/listPlayers.ts
T

62 lines
2.0 KiB
TypeScript
Raw Normal View History

import { Region } from '../../constants/regions';
2023-09-23 21:51:00 +00:00
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
2023-07-11 19:54:57 +00:00
import getWargamingResponse from './getWargamingResponse';
2023-05-16 02:02:50 +00:00
2023-09-18 23:05:19 +00:00
interface Account {
nickname: string;
account_id: number;
}
2023-09-24 17:27:04 -05:00
export type AccountList = Account[];
2023-05-16 02:02:50 +00:00
export type AccountListWithServer = (Account & {
2023-09-18 23:05:19 +00:00
region: Region;
2023-05-16 02:02:50 +00:00
})[];
export const usernamePattern = /^[a-zA-Z0-9_]{3,24}$/;
export const usernamePatternWithoutPosition = /[a-zA-Z0-9_]{3,24}/;
2023-05-16 02:02:50 +00:00
2023-06-27 02:13:57 +00:00
export default async function listPlayers(search: string, limit = 9) {
2023-06-02 00:28:34 +00:00
const trimmedSearch = search.trim();
const normalizedLimit = Math.round(limit / 3);
const encodedSearch = encodeURIComponent(trimmedSearch);
if (usernamePattern.test(trimmedSearch)) {
2023-05-28 16:56:15 +00:00
return (
await Promise.all([
getWargamingResponse<AccountList>(
2023-09-23 21:51:00 +00:00
`https://api.wotblitz.com/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
2023-05-28 16:56:15 +00:00
).then(
(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
})),
),
getWargamingResponse<AccountList>(
2023-09-23 21:51:00 +00:00
`https://api.wotblitz.eu/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
2023-05-28 16:56:15 +00:00
).then(
(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
})),
),
getWargamingResponse<AccountList>(
2023-09-23 21:51:00 +00:00
`https://api.wotblitz.asia/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
2023-05-28 16:56:15 +00:00
).then(
(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
}