import { createAction } from "@reduxjs/toolkit" ; import { take , select , put } from "@redux-saga/core/effects" ; import { getPiece , PlayerActions } from "@creature-chess/gamemode" ; import { BoardState } from "@shoki/board" ; import { PieceModel , PlayerPieceLocation } from "@creature-chess/models" ; import { AppState } from "../../../store" ; import { clearSelectedPiece } from "../../ui/actions" ; import { getLocationForPiece } from "../getLocationForPiece" ; export type PlayerClickTileAction = ReturnType < typeof playerClickTileAction > ; export const playerClickTileAction = createAction < { tile : PlayerPieceLocation ; } > ( "playerClickTileAction" ) ; export const clickTileSaga = function * ( ) { while ( true ) { const action = yield take ( playerClickTileAction . toString ( ) ) ; const { tile } = action . payload ; const selectedPiece = yield select ( ( state ) => state . game . ui . selectedPieceId ? getPiece ( state . game , state . game . ui . selectedPieceId ) : null ) ; if ( ! selectedPiece ) { continue ; } let tileEmpty = false ; const board = yield select ( ( state ) => state . game . board ) ; const bench = yield select ( ( state ) => state . game . bench ) ; const piecePositionKey = `${ tile . location . x },${ tile . location . y }` ; if ( tile . type === "bench" ) { tileEmpty = ! bench . piecePositions [ piecePositionKey ] ; } else if ( tile . type === "board" ) { tileEmpty = ! board . piecePositions [ piecePositionKey ] ; } if ( ! tileEmpty ) { continue ; } const from = getLocationForPiece ( selectedPiece . id , board , bench ) ; if ( ! from ) { continue ; } yield put ( PlayerActions . dropPiecePlayerAction ( { pieceId : selectedPiece . id , from , to : tile } ) ) ; yield put ( clearSelectedPiece ( ) ) ; } } ;