Files
blitzkit/packages/website/src/hooks/useTankCompatibility.ts
T

66 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-10-05 23:20:04 -05:00
import {
ConsumableTankCategoryFilterCategory,
2026-09-23 16:51:09 -05:00
GunDefinition,
TankDefinition,
TankInclusivityFilter,
} from "@blitzkit/protos";
2024-01-25 11:50:43 -06:00
2026-09-23 16:51:09 -05:00
export function useTankCompatibility(tank: TankDefinition, gun: GunDefinition) {
2026-09-23 10:39:12 -05:00
return function (
include?: TankInclusivityFilter[],
exclude?: TankInclusivityFilter[],
) {
2026-09-23 16:51:09 -05:00
return isTankCompatible(tank, gun, include, exclude);
2026-09-23 10:39:12 -05:00
};
}
2026-09-23 16:51:09 -05:00
export function isTankCompatible(
2024-01-25 11:50:43 -06:00
tank: TankDefinition,
2026-03-31 14:11:45 -05:00
gun: GunDefinition,
2026-09-23 10:39:12 -05:00
include?: TankInclusivityFilter[],
exclude?: TankInclusivityFilter[],
2024-01-25 11:50:43 -06:00
) {
2026-09-23 10:39:12 -05:00
const included = include?.every((rule) => {
2026-01-02 11:32:27 -06:00
switch (rule.filter_type!.$case) {
case "tiers":
return (
rule.filter_type!.value.min <= tank.tier &&
tank.tier <= rule.filter_type!.value.max
);
2024-01-25 11:50:43 -06:00
2026-01-02 11:32:27 -06:00
case "ids":
return rule.filter_type!.value.ids.includes(tank.id);
2024-01-25 11:50:43 -06:00
2026-01-02 11:32:27 -06:00
case "nations":
return rule.filter_type!.value.nations.includes(tank.nation);
2024-01-25 13:51:30 -06:00
2026-01-02 11:32:27 -06:00
case "categories":
throw new SyntaxError("Category filtering found in include rule");
}
});
2026-09-23 10:39:12 -05:00
const excluded = exclude?.some((rule) => {
2026-01-02 11:32:27 -06:00
switch (rule.filter_type!.$case) {
case "tiers":
return (
rule.filter_type!.value.min <= tank.tier &&
tank.tier <= rule.filter_type!.value.max
);
2024-01-25 11:50:43 -06:00
2026-01-02 11:32:27 -06:00
case "ids":
return rule.filter_type!.value.ids.includes(tank.id);
2024-01-25 11:50:43 -06:00
2026-01-02 11:32:27 -06:00
case "nations":
return rule.filter_type!.value.nations.includes(tank.nation);
2024-01-25 13:51:30 -06:00
2026-01-02 11:32:27 -06:00
case "categories":
return rule.filter_type!.value.categories.some((category) => {
switch (category) {
2026-03-31 14:11:45 -05:00
case ConsumableTankCategoryFilterCategory.CONSUMABLE_TANK_CATEGORY_FILTER_CATEGORY_CLIP:
2026-01-02 11:32:27 -06:00
return gun.gun_type!.$case !== "regular";
}
});
}
});
2024-01-25 11:50:43 -06:00
2026-09-23 10:39:12 -05:00
return include !== undefined && include?.length > 0 && included && !excluded;
2024-01-25 11:50:43 -06:00
}