mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-04-25 07:22:09 -05:00
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.
102 lines
3.0 KiB
TypeScript
102 lines
3.0 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, user?: ID }) {
|
|
let formatid = this.format.id;
|
|
if (this.format.customRules) formatid += '@@@' + this.format.customRules.join(',');
|
|
if (team.length > (25 * 1024 - 6)) { // don't even let it go to the child process
|
|
return Promise.resolve('0Your team is over 25KB. Please use a smaller team.');
|
|
}
|
|
return PM.query({ formatid, options, team });
|
|
}
|
|
|
|
static get(this: void, 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;
|
|
}, 2 * 60 * 1000);
|
|
|
|
if (!PM.isParentProcess) {
|
|
// This is a child process!
|
|
global.Config = require('./config-loader').Config;
|
|
|
|
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);
|
|
}
|