import type { Object } from 'ts-toolbelt'; import type Koa from 'koa'; import type { Store as ReduxStore } from 'redux'; import type * as ActionCreators from './core/action-creators'; import type { ActionErrorType, UpdateErrorType } from './core/errors'; import type { Flow } from './core/flow'; import type { CreateGameReducer } from './core/reducer'; import type { INVALID_MOVE } from './core/constants'; import type { GameMethod } from './core/game-methods'; import type { Auth } from './server/auth'; import type * as StorageAPI from './server/db/base'; import type { EventsAPI } from './plugins/plugin-events'; import type { LogAPI } from './plugins/plugin-log'; import type { RandomAPI } from './plugins/random/random'; import type { Operation } from 'rfc6902'; export type { StorageAPI }; export type AnyFn = (...args: any[]) => any; // "Public" state to be communicated to clients. export interface State { G: G; ctx: Ctx | CtxWithPlugins; deltalog?: Array; plugins: { [pluginName: string]: PluginState; }; _undo: Array>; _redo: Array>; _stateID: number; } export type ErrorType = UpdateErrorType | ActionErrorType; export interface ActionError { type: ErrorType; // TODO(#723): Figure out if we want to strongly type payloads. payload?: any; } export interface TransientMetadata { error?: ActionError; } // TODO(#732): Actually define a schema for the action dispatch results. export type ActionResult = any; // "Private" state that may include garbage that should be stripped before // being handed back to a client. export interface TransientState< G extends any = any, CtxWithPlugins extends Ctx = Ctx > extends State { transients?: TransientMetadata; } export type PartialGameState = Pick; export type StageName = string; export type PlayerID = string; export type StageArg = StageName | { stage?: StageName; moveLimit?: number }; export interface ActivePlayersArg { currentPlayer?: StageArg; others?: StageArg; all?: StageArg; value?: Record; moveLimit?: number; revert?: boolean; next?: ActivePlayersArg; } export interface ActivePlayers { [playerID: string]: StageName; } export interface Ctx { numPlayers: number; playOrder: Array; playOrderPos: number; playerID?: PlayerID; activePlayers: null | ActivePlayers; currentPlayer: PlayerID; numMoves?: number; gameover?: any; turn: number; phase: string; _activePlayersMoveLimit?: Record; _activePlayersNumMoves?: Record; _prevActivePlayers?: Array<{ activePlayers: null | ActivePlayers; _activePlayersMoveLimit?: Record; _activePlayersNumMoves?: Record; }>; _nextActivePlayers?: ActivePlayersArg; _random?: { seed: string | number; }; // TODO public api should have these as non-optional // internally there are two contexts, one is a serialized POJO and another // "enhanced" context that has plugin api methods attached events?: EventsAPI; log?: LogAPI; random?: RandomAPI; } export interface PluginState { data: any; api?: any; } export interface LogEntry { action: | ActionShape.MakeMove | ActionShape.GameEvent | ActionShape.Undo | ActionShape.Redo; _stateID: number; turn: number; phase: string; redact?: boolean; automatic?: boolean; metadata?: any; patch?: Operation[]; } export interface PluginContext< API extends any = any, Data extends any = any, G extends any = any > { G: G; ctx: Ctx; game: Game; api: API; data: Data; } export interface Plugin< API extends any = any, Data extends any = any, G extends any = any > { name: string; noClient?: (context: PluginContext) => boolean; isInvalid?: ( context: Omit, 'api'> ) => false | string; setup?: (setupCtx: { G: G; ctx: Ctx; game: Game }) => Data; action?: (data: Data, payload: ActionShape.Plugin['payload']) => Data; api?: (context: { G: G; ctx: Ctx; game: Game; data: Data; playerID?: PlayerID; }) => API; flush?: (context: PluginContext) => Data; dangerouslyFlushRawState?: (flushCtx: { state: State; game: Game; api: API; data: Data; }) => State; fnWrap?: ( moveOrHook: (G: G, ctx: Ctx, ...args: any[]) => any, methodType: GameMethod ) => (G: G, ctx: Ctx, ...args: any[]) => any; playerView?: (context: { G: G; ctx: Ctx; game: Game; data: Data; playerID?: PlayerID | null; }) => any; } export type MoveFn = ( G: G, ctx: CtxWithPlugins, ...args: any[] ) => any; export interface LongFormMove< G extends any = any, CtxWithPlugins extends Ctx = Ctx > { move: MoveFn; redact?: boolean; noLimit?: boolean; client?: boolean; undoable?: boolean | ((G: G, ctx: CtxWithPlugins) => boolean); ignoreStaleStateID?: boolean; } export type Move = | MoveFn | LongFormMove; export interface MoveMap< G extends any = any, CtxWithPlugins extends Ctx = Ctx > { [moveName: string]: Move; } export interface PhaseConfig< G extends any = any, CtxWithPlugins extends Ctx = Ctx > { start?: boolean; next?: ((G: G, ctx: CtxWithPlugins) => string | void) | string; onBegin?: (G: G, ctx: CtxWithPlugins) => any; onEnd?: (G: G, ctx: CtxWithPlugins) => any; endIf?: (G: G, ctx: CtxWithPlugins) => boolean | void | { next: string }; moves?: MoveMap; turn?: TurnConfig; wrapped?: { endIf?: ( state: State ) => boolean | void | { next: string }; onBegin?: (state: State) => any; onEnd?: (state: State) => any; next?: (state: State) => string | void; }; } export interface StageConfig< G extends any = any, CtxWithPlugins extends Ctx = Ctx > { moves?: MoveMap; next?: string; } export interface StageMap< G extends any = any, CtxWithPlugins extends Ctx = Ctx > { [stageName: string]: StageConfig; } export interface TurnOrderConfig< G extends any = any, CtxWithPlugins extends Ctx = Ctx > { first: (G: G, ctx: CtxWithPlugins) => number; next: (G: G, ctx: CtxWithPlugins) => number | undefined; playOrder?: (G: G, ctx: CtxWithPlugins) => PlayerID[]; } export interface TurnConfig< G extends any = any, CtxWithPlugins extends Ctx = Ctx > { activePlayers?: ActivePlayersArg; moveLimit?: number; onBegin?: (G: G, ctx: CtxWithPlugins) => any; onEnd?: (G: G, ctx: CtxWithPlugins) => any; endIf?: (G: G, ctx: CtxWithPlugins) => boolean | void | { next: PlayerID }; onMove?: (G: G, ctx: CtxWithPlugins) => any; stages?: StageMap; order?: TurnOrderConfig; wrapped?: { endIf?: ( state: State ) => boolean | void | { next: PlayerID }; onBegin?: (state: State) => any; onEnd?: (state: State) => any; onMove?: (state: State) => any; }; } export interface PhaseMap< G extends any = any, CtxWithPlugins extends Ctx = Ctx > { [phaseName: string]: PhaseConfig; } export interface Game< G extends any = any, CtxWithPlugins extends Ctx = Ctx, SetupData extends any = any > { name?: string; minPlayers?: number; maxPlayers?: number; deltaState?: boolean; disableUndo?: boolean; seed?: string | number; setup?: (ctx: CtxWithPlugins, setupData?: SetupData) => any; validateSetupData?: ( setupData: SetupData | undefined, numPlayers: number ) => string | undefined; moves?: MoveMap; phases?: PhaseMap; turn?: TurnConfig; events?: { endGame?: boolean; endPhase?: boolean; endTurn?: boolean; setPhase?: boolean; endStage?: boolean; setStage?: boolean; pass?: boolean; setActivePlayers?: boolean; }; endIf?: (G: G, ctx: CtxWithPlugins) => any; onEnd?: (G: G, ctx: CtxWithPlugins) => any; playerView?: (G: G, ctx: CtxWithPlugins, playerID: PlayerID) => any; plugins?: Array>; ai?: { enumerate: ( G: G, ctx: Ctx, playerID: PlayerID ) => Array< | { event: string; args?: any[] } | { move: string; args?: any[] } | ActionShape.MakeMove | ActionShape.GameEvent >; }; processMove?: ( state: State, action: ActionPayload.MakeMove ) => State | typeof INVALID_MOVE; flow?: ReturnType; } export type Undo = { G: G; ctx: Ctx; plugins: { [pluginName: string]: PluginState; }; moveType?: string; playerID?: PlayerID; }; export namespace Server { export type GenerateCredentials = ( ctx: Koa.DefaultContext ) => Promise | string; export type AuthenticateCredentials = ( credentials: string, playerMetadata: PlayerMetadata ) => Promise | boolean; export type PlayerMetadata = { id: number; name?: string; credentials?: string; data?: any; isConnected?: boolean; }; export interface MatchData { gameName: string; players: { [id: number]: PlayerMetadata }; setupData?: any; gameover?: any; nextMatchID?: string; unlisted?: boolean; createdAt: number; updatedAt: number; } export type AppCtx = Koa.DefaultContext & { db: StorageAPI.Async | StorageAPI.Sync; auth: Auth; }; export type App = Koa; } export namespace LobbyAPI { export type GameList = string[]; type PublicPlayerMetadata = Omit; export type Match = Omit & { matchID: string; players: PublicPlayerMetadata[]; }; export interface MatchList { matches: Match[]; } export interface CreatedMatch { matchID: string; } export interface JoinedMatch { playerCredentials: string; } export interface NextMatch { nextMatchID: string; } } export type Reducer = ReturnType; export type Store = ReduxStore; export namespace CredentialedActionShape { export type MakeMove = ReturnType; export type GameEvent = ReturnType; export type Plugin = ReturnType; export type AutomaticGameEvent = ReturnType< typeof ActionCreators.automaticGameEvent >; export type Undo = ReturnType; export type Redo = ReturnType; export type Any = | MakeMove | GameEvent | AutomaticGameEvent | Undo | Redo | Plugin; } export namespace ActionShape { type StripCredentials = Object.P.Omit< T, ['payload', 'credentials'] >; export type MakeMove = StripCredentials; export type GameEvent = StripCredentials; export type Plugin = StripCredentials; export type AutomaticGameEvent = StripCredentials; export type Sync = ReturnType; export type Update = ReturnType; export type Patch = ReturnType; export type Reset = ReturnType; export type Undo = StripCredentials; export type Redo = StripCredentials; // Private type used only for internal error processing. // Included here to preserve type-checking of reducer inputs. export type StripTransients = ReturnType< typeof ActionCreators.stripTransients >; export type Any = | MakeMove | GameEvent | AutomaticGameEvent | Sync | Update | Patch | Reset | Undo | Redo | Plugin | StripTransients; } export namespace ActionPayload { type GetPayload = Object.At; export type MakeMove = GetPayload; export type GameEvent = GetPayload; } export type FilteredMetadata = { id: number; name?: string; }[]; export interface SyncInfo { state: State; filteredMetadata: FilteredMetadata; initialState: State; log: LogEntry[]; } export interface ChatMessage { id: string; sender: PlayerID; payload: any; }