Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential RandomPlayerAI bug in simulator #32

Closed
cyi1341 opened this issue Sep 11, 2024 · 1 comment
Closed

Potential RandomPlayerAI bug in simulator #32

cyi1341 opened this issue Sep 11, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@cyi1341
Copy link

cyi1341 commented Sep 11, 2024

Describe the bug:

Example in README of @pkmn/sim not working as expected when string omniscient is changed from p1 or p2 for the following:

void (async () => {
  for await (const chunk of streams.omniscient) {
    console.log(chunk);
  }
})();

Example:

p1:

import {Dex, BattleStreams, RandomPlayerAI, Teams} from '@pkmn/sim';
import {TeamGenerators} from '@pkmn/randoms';

Teams.setGeneratorFactory(TeamGenerators);

const streams = BattleStreams.getPlayerStreams(new BattleStreams.BattleStream());
const spec = {formatid: 'gen7customgame'};

const p1spec = {name: 'Bot 1', team: Teams.pack(Teams.generate('gen7randombattle'))};
const p2spec = {name: 'Bot 2', team: Teams.pack(Teams.generate('gen7randombattle'))};

const p1 = new RandomPlayerAI(streams.p1);
const p2 = new RandomPlayerAI(streams.p2);

void p1.start();
void p2.start();

void (async () => {
  for await (const chunk of streams.p1) {
    console.log(chunk);
  }
})();

void streams.omniscient.write(`>start ${JSON.stringify(spec)}
>player p1 ${JSON.stringify(p1spec)}
>player p2 ${JSON.stringify(p2spec)}`);

p2:

import {Dex, BattleStreams, RandomPlayerAI, Teams} from '@pkmn/sim';
import {TeamGenerators} from '@pkmn/randoms';

Teams.setGeneratorFactory(TeamGenerators);

const streams = BattleStreams.getPlayerStreams(new BattleStreams.BattleStream());
const spec = {formatid: 'gen7customgame'};

const p1spec = {name: 'Bot 1', team: Teams.pack(Teams.generate('gen7randombattle'))};
const p2spec = {name: 'Bot 2', team: Teams.pack(Teams.generate('gen7randombattle'))};

const p1 = new RandomPlayerAI(streams.p1);
const p2 = new RandomPlayerAI(streams.p2);

void p1.start();
void p2.start();

void (async () => {
  for await (const chunk of streams.p2) {
    console.log(chunk);
  }
})();

void streams.omniscient.write(`>start ${JSON.stringify(spec)}
>player p1 ${JSON.stringify(p1spec)}
>player p2 ${JSON.stringify(p2spec)}`);

With p1 it returns nothing, with p2 it returns the battle stream of p2, but does not include any |request| messages

Expected behavior:

Accurate and complete battle stream for both sides with |request| messages.

Additional context:

Most likely an issue originated from showdown, see smogon/pokemon-showdown#10542

@cyi1341 cyi1341 added the bug Something isn't working label Sep 11, 2024
@scheibo
Copy link
Contributor

scheibo commented Sep 11, 2024

You have a race because after your changes two places are trying to consume the same player stream at once (the respective RandomPlayerAI and your loop printing the stream). Print from within your player or create a wrapping stream that can tee it, eg.

class TeedStream extends sim.Streams.ObjectReadWriteStream {

@scheibo scheibo closed this as completed Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants