2023-05-30 17:24:24 +00:00
|
|
|
import { go } from 'fuzzysort';
|
2023-10-14 11:36:48 -05:00
|
|
|
import throwError from '../blitzkrieg/throwError';
|
2023-10-14 11:25:42 -05:00
|
|
|
import { TANKS, tankopedia } from '../blitzstars/tankopedia';
|
2023-05-30 17:24:24 +00:00
|
|
|
|
2023-10-14 11:15:42 -05:00
|
|
|
export default async function resolveTankId(tank: string | number) {
|
|
|
|
|
const number = typeof tank === 'string' ? parseInt(tank) : tank;
|
2023-05-30 17:24:24 +00:00
|
|
|
|
|
|
|
|
if (Number.isNaN(number)) {
|
2023-10-14 11:15:42 -05:00
|
|
|
const searchResult = go(`${tank}`, await TANKS, {
|
|
|
|
|
keys: ['name'],
|
|
|
|
|
limit: 1,
|
|
|
|
|
});
|
2023-05-30 17:24:24 +00:00
|
|
|
|
|
|
|
|
if (searchResult.length === 0) {
|
2023-06-27 17:22:27 +00:00
|
|
|
throw throwError(
|
2023-06-06 16:50:33 +00:00
|
|
|
'Tank not found',
|
|
|
|
|
`Could not find tank by the name "${tank}".`,
|
|
|
|
|
);
|
2023-05-30 17:24:24 +00:00
|
|
|
} else {
|
|
|
|
|
return searchResult[0].obj.tank_id;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2023-07-05 02:09:36 +00:00
|
|
|
if ((await tankopedia)[number]) {
|
2023-05-30 17:24:24 +00:00
|
|
|
return number;
|
|
|
|
|
} else {
|
2023-06-27 17:22:27 +00:00
|
|
|
throw throwError(
|
2023-06-06 16:50:33 +00:00
|
|
|
'Tank not found',
|
|
|
|
|
`Could not find tank by the ID "${number}".`,
|
|
|
|
|
);
|
2023-05-30 17:24:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|