1import * as _ from 'lodash-es' 11type GetOldGameProps = { 13 isEphemeralGame?: boolean 14 settings?: Record<string, unknown> 17export const getGameStateAtIdx = async (props: GetOldGameProps) => { 18 const oldGame = await doGetOldGame(props) 31export const getApplyToIdx = () => (global as any).apply2Idx 32export const setApplyToIdx = (newval: number) => (global as any).apply2Idx = newval 34type ApplyHistActionParams = { 41export const applyGameHistActionIdx = async ({gameActionHistA, gqActionsHistA, existingGame, curActionId}: ApplyHistActionParams) => { 42 const gameActionH = gameActionHistA[curActionId] 44 existingGame, gameActionH, gameRoom: undefined, 46 gqActionsHistA.push(gameActionH) 49type GameStateFromActionParams = { 51 cpGameStateH: {id: string; isEphemeralGame?: boolean; settings?: Record<string, unknown>} 54export const gameStateFromActionA = async ({gameActionHistA, cpGameStateH}: GameStateFromActionParams) => { 55 let applyToIdx = getApplyToIdx() 58 if (!_.isInteger(applyToIdx)) { 59 applyToIdx = gameActionHistA.length - 1 62 while (curActionId <= applyToIdx) { 63 await applyGameHistActionIdx({gameActionHistA, gqActionsHistA, existingGame, curActionId}) 66 return {gqGameState: existingGame, gqActionsHistA, gameActionHistA} 69export const isReplayGameCtx = genContext<{isReplay: boolean} | undefined>() 71export const doGetOldGame = async ({id, isEphemeralGame, settings}: GetOldGameProps) => { 73 const ret = await db.selectFrom('dbgames') 75 .select(['game_state']) 81 if (gameActionHistA) { 82 return await gameStateFromActionA({ 83 gameActionHistA, cpGameStateH: {id, isEphemeralGame, settings: replaySettings}, 86 const {game_state} = ret // support old game fmt 87 return {gqGameState: game_state}