2023-05-30 17:24:24 +00:00
|
|
|
import { go } from 'fuzzysort';
|
2024-02-17 10:20:43 -06:00
|
|
|
import { UserError } from '../../hooks/userError';
|
2024-01-21 18:29:32 -06:00
|
|
|
import markdownEscape from 'markdown-escape';
|
2023-12-05 17:36:18 -06:00
|
|
|
import {
|
|
|
|
|
tankDefinitions,
|
|
|
|
|
tanksDefinitionsArray,
|
2023-12-14 22:19:01 -06:00
|
|
|
} from '../blitzkrieg/tankDefinitions';
|
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-12-05 17:36:18 -06:00
|
|
|
const searchResult = go(`${tank}`, await tanksDefinitionsArray, {
|
2023-10-14 11:15:42 -05:00
|
|
|
keys: ['name'],
|
|
|
|
|
limit: 1,
|
|
|
|
|
});
|
2023-05-30 17:24:24 +00:00
|
|
|
|
|
|
|
|
if (searchResult.length === 0) {
|
2024-01-19 16:07:15 -06:00
|
|
|
throw new UserError(
|
2024-01-21 18:46:38 -06:00
|
|
|
`# Tank not found\nCould not find tank by the name "${markdownEscape(`${tank}`)}".`,
|
2024-01-19 16:07:15 -06:00
|
|
|
);
|
2023-05-30 17:24:24 +00:00
|
|
|
} else {
|
2023-11-29 12:17:21 -06:00
|
|
|
return searchResult[0].obj.id;
|
2023-05-30 17:24:24 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-12-05 17:36:18 -06:00
|
|
|
if ((await tankDefinitions)[number]) {
|
2023-05-30 17:24:24 +00:00
|
|
|
return number;
|
|
|
|
|
} else {
|
2024-01-19 16:07:15 -06:00
|
|
|
throw new UserError(
|
2024-01-21 18:29:32 -06:00
|
|
|
`# Tank not found\nCould not find tank by the ID "${number}". Try typing the name of the tank instead of a number.`,
|
2024-01-19 16:07:15 -06:00
|
|
|
);
|
2023-05-30 17:24:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|