diff --git a/src/app/tools/tankopedia/[id]/components/Model/TechTreeSection.tsx b/src/app/tools/tankopedia/[id]/components/Model/TechTreeSection.tsx index 8f82fac94..ba181b169 100644 --- a/src/app/tools/tankopedia/[id]/components/Model/TechTreeSection.tsx +++ b/src/app/tools/tankopedia/[id]/components/Model/TechTreeSection.tsx @@ -1,9 +1,18 @@ -import { ChevronRightIcon } from '@radix-ui/react-icons'; -import { Box, ChevronDownIcon, Flex, Heading, Text } from '@radix-ui/themes'; +import { CaretRightIcon, ChevronRightIcon } from '@radix-ui/react-icons'; +import { + ChevronDownIcon, + Flex, + Heading, + ScrollArea, + Text, +} from '@radix-ui/themes'; import Link from 'next/link'; -import { ComponentProps, use } from 'react'; +import { ComponentProps, use, useMemo } from 'react'; import { asset } from '../../../../../../core/blitzkit/asset'; -import { tankDefinitions } from '../../../../../../core/blitzkit/tankDefinitions'; +import { + TankDefinition, + tankDefinitions, +} from '../../../../../../core/blitzkit/tankDefinitions'; import { TIER_ROMAN_NUMERALS } from '../../../../../../core/blitzkit/tankDefinitions/constants'; import { formatCompact } from '../../../../../../core/math/formatCompact'; import * as Duel from '../../../../../../stores/duel'; @@ -49,158 +58,147 @@ export function TreeArrow({ ); } -export function TechTreeSection() { +function Card({ id }: { id: number }) { const awaitedTankDefinitions = use(tankDefinitions); - const tank = Duel.use((state) => state.protagonist.tank); + const ancestor = awaitedTankDefinitions[id]; - if (tank.treeType !== 'researchable') return null; + return ( + + + {ancestor.name} - function Card({ id }: { id: number }) { - const ancestor = awaitedTankDefinitions[id]; - - return ( - - {ancestor.name} + + + {TIER_ROMAN_NUMERALS[ancestor.tier]} + + {ancestor.name} + - - - - {TIER_ROMAN_NUMERALS[ancestor.tier]} + + + XP + + {ancestor.xp === undefined ? 0 : formatCompact(ancestor.xp!)} - {ancestor.name} - - - XP - - {formatCompact(ancestor.xp!)} - - - - - Silver - - {formatCompact(ancestor.price.value)} - - + + Silver + + {formatCompact(ancestor.price.value)} + - - ); + + + ); +} + +export function TechTreeSection() { + const tank = Duel.use((state) => state.protagonist.tank); + const awaitedTankDefinitions = use(tankDefinitions); + + if ( + tank.treeType !== 'researchable' || + tank.ancestors === undefined || + tank.ancestors.length === 0 + ) { + return null; } + const ancestors = useMemo(() => { + const treeBackwards: number[] = []; + let ancestor: TankDefinition | undefined = tank; + + while (ancestor !== undefined) { + if (ancestor.ancestors === undefined || ancestor.ancestors.length === 0) { + ancestor = undefined; + break; + } else if (ancestor.ancestors.length === 1) { + ancestor = awaitedTankDefinitions[ancestor.ancestors[0]]; + } else { + let bestAncestor = awaitedTankDefinitions[ancestor.ancestors[0]]; + + ancestor.ancestors.forEach((thisAncestorId) => { + const possibleAncestor = awaitedTankDefinitions[thisAncestorId]; + + if (possibleAncestor.xp! < bestAncestor.xp!) { + bestAncestor = possibleAncestor; + } + }); + + ancestor = bestAncestor as TankDefinition; + } + + treeBackwards.push(ancestor.id); + } + + return treeBackwards.reverse(); + }, []); + + console.log(ancestors); + return ( - + Tech tree - Tech tree - - - {tank.ancestors && ( - <> - - {tank.ancestors.map((id) => ( - - ))} - - - - - - - - - - )} - - - - {tank.successors && ( - <> - - - - - - - - - {tank.successors.map((id) => ( - - ))} - - - )} - + + + {ancestors.map((id, index) => ( + <> + {index > 0 && ( + + + + )} + + + ))} + + ); }