Skip to content

Commit

Permalink
Clear guess timeout for ClipAction.END_ROUND (#2235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brainicism authored Oct 7, 2024
1 parent 81778ba commit 595b2fb
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/structures/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,8 @@ export default abstract class Session {
round.songStartedAt = Date.now();
}

await this.ensureConnectionReady();

this.connection.play(stream, {
inputArgs,
encoderArgs: Object.entries(encoderArgs).flatMap((x) => [
Expand Down Expand Up @@ -901,13 +903,13 @@ export default abstract class Session {
}
}

this.stopGuessTimeout();

if (clipAction === ClipAction.END_ROUND) {
// The end round clip doesn't deal with round state, it just plays and ends
return;
}

this.stopGuessTimeout();

if (this.isGameSession() && this.isClipMode()) {
const clipGameRound = round as ClipGameRound;
if (!round.finished) {
Expand Down Expand Up @@ -1319,4 +1321,28 @@ export default abstract class Session {
);
});
}

private async ensureConnectionReady(): Promise<void> {
if (!this.connection) {
throw new Error(
"Connection is unexpectedly null in ensureConnectionReady",
);
}

if (!this.connection.piper.encoding) {
return;
}

logger.warn("Connection is unexpectedly in encoding state.");
await delay(1000);

// if still encoding, force stop
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (this.connection.piper.encoding) {
logger.warn(
"Connection is still in encoding state after timeout, force stop.",
);
this.connection.stopPlaying();
}
}
}

0 comments on commit 595b2fb

Please sign in to comment.