'use client'; import { useId, useState } from 'react'; import { Dialog as DialogPrimitive } from '@base-ui/react/dialog'; import { Eye, X } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogClose, DialogDescription, DialogOverlay, DialogPortal, DialogTitle, DialogTrigger, } from '@/components/ui/dialog'; import type { Action, GameView } from '@/game/engine'; import { territory } from '@/game/board'; import { moritaniPlacementChoices, nexusMoritaniAction } from '@/game/nexus-moritani-options'; import { TERROR_COMMON_GAMEPLAY, TERROR_DEFINITIONS, type TerrorKind, } from '@/game/moritani-terror'; type ProjectedToken = NonNullable['tokens'][number]; /** Original vector marks. A hidden token always uses the same face and geometry. */ function TerrorArt({ kind, large = false, }: { kind?: TerrorKind; large?: boolean; }) { return ( ); } function tokenContext(token: ProjectedToken) { if (token.status === 'placed' && token.location) return `Placed · ${territory(token.location).name}`; if (token.status === 'extortion') return 'Revealed · awaiting Mentat'; if (token.status === 'removed') return 'Revealed · removed from play'; return 'Private supply'; } function TokenInspector({ token, owner, }: { token: ProjectedToken; owner: boolean; }) { const revealed = token.status === 'removed' || token.status === 'extortion'; // Defense in depth: even an accidentally over-broad projection cannot render // an opponent's hidden face. Never derive a secret kind from a physical ID. const kind = (owner || revealed) && 'kind' in token ? token.kind : undefined; const definition = kind ? TERROR_DEFINITIONS[kind] : undefined; const title = definition?.name ?? 'Hidden Terror'; const context = tokenContext(token); return ( } aria-label={`Inspect ${title}: ${context}`} > {title} {context}
Moritani · Terror } >

{context}

{title}

{kind ? 'Original token illustration · gameplay guide' : 'This placed token remains face down. Its identity is visible only to Moritani.'}

{definition?.summary ?? 'A hidden Terror token marks this stronghold. Inspecting it does not reveal its identity or trigger an effect.'} {(definition?.gameplay ?? TERROR_COMMON_GAMEPLAY).map( (paragraph) => (

{paragraph}

), )}

Supported entry reactions include Robbery, Sabotage, Sneak Attack, ordinary native-leader Assassination and Extortion. Enemy of My Enemy has partial alliance controls. Atomics can be revealed only on supported tables; complete Moritani games remain unavailable. Inspection does not reveal a token or activate an effect.

); } export function MoritaniTerrorSupply({ game }: { game: GameView }) { const titleId = useId(); const view = game.moritaniTerror; const aftermath = game.moritaniAtomics; const affectedNames = aftermath ? [ game.players.find((player) => player.id === aftermath.moritaniId)?.name ?? 'Moritani', ...(aftermath.allyAtActivation ? [game.players.find((player) => player.id === aftermath.allyAtActivation)?.name ?? 'their ally at activation'] : []), ].join(' and ') : ''; if (!view) return null; const owner = game.players.find((p) => p.id === game.me)?.faction === 'moritani'; const tokens = view.tokens.filter( (token) => owner || token.status !== 'available', ); return (

Moritani Terror

{owner ? 'Your supply and placed tokens. Inspect a face without revealing it to the table.' : 'Placed tokens stay hidden. Revealed tokens can be inspected by everyone.'}

{aftermath && (

Atomics Aftermath · {territory(aftermath.territory).name}

From turn {aftermath.turn}, no faction may ship forces into this territory, including Fremen reinforcements. Otherwise legal ordinary movement and Sneak Attack remain possible.

A lasting hand-limit reduction of one applies to {affectedNames}. Any excess cards were discarded at random when Atomics resolved. The provisional penalty stays on these original players if alliances later change; a new ally does not inherit it.

)} {tokens.length ? (
{tokens.map((token) => ( ))}
) : (

No public Terror tokens.

)}
); } export function MoritaniPlacement({ game, act, busy, }: { game: GameView; act: (action: Action) => void; busy: boolean; }) { const formId = useId(); const [useNexus, setUseNexus] = useState(false); const [selectedToken, setSelectedToken] = useState(''); const [selectedTerritory, setSelectedTerritory] = useState(''); const decision = game.decision; if ( decision?.kind !== 'moritaniPlacement' || decision.player !== game.me || game.players.find((p) => p.id === game.me)?.faction !== 'moritani' || !game.moritaniTerror ) return null; const nexus = useNexus && !!game.nexusMoritani; const { tokens, token, destinations } = moritaniPlacementChoices(game, nexus, selectedToken); const destination = destinations.includes(selectedTerritory) ? selectedTerritory : destinations[0]; const waiting = Boolean(game.response); const disabled = busy || waiting; const nexusAction = nexus && token && destination ? nexusMoritaniAction(game, game.nexusMoritani!.event, token.id, destination) : null; return (

Place or relocate Terror

{nexus ? 'Choose one available supply token and a projected Arrakis territory for this Mentat Pause. Existing Terror tokens may remain there.' : 'Choose one token and a stronghold without a Terror token for this Mentat Pause.'}{' '} Storm does not prevent placement. Choose privately; the token’s face stays hidden from opponents.

{game.nexusMoritani && <>

Cunning places one available supply token in an Arrakis territory, including one with Terror already present. It spends the Nexus card and this Mentat placement opportunity. It never enhances relocation or permits Homeworlds or the Hidden Mobile Stronghold. High-Grumman Cunning additions use their separate Collection controls and require an ordinary eligible addition.

{game.nexusMoritani.blocked &&

{game.nexusMoritani.blocked}

} }
{waiting ? 'Waiting for the table’s Karama response. The token has not moved.' : 'The table may use Karama before your placement or relocation takes effect.'}
); } /** The card identity is private to the recipient and never part of the public * Terror marker or another seat's controls. */ export function SabotageGiftDiscardButton({ card, act, disabled }: { card: string | null; act: (action: Action) => void; disabled: boolean; }) { return ( ); } export function MoritaniEntry({ game, act, busy, }: { game: GameView; act: (action: Action) => void; busy: boolean; }) { const formId = useId(); const [selectedCard, setSelectedCard] = useState(''); const [selectedAmount, setSelectedAmount] = useState(0); const entry = game.terrorEntry; const me = game.players.find((player) => player.id === game.me); if ( game.decision?.kind !== 'moritaniTerror' || game.decision.player !== game.me || !entry || !me ) return null; const disabled = busy || Boolean(game.response) || Boolean(game.truthtrance); const controlClass = 'h-auto min-h-11 whitespace-normal motion-reduce:transition-none'; // The giver's selection is already committed. Only the entrant sees their // own actual hand and chooses one card to restore their hand limit. if (entry.stage === 'giftDiscard') { if (entry.entrant !== game.me) return null; const hand = me.hand ?? []; const card = hand.find(held => held.id === selectedCard) ?? hand[0]; const unavailable = disabled || !!game.phaseOpening || !!game.automaticContinuationPending; return (

Sabotage gift · discard a card

You received a Treachery card and are over your hand limit. Choose one card from your hand to discard, including the card you just received. This recipient-choice procedure is a provisional interpretation.

); } // This entrant branch must precede every private kind, definition and token lookup. if (entry.stage === 'allianceReply') { if (entry.entrant !== game.me) return null; const moritani = game.players.find( (player) => player.faction === 'moritani', ); if (!moritani) return null; return (

Enemy of My Enemy

{moritani.name} offers you an alliance after your arrival in{' '} {territory(entry.territory).name}. The Terror token remains hidden.

Accepting forms your alliance with Moritani, ends any existing alliances either of you has, and returns the token to Moritani’s supply without revealing it. Refusing requires the token to be revealed and its effect to resolve.

{[me, moritani] .filter((player) => player.ally) .map((player) => { const formerPartner = game.players.find( (partner) => partner.id === player.ally, ); return (

{player.name}’s alliance with{' '} {formerPartner?.name ?? 'their current ally'} will end if you accept.

); })}
); } // Stacked tokens are selected privately before any selected-kind lookup. if (entry.stage === 'select') { if (me.faction !== 'moritani') return null; const selectionDisabled = disabled || game.status !== 'playing' || !!game.phaseOpening || !!game.automaticContinuationPending; return (

Choose a Terror token

Choose one token in {territory(entry.territory).name} for this arrival. Selecting a token keeps its face private; its reaction choices follow. You may instead leave all tokens hidden and continue the arrival.

{entry.candidates?.map((candidate) => (
{candidate.revealBlocked && (

{candidate.revealBlocked}

)} {candidate.canOfferAlliance && (

An alliance offer is available after selection.

)}
))}
); } if (me.faction !== 'moritani' || !('kind' in entry) || !entry.kind) return null; const definition = TERROR_DEFINITIONS[entry.kind]; const token = game.moritaniTerror?.tokens.find( (candidate) => 'kind' in candidate && candidate.kind === entry.kind, ); const entrant = game.players.find((player) => player.id === entry.entrant); const hand = me.hand ?? []; const selected = hand.find((card) => card.id === selectedCard); const discardCard = selected ?? hand[0]; const giving = entry.stage === 'gift'; const sneak = entry.sneakAttack; const maximum = sneak?.maximum ?? 0; const amount = selectedAmount <= maximum ? selectedAmount : 0; const arrival = { shipment: 'by shipment', movement: 'by movement', guildTransport: 'by Guild transport', advisor: 'as an accompanying advisor', wormRide: 'by worm ride', ambassador: 'through the Fremen Ambassador', homeworldRevival: 'through Homeworld revival', caladanReinforcement: 'through Caladan reinforcement', }[entry.cause]; return (

{definition.name} · Terror reaction

{entrant?.name ?? 'Another faction'} entered{' '} {territory(entry.territory).name}, sector {entry.sector}, {arrival}.

{entry.revivalArrival &&

The revived group is already placed. Finish this Terror effect and any Ambassador reaction before the original revival continues. Resolving this entry uses no ordinary shipment or additional revival payment.

} {token && (
)}
{entry.stage === 'offer' && ( <>

You may reveal this token against the entrant, or leave it hidden and continue their action. Inspection keeps its face private.

{entry.kind === 'assassination' && entry.canReveal && (

Revealing Assassination immediately selects a random eligible native leader. You do not choose the victim.

)} {entry.kind === 'atomics' && entry.canReveal && (

Revealing Atomics sends every faction’s forces in{' '} {territory(entry.territory).name} to the Tanks, including your own. The public Aftermath permanently blocks shipments into that territory, including Fremen reinforcements, but not otherwise legal movement or Sneak Attack. Your hand limit falls by one starting this turn; your ally’s at activation does too, if you have one. Each hand over its new limit discards one random card per excess card. Under the provisional persistent-target interpretation, later alliance changes do not move this penalty to a new ally. A caught concealed No-Field provisionally reveals and places only its available reserve forces before those forces go to the Tanks.

)} {entry.kind === 'sneakAttack' && sneak && (

Up to {maximum} of your reserve forces are available. {sneak.blocked ? ` Positive entry is blocked: ${sneak.blocked}` : ' They may enter this territory and sector for free.'}

You may leave this token hidden, or reveal it and choose how many forces to send. Revealing spends the token even if you send no forces; zero remains available when entry is blocked.

)} {entry.canOfferAlliance ? (

Enemy of My Enemy lets you offer an alliance before revealing this token. Acceptance ends both existing alliances and returns the token hidden. Refusal requires its effect to resolve. The table’s Karama response comes before the entrant decides.

) : entry.allianceBlockedReason ? (

Enemy of My Enemy: {entry.allianceBlockedReason}

) : null} {entry.canReveal ? ( ) : (

{entry.revealBlocked ?? `${definition.name} is still under implementation.`}{' '} Leave the token hidden to continue this arrival.

)}

Robbery, Sabotage, Sneak Attack, ordinary native-leader Assassination and Extortion support eligible arrivals. Atomics is available only on supported tables. Enemy of My Enemy has partial alliance controls; complete Moritani games remain unavailable.

)} {entry.stage === 'robbery' && ( <>

Take half the entrant’s spice, rounded up, or draw the top Treachery Card. A draw above your hand limit requires you to choose a card to discard afterward.

)} {entry.stage === 'sneakAttack' && ( <>

Send forces from your reserves into{' '} {territory(entry.territory).name}, sector {entry.sector}, for free. This entry does not use your ordinary shipment or movement turn. You may send no forces.

{sneak?.blocked ?? `Up to ${maximum} forces are available for this entry.`}

)} {(entry.stage === 'discard' || giving) && ( <>

{giving ? 'Sabotage’s random discard has resolved where possible. You may now give the entrant one of your own cards, or keep your hand. If the gift exceeds their limit, they choose a card to discard under a provisional procedure.' : 'Your Robbery draw exceeded your hand limit. Choose one of your own cards to discard, including the card just drawn.'}

)}
); }