2023-07-15 18:44:20 +00:00
|
|
|
import { Region } from '../../constants/regions';
|
2023-09-23 21:51:00 +00:00
|
|
|
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
|
2023-10-20 10:50:54 -05:00
|
|
|
import fetchBlitz from './fetchBlitz';
|
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
|
|
|
})[];
|
|
|
|
|
|
2023-06-03 18:05:09 +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-10-14 11:15:42 -05:00
|
|
|
export default async function searchPlayersAcrossRegions(
|
|
|
|
|
search: string,
|
2023-11-01 11:45:19 -05:00
|
|
|
limit = 25,
|
2023-10-14 11:15:42 -05:00
|
|
|
) {
|
2023-06-02 00:28:34 +00:00
|
|
|
const trimmedSearch = search.trim();
|
2023-11-01 11:45:19 -05:00
|
|
|
const normalizedLimit = Math.floor(limit / 3);
|
2023-06-02 00:28:34 +00:00
|
|
|
const encodedSearch = encodeURIComponent(trimmedSearch);
|
|
|
|
|
|
2023-06-03 18:05:09 +00:00
|
|
|
if (usernamePattern.test(trimmedSearch)) {
|
2023-05-28 16:56:15 +00:00
|
|
|
return (
|
|
|
|
|
await Promise.all([
|
2023-10-14 10:58:48 -05:00
|
|
|
fetchBlitz<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
|
|
|
})),
|
|
|
|
|
),
|
2023-10-14 10:58:48 -05:00
|
|
|
fetchBlitz<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
|
|
|
})),
|
|
|
|
|
),
|
2023-10-14 10:58:48 -05:00
|
|
|
fetchBlitz<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
|
|
|
}
|