mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 03:59:50 +02:00
feat: generalize render chars
This commit is contained in:
1 parent
99bae8f2c5
commit
698731cb9a
4 files changed
+41
-36
No files matched your search
@@ -84,7 +84,7 @@ export const characteristics = {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
reloads({ characteristic, attribute, parameters }) {
|
reloads({ characteristic, parameters }) {
|
||||||
const gunType = characteristic("gun_type")!;
|
const gunType = characteristic("gun_type")!;
|
||||||
|
|
||||||
if (gunType !== GunType.AutoReloader) return null;
|
if (gunType !== GunType.AutoReloader) return null;
|
||||||
|
|||||||
@@ -5,14 +5,16 @@ export enum BetterDirection {
|
|||||||
Lower,
|
Lower,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CharacteristicRenderConfig {
|
||||||
|
name: CharacteristicName;
|
||||||
|
decimals?: number;
|
||||||
|
units?: string;
|
||||||
|
}
|
||||||
|
|
||||||
type CharacteristicsOrder = {
|
type CharacteristicsOrder = {
|
||||||
group: string;
|
group: string;
|
||||||
order: (
|
order: (
|
||||||
| {
|
| CharacteristicRenderConfig
|
||||||
name: CharacteristicName;
|
|
||||||
decimals?: number;
|
|
||||||
units?: string;
|
|
||||||
}
|
|
||||||
| {
|
| {
|
||||||
toy: string;
|
toy: string;
|
||||||
}
|
}
|
||||||
@@ -25,11 +27,12 @@ export const characteristicsOrder: CharacteristicsOrder = [
|
|||||||
order: [
|
order: [
|
||||||
{ name: "gun_type" },
|
{ name: "gun_type" },
|
||||||
{ name: "dpm", decimals: 0, units: "hp_min" },
|
{ name: "dpm", decimals: 0, units: "hp_min" },
|
||||||
|
{ name: "reload", decimals: 2, units: "s" },
|
||||||
|
{ name: "reloads", decimals: 2, units: "s" },
|
||||||
{ name: "damage", units: "hp" },
|
{ name: "damage", units: "hp" },
|
||||||
{ name: "module_damage", units: "hp" },
|
{ name: "module_damage", units: "hp" },
|
||||||
{ name: "penetration", units: "mm" },
|
{ name: "penetration", units: "mm" },
|
||||||
{ name: "clip_size" },
|
{ name: "clip_size" },
|
||||||
{ name: "reload", decimals: 2, units: "s" },
|
|
||||||
{ name: "intra_clip", decimals: 2, units: "s" },
|
{ name: "intra_clip", decimals: 2, units: "s" },
|
||||||
|
|
||||||
{ toy: "reload" },
|
{ toy: "reload" },
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import type { CharacteristicOutput } from "./characteristics";
|
||||||
|
import type { CharacteristicRenderConfig } from "./characteristicsOrder";
|
||||||
|
|
||||||
|
export function renderCharacteristic(
|
||||||
|
characteristic: CharacteristicOutput,
|
||||||
|
config: CharacteristicRenderConfig
|
||||||
|
): string | null {
|
||||||
|
if (characteristic === null) return null;
|
||||||
|
|
||||||
|
if (typeof characteristic === "number") {
|
||||||
|
if (Number.isFinite(characteristic)) {
|
||||||
|
if (config.decimals === undefined) {
|
||||||
|
return characteristic.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return characteristic.toFixed(config.decimals);
|
||||||
|
}
|
||||||
|
|
||||||
|
return `${characteristic < 0 ? "-" : ""}∞`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return characteristic.map((c) => renderCharacteristic(c, config)).join(", ");
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ import { Suspense, useMemo } from "react";
|
|||||||
import { api } from "../../../core/api/dynamic";
|
import { api } from "../../../core/api/dynamic";
|
||||||
import { characteristicsOrder } from "../../../core/tankopedia/characteristicsOrder";
|
import { characteristicsOrder } from "../../../core/tankopedia/characteristicsOrder";
|
||||||
import { computeCharacteristics } from "../../../core/tankopedia/computeCharacteristics";
|
import { computeCharacteristics } from "../../../core/tankopedia/computeCharacteristics";
|
||||||
|
import { renderCharacteristic } from "../../../core/tankopedia/renderCharacteristic";
|
||||||
import { useAwait } from "../../../hooks/useAwait";
|
import { useAwait } from "../../../hooks/useAwait";
|
||||||
import { Tankopedia } from "../../../stores/tankopedia";
|
import { Tankopedia } from "../../../stores/tankopedia";
|
||||||
|
|
||||||
@@ -66,41 +67,19 @@ function Content({ id }: { id: string }) {
|
|||||||
<>
|
<>
|
||||||
<h2 key={group}>{group}</h2>
|
<h2 key={group}>{group}</h2>
|
||||||
|
|
||||||
{order.map((entry) => {
|
{order.map((config) => {
|
||||||
if ("toy" in entry) {
|
if ("toy" in config) {
|
||||||
return <p key={`toy-${entry.toy}`}>toy: {entry.toy}</p>;
|
return <p key={`toy-${config.toy}`}>toy: {config.toy}</p>;
|
||||||
} else {
|
} else {
|
||||||
const characteristic = characteristics[entry.name];
|
const characteristic = characteristics[config.name];
|
||||||
|
|
||||||
if (characteristic === null) return null;
|
if (characteristic === null) return null;
|
||||||
|
|
||||||
let rendered: string | number;
|
const rendered = renderCharacteristic(characteristic, config);
|
||||||
|
|
||||||
if (Number.isFinite(characteristic)) {
|
|
||||||
if (entry.decimals === undefined) {
|
|
||||||
if (typeof characteristic === "number") {
|
|
||||||
rendered = characteristic;
|
|
||||||
} else {
|
|
||||||
rendered = characteristic.join(", ");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (typeof characteristic === "number") {
|
|
||||||
rendered = characteristic.toFixed(entry.decimals);
|
|
||||||
} else {
|
|
||||||
rendered = characteristic
|
|
||||||
.map((c) => c.toFixed(entry.decimals))
|
|
||||||
.join(", ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
rendered = `${
|
|
||||||
Math.sign(characteristic as number) === -1 ? "-" : ""
|
|
||||||
}∞`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<p key={`characteristic-${entry.name}`}>
|
<p key={`characteristic-${config.name}`}>
|
||||||
{entry.name}: {rendered} {entry.units && `${entry.units}`}
|
{config.name}: {rendered} {config.units && `${config.units}`}
|
||||||
</p>
|
</p>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user