'use client'; import { useState } from 'react'; import type { Action, GameView } from '@/game/engine'; import { splitLocation } from '@/game/board'; import { Button } from './ui/button'; import { Input } from './ui/input'; export function IxSubstitution({ game: g, act, busy, }: { game: GameView; act: (action: Action) => void; busy: boolean; }) { const [sources, setSources] = useState>({}); const [recover, setRecover] = useState>({}); if (g.decision?.kind !== 'ixSubstitution') return null; const d = g.decision; const me = g.players.find((p) => p.id === g.me)!; const world = g.combatLocations?.find( (place) => place.id === d.territory && place.kind === 'homeworld', ); const available = ( world ? [[world.id, world.forces?.[me.id]?.normal ?? 0] as const] : Object.entries(me.forces) .filter(([key]) => splitLocation(key).territory === d.territory) .map( ([key, count]) => [key, count - (me.elites?.forces[key] ?? 0)] as const, ) ).filter(([, count]) => count > 0); const total = (value: Record) => Object.values(value).reduce((a, b) => a + b, 0); const invalid = ( selected: Record, limits: Record, ) => Object.entries(selected).some( ([key, count]) => !Number.isSafeInteger(count) || count < 0 || count > (limits[key] ?? 0), ); return ( <>

Exchange surviving suboids, one for one, to retain cyborgs lost in this battle. Retained cyborgs return to{' '} {world ? world.name : 'their casualty sectors'}. Opponents may cancel this substitution with Karama.

Suboids to sacrifice

{available.map(([key, max]) => ( ))}

Cyborgs to retain

{Object.entries(d.losses).map(([key, max]) => ( ))}

{total(sources)} suboids for {total(recover)} cyborgs

); }