1import * as _ from 'lodash-es' 8type PlayerState = { publicState?: { playerAlias?: string; gameActiveAt?: number }; hasBeenKicked?: boolean } 9type ExistingGame = { playerStates: Record<string, PlayerState>; justFinishedGameId?: string } 11export const doKickPlayer = ({existingGame, playerAlias}: {existingGame: ExistingGame, playerAlias: string}) => { 12 _.each(existingGame.playerStates, (playerState, sessId) => { 13 if (_.get(playerState, ['publicState', 'playerAlias']) == playerAlias) { 14 _.set(existingGame, ['playerStates', sessId, 'hasBeenKicked'], true) 21export const getReqGameRoom = () => { 22 const store = gameRoomCtx.getStore() 24 return store.gameRoom as string 27export const doResetGame = async ({justFinishedGameId, markOldGameFinished}: {justFinishedGameId?: string, markOldGameFinished?: boolean} = {}) => { 28 const gameRoom = getReqGameRoom() 29 if (markOldGameFinished && justFinishedGameId) { 30 const {saveResults} = await import('./telgame/saveResultsF.mts') 31 const gamesById = (global as any).gamesStore || {} 32 const oldGame = gamesById[gameRoom] 33 if (oldGame) await saveResults({existingGame: oldGame}) 36 if (justFinishedGameId) freshState.justFinishedGameId = justFinishedGameId 40export const doSetPlayerAlias = (props: {existingGame: ExistingGame, sessId: string, playerAlias: string}) => { 41 let {existingGame, sessId, playerAlias} = props 43 playerAlias = _.trim(playerAlias).toLowerCase() 46 const playerAliasPath = ['playerStates', sessId, 'publicState', 'playerAlias'] 48 const existingPlayerP = _.find(_.toPairs(existingGame.playerStates), ([_sessId, playerState]) => { 49 const ps = playerState as PlayerState 50 return playerAlias == ps.publicState?.playerAlias 53 if (existingPlayerP) { 54 const [_sessId, playerState] = existingPlayerP as [string, PlayerState] 55 delete playerState.hasBeenKicked 56 // Reset gameActiveAt so rejoining player doesn't get immediately re-kicked 57 const {actionAtMs} = (gameActionCtx.getStore() || {}) as {actionAtMs?: number} 58 if (actionAtMs && playerState.publicState) { 59 playerState.publicState.gameActiveAt = actionAtMs 61 existingGame.playerStates[sessId] = playerState 62 if (sessId != _sessId) { 63 delete existingGame.playerStates[_sessId] 66 _.set(existingGame, playerAliasPath, playerAlias)