fix: interpret Choice.timeLimit as seconds, convert to ms internally

Previously maxLimit was passed directly to setTimeout/setInterval (ms),
so a JSON value of 10 meant 10ms instead of the intended 10 seconds.
Now timeLimit in JSON represents seconds, engine multiplies by 1000.
This commit is contained in:
2026-06-07 18:56:30 +08:00
parent 25ea9ce9fd
commit c7d035bd8f

View File

@@ -23,11 +23,10 @@ export class ChoiceSystem {
const timed = choices.filter((c) => c.timeLimit && c.timeLimit > 0) const timed = choices.filter((c) => c.timeLimit && c.timeLimit > 0)
if (timed.length === 0) return if (timed.length === 0) return
const maxLimit = Math.max(...timed.map((c) => c.timeLimit!)) const maxLimitSec = Math.max(...timed.map((c) => c.timeLimit!))
const maxLimitMs = maxLimitSec * 1000
const maxLimitSec = maxLimit / 1000 this.timeLimit = maxLimitMs
this.timeLimit = maxLimit
this.elapsed = 0 this.elapsed = 0
this.onUpdate = onUpdate this.onUpdate = onUpdate
this.onTimeout = onTimeout this.onTimeout = onTimeout
@@ -36,21 +35,19 @@ export class ChoiceSystem {
this.timerId = setInterval(() => { this.timerId = setInterval(() => {
this.elapsed += this.tickMs this.elapsed += this.tickMs
const remaining = Math.max(0, this.timeLimit - this.elapsed) / 1000 const remainingSec = Math.max(0, maxLimitMs - this.elapsed) / 1000
const nextState: ChoiceTimerState = { this.onUpdate?.({
total: this.timeLimit / 1000, total: maxLimitSec,
remaining: Math.ceil(remaining * 10) / 10, remaining: Math.ceil(remainingSec * 10) / 10,
} })
this.onUpdate?.(nextState)
}, this.tickMs) }, this.tickMs)
this.timeoutId = setTimeout(() => { this.timeoutId = setTimeout(() => {
this.clear() this.clear()
// Pick the first choice as default on timeout
if (choices.length > 0) { if (choices.length > 0) {
this.onTimeout?.(choices[0]) this.onTimeout?.(choices[0])
} }
}, this.timeLimit) }, maxLimitMs)
} }
stop() { stop() {