pokemon-showdown/server/team-validator-async.js
Guangcong Luo 87ef0cd5c4 TypeScript: export default
I'm currently pretty annoyed at TypeScript and TC39 for default exports
being a mess.

The goal here is to be able to type

    import Dex from './dex';

instead of any of

    import Dex = require('./dex');
    import {Dex} from './dex';
    import * as Dex from './dex';

This part involves a significant amount of struggle.

First, you can't automatically package up all your exports as your
default export. This leads to extremely un-DRY code, like in sim/index:

    export {
        Pokemon,
        Side,
        Battle,
        PRNG,
        Dex,
        TeamValidator,

        BattleStream,
    };

    export const Sim = {
        Pokemon,
        Side,
        Battle,
        PRNG,
        Dex,
        TeamValidator,

        BattleStream,
    };

(Both of these exports would be entirely unnecessary if you could just
automatically declare the file's exports as a default namespace.)

Second, a default export can't easily be a namespace. And TypeScript
doesn't allow types to exist in objects. Take the example from earlier:

    export const Sim = {
        Pokemon,
    };

If we were to try to use it:

    import Sim from './sim';
    let pokemon: Sim.Pokemon;

you'll get this error:

    Cannot find namespace 'Sim'. ts(2503)

You can, of course, fix this by making Sim a namespace:

    const PokemonT = Pokemon;
    type PokemonT = Pokemon;
    export namespace Sim {
        export const Pokemon = PokemonT;
        type Pokemon = PokemonT;
    }

But this quickly gets ridiculous the more classes you try to export.

You'd think there'd be a better way to do this. But I, at least,
haven't found one.
2019-05-14 20:33:33 +10:00

120 lines
3.4 KiB
JavaScript

/**
* Team Validator
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Spawns a child process to validate teams.
*
* @license MIT
*/
'use strict';
class ValidatorAsync {
/**
* @param {string} format
*/
constructor(format) {
this.format = Dex.getFormat(format);
}
/**
* @param {string} team
* @param {boolean} [removeNicknames]
*/
validateTeam(team, removeNicknames = false) {
let formatid = this.format.id;
if (this.format.customRules) formatid += '@@@' + this.format.customRules.join(',');
return PM.query({formatid, removeNicknames, team});
}
}
/*********************************************************
* Process manager
*********************************************************/
/** @type {typeof import('../lib/process-manager').QueryProcessManager} */
const QueryProcessManager = require(/** @type {any} */('../.lib-dist/process-manager')).QueryProcessManager;
/** @type {QueryProcessManager} */
// @ts-ignore
const PM = new QueryProcessManager(module, async message => {
let {formatid, removeNicknames, team} = message;
let parsedTeam = Dex.fastUnpackTeam(team);
let problems;
try {
problems = TeamValidator(formatid).validateTeam(parsedTeam, removeNicknames);
} catch (err) {
require(/** @type {any} */('../.lib-dist/crashlogger')).crashlogger(err, 'A team validation', {
formatid: formatid,
team: 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 && problems.length) {
return '0' + problems.join('\n');
}
let packedTeam = Dex.packTeam(parsedTeam);
// console.log('FROM: ' + message.substr(pipeIndex2 + 1));
// console.log('TO: ' + packedTeam);
return '1' + packedTeam;
});
if (!PM.isParentProcess) {
// This is a child process!
// @ts-ignore This file doesn't exist on the repository, so Travis checks fail if this isn't ignored
global.Config = require('../config/config');
global.TeamValidator = require(/** @type {any} */ ('../.sim-dist/team-validator')).TeamValidator;
// @ts-ignore ???
global.Monitor = {
/**
* @param {Error} error
* @param {string} source
* @param {{}?} details
*/
crashlog(error, source = 'A team validator process', details = 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 => {
Monitor.crashlog(err, `A team validator process`);
});
process.on('unhandledRejection', err => {
if (err instanceof Error) {
Monitor.crashlog(err, 'A team validator process Promise');
}
});
}
global.Dex = require(/** @type {any} */ ('../.sim-dist/dex')).Dex.includeData();
global.toID = Dex.getId;
global.Chat = require('./chat');
/** @type {typeof import('../lib/repl').Repl} */
const Repl = require(/** @type {any} */('../.lib-dist/repl')).Repl;
Repl.start(`team-validator-${process.pid}`, cmd => eval(cmd));
} else {
PM.spawn(global.Config ? Config.validatorprocesses : 1);
}
/*********************************************************
* Exports
*********************************************************/
function getAsyncValidator(/** @type {string} */ format) {
return new ValidatorAsync(format);
}
let TeamValidatorAsync = Object.assign(getAsyncValidator, {
ValidatorAsync,
PM,
});
module.exports = TeamValidatorAsync;