pokemon-showdown/server/team-validator-async.ts
Waleed Hassan 8aa4f053bb
Use optional chaining and linter improvements (#6422)
This is mostly just a follow up to #6342.

`prefer-optional-chaining` was turned on and fixed in every location it
complained in. The transformed function [0] looks expensive from a
glance but from skimming through the replaced sites it doesn't appear
to be ran in any important place, so it should be OK.

The linter improvements are:
- Increase linter performance
	- Make `full-lint` and `lint` write to different caches so we
	  avoid overwriting their caches since they're different configs
	- Change husky's hook to `npm run lint` so as to write to the
	  same cache
	- Remove `@typescript-eslint/eslint-plugin-tslint` which is
	  essentially a wrapper to TSLint because the rules aren't worth
	  running another linter
- Convert `.eslintrc.json` and `.eslintrc-syntax.json` to two spaces
  rather than four tabs to respect PS' `.editorconfig`
- Rename `fulllint` to `full-lint` to ease spelling it

[0] - https://pastie.io/mmtxpf.js (prettified)
2020-03-06 11:44:32 -08:00

109 lines
2.8 KiB
TypeScript

/**
* Team Validator
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Spawns a child process to validate teams.
*
* @license MIT
*/
import {crashlogger} from '../lib/crashlogger';
declare let global: any;
export class TeamValidatorAsync {
format: Format;
constructor(format: string) {
this.format = Dex.getFormat(format);
}
validateTeam(team: string, removeNicknames = false) {
let formatid = this.format.id;
if (this.format.customRules) formatid += '@@@' + this.format.customRules.join(',');
return PM.query({formatid, removeNicknames, 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(module, message => {
const {formatid, removeNicknames, team} = message;
const parsedTeam = Dex.fastUnpackTeam(team);
let problems;
try {
problems = TeamValidator.get(formatid).validateTeam(parsedTeam, removeNicknames);
} catch (err) {
crashlogger(err, 'A team validation', {
formatid,
team,
});
problems = [
`Your team crashed the team validator. We've been automatically notified and will fix this crash,` +
` but you should use a different team for now.`,
];
}
if (problems?.length) {
return '0' + problems.join('\n');
}
const packedTeam = Dex.packTeam(parsedTeam);
// console.log('FROM: ' + message.substr(pipeIndex2 + 1));
// console.log('TO: ' + packedTeam);
return '1' + packedTeam;
});
import {Repl} from '../lib/repl';
import {Dex as importedDex} from '../sim/dex';
import {TeamValidator} from '../sim/team-validator';
import {Chat} from './chat';
import {Config} from './config-loader';
if (!PM.isParentProcess) {
// This is a child process!
global.Config = Config;
global.TeamValidator = TeamValidator;
// @ts-ignore ???
global.Monitor = {
crashlog(error: Error, source = 'A team validator process', details: any = null) {
const repr = JSON.stringify([error.name, error.message, source, details]);
// @ts-ignore
process.send(`THROW\n@!!@${repr}\n${error.stack}`);
},
};
if (Config.crashguard) {
process.on('uncaughtException', (err: Error) => {
Monitor.crashlog(err, `A team validator process`);
});
// Typescript doesn't like this call
// @ts-ignore
process.on('unhandledRejection', (err: Error, promise: Promise) => {
if (err instanceof Error) {
Monitor.crashlog(err, 'A team validator process Promise');
}
});
}
global.Dex = importedDex.includeData();
global.toID = Dex.getId;
// tslint:disable-next-line: no-eval
Repl.start(`team-validator-${process.pid}`, cmd => eval(cmd));
} else {
PM.spawn(global.Config ? Config.validatorprocesses : 1);
}