import { put , delay , getContext } from "redux-saga/effects" ; import { GameOptions , GamePhase } from "@creature-chess/models" ; import { RoundInfoCommands } from "../../roundInfo" ; import { Match } from "../../match" ; import { GameSagaContextPlayers , GetMatchupsFn } from "../../sagas" ; import { playerBeforeReadyPhaseEvent , playerRunReadyPhaseEvent } from "../../events" ; import { Logger } from "winston" ; export const runReadyPhase = function * ( ) { const options = yield getContext ( "options" ) ; const players = yield getContext ( "players" ) ; const getMatchups = yield getContext ( "getMatchups" ) ; const logger = yield getContext ( "logger" ) ; players . getAll ( ) . forEach ( p => p . put ( playerBeforeReadyPhaseEvent ( ) ) ) ; yield delay ( 500 ) ; const matchups = getMatchups ( ) ; matchups . forEach ( ( { homeId , awayId , awayIsClone } ) => { const homePlayer = players . getById ( homeId ) ; const awayPlayer = players . getById ( awayId ) ; if ( ! homePlayer || ! awayPlayer ) { return ; } const match = new Match ( homePlayer , awayPlayer , awayIsClone , logger , options ) ; homePlayer . put ( playerRunReadyPhaseEvent ( { match } ) ) ; if ( ! awayIsClone ) { awayPlayer . put ( playerRunReadyPhaseEvent ( { match } ) ) ; } } ) ; const phase = GamePhase . READY ; const startedAt = Date . now ( ) / 1000 ; yield put ( RoundInfoCommands . setRoundInfoCommand ( { phase , startedAt } ) ) ; yield delay ( options . phaseLengths [ GamePhase . READY ] * 1000 ) ; } ;