import { takeEvery , put } from "redux-saga/effects" ; import { getContext , select } from "typed-redux-saga" ; import { PlayerState } from "../state" ; import { PlayerPieceLocation } from "@creature-chess/models" ; import { BoardSelectors } from "@shoki/board" ; import { dropPiecePlayerAction } from "../../../playerActions" ; import { isPlayerAlive , getPlayerBelowPieceLimit , getMostExpensiveBenchPiece } from "../state/selectors" ; const FILL_BOARD_COMMAND = "FILL_BOARD_COMMAND" ; type FILL_BOARD_COMMAND = typeof FILL_BOARD_COMMAND ; type FillBoardCommand = ( { type : FILL_BOARD_COMMAND ; } ) ; export const fillBoardCommand = ( ) : FillBoardCommand => ( { type : FILL_BOARD_COMMAND } ) ; export const fillBoard = function * ( ) { const playerId = yield * getContext < string > ( "id" ) ; yield takeEvery < FillBoardCommand > ( FILL_BOARD_COMMAND , function * ( ) { const isAlive = yield * select ( isPlayerAlive ) ; if ( ! isAlive ) { return ; } while ( true ) { const state = yield * select ( ) ; const belowPieceLimit = getPlayerBelowPieceLimit ( state , playerId ) ; if ( ! belowPieceLimit ) { return ; } const benchPiece = getMostExpensiveBenchPiece ( state ) ; if ( ! benchPiece ) { return ; } const destination = BoardSelectors . getFirstEmptySlot ( state . board ) ; if ( ! destination ) { return ; } const benchPiecePosition = BoardSelectors . getPiecePosition ( state . bench , benchPiece . id ) ; if ( ! benchPiecePosition ) { return ; } const fromLocation = { type : "bench" , location : benchPiecePosition } ; const toLocation = { type : "board" , location : { x : destination . x , y : destination . y } } ; yield put ( dropPiecePlayerAction ( { pieceId : benchPiece . id , from : fromLocation , to : toLocation } ) ) ; } } ) ; } ;