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 const GAME_STATUS_SENTINAL_WIN = 4 const GAME_STATUS_SCOURGE_WIN = 5 export default function Game(props: GameProps): JSX.Element { const { team, board, player, deckSize, 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]) const enemyBoard = team === 1 ? board.scourge : board.sentinal const allyBoard = team === 1 ? board.sentinal : board.scourge const isMyTurn = currentTurn === team const maybeIsGameWinner = gameStatus === GAME_STATUS_SCOURGE_WIN || gameStatus === GAME_STATUS_SENTINAL_WIN ? ( team === 1 ? gameStatus === GAME_STATUS_SENTINAL_WIN : gameStatus === GAME_STATUS_SCOURGE_WIN ) : null return (

Opponent

Life: {enemyLife}

Deck: {enemyDeckSize}

{maybeIsGameWinner === null ? (

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

{isMyTurn && canDraw && } {isMyTurn && hasDrawn && }
) : (
{maybeIsGameWinner ? "You Win!" : "You Lose!"}
)}

Life: {player.life}

Deck: {deckSize}

{Array(enemyHandSize).fill(null).map((_, i) => ( ))}
{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)} /> ))}
) }