pokemon-showdown/server/team-validator-async.ts
Guangcong Luo 13189fdb02
Update Dex API (#8181)
This is the change that renames:

- `Dex.getMove` -> `Dex.moves.get`
- `Dex.getAbility` -> `Dex.abilities.get`
- `Dex.getItem` -> `Dex.items.get`
- `Dex.getSpecies` -> `Dex.species.get`
- `Dex.getEffect` -> `Dex.conditions.get`
- `Dex.getNature` -> `Dex.natures.get`
- `Dex.getType` -> `Dex.types.get`
- `Dex.getFormat` -> `Dex.formats.get`

In addition, some other APIs have been updated:

- `getByID` methods have also been added to every other table.
- `Dex.moves.all()` now gets an array of all moves
  - Plus equivalent methods for `abilities`, `items`, `species`, `formats`, `natures`, `types`
  - Note: there's no `Dex.conditions.all()`
- new API: `Dex.stats` for naming/iterating stats
- `Dex.getEffectByID` -> `Dex.conditions.getByID`
- `Dex.getType` -> `Dex.types.get`
- `Dex.data.Formats` -> `Dex.data.Rulesets`
- `Dex.formats` -> now an array `Dex.formats.all()`
- `Dex.getRuleTable` -> `Dex.formats.getRuleTable`
- `Dex.validateFormat` -> `Dex.formats.validate`

Team functions have been split off into a new `sim/teams` package:

- `Dex.packTeam` -> `Teams.pack`
- `Dex.fastUnpackTeam` -> `Teams.unpack`
- `Dex.generateTeam` -> `Teams.generate`
- `Dex.stringifyTeam` -> `Teams.export`

`Teams.export` has also been rewritten to better match how it works in client.

This implements #8178
2021-04-08 03:00:37 -07:00

99 lines
2.7 KiB
TypeScript

/**
* Team Validator
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Spawns a child process to validate teams.
*
* @license MIT
*/
import {TeamValidator} from '../sim/team-validator';
export class TeamValidatorAsync {
format: Format;
constructor(format: string) {
this.format = Dex.formats.get(format);
}
validateTeam(team: string, options?: {removeNicknames?: boolean}) {
let formatid = this.format.id;
if (this.format.customRules) formatid += '@@@' + this.format.customRules.join(',');
return PM.query({formatid, options, team});
}
static get(format: string) {
return new TeamValidatorAsync(format);
}
}
export const get = TeamValidatorAsync.get;
/*********************************************************
* Process manager
*********************************************************/
import {QueryProcessManager} from '../lib/process-manager';
export const PM = new QueryProcessManager<{
formatid: string, options?: {removeNicknames?: boolean}, team: string,
}>(module, message => {
const {formatid, options, team} = message;
const parsedTeam = Teams.unpack(team);
if (Config.debugvalidatorprocesses && process.send) {
process.send('DEBUG\n' + JSON.stringify(message));
}
let problems;
try {
problems = TeamValidator.get(formatid).validateTeam(parsedTeam, options);
} catch (err) {
Monitor.crashlog(err, 'A team validation', {
formatid,
team,
});
problems = [
`Your team crashed the validator. We'll fix this crash within a few hours (we're automatically notified),` +
` but if you don't want to wait, just use a different team for now.`,
];
}
if (problems?.length) {
return '0' + problems.join('\n');
}
const packedTeam = Teams.pack(parsedTeam);
// console.log('FROM: ' + message.substr(pipeIndex2 + 1));
// console.log('TO: ' + packedTeam);
return '1' + packedTeam;
});
if (!PM.isParentProcess) {
// This is a child process!
global.Config = require('./config-loader');
global.Monitor = {
crashlog(error: Error, source = 'A team validator process', details: AnyObject | null = null) {
const repr = JSON.stringify([error.name, error.message, source, details]);
process.send!(`THROW\n@!!@${repr}\n${error.stack}`);
},
};
if (Config.crashguard) {
process.on('uncaughtException', (err: Error) => {
Monitor.crashlog(err, `A team validator process`);
});
process.on('unhandledRejection', err => {
Monitor.crashlog(err as any || {}, 'A team validator process Promise');
});
}
global.Dex = require('../sim/dex').Dex.includeData();
global.Teams = require('../sim/teams').Teams;
// eslint-disable-next-line no-eval
require('../lib/repl').Repl.start(`team-validator-${process.pid}`, (cmd: string) => eval(cmd));
} else {
PM.spawn(global.Config ? Config.validatorprocesses : 1);
}