2023-05-30 17:24:24 +00:00
|
|
|
import { go } from 'fuzzysort';
|
2023-06-06 16:50:33 +00:00
|
|
|
import errorWithCause from '../process/errorWithCause.js';
|
2023-05-30 18:42:34 +00:00
|
|
|
import { TANKS, tankopedia } from './tankopedia.js';
|
2023-05-30 17:24:24 +00:00
|
|
|
|
2023-06-19 17:35:28 +00:00
|
|
|
export default async function resolveTankId(tank: string) {
|
2023-05-30 17:24:24 +00:00
|
|
|
const number = Number(tank);
|
|
|
|
|
|
|
|
|
|
if (Number.isNaN(number)) {
|
2023-05-30 18:42:34 +00:00
|
|
|
const searchResult = go(tank, TANKS, { keys: ['name'], limit: 1 });
|
2023-05-30 17:24:24 +00:00
|
|
|
|
|
|
|
|
if (searchResult.length === 0) {
|
2023-06-06 16:50:33 +00:00
|
|
|
throw errorWithCause(
|
|
|
|
|
'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 {
|
|
|
|
|
if (tankopedia[number]) {
|
|
|
|
|
return number;
|
|
|
|
|
} else {
|
2023-06-06 16:50:33 +00:00
|
|
|
throw errorWithCause(
|
|
|
|
|
'Tank not found',
|
|
|
|
|
`Could not find tank by the ID "${number}".`,
|
|
|
|
|
);
|
2023-05-30 17:24:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|