-
Notifications
You must be signed in to change notification settings - Fork 703
/
types.ts
377 lines (340 loc) · 9.87 KB
/
types.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
import { Object } from 'ts-toolbelt';
import Koa from 'koa';
import { Store as ReduxStore } from 'redux';
import 'svelte';
import * as ActionCreators from './core/action-creators';
import { Flow } from './core/flow';
import { CreateGameReducer } from './core/reducer';
import { INVALID_MOVE } from './core/constants';
import * as StorageAPI from './server/db/base';
import { EventsAPI } from './plugins/plugin-events';
import { RandomAPI } from './plugins/plugin-random';
export { StorageAPI };
export type AnyFn = (...args: any[]) => any;
export interface State<G extends any = any, CtxWithPlugins extends Ctx = Ctx> {
G: G;
ctx: Ctx | CtxWithPlugins;
deltalog?: Array<LogEntry>;
plugins: {
[pluginName: string]: PluginState;
};
_undo: Array<Undo<G>>;
_redo: Array<Undo<G>>;
_stateID: number;
}
export type PartialGameState = Pick<State, 'G' | 'ctx' | 'plugins'>;
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<PlayerID, StageArg>;
moveLimit?: number;
revert?: boolean;
next?: ActivePlayersArg;
}
export interface ActivePlayers {
[playerID: string]: StageName;
}
export interface Ctx {
numPlayers: number;
playOrder: Array<PlayerID>;
playOrderPos: number;
playerID?: PlayerID;
activePlayers: null | ActivePlayers;
currentPlayer: PlayerID;
numMoves?: number;
gameover?: any;
turn: number;
phase: string;
_activePlayersMoveLimit?: Record<PlayerID, number>;
_activePlayersNumMoves?: Record<PlayerID, number>;
_prevActivePlayers?: Array<{
activePlayers: null | ActivePlayers;
_activePlayersMoveLimit?: Record<PlayerID, number>;
_activePlayersNumMoves?: Record<PlayerID, number>;
}>;
_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;
random?: RandomAPI;
}
export interface PluginState {
data: any;
api?: any;
}
export interface LogEntry {
action: ActionShape.MakeMove | ActionShape.GameEvent;
_stateID: number;
turn: number;
phase: string;
redact?: boolean;
automatic?: boolean;
}
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<API, Data, G>) => boolean;
setup?: (setupCtx: { G: G; ctx: Ctx; game: Game<G, Ctx> }) => Data;
action?: (data: Data, payload: ActionShape.Plugin['payload']) => Data;
api?: (context: {
G: G;
ctx: Ctx;
game: Game<G, Ctx>;
data: Data;
playerID?: PlayerID;
}) => API;
flush?: (context: PluginContext<API, Data, G>) => Data;
dangerouslyFlushRawState?: (flushCtx: {
state: State<G, Ctx>;
game: Game<G, Ctx>;
api: API;
data: Data;
}) => State<G, Ctx>;
fnWrap?: (fn: AnyFn) => (G: G, ctx: Ctx, ...args: any[]) => any;
}
type MoveFn<G extends any = any, CtxWithPlugins extends Ctx = Ctx> = (
G: G,
ctx: CtxWithPlugins,
...args: any[]
) => any;
export interface LongFormMove<
G extends any = any,
CtxWithPlugins extends Ctx = Ctx
> {
move: MoveFn<G, CtxWithPlugins>;
redact?: boolean;
noLimit?: boolean;
client?: boolean;
undoable?: boolean | ((G: G, ctx: CtxWithPlugins) => boolean);
}
export type Move<G extends any = any, CtxWithPlugins extends Ctx = Ctx> =
| MoveFn<G, CtxWithPlugins>
| LongFormMove<G, CtxWithPlugins>;
export interface MoveMap<
G extends any = any,
CtxWithPlugins extends Ctx = Ctx
> {
[moveName: string]: Move<G, CtxWithPlugins>;
}
export interface PhaseConfig<
G extends any = any,
CtxWithPlugins extends Ctx = Ctx
> {
start?: boolean;
next?: string;
onBegin?: (G: G, ctx: CtxWithPlugins) => any;
onEnd?: (G: G, ctx: CtxWithPlugins) => any;
endIf?: (G: G, ctx: CtxWithPlugins) => boolean | void | { next: string };
moves?: MoveMap<G, CtxWithPlugins>;
turn?: TurnConfig<G, CtxWithPlugins>;
wrapped?: {
endIf?: (
state: State<G, CtxWithPlugins>
) => boolean | void | { next: string };
onBegin?: (state: State<G, CtxWithPlugins>) => any;
onEnd?: (state: State<G, CtxWithPlugins>) => any;
};
}
export interface StageConfig<
G extends any = any,
CtxWithPlugins extends Ctx = Ctx
> {
moves?: MoveMap<G, CtxWithPlugins>;
next?: string;
}
export interface StageMap<
G extends any = any,
CtxWithPlugins extends Ctx = Ctx
> {
[stageName: string]: StageConfig<G, CtxWithPlugins>;
}
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?: object;
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<G, CtxWithPlugins>;
moves?: MoveMap<G, CtxWithPlugins>;
order?: TurnOrderConfig<G, CtxWithPlugins>;
wrapped?: {
endIf?: (
state: State<G, CtxWithPlugins>
) => boolean | void | { next: PlayerID };
onBegin?: (state: State<G, CtxWithPlugins>) => any;
onEnd?: (state: State<G, CtxWithPlugins>) => any;
onMove?: (state: State<G, CtxWithPlugins>) => any;
};
}
interface PhaseMap<G extends any = any, CtxWithPlugins extends Ctx = Ctx> {
[phaseName: string]: PhaseConfig<G, CtxWithPlugins>;
}
export interface Game<G extends any = any, CtxWithPlugins extends Ctx = Ctx> {
name?: string;
disableUndo?: boolean;
seed?: string | number;
setup?: (ctx: CtxWithPlugins, setupData?: any) => any;
moves?: MoveMap<G, CtxWithPlugins>;
phases?: PhaseMap<G, CtxWithPlugins>;
turn?: TurnConfig<G, CtxWithPlugins>;
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<Plugin<any, any, G>>;
ai?: {
enumerate: (
G: G,
ctx: Ctx,
playerID: PlayerID
) => Array<
| { event: string; args?: any[] }
| { move: string; args?: any[] }
| ActionShape.MakeMove
| ActionShape.GameEvent
>;
};
processMove?: (
state: State<G, Ctx | CtxWithPlugins>,
action: ActionPayload.MakeMove
) => State<G, CtxWithPlugins> | typeof INVALID_MOVE;
flow?: ReturnType<typeof Flow>;
}
type Undo<G extends any = any> = {
G: G;
ctx: Ctx;
moveType?: string;
};
export namespace Server {
export type GenerateCredentials = (
ctx: Koa.DefaultContext
) => Promise<string> | string;
export type AuthenticateCredentials = (
credentials: string,
playerMetadata: PlayerMetadata
) => Promise<boolean> | boolean;
export type PlayerMetadata = {
id: number;
name?: string;
credentials?: string;
data?: any;
};
export interface GameMetadata {
gameName: string;
players: { [id: number]: PlayerMetadata };
setupData?: any;
gameover?: any;
nextRoomID?: string;
unlisted?: boolean;
}
export interface LobbyConfig {
uuid?: () => string;
generateCredentials?: GenerateCredentials;
apiPort?: number;
apiCallback?: () => void;
}
}
export type Reducer = ReturnType<typeof CreateGameReducer>;
export type Store = ReduxStore<State, ActionShape.Any>;
export namespace CredentialedActionShape {
export type MakeMove = ReturnType<typeof ActionCreators.makeMove>;
export type GameEvent = ReturnType<typeof ActionCreators.gameEvent>;
export type Plugin = ReturnType<typeof ActionCreators.plugin>;
export type AutomaticGameEvent = ReturnType<
typeof ActionCreators.automaticGameEvent
>;
export type Undo = ReturnType<typeof ActionCreators.undo>;
export type Redo = ReturnType<typeof ActionCreators.redo>;
export type Any =
| MakeMove
| GameEvent
| AutomaticGameEvent
| Undo
| Redo
| Plugin;
}
export namespace ActionShape {
type StripCredentials<T extends object> = Object.P.Omit<
T,
['payload', 'credentials']
>;
export type MakeMove = StripCredentials<CredentialedActionShape.MakeMove>;
export type GameEvent = StripCredentials<CredentialedActionShape.GameEvent>;
export type Plugin = StripCredentials<CredentialedActionShape.Plugin>;
export type AutomaticGameEvent = StripCredentials<
CredentialedActionShape.AutomaticGameEvent
>;
export type Sync = ReturnType<typeof ActionCreators.sync>;
export type Update = ReturnType<typeof ActionCreators.update>;
export type Reset = ReturnType<typeof ActionCreators.reset>;
export type Undo = StripCredentials<CredentialedActionShape.Undo>;
export type Redo = StripCredentials<CredentialedActionShape.Redo>;
export type Any =
| MakeMove
| GameEvent
| AutomaticGameEvent
| Sync
| Update
| Reset
| Undo
| Redo
| Plugin;
}
export namespace ActionPayload {
type GetPayload<T extends object> = Object.At<T, 'payload'>;
export type MakeMove = GetPayload<ActionShape.MakeMove>;
export type GameEvent = GetPayload<ActionShape.GameEvent>;
}
export type FilteredMetadata = {
id: number;
name?: string;
}[];
export interface SyncInfo {
state: State;
filteredMetadata: FilteredMetadata;
initialState: State;
log: LogEntry[];
}