-
Notifications
You must be signed in to change notification settings - Fork 5
/
battle.ts
379 lines (353 loc) · 12 KB
/
battle.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
import {TeamGenerators} from "@pkmn/randoms";
import {BattleStreams, PRNGSeed, Streams, Teams} from "@pkmn/sim";
import {SideID} from "@pkmn/types";
import {RoomEvent} from "../../protocol/Event";
import {protocolParser} from "../../protocol/parser";
import {Sender} from "../../psbot/PsBot";
import {DeferredFile} from "../../utils/DeferredFile";
import {WrappedError} from "../../utils/errors/WrappedError";
import {LogFunc, Logger} from "../../utils/logging/Logger";
import {Verbose} from "../../utils/logging/Verbose";
import {wrapTimeout} from "../../utils/timeout";
import {BattleDriver} from "../BattleDriver";
import {BattleAgent} from "../agent";
import {BattleParser} from "../parser/BattleParser";
Teams.setGeneratorFactory(TeamGenerators);
/** Identifier type for the sides of a battle. */
export type PlayerSide = Exclude<SideID, "p3" | "p4">;
/** Options for {@link simulateBattle}. */
export interface BattleOptions {
/** Player configs. */
readonly players: {readonly [P in PlayerSide]: PlayerOptions};
/**
* Maximum amount of turns until the game is truncated. The
* {@link PlayerOptions.agent BattleAgents} will not be called at the end of
* the `maxTurns`th turn. If this is not set, it's possible for both agents
* to force an endless game.
*/
readonly maxTurns?: number;
/**
* Path to the file to store game logs in. If not specified, and the
* simulator encounters an error, then the logs will be stored in a temp
* file.
*/
readonly logPath?: string;
/**
* If true, logs should only be written to disk (when {@link logPath} is
* provided if an error is encountered, and discarded if no error. If
* `logPath` is not provided, regardless of the value of this option, the
* battle log will be stored in a temp file.
*/
readonly onlyLogOnError?: boolean;
/** Seed for the battle PRNG. */
readonly seed?: PRNGSeed;
/**
* Timeout in milliseconds for processing battle-related actions and events.
* Used for catching rare async bugs or timing out BattleAgent
* communications.
*/
readonly timeoutMs?: number;
}
/**
* Player options for {@link simulateBattle}.
*
* @template TResult Parser result type.
*/
export interface PlayerOptions {
/** Player name. */
readonly name: string;
/** Battle decision-maker. */
readonly agent: BattleAgent;
/** Battle event parser. Responsible for calling the {@link agent}. */
readonly parser: BattleParser;
/** Seed for generating the random team. */
readonly seed?: PRNGSeed;
}
/**
* Result from playing a PS game.
*
* @template TResult Result from each player's {@link BattleParser}.
*/
export interface BattleResult {
/** Side of the winner if it's not a tie. */
winner?: PlayerSide;
/** Whether the game was truncated due to max turn limit. */
truncated?: boolean;
/**
* Path to the file containing game logs if enabled. Should be equal to
* {@link BattleOptions.logPath} if specified, otherwise points to a temp
* file.
*/
logPath?: string;
/**
* If an exception was thrown during the game, store it here instead of
* propagating it through the pipeline.
*/
err?: Error;
}
/** Temp log file template. */
const template = "psbattle-XXXXXX";
/**
* Runs a simulated PS battle.
*
* @template TResult Result from each player's {@link BattleParser}.
*/
export async function simulateBattle(
options: BattleOptions,
): Promise<BattleResult> {
let logPath: string | undefined;
const file = new DeferredFile();
if (!options.onlyLogOnError) {
logPath = await file.ensure(options.logPath, template);
}
// Setup logger.
const logFunc: LogFunc = msg => file.stream.write(msg);
const logger = new Logger(logFunc, Verbose.Debug, "Battle: ");
// Start simulating a battle.
const battleStream = new BattleStreams.BattleStream({keepAlive: false});
const streams = BattleStreams.getPlayerStreams(battleStream);
const startOptions = {
formatid: "gen4randombattle",
...(options.seed && {seed: options.seed}),
};
void streams.omniscient.write(`>start ${JSON.stringify(startOptions)}`);
logger.debug(
`Starting battle with options: ${JSON.stringify(startOptions)}`,
);
const gamePromises: Promise<void>[] = [];
let truncated: boolean | undefined;
const truncate = async () => {
truncated = true;
if (!battleStream.atEOF) {
await battleStream.writeEnd();
}
};
let winner: PlayerSide | undefined;
gamePromises.push(
(async function omniscientStreamHandler() {
try {
const winnerName = await omniscientEventPipeline(
streams.omniscient,
options.timeoutMs,
);
for (const id of ["p1", "p2"] as const) {
if (winnerName === options.players[id].name) {
winner = id;
}
}
} catch (err) {
await truncate();
logPath = await file.ensure(options.logPath, template);
handleEventPipelineError(err as Error, logger, logPath);
}
})(),
);
for (const id of ["p1", "p2"] as const) {
const playerLog = logger.addPrefix(
`${id}(${options.players[id].name}): `,
);
gamePromises.push(
(async function playerStreamHandler() {
try {
await playerEventPipeline(
streams[id],
options.players[id],
playerLog,
options.maxTurns,
truncate,
options.timeoutMs,
);
} catch (err) {
await truncate();
logPath = await file.ensure(options.logPath, template);
handleEventPipelineError(err as Error, playerLog, logPath);
}
})(),
);
const playerOptions = {
name: options.players[id].name,
...(options.players[id].seed && {seed: options.players[id].seed}),
};
playerLog.debug(
`Setting up player with options: ${JSON.stringify(playerOptions)}`,
);
await battleStream.write(
`>player ${id} ${JSON.stringify(playerOptions)}`,
);
}
// Capture the first game error so we can notify the main thread.
let err: Error | undefined;
try {
await Promise.all(gamePromises);
if (!battleStream.atEOF) {
await battleStream.writeEnd();
}
} catch (e) {
err = e as Error;
}
// Should probably never happen, but not a big deal if it does.
if (!battleStream.atEOF) {
logger.info("Destroying battle stream");
void battleStream.destroy();
}
// Make sure the game completely ends so that the logs are complete.
// Note: Subsequent errors are swallowed since they've already been logged
// and we already captured one from the earlier Promise.all() to notify the
// main thread.
logger.info("Settling");
await Promise.allSettled(gamePromises);
// Close the log file and return.
file.stream.write("Done\n");
await file.finish();
return {
winner,
...(truncated && {truncated: true}),
...(logPath && {logPath}),
...(err && {err}),
};
}
/** Wraps the error for top-level and/or points to log file in the err msg. */
function handleEventPipelineError(
err: Error,
logger: Logger,
logPath?: string,
): never {
logger.error(err.stack ?? err.toString());
logger.info("Error encountered; truncating");
if (logPath) {
throw new Error(
`${simulateBattle.name}() encountered an error; check ${logPath} ` +
"for details",
);
}
throw new WrappedError(
err,
msg => `${simulateBattle.name}() encountered an error: ${msg}`,
);
}
/**
* Processes the omniscient battle stream and returns the name of the winner if
* there was one.
*/
async function omniscientEventPipeline(
battleTextStream: Streams.ObjectReadWriteStream<string>,
timeoutMs?: number,
): Promise<string | undefined> {
const maybeTimeout: <T>(p: () => Promise<T>) => Promise<T> = timeoutMs
? async p => await wrapTimeout(p, timeoutMs)
: async p => await p();
let winner: string | undefined;
let chunk: string | null | undefined;
while (
(chunk = await maybeTimeout(async () => await battleTextStream.read()))
) {
for (const event of protocolParser(chunk)) {
if (event.args[0] === "win") {
[, winner] = event.args;
}
}
}
return winner;
}
async function playerEventPipeline(
battleTextStream: Streams.ObjectReadWriteStream<string>,
options: PlayerOptions,
logger: Logger,
maxTurns?: number,
truncate?: () => Promise<void>,
timeoutMs?: number,
): Promise<void> {
const sender = createSender(battleTextStream, logger);
const driver = new BattleDriver({
username: options.name,
agent: options.agent,
parser: options.parser,
sender,
logger,
});
const maybeTimeout: <T>(p: () => Promise<T>) => Promise<T> = timeoutMs
? async p => await wrapTimeout(p, timeoutMs)
: async p => await p();
let truncated = false;
let loopErr: Error | undefined;
try {
let chunk: string | null | undefined;
while (
(chunk = await maybeTimeout(
async () => await battleTextStream.read(),
))
) {
logger.debug(`Received:\n${chunk}`);
for (const event of protocolParser(chunk)) {
if (event.args[0] === "halt") {
driver.halt();
continue;
}
await maybeTimeout(
async () => await driver.handle(event as RoomEvent),
);
if (
event.args[0] === "turn" &&
maxTurns &&
Number(event.args[1]) >= maxTurns
) {
logger.info(`Reached max turn ${maxTurns}; truncating`);
truncated = true;
break;
}
}
if (truncated) {
break;
}
}
} catch (err) {
loopErr = err as Error;
truncated = true;
throw err;
} finally {
try {
if (truncated) {
await maybeTimeout(
async () =>
await Promise.all([truncate?.(), driver.forceFinish()]),
);
} else {
driver.finish();
}
} catch (err) {
if (loopErr) {
// Preserve and bubble up original error.
logger.error(
`Error while finishing: ${
(err as Error).stack ?? (err as Error).toString()
}`,
);
// eslint-disable-next-line no-unsafe-finally
throw loopErr;
}
// eslint-disable-next-line no-unsafe-finally
throw err;
}
}
}
function createSender(
battleTextStream: Streams.ObjectReadWriteStream<string>,
logger: Logger,
): Sender {
return (...responses) => {
for (const res of responses) {
// Extract choice from args.
// Format: |/choose <choice>
if (res.startsWith("|/choose ")) {
const choice = res.substring("|/choose ".length);
if (!battleTextStream.atEOF) {
void battleTextStream.write(choice);
} else {
logger.error("Can't send choice: At end of stream");
return false;
}
}
}
return true;
};
}