From 891b046a6af7fbae3a22269ee6fcc2a46eaaa98e Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Tue, 13 May 2025 11:27:29 -0700 Subject: [PATCH] Sim: Emit requests after updates Fixes #8546 --- sim/battle.ts | 8 +++++++- sim/side.ts | 4 ++-- test/server/room-battle.js | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/sim/battle.ts b/sim/battle.ts index fcb272dab8..6842b42a90 100644 --- a/sim/battle.ts +++ b/sim/battle.ts @@ -145,6 +145,7 @@ export class Battle { readonly messageLog: string[]; sentLogPos: number; sentEnd: boolean; + sentRequests = true; requestState: RequestState; turn: number; @@ -1329,8 +1330,9 @@ export class Battle { const requests = this.getRequests(type); for (let i = 0; i < this.sides.length; i++) { - this.sides[i].emitRequest(requests[i]); + this.sides[i].activeRequest = requests[i]; } + this.sentRequests = false; if (this.sides.every(side => side.isChoiceDone())) { throw new Error(`Choices are done immediately after a request`); @@ -3211,6 +3213,10 @@ export class Battle { sendUpdates() { if (this.sentLogPos >= this.log.length) return; this.send('update', this.log.slice(this.sentLogPos)); + if (!this.sentRequests) { + for (const side of this.sides) side.emitRequest(); + this.sentRequests = true; + } this.sentLogPos = this.log.length; if (!this.sentEnd && this.ended) { diff --git a/sim/side.ts b/sim/side.ts index 59b4df7940..d2bd2636e8 100644 --- a/sim/side.ts +++ b/sim/side.ts @@ -483,7 +483,7 @@ export class Side { this.battle.send('sideupdate', `${this.id}\n${sideUpdate}`); } - emitRequest(update: ChoiceRequest) { + emitRequest(update: ChoiceRequest = this.activeRequest!) { this.battle.send('sideupdate', `${this.id}\n|request|${JSON.stringify(update)}`); this.activeRequest = update; } @@ -495,7 +495,7 @@ export class Side { const updated = update ? this.updateRequestForPokemon(update.pokemon, update.update) : null; const type = `[${updated ? 'Unavailable' : 'Invalid'} choice]`; this.battle.send('sideupdate', `${this.id}\n|error|${type} ${message}`); - if (updated) this.emitRequest(this.activeRequest!); + if (updated) this.emitRequest(); if (this.battle.strictChoices) throw new Error(`${type} ${message}`); return false; } diff --git a/test/server/room-battle.js b/test/server/room-battle.js index 773c2dd7e3..f8b51ace04 100644 --- a/test/server/room-battle.js +++ b/test/server/room-battle.js @@ -53,16 +53,16 @@ describe('Simulator abstraction layer features', () => { '>player p1 {"name":"p1","avatar":"ethan","team":"","rating":1507,"seed":[59512,58581,51338,7861]}\n' + '>player p2 {"name":"p2","avatar":"dawn","team":"","rating":1447,"seed":[33758,53485,62378,29757]}\n' ); + assert((await stream.read()).includes('|switch|')); assert((await stream.read()).startsWith('sideupdate\np1\n|request|')); assert((await stream.read()).startsWith('sideupdate\np2\n|request|')); - assert((await stream.read()).includes('|switch|')); stream.write( '>p1 move 1\n' + '>p2 move 1\n' ); + assert((await stream.read()).includes('|move|')); assert((await stream.read()).startsWith('sideupdate\np1\n|request|')); assert((await stream.read()).startsWith('sideupdate\np2\n|request|')); - assert((await stream.read()).includes('|move|')); stream.destroy(); assert.equal(PM.processes[0].getLoad(), 0);