pokemon-showdown/sim/tools/multi-random-runner.ts
Guangcong Luo 78439b4a02
Update to ESLint 9 (#10926)
ESLint has a whole new config format, so I figure it's a good time to
make the config system saner.

- First, we no longer have separate eslint-no-types configs. Lint
  performance shouldn't be enough of a problem to justify the
  relevant maintenance complexity.

- Second, our base config should work out-of-the-box now. `npx eslint`
  will work as expected, without any CLI flags. You should still use
  `npm run lint` which adds the `--cached` flag for performance.

- Third, whatever updates I did fixed style linting, which apparently
  has been bugged for quite some time, considering all the obvious
  mixed-tabs-and-spaces issues I found in the upgrade.

Also here are some changes to our style rules. In particular:

- Curly brackets (for objects etc) now have spaces inside them. Sorry
  for the huge change. ESLint doesn't support our old style, and most
  projects use Prettier style, so we might as well match them in this way.
  See https://github.com/eslint-stylistic/eslint-stylistic/issues/415

- String + number concatenation is no longer allowed. We now
  consistently use template strings for this.
2025-02-25 20:03:46 -08:00

116 lines
2.8 KiB
TypeScript

/**
* Battle Simulator multi random runner.
* Pokemon Showdown - http://pokemonshowdown.com/
*
* @license MIT
*/
import { PRNG, type PRNGSeed } from '../prng';
import { Runner, type RunnerOptions } from './runner';
export interface MultiRandomRunnerOptions extends Partial<RunnerOptions> {
totalGames: number;
prng?: PRNG | PRNGSeed | null;
format?: string;
cycle?: boolean;
all?: boolean;
async?: boolean;
}
export class MultiRandomRunner {
static readonly FORMATS = [
'gen8randombattle', 'gen8randomdoublesbattle', 'gen8battlefactory',
'gen7randombattle', 'gen7battlefactory',
'gen6randombattle', 'gen6battlefactory',
'gen5randombattle',
'gen4randombattle',
'gen3randombattle',
'gen2randombattle',
'gen1randombattle',
];
private readonly options: Partial<RunnerOptions>;
private readonly totalGames: number;
private readonly prng: PRNG;
private readonly format: string | undefined;
private readonly cycle: boolean;
private readonly all: boolean;
private readonly isAsync: boolean;
private formatIndex: number;
private numGames: number;
constructor(options: MultiRandomRunnerOptions) {
this.options = { ...options };
this.totalGames = options.totalGames;
this.prng = PRNG.get(options.prng);
this.options.prng = this.prng;
this.format = options.format;
this.cycle = !!options.cycle;
this.all = !!options.all;
this.isAsync = !!options.async;
this.formatIndex = 0;
this.numGames = 0;
}
async run() {
let games = [];
let format: string | false;
let lastFormat: string | false = false;
let failures = 0;
while ((format = this.getNextFormat())) {
if (this.all && lastFormat && format !== lastFormat) {
if (this.isAsync) await Promise.all(games);
games = [];
}
const seed = this.prng.getSeed();
const game = new Runner({ format, ...this.options }).run().catch(err => {
failures++;
console.error(
`Run \`node tools/simulate multi 1 --format=${format} --seed=${seed}\` ` +
`to debug (optionally with \`--output\` and/or \`--input\` for more info):\n`,
err
);
});
if (!this.isAsync) await game;
games.push(game);
lastFormat = format;
}
if (this.isAsync) await Promise.all(games);
return failures;
}
private getNextFormat() {
const FORMATS = MultiRandomRunner.FORMATS;
if (this.formatIndex > FORMATS.length) return false;
if (this.numGames++ < this.totalGames) {
if (this.format) {
return this.format;
} else if (this.all) {
return FORMATS[this.formatIndex];
} else if (this.cycle) {
const format = FORMATS[this.formatIndex];
this.formatIndex = (this.formatIndex + 1) % FORMATS.length;
return format;
} else {
return this.prng.sample(FORMATS);
}
} else if (this.all) {
this.numGames = 1;
this.formatIndex++;
return FORMATS[this.formatIndex];
}
return false;
}
}