Use require instead of eval in ./test/parse

I still don't like how everything's a global in Node, but for now we
don't have a better solution.
This commit is contained in:
Guangcong Luo 2019-01-05 05:14:32 -06:00
parent 86a55ee61e
commit d05fe62375
5 changed files with 35 additions and 26 deletions

View File

@ -961,6 +961,7 @@ class Template implements Effect {
if (typeof require === 'function') {
// in Node
(global as any).BattleBaseSpeciesChart = BattleBaseSpeciesChart;
(global as any).BattleStats = BattleStats;
(global as any).PureEffect = PureEffect;
(global as any).Template = Template;
(global as any).Ability = Ability;

View File

@ -73,7 +73,13 @@ if (!Object.assign) {
// };
// }
if (!window.exports) window.exports = window;
if (typeof window === 'undefined') {
// Node
(global as any).window = global;
} else {
// browser (possibly NW.js!)
window.exports = window;
}
if (window.soundManager) {
soundManager.setup({url: 'https://play.pokemonshowdown.com/swf/'});

View File

@ -926,4 +926,7 @@ class BattleTextParser {
}
}
exports.BattleTextParser = BattleTextParser;
if (typeof require === 'function') {
// in Node
(global as any).BattleTextParser = BattleTextParser;
}

View File

@ -3165,35 +3165,35 @@ class Battle {
break;
}}
}
static lineParse(str: string): {args: Args, kwArgs: KWArgs} {
if (!str.startsWith('|')) {
return {args: ['', str], kwArgs: {}};
static lineParse(line: string): {args: Args, kwArgs: KWArgs} {
if (!line.startsWith('|')) {
return {args: ['', line], kwArgs: {}};
}
if (str === '|') {
if (line === '|') {
return {args: ['done'], kwArgs: {}};
}
const index = str.indexOf('|', 1);
const cmd = str.slice(1, index);
const index = line.indexOf('|', 1);
const cmd = line.slice(1, index);
switch (cmd) {
case 'chatmsg': case 'chatmsg-raw': case 'raw': case 'error': case 'html':
case 'inactive': case 'inactiveoff': case 'warning':
case 'fieldhtml': case 'controlshtml': case 'bigerror':
case 'debug': case 'tier':
return {args: [cmd, str.slice(index + 1)], kwArgs: {}};
return {args: [cmd, line.slice(index + 1)], kwArgs: {}};
case 'c': case 'chat': case 'uhtml': case 'uhtmlchange':
// three parts
const index2a = str.indexOf('|', index + 1);
return {args: [cmd, str.slice(index + 1, index2a), str.slice(index2a + 1)], kwArgs: {}};
const index2a = line.indexOf('|', index + 1);
return {args: [cmd, line.slice(index + 1, index2a), line.slice(index2a + 1)], kwArgs: {}};
case 'c:':
// four parts
const index2b = str.indexOf('|', index + 1);
const index3b = str.indexOf('|', index2b + 1);
const index2b = line.indexOf('|', index + 1);
const index3b = line.indexOf('|', index2b + 1);
return {
args: [cmd, str.slice(index + 1, index2b), str.slice(index2b + 1, index3b), str.slice(index3b + 1)],
args: [cmd, line.slice(index + 1, index2b), line.slice(index2b + 1, index3b), line.slice(index3b + 1)],
kwArgs: {},
};
}
let args: Args = str.slice(1).split('|') as any;
let args: Args = line.slice(1).split('|') as any;
let kwArgs: KWArgs = {};
while (args.length > 1) {
const lastArg = args[args.length - 1];
@ -3206,6 +3206,10 @@ class Battle {
}
return {args, kwArgs};
}
static extractMessage(line: string, parser: BattleTextParser) {
const {args, kwArgs} = Battle.lineParse(line);
return parser.parseLine(args, kwArgs) || '';
}
run(str: string, preempt?: boolean) {
if (!preempt && this.preemptActivityQueue.length && str === this.preemptActivityQueue[0]) {

View File

@ -5,15 +5,11 @@ const path = require('path');
process.chdir(path.resolve(__dirname, '..'));
window = global;
{
let exports = global;
eval('' + fs.readFileSync(`js/battle-dex.js`));
eval('' + fs.readFileSync(`js/battle-dex-data.js`));
eval('' + fs.readFileSync(`js/battle.js`));
eval('' + fs.readFileSync(`data/text.js`));
eval('' + fs.readFileSync(`js/battle-text-parser.js`));
}
require(`../js/battle-dex.js`);
require(`../js/battle-dex-data.js`);
require(`../js/battle.js`);
global.BattleText = require(`../data/text.js`).BattleText;
require(`../js/battle-text-parser.js`);
const parser = new BattleTextParser();
@ -25,6 +21,5 @@ const rl = readline.createInterface({
});
rl.on('line', line => {
const {args, kwArgs} = Battle.lineParse(line);
process.stdout.write(parser.parseLine(args, kwArgs) || '');
process.stdout.write(Battle.extractMessage(line, parser));
});