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

58 lines
1.9 KiB
TypeScript
Raw Normal View History

import { Region } from '../../constants/regions';
2023-07-11 19:54:57 +00:00
import { Account, AccountList } from '../../types/accountList';
2023-07-16 18:26:33 +00:00
import { secrets } from '../node/secrets';
2023-07-11 19:54:57 +00:00
import getWargamingResponse from './getWargamingResponse';
2023-05-16 02:02:50 +00:00
export type AccountListWithServer = (Account & {
server: 'com' | 'eu' | 'asia';
})[];
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-07-16 18:26:33 +00:00
`https://api.wotblitz.com/wotb/account/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
2023-05-28 16:56:15 +00:00
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'com' as Region,
2023-05-28 16:56:15 +00:00
})),
),
getWargamingResponse<AccountList>(
2023-07-16 18:26:33 +00:00
`https://api.wotblitz.eu/wotb/account/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
2023-05-28 16:56:15 +00:00
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'eu' as Region,
2023-05-28 16:56:15 +00:00
})),
),
getWargamingResponse<AccountList>(
2023-07-16 18:26:33 +00:00
`https://api.wotblitz.asia/wotb/account/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
2023-05-28 16:56:15 +00:00
).then(
(value) =>
value &&
value.map((account) => ({
...account,
server: 'asia' as Region,
2023-05-28 16:56:15 +00:00
})),
),
])
)
.filter(Boolean)
.flat();
} else {
return [];
}
2023-05-16 02:02:50 +00:00
}