diff --git a/src/battle-dex-data.ts b/src/battle-dex-data.ts index 132b5f096..4ce5d06d9 100644 --- a/src/battle-dex-data.ts +++ b/src/battle-dex-data.ts @@ -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; diff --git a/src/battle-dex.ts b/src/battle-dex.ts index e2644c29a..1c86deb3f 100644 --- a/src/battle-dex.ts +++ b/src/battle-dex.ts @@ -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/'}); diff --git a/src/battle-text-parser.ts b/src/battle-text-parser.ts index 953ab9e5f..d217c59c9 100644 --- a/src/battle-text-parser.ts +++ b/src/battle-text-parser.ts @@ -926,4 +926,7 @@ class BattleTextParser { } } -exports.BattleTextParser = BattleTextParser; +if (typeof require === 'function') { + // in Node + (global as any).BattleTextParser = BattleTextParser; +} diff --git a/src/battle.ts b/src/battle.ts index e14c5d113..d97e06df6 100644 --- a/src/battle.ts +++ b/src/battle.ts @@ -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]) { diff --git a/test/parse b/test/parse index 2862eb37f..dac29c5cf 100755 --- a/test/parse +++ b/test/parse @@ -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)); });