smogon / pokemon-showdown

Pokémon battle simulator.
https://pokemonshowdown.com
MIT License
4.77k stars 2.79k forks source link

RandomPlayerAI wrong teamsize assumption #6212

Closed 1iminal closed 4 years ago

1iminal commented 4 years ago

Running a battle versus a RandomPlayerAI with a team of size 1 causes an error:

(node:5460) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'condition' of undefined
    at request.active.map (/usr/src/pokemon-showdown-master/.sim-dist/tools/random-player-ai.js:103:37)
    at Array.map (<anonymous>)
    at RandomPlayerAI.receiveRequest (/usr/src/pokemon-showdown-master/.sim-dist/tools/random-player-ai.js:70:35)
    at RandomPlayerAI.receiveLine (/usr/src/pokemon-showdown-master/.sim-dist/battle-stream.js:232:38)
    at RandomPlayerAI.receive (/usr/src/pokemon-showdown-master/.sim-dist/battle-stream.js:224:9)
    at RandomPlayerAI.start (/usr/src/pokemon-showdown-master/.sim-dist/battle-stream.js:218:9)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:5460) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5460) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

At /sim/tools/random-player-ai.ts#L102 RandomPlayerAI assumes teamsize is at least 2, which may be not necessarily true.

This can be fixed by checking the teamsize before running the status check on allies.

let hasAlly = false;
if(pokemon.length>1){
  hasAlly = !pokemon[i ^ 1].condition.endsWith(` fnt`);
}
Zarel commented 4 years ago

Do you want to PR that or should I?

Also @scheibo do you have any opinions on this?

1iminal commented 4 years ago

I can do a PR if noone is against it

Zarel commented 4 years ago

(Fixed in #6216)

scheibo commented 4 years ago

Also @scheibo do you have any opinions on this?

Sorry, missed this bug (though apparently I had seen the PR). Thank you for finding and fixing.