import React from 'react' import type { GameHandle, Selection } from './types.ts' import BoardSlot from './BoardSlot.tsx' import CardToken from './cards/Card.tsx' import { isAttackSelection, isMoveSelection, isPlayCardSelection, isAlly, isBoard, isHand, } from './selection.tsx' type GameProps = GameHandle export default function Game(props: GameProps): JSX.Element { const { team, board, player, deck, enemyLife, enemyDeckSize, enemyHandSize, currentTurn, canDraw, hasDrawn, gameStatus, drawChoices, startTurn, endTurn, startDraw, commitDraw, attackCard, moveCard, playCard, getView, } = props const [selection, setSelection] = React.useState(null) function selectCard(nextSelection: Selection): void { if (selection === null) { setSelection(nextSelection) } else { if (isAttackSelection(selection, nextSelection)) { attackCard(selection.index, nextSelection.index) } else if (isMoveSelection(selection, nextSelection)) { moveCard(selection.index, nextSelection.index) } else if (isPlayCardSelection(selection, nextSelection)) { playCard(selection.index, nextSelection.index) } setSelection(null) } } React.useEffect(() => { if (currentTurn === team) { startTurn() } }, [currentTurn, startTurn]) React.useEffect(() => { const intervalId = setInterval( () => { getView() }, 1000, ) return () => { clearInterval(intervalId) } }, [getView]) const enemyBoard = team === 1 ? board.scourge : board.sentinal const allyBoard = team === 1 ? board.sentinal : board.scourge const isMyTurn = currentTurn === team return (

Opponent

Life: {enemyLife}

Deck: {enemyDeckSize}

{isMyTurn ? 'My' : 'Enemy'} Turn

{isMyTurn && canDraw && } {isMyTurn && hasDrawn && }

Life: {player.life}

Deck: {deck.cards.length}

{Array(enemyHandSize).fill(null).map(() => ( ))}
{enemyBoard.map((card, index) => ( selectCard({target: 'opponent', type: 'board', index})} isSelected={ selection ? !isAlly(selection) && isBoard(selection) && selection.index === index : false } disabled={!(currentTurn === team && hasDrawn)} /> ))}
{allyBoard.map((card, index) => ( selectCard({target: 'ally', type: 'board', index})} isSelected={ selection ? isAlly(selection) && isBoard(selection) && selection.index === index : false } disabled={!(currentTurn === team && hasDrawn)} /> ))}
{drawChoices.length > 0 && (
{drawChoices.map((card, index) => ( selectCard({target: 'ally', type: 'draws', index})} isSelected={ selection ? selection.type === 'draws' && selection.index === index : false } disabled={false} /> ))}
)}
{player.hand.map((card, index) => ( selectCard({target: 'ally', type: 'hand', index})} isSelected={ selection ? isAlly(selection) && isHand(selection) && selection.index === index : false } disabled={!(isMyTurn && hasDrawn)} /> ))}
) }