Skip to content

Commit

Permalink
Merge pull request #2630 from Phoenix-Starlight/autostart-timer
Browse files Browse the repository at this point in the history
Set timer before auto-starting matches
  • Loading branch information
sylvainpolletvillard authored Jan 5, 2025
2 parents b3d6254 + 92d56cf commit adbc14f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
48 changes: 35 additions & 13 deletions app/rooms/commands/preparation-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { CloseCodes } from "../../types/enum/CloseCodes"
import { getRank } from "../../utils/elo"
import { SpecialGameRule } from "../../types/enum/SpecialGameRule"
import { UserRecord } from "firebase-admin/lib/auth/user-record"
import { setTimeout } from "node:timers/promises"
import { AbortError } from "node-fetch"

export class OnJoinCommand extends Command<
PreparationRoom,
Expand Down Expand Up @@ -625,28 +627,48 @@ export class OnToggleReadyCommand extends Command<
// auto start when ranked lobby is full and all ready
this.room.state.addMessage({
authorId: "server",
payload: `Lobby is full, starting match...`
payload: "Lobby is full, starting match in 3..."
})

if (
[GameMode.RANKED, GameMode.SCRIBBLE].includes(this.state.gameMode)
) {
// open another one
this.room.presence.publish("lobby-full", {
gameMode: this.state.gameMode,
minRank: this.state.minRank,
noElo: this.state.noElo
})
}

return [new OnGameStartRequestCommand()]
return new CheckAutoStartRoom()
}
} catch (error) {
logger.error(error)
}
}
}

export class CheckAutoStartRoom extends Command<PreparationRoom, void> {
async execute() {
try {
this.state.abortOnPlayerLeave = new AbortController()
const signal = this.state.abortOnPlayerLeave.signal
await setTimeout(3000, null, { signal })

this.room.state.addMessage({
authorId: "server",
payload: "Starting match..."
})

if ([GameMode.RANKED, GameMode.SCRIBBLE].includes(this.state.gameMode)) {
// open another one
this.room.presence.publish("lobby-full", {
gameMode: this.state.gameMode,
minRank: this.state.minRank,
noElo: this.state.noElo
})
}

return new OnGameStartRequestCommand()
} catch (e) {
this.room.state.addMessage({
authorId: "server",
payload: "Waiting for the room to fill up."
})
}
}
}

export class InitializeBotsCommand extends Command<
PreparationRoom,
{
Expand Down
1 change: 1 addition & 0 deletions app/rooms/preparation-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ export default class PreparationRoom extends Room<PreparationState> {
)*/
}
try {
this.state.abortOnPlayerLeave?.abort()
if (consented) {
throw new Error("consented leave")
}
Expand Down
1 change: 1 addition & 0 deletions app/rooms/states/preparation-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default class PreparationState
@type("boolean") noElo: boolean
@type(["string"]) whitelist: string[]
@type(["string"]) blacklist: string[]
abortOnPlayerLeave?: AbortController

constructor(params: {
ownerId?: string
Expand Down

0 comments on commit adbc14f

Please sign in to comment.