forked from shirakaba/boardgame.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.d.ts
654 lines (643 loc) · 21.8 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
* Copyright 2018 The boardgame.io Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare module 'boardgame.io/ui' {
import React, {
CSSProperties,
DOMAttributes,
HTMLAttributes,
ReactElement,
ReactNode,
} from 'react';
/* card */
export interface ICardProps {
back?: ReactNode,
canHover?: boolean,
className?: string,
front?: ReactNode,
isFaceUp?: boolean
}
export type ICardPropsCombined = ICardProps & HTMLAttributes<HTMLDivElement>;
export const Card: (props: ICardPropsCombined, ...rest: any[]) => ReactElement<ICardPropsCombined>;
export interface IDeckState {
cards: ReactNode[]
}
/* deck */
export interface IDeckProps {
cards?: ReactElement<ICardPropsCombined>[],
className?: string,
onClick?(mouseEvent?: React.MouseEvent<HTMLDivElement>): void,
splayWidth?: number,
}
export type IDeckPropsCombined = IDeckProps & HTMLAttributes<HTMLDivElement>;
export class Deck extends React.Component<IDeckPropsCombined, IDeckState>{
constructor(props: IDeckPropsCombined);
}
/* grid */
export interface IGridColorMap {
[key: string]: string;
}
export interface IGridProps extends GenericGridProps {
rows: number,
cols: number
}
export interface GenericGridProps extends GenericInteractiveProps<SVGElement> {
outline?: boolean,
colorMap?: IGridColorMap,
cellSize?: number
}
export interface GenericInteractiveProps<T extends Element> {
style?: CSSProperties,
onClick?: (mouseEvent: React.MouseEvent<T>) => void,
onMouseOver?: (mouseEvent: React.MouseEvent<T>) => void,
onMouseOut?: (mouseEvent: React.MouseEvent<T>) => void,
children?: ReactNode[]|ReactNode,
}
export type IGridPropsCombined = IGridProps & HTMLAttributes<SVGElement>;
export class Grid extends React.Component<IGridPropsCombined, {}> {
private _getCellColor(x: number, y: number): string;
private _getGrid(): Square[]|null;
onClick(mouseEvent: React.MouseEvent<SVGElement>): void;
onMouseOver(mouseEvent: React.MouseEvent<SVGElement>): void;
onMouseOut(mouseEvent: React.MouseEvent<SVGElement>): void;
}
export interface ISquareProps {
x: number;
y: number;
size?: number,
style?: CSSProperties,
transform?: string,
onClick?: (mouseEvent: React.MouseEvent<SVGGElement>) => void,
onMouseOver?: (mouseEvent: React.MouseEvent<SVGGElement>) => void,
onMouseOut?: (mouseEvent: React.MouseEvent<SVGGElement>) => void,
children?: ReactNode
}
export type ISquarePropsCombined = ISquareProps & HTMLAttributes<SVGGElement>;
export class Square extends React.Component<ISquarePropsCombined, {}> {
onClick(mouseEvent: React.MouseEvent<Element>): void;
onMouseOver(mouseEvent: React.MouseEvent<Element>): void;
onMouseOut(mouseEvent: React.MouseEvent<Element>): void;
}
/* hex */
export interface IHexGridProps extends GenericGridProps {
levels?: number
}
export type IHexGridPropsCombined = IHexGridProps & HTMLAttributes<SVGElement>;
export class HexGrid extends React.Component<IHexGridPropsCombined, {}> {
private _getCellColor(x: number, y: number, z: number): string;
private _getGrid(): Hex[]|null;
onClick(mouseEvent: React.MouseEvent<SVGGElement>): void;
onMouseOver(mouseEvent: React.MouseEvent<SVGGElement>): void;
onMouseOut(mouseEvent: React.MouseEvent<SVGGElement>): void;
}
export interface IHexProps extends GenericInteractiveProps<SVGGElement> {
x?: number,
y?: number,
z?: number,
size?: number,
children: ReactNode // Doesn't accept an array.
}
export type IHexPropsCombined = IHexProps & HTMLAttributes<SVGGElement>;
export class Hex extends React.Component<IHexPropsCombined, {}>{
constructor(props: IHexPropsCombined);
width(): number;
height(): number;
center(): { x: number, y: number };
points(): string;
}
/* token */
export interface ITokenProps extends GenericInteractiveProps<HTMLElement> {
x: number;
y: number;
z?: number;
template?: Square, // FIXME: Best guess.
animate?: boolean,
children?: ReactNode,
style?: CSSProperties,
onClick?: (mouseEvent: React.MouseEvent<HTMLElement>) => void,
onMouseOver?: (mouseEvent: React.MouseEvent<HTMLElement>) => void,
onMouseOut?: (mouseEvent: React.MouseEvent<HTMLElement>) => void,
animationDuration?: number
}
export type ITokenPropsCombined = ITokenProps & HTMLAttributes<HTMLElement>;
export interface ITokenState {
x?: number,
y?: number,
z?: number,
originTime?: number,
originX?: number,
originY?: number,
originZ?: number,
}
export class Token extends React.Component<ITokenPropsCombined, ITokenState> {
private _animate(now: number): void;
getCoords(props?: ITokenProps): { x: number, y: number, z: number };
private _easeInOutCubic(t: number, b: number, c: number, d: number): number;
}
}
declare module 'boardgame.io/core' {
import { Context } from 'boardgame.io/server';
/* game */
export interface GameState {
G: G,
ctx: Context,
log: Action[],
_undo: { G: G, ctx: Context }[],
_redo: { G: G, ctx: Context }[],
_stateID: number,
_initial: GameState,
}
export interface FlowValue {
endGameIf: (G: G, ctx: Context, playerID: string) => string,
endTurnIf: (G: G, ctx: Context, playerID: string) => string,
phases: Phase[]
}
export interface Phase {
name: string,
setup?: (G: G, ctx: Context) => G,
cleanup?: (G: G, ctx: Context) => G,
}
export interface G {
name: string,
setup: (numPlayers: number) => G,
playerView: (G: G, ctx: Context, playerID: string) => G,
flow: FlowValue,
seed: string,
moveNames: (ActionTypes)[],
// TODO: determine whether the MoveAction is likely to have args of type other than simply 'any'.
processMove: (G: G, action: MoveAction<any>, ctx: Context) => G;
}
export interface GameArgs<T extends G> {
name?: string,
setup?: any,
// moves: Pick<ActionTypesKeyMap, "MAKE_MOVE">,
// moves?: Partial<ActionTypesKeyMap>,
moves?: { [moveName: string]: Move<T> },
playerView?: any,
flow?: any,
seed?: number,
}
export type GameArgsSpread = [string, any, any[], any, any, number];
export default function Game<T extends G>({ name, setup, moves, playerView, flow, seed }: GameArgs<T>): T;
/* flow */
interface GAndCtx {
G: G,
ctx: Context
}
export interface FlowEvents {
[event: string]: (gAndCtx: GAndCtx) => GAndCtx;
}
export interface IFlow {
ctx?: Context,
events?: FlowEvents,
init?: GameState,
processMove?: (state: GameState, action: Action, dispatch: Dispatch) => void,
optimisticUpdate?: (G: G, ctx: Context, move: MoveAction<any>) => boolean,
canMakeMove?: (G: G, ctx: Context, gameArgsPayload: GameArgsPayload<any>) => boolean,
canUndoMove?: (G: G, ctx: Context, moveName: string) => boolean,
}
export interface IFlowReturn extends Pick<IFlow, "ctx"|"init"|"canUndoMove"|"processMove"|"optimisticUpdate"|"canMakeMove"> {
eventNames: string[],
processGameEvent?: (state: GameState, action: Action) => Dispatch,
}
export function Flow({
ctx,
events,
init,
processMove,
optimisticUpdate,
canMakeMove,
canUndoMove,
}: IFlow): IFlowReturn;
export interface IFlowWithPhases {
phases?: Phase[],
movesPerTurn?: number|undefined, // Not always initialised in defaults init process.
endTurnIf?: () => boolean,
endGameIf?: () => boolean,
onTurnBegin?: (G: G) => G,
onTurnEnd?: (G: G) => G,
onMove?: (G: G) => G,
turnOrder?: DefaultTurnOrder,
endTurn?: boolean,
endPhase?: boolean|undefined, // Not always initialised in defaults init process.
endGame?: boolean,
undoableMoves?: string[]|null,
allowedMoves?: string[]|null,
optimisticUpdate?: () => boolean,
}
export function FlowWithPhases({
phases,
movesPerTurn,
endTurnIf,
endGameIf,
onTurnBegin,
onTurnEnd,
onMove,
turnOrder,
endTurn,
endPhase,
endGame,
undoableMoves,
allowedMoves,
optimisticUpdate
}: IFlowWithPhases): IFlow;
/* turn-order */
export const Pass: (G: G, ctx: Context)=> G;
export interface DefaultTurnOrder {
first(G: G, ctx: Context): number;
next(G: G, ctx: Context): number;
}
export interface AnyTurnOrder {
first(): undefined;
next(): undefined;
}
export interface SkipTurnOrder {
first(G: G, ctx: Context): number;
next(G: G, ctx: Context): number|void;
}
export interface ITurnOrder {
DEFAULT: DefaultTurnOrder,
ANY: AnyTurnOrder,
SKIP: SkipTurnOrder
}
export const TurnOrder: ITurnOrder;
/* player-view */
export interface IPlayerView {
STRIP_SECRETS(): (G: G, action: Action, ctx: Context) => G;
}
export const PlayerView: IPlayerView;
/* random */
export interface RandomState extends GameState {
seed: string;
}
interface API {
Die(spotvalue?: number, diceCount?: number): number|number[];
Number(): number;
Shuffle<T>(deck: T[]): T[];
}
export class Random {
public state: RandomState;
constructor(ctx: Context);
public update(ctx: Context): Context;
public attach(ctx: Context): Context;
private _random(): number;
private _api(): API;
public static detach(ctx: Context): Context;
public static seed(): string;
}
/* reducer */
export interface CreateGameReducerInput {
game: any,
numPlayers: number,
multiplayer: boolean,
}
export function createGameReducer({ game, numPlayers, multiplayer }: CreateGameReducerInput): (state: GameState, action: Action) => GameState;
/* action-creators */
export interface GameArgsPayload<Args> {
type: string,
args: Array<Args>,
playerID: string,
}
export interface RestoreAction extends Action {
state: GameState
}
export interface MoveAction<Args> extends Action {
payload: GameArgsPayload<Args>
}
export interface Action {
type: ActionTypes;
}
export const makeMove: <Args>(type: string, args: Array<Args>, playerID: string) => MoveAction<Args>;
export const gameEvent: <Args>(type: string, args: Array<Args>, playerID: string) => MoveAction<Args>;
export const restore: (state: GameState) => RestoreAction;
export const reset: () => Action;
export const undo: () => Action;
export const redo: () => Action;
export type ActionSignatures = typeof makeMove | typeof gameEvent | typeof restore | typeof reset | typeof undo | typeof redo;
/* action-types */
export const MAKE_MOVE: 'MAKE_MOVE';
export const GAME_EVENT: 'GAME_EVENT';
export const RESTORE: 'RESTORE';
export const RESET: 'RESET';
export const UNDO: 'UNDO';
export const REDO: 'REDO';
export type ActionTypes = 'MAKE_MOVE'|'GAME_EVENT'|'RESTORE'|'RESET'|'UNDO'|'REDO';
export type Move<T extends G> = (...args: any[]) => T;
export interface ActionTypesKeyMap {
MAKE_MOVE: Move<G>;
GAME_EVENT: Move<G>;
RESTORE: (state: GameState) => G;
RESET: () => G;
UNDO: () => G;
REDO: () => G;
}
/* events */
export interface Dispatch {
key: string,
args: any[]
}
export interface Attachment {
events: Events,
}
export interface EventsDict {
[name: string]: Events;
}
export class Events {
public flow: IFlow;
public playerID: string;
public dispatch: Dispatch[];
constructor(flow: IFlow, playerID: string);
attach(ctx: Context): Attachment;
update(state: GameState): GameState;
public static detach(ctx: Context): Attachment;
}
}
declare module 'boardgame.io/react-native' {
import React, { Component } from "react";
import { G } from 'boardgame.io/core';
import { _ClientImpl } from 'boardgame.io/client';
export interface ClientInput<T extends G> {
game: T,
numPlayers?: number,
board: any,
multiplayer?: boolean|{ server: string },
debug?: boolean,
enhancer?: any,
}
export function Client<T extends G>({
game,
numPlayers,
board,
multiplayer,
enhancer,
}: ClientInput<T>): WrappedBoard;
export interface WrappedBoardProps {
gameID?: string,
playerID?: string|null,
}
export interface WrappedBoardState {
// None presently.
}
class WrappedBoard extends Component<WrappedBoardProps, WrappedBoardState>{
client: _ClientImpl;
}
// https://github.com/Microsoft/TypeScript/issues/15449
global {
namespace JSX {
interface IntrinsicElements {
// ClientTag: DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement> | WrappedBoardProps
ClientTag: WrappedBoardProps
}
}
}
}
declare module 'boardgame.io/react' {
import React, { Component } from "react";
import { WrappedBoardProps, ClientInput } from 'boardgame.io/react-native';
import { G } from 'boardgame.io/core';
import { _ClientImpl } from 'boardgame.io/client';
export interface DebuggableClientInput<T extends G> extends ClientInput<T> {
debug?: boolean,
}
export function Client<T extends G>({
game,
numPlayers,
board,
multiplayer,
debug,
enhancer,
}: DebuggableClientInput<T>): WrappedBoard;
export interface DebuggableWrappedBoardProps extends WrappedBoardProps {
debug?: boolean,
}
export interface WrappedBoardState {
// None presently.
}
class WrappedBoard extends Component<DebuggableWrappedBoardProps, WrappedBoardState> {
client: _ClientImpl;
}
}
declare module 'boardgame.io/client' {
import React, { ReactNode } from 'react';
import { Store } from 'redux';
import { Game } from 'boardgame.io/server';
import { GameState, EventsDict, ActionTypes } from 'boardgame.io/core';
/* client */
export interface Dispatchers {
[name: string]: (...args: any[]) => void;
}
export interface ClientState extends GameState {
isActive: boolean,
isConnected?: boolean
}
export function createEventDispatchers(eventNames: string[], store: Store<ClientState>, playerID: string): Dispatchers;
export function createMoveDispatchers(moveNames: string[], store: Store<ClientState>, playerID: string): Dispatchers;
export interface ClientInput {
game: Game,
numPlayers: number,
multiplayer: boolean,
socketOpts: any,
gameID: string,
playerID: string,
enhancer: any,
}
export class _ClientImpl {
readonly game: Game;
playerID?: string;
moves: Dispatchers;
events: Dispatchers;
gameID?: string;
readonly store: Store<ClientState>;
constructor({
game,
numPlayers,
multiplayer,
socketOpts,
gameID,
playerID,
enhancer,
}: ClientInput);
subscribe(fn: (callback?: () => any) => any): void;
getState(): ClientState;
connect(): void;
createDispatchers(): void;
}
export function Client(opts: ClientInput): _ClientImpl;
/* debug */
export interface DebugMoveProps {
name: string,
shortcut: string,
fn: () => any,
}
export interface DebugMoveState {
error: string,
focus?: boolean,
enterArg?: boolean,
}
export class DebugMove extends React.Component<DebugMoveProps, DebugMoveState> {
onSubmit(value: string): void;
}
export interface DebugMoveArgFieldProps {
name: string,
onSubmit: (value: string) => void,
active?: boolean,
activate?: (event: React.MouseEvent<HTMLDivElement>) => void,
deactivate?: (event: React.FocusEvent<HTMLSpanElement>) => void,
}
export interface DebugMoveArgFieldState {
// None presently
}
export class DebugMoveArgField extends React.Component<DebugMoveArgFieldProps, DebugMoveArgFieldState> {
onSubmit(value: any): void;
}
export interface KeyboardShortcutProps {
value: string,
children?: ReactNode,
onPress?: (event: React.MouseEvent<HTMLDivElement>) => void,
}
export interface KeyboardShortcutState {
active: boolean
}
export class KeyboardShortcut extends React.Component<KeyboardShortcutProps, KeyboardShortcutState> {
onSubmit(value: any): void;
deactivate(event: React.FocusEvent<HTMLElement>): void;
activate(event: React.MouseEvent<HTMLElement>): void;
}
export interface DebugProps {
gamestate: GameState,
gameID: string,
playerID?: string,
moves?: any,
events?: EventsDict,
restore?: (gamestate: GameState) => void,
showLog?: boolean,
store?: Store<GameState>,
}
export interface DebugState {
showDebugUI: boolean,
showLog: boolean,
help: boolean,
}
export class Debug extends React.Component<DebugProps, DebugState> {
shortcuts: { [name: string]: string; };
assignShortcuts(): void;
saveState(event: React.MouseEvent<HTMLDivElement>): void;
restoreState(event: React.MouseEvent<HTMLDivElement>): void;
onClickMain(event: React.MouseEvent<HTMLDivElement>): void;
onClickLog(event: React.MouseEvent<HTMLDivElement>): void;
toggleHelp(event: React.MouseEvent<HTMLDivElement>): void;
}
/* log */
export interface GameLogProps {
store: any;
}
export interface GameLogState {
// None presently.
}
export class GameLog extends React.Component<GameLogProps, GameLogState> {
onRewind(logIndex: number|null): void;
}
/* multiplayer */
const blacklistedActions: Set<ActionTypes>;
export interface MultiplayerInput {
socket: SocketIOClient.Socket|MockSocket,
socketOpts?: SocketIOClient.ConnectOpts,
gameID: string,
playerID: string|null,
gameName: string,
numPlayers: number,
server: string,
}
export type SocketEvents = "action"|"sync"|"connect"|"disconnect";
export type SocketCallback = (arg0: any, arg1: any) => void;
export interface MockSocket {
callbacks: { [type: string]: SocketCallback; }
emit(type: string, ...args: any[]): any;
receive(type: string, ...args: any[]): void;
on(event: string, fn: Function): SocketIOClient.Emitter;
}
export class Multiplayer {
socket?: SocketIOClient.Socket|MockSocket;
readonly socketOpts?: SocketIOClient.ConnectOpts;
gameID: string;
playerID: string|null;
readonly gameName: string;
readonly numPlayers: number;
readonly server: string;
callback: () => any;
store: Store<any>|null;
constructor({
socket,
socketOpts,
gameID,
playerID,
gameName,
numPlayers,
server,
}: MultiplayerInput);
createStore(reducer: any, enhancer?: any): Store<any>;
connect(): void;
subscribe(fn: () => any): void;
updateGameID(id: string): void;
updatePlayerID(id: string): void;
}
}
declare module 'boardgame.io/server' {
import * as LRU from "lru-cache";
import { Db, MongoClient } from 'mongodb';
import { GameState, IFlow, G } from 'boardgame.io/core';
/* server */
export interface Context {
_random?: any
}
export interface Game {
name: string,
flow: IFlow,
playerView(G: G, ctx: Context, playerID: number|string): G;
}
export interface ServerArgs {
games: Game[],
db: any,
_clientInfo: any,
_roomInfo: any
}
export interface ServerReturn {
app: any,
db: any,
run(): (port: number, callback: ()=>void) => Promise<void>;
}
export function Server({ games, db, _clientInfo, _roomInfo }: ServerArgs): ServerReturn;
/* db */
type Id = string;
type GamesMap = Map<Id, GameState>;
export class InMemory {
public games: GamesMap;
constructor();
connect(): Promise<void>;
set(id: Id, state: GameState): Promise<GamesMap>;
get(id: Id): Promise<GameState|undefined>;
has(id: Id): Promise<boolean>;
}
export interface MongoInput {
url: string,
dbname?: string,
cacheSize?: number,
mockClient?: MongoClient,
}
export class Mongo {
public client: MongoClient;
public url: string;
public dbname: string;
public cache: LRU;
public db: Db;
constructor({ url, dbname, cacheSize, mockClient }: MongoInput);
connect(): Promise<void>;
set(id: Id, state: GameState): Promise<GamesMap>;
get(id: Id): Promise<GameState|undefined>;
has(id: Id): Promise<boolean>;
}
}