mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-22 03:07:04 +02:00
36 lines
943 B
TypeScript
36 lines
943 B
TypeScript
import { go } from 'fuzzysort';
|
|
import {
|
|
tankDefinitions,
|
|
tanksDefinitionsArray,
|
|
} from '../blitzkrieg/definitions/tanks';
|
|
import { UserError } from '../blitzkrieg/userError';
|
|
|
|
export default async function resolveTankId(tank: string | number) {
|
|
const number = typeof tank === 'string' ? parseInt(tank) : tank;
|
|
|
|
if (Number.isNaN(number)) {
|
|
const searchResult = go(`${tank}`, await tanksDefinitionsArray, {
|
|
keys: ['name'],
|
|
limit: 1,
|
|
});
|
|
|
|
if (searchResult.length === 0) {
|
|
throw new UserError(
|
|
'Tank not found',
|
|
`Could not find tank by the name "${tank}".`,
|
|
);
|
|
} else {
|
|
return searchResult[0].obj.id;
|
|
}
|
|
} else {
|
|
if ((await tankDefinitions)[number]) {
|
|
return number;
|
|
} else {
|
|
throw new UserError(
|
|
'Tank not found',
|
|
`Could not find tank by the ID "${number}". Try typing the name of the tank instead of a number.`,
|
|
);
|
|
}
|
|
}
|
|
}
|