-
Notifications
You must be signed in to change notification settings - Fork 702
/
base.ts
296 lines (267 loc) · 6.86 KB
/
base.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
import type { Object } from 'ts-toolbelt';
import type { State, Server, LogEntry } from '../../types';
export enum Type {
SYNC = 0,
ASYNC = 1,
}
/**
* Type guard that checks if a storage implementation is synchronous.
*/
export function isSynchronous(storageAPI: Sync | Async): storageAPI is Sync {
return storageAPI.type() === Type.SYNC;
}
/**
* Indicates which fields the fetch operation should return.
*/
export interface FetchOpts {
state?: boolean;
log?: boolean;
metadata?: boolean;
initialState?: boolean;
}
/**
* Data that can be retrieved from a database fetch query
*/
export interface FetchFields {
state: State;
log: LogEntry[];
metadata: Server.MatchData;
initialState: State;
}
/**
* The result of the fetch operation.
*/
export type FetchResult<O extends FetchOpts> = Object.Pick<
FetchFields,
Object.SelectKeys<O, true>
>;
/**
* Options passed when listing matches.
*/
export interface ListMatchesOpts {
gameName?: string;
where?: {
isGameover?: boolean;
updatedBefore?: number;
updatedAfter?: number;
};
}
/**
* @deprecated Use ListMatchesOpts instead
*/
export interface ListGamesOpts {
gameName?: string;
where?: {
isGameover?: boolean;
updatedBefore?: number;
updatedAfter?: number;
};
}
/**
* Options passed when creating a new match.
*/
export interface CreateMatchOpts {
initialState: State;
metadata: Server.MatchData;
}
/**
* @deprecated Use CreateMatchOpts instead
*/
export interface CreateGameOpts {
initialState: State;
metadata: Server.MatchData;
}
export abstract class Async {
/* istanbul ignore next */
type() {
/* istanbul ignore next */
return Type.ASYNC;
}
/**
* Connect.
*/
abstract connect();
/**
* Create a new match.
*
* This might just need to call setState and setMetadata in
* most implementations.
*
* However, it exists as a separate call so that the
* implementation can provision things differently when
* a match is created. For example, it might stow away the
* initial match state in a separate field for easier retrieval.
*/
/* istanbul ignore next */
async createMatch(matchID: string, opts: CreateMatchOpts): Promise<void> {
if (this.createGame) {
console.warn(
'The database connector does not implement a createMatch method.',
'\nUsing the deprecated createGame method instead.'
);
return this.createGame(matchID, opts);
} else {
console.error(
'The database connector does not implement a createMatch method.'
);
}
}
/**
* Create a new game.
*
* This might just need to call setState and setMetadata in
* most implementations.
*
* However, it exists as a separate call so that the
* implementation can provision things differently when
* a game is created. For example, it might stow away the
* initial game state in a separate field for easier retrieval.
*
* @deprecated Use createMatch instead, if implemented
*/
async createGame?(matchID: string, opts: CreateGameOpts): Promise<void>;
/**
* Update the game state.
*
* If passed a deltalog array, setState should append its contents to the
* existing log for this game.
*/
abstract setState(
matchID: string,
state: State,
deltalog?: LogEntry[]
): Promise<void>;
/**
* Update the game metadata.
*/
abstract setMetadata(
matchID: string,
metadata: Server.MatchData
): Promise<void>;
/**
* Fetch the game state.
*/
abstract fetch<O extends FetchOpts>(
matchID: string,
opts: O
): Promise<FetchResult<O>>;
/**
* Remove the game state.
*/
abstract wipe(matchID: string): Promise<void>;
/**
* Return all matches.
*/
/* istanbul ignore next */
async listMatches(opts?: ListMatchesOpts): Promise<string[]> {
if (this.listGames) {
console.warn(
'The database connector does not implement a listMatches method.',
'\nUsing the deprecated listGames method instead.'
);
return this.listGames(opts);
} else {
console.error(
'The database connector does not implement a listMatches method.'
);
}
}
/**
* Return all games.
*
* @deprecated Use listMatches instead, if implemented
*/
async listGames?(opts?: ListGamesOpts): Promise<string[]>;
}
export abstract class Sync {
type() {
return Type.SYNC;
}
/**
* Connect.
*/
connect() {
return;
}
/**
* Create a new match.
*
* This might just need to call setState and setMetadata in
* most implementations.
*
* However, it exists as a separate call so that the
* implementation can provision things differently when
* a match is created. For example, it might stow away the
* initial match state in a separate field for easier retrieval.
*/
/* istanbul ignore next */
createMatch(matchID: string, opts: CreateMatchOpts): void {
if (this.createGame) {
console.warn(
'The database connector does not implement a createMatch method.',
'\nUsing the deprecated createGame method instead.'
);
return this.createGame(matchID, opts);
} else {
console.error(
'The database connector does not implement a createMatch method.'
);
}
}
/**
* Create a new game.
*
* This might just need to call setState and setMetadata in
* most implementations.
*
* However, it exists as a separate call so that the
* implementation can provision things differently when
* a game is created. For example, it might stow away the
* initial game state in a separate field for easier retrieval.
*
* @deprecated Use createMatch instead, if implemented
*/
createGame?(matchID: string, opts: CreateGameOpts): void;
/**
* Update the match state.
*
* If passed a deltalog array, setState should append its contents to the
* existing log for this match.
*/
abstract setState(matchID: string, state: State, deltalog?: LogEntry[]): void;
/**
* Update the match metadata.
*/
abstract setMetadata(matchID: string, metadata: Server.MatchData): void;
/**
* Fetch the match state.
*/
abstract fetch<O extends FetchOpts>(matchID: string, opts: O): FetchResult<O>;
/**
* Remove the match state.
*/
abstract wipe(matchID: string): void;
/**
* Return all matches.
*/
/* istanbul ignore next */
listMatches(opts?: ListMatchesOpts): string[] {
if (this.listGames) {
console.warn(
'The database connector does not implement a listMatches method.',
'\nUsing the deprecated listGames method instead.'
);
return this.listGames(opts);
} else {
console.error(
'The database connector does not implement a listMatches method.'
);
}
}
/**
* Return all games.
*
* @deprecated Use listMatches instead, if implemented
*/
listGames?(opts?: ListGamesOpts): string[];
}