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 declare type AnyFn = (...args: any[]) => any; export interface State { G: G; ctx: Ctx | CtxWithPlugins; deltalog?: Array; plugins: { [pluginName: string]: PluginState; }; _undo: Array>; _redo: Array>; _stateID: number; } export declare type ErrorType = UpdateErrorType | ActionErrorType; export interface ActionError { type: ErrorType; payload?: any; } export interface TransientMetadata { error?: ActionError; } export declare type ActionResult = any; export interface TransientState extends State { transients?: TransientMetadata; } export declare type PartialGameState = Pick; export declare type StageName = string; export declare type PlayerID = string; export declare 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; }; 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 { G: G; ctx: Ctx; game: Game; api: API; data: Data; } export interface Plugin { 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 declare type MoveFn = (G: G, ctx: CtxWithPlugins, ...args: any[]) => any; export interface LongFormMove { move: MoveFn; redact?: boolean; noLimit?: boolean; client?: boolean; undoable?: boolean | ((G: G, ctx: CtxWithPlugins) => boolean); ignoreStaleStateID?: boolean; } export declare type Move = MoveFn | LongFormMove; export interface MoveMap { [moveName: string]: Move; } export interface PhaseConfig { 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 { moves?: MoveMap; next?: string; } export interface StageMap { [stageName: string]: StageConfig; } export interface TurnOrderConfig { first: (G: G, ctx: CtxWithPlugins) => number; next: (G: G, ctx: CtxWithPlugins) => number | undefined; playOrder?: (G: G, ctx: CtxWithPlugins) => PlayerID[]; } export interface TurnConfig { 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 { [phaseName: string]: PhaseConfig; } export interface Game { 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 declare type Undo = { G: G; ctx: Ctx; plugins: { [pluginName: string]: PluginState; }; moveType?: string; playerID?: PlayerID; }; export declare namespace Server { type GenerateCredentials = (ctx: Koa.DefaultContext) => Promise | string; type AuthenticateCredentials = (credentials: string, playerMetadata: PlayerMetadata) => Promise | boolean; type PlayerMetadata = { id: number; name?: string; credentials?: string; data?: any; isConnected?: boolean; }; interface MatchData { gameName: string; players: { [id: number]: PlayerMetadata; }; setupData?: any; gameover?: any; nextMatchID?: string; unlisted?: boolean; createdAt: number; updatedAt: number; } type AppCtx = Koa.DefaultContext & { db: StorageAPI.Async | StorageAPI.Sync; auth: Auth; }; type App = Koa; } export declare 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 {}; } export declare type Reducer = ReturnType; export declare type Store = ReduxStore; export declare namespace CredentialedActionShape { type MakeMove = ReturnType; type GameEvent = ReturnType; type Plugin = ReturnType; type AutomaticGameEvent = ReturnType; type Undo = ReturnType; type Redo = ReturnType; type Any = MakeMove | GameEvent | AutomaticGameEvent | Undo | Redo | Plugin; } export declare namespace ActionShape { type StripCredentials = Object.P.Omit; 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; export type StripTransients = ReturnType; export type Any = MakeMove | GameEvent | AutomaticGameEvent | Sync | Update | Patch | Reset | Undo | Redo | Plugin | StripTransients; export {}; } export declare namespace ActionPayload { type GetPayload = Object.At; export type MakeMove = GetPayload; export type GameEvent = GetPayload; export {}; } export declare 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; }