All files / web/src/arcade-games/rithmomachia/components/capture HelperSelectionOptions.tsx

0% Statements 0/162
0% Branches 0/1
0% Functions 0/1
0% Lines 0/162

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                                                                                                                                                                                                                                                                                                     
'use client'

import { useState } from 'react'
import { useAbacusSettings } from '@/hooks/useAbacusSettings'
import { useCaptureContext } from '../../contexts/CaptureContext'
import { getRelationColor, getRelationOperator } from '../../constants/captureRelations'
import type { Piece } from '../../types'
import { AnimatedHelperPiece } from './AnimatedHelperPiece'

interface HelperSelectionOptionsProps {
  helpers: Array<{ piece: Piece; boardPos: { x: number; y: number } }>
}

/**
 * Helper piece selection - pieces fly from board to selection ring
 * Hovering over a helper shows a preview of the number bond
 */
export function HelperSelectionOptions({ helpers }: HelperSelectionOptionsProps) {
  const { layout, pieces, selectedRelation, closing, selectHelper } = useCaptureContext()
  const { targetPos, cellSize, gap, padding } = layout
  const { mover: moverPiece, target: targetPiece } = pieces
  const relation = selectedRelation!

  // Get abacus settings
  const { data: abacusSettings } = useAbacusSettings()
  const useNativeAbacusNumbers = abacusSettings?.nativeAbacusNumbers ?? false
  const [hoveredHelperId, setHoveredHelperId] = useState<string | null>(null)
  const maxRadius = cellSize * 1.2
  const angleStep = helpers.length > 1 ? 360 / helpers.length : 0

  console.log('[HelperSelectionOptions] targetPos:', targetPos)
  console.log('[HelperSelectionOptions] cellSize:', cellSize)
  console.log('[HelperSelectionOptions] maxRadius:', maxRadius)
  console.log('[HelperSelectionOptions] angleStep:', angleStep)
  console.log('[HelperSelectionOptions] helpers.length:', helpers.length)

  // Find the hovered helper and its ring position
  const hoveredHelperData = helpers.find((h) => h.piece.id === hoveredHelperId)
  const hoveredHelperIndex = helpers.findIndex((h) => h.piece.id === hoveredHelperId)
  let hoveredHelperRingPos = null
  if (hoveredHelperIndex !== -1) {
    const angle = hoveredHelperIndex * angleStep
    const rad = (angle * Math.PI) / 180
    hoveredHelperRingPos = {
      x: targetPos.x + Math.cos(rad) * maxRadius,
      y: targetPos.y + Math.sin(rad) * maxRadius,
    }
  }

  const color = getRelationColor(relation)
  const operator = getRelationOperator(relation)

  return (
    <g>
      {helpers.map(({ piece, boardPos }, index) => {
        const angle = index * angleStep
        const rad = (angle * Math.PI) / 180

        // Target position in ring
        const ringX = targetPos.x + Math.cos(rad) * maxRadius
        const ringY = targetPos.y + Math.sin(rad) * maxRadius

        console.log(
          `[HelperSelectionOptions] piece ${piece.id} (${piece.square}): index=${index}, angle=${angle}°, boardPos=(${boardPos.x}, ${boardPos.y}), ringPos=(${ringX}, ${ringY})`
        )

        return (
          <AnimatedHelperPiece
            key={piece.id}
            piece={piece}
            boardPos={boardPos}
            ringX={ringX}
            ringY={ringY}
            cellSize={cellSize}
            onSelectHelper={selectHelper}
            closing={closing}
            useNativeAbacusNumbers={useNativeAbacusNumbers}
            onHover={setHoveredHelperId}
          />
        )
      })}

      {/* Show number bond preview when hovering over a helper - draw triangle between actual pieces */}
      {hoveredHelperData && hoveredHelperRingPos && (
        <g>
          {(() => {
            // Use actual positions of all three pieces
            const helperPos = hoveredHelperRingPos // Helper is in the ring
            const moverBoardPos = hoveredHelperData.boardPos // Mover is on the board at its current position
            const targetBoardPos = targetPos // Target is on the board at capture position

            // Calculate positions from square coordinates
            const file = moverPiece.square.charCodeAt(0) - 65
            const rank = Number.parseInt(moverPiece.square.slice(1), 10)
            const row = 8 - rank
            const moverPos = {
              x: padding + file * (cellSize + gap) + cellSize / 2,
              y: padding + row * (cellSize + gap) + cellSize / 2,
            }

            const targetFile = targetPiece.square.charCodeAt(0) - 65
            const targetRank = Number.parseInt(targetPiece.square.slice(1), 10)
            const targetRow = 8 - targetRank
            const targetBoardPosition = {
              x: padding + targetFile * (cellSize + gap) + cellSize / 2,
              y: padding + targetRow * (cellSize + gap) + cellSize / 2,
            }

            return (
              <>
                {/* Triangle connecting lines between actual piece positions */}
                <g opacity={0.5}>
                  <line
                    x1={moverPos.x}
                    y1={moverPos.y}
                    x2={helperPos.x}
                    y2={helperPos.y}
                    stroke={color}
                    strokeWidth={4}
                  />
                  <line
                    x1={moverPos.x}
                    y1={moverPos.y}
                    x2={targetBoardPosition.x}
                    y2={targetBoardPosition.y}
                    stroke={color}
                    strokeWidth={4}
                  />
                  <line
                    x1={helperPos.x}
                    y1={helperPos.y}
                    x2={targetBoardPosition.x}
                    y2={targetBoardPosition.y}
                    stroke={color}
                    strokeWidth={4}
                  />
                </g>

                {/* Operator symbol in center of triangle */}
                <text
                  x={(moverPos.x + helperPos.x + targetBoardPosition.x) / 3}
                  y={(moverPos.y + helperPos.y + targetBoardPosition.y) / 3}
                  textAnchor="middle"
                  dominantBaseline="central"
                  fill={color}
                  fontSize={cellSize * 0.8}
                  fontWeight="900"
                  fontFamily="Georgia, 'Times New Roman', serif"
                  opacity={0.9}
                >
                  {operator}
                </text>

                {/* No cloned pieces - using actual pieces already on board/ring */}
              </>
            )
          })()}
        </g>
      )}
    </g>
  )
}