-
Notifications
You must be signed in to change notification settings - Fork 0
/
team-validator-async.js
95 lines (79 loc) · 2.48 KB
/
team-validator-async.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* 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
*********************************************************/
const QueryProcessManager = require('./lib/process-manager').QueryProcessManager;
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('./lib/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
global.Config = require('./config/config');
global.TeamValidator = require('./sim/team-validator');
if (Config.crashguard) {
process.on('uncaughtException', err => {
require('./lib/crashlogger')(err, `A team validator process`, true);
});
}
global.Dex = require('./sim/dex').includeData();
global.toId = Dex.getId;
global.Chat = require('./chat');
require('./lib/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;