another fix for human choices
This commit is contained in:
@@ -42,6 +42,7 @@ export function useFightPolling(fightId: Ref<string>) {
|
||||
async function loadFight(): Promise<string | null> {
|
||||
try {
|
||||
const res = await fetch(`/api/fights/${fightId.value}`)
|
||||
if (res.status === 429) return null // Rate limited, skip this poll
|
||||
if (res.ok) {
|
||||
const data = await res.json()
|
||||
liveRounds.value = data.rounds?.length || 0
|
||||
|
||||
@@ -97,11 +97,19 @@ export function useHumanChallenge(
|
||||
}
|
||||
}
|
||||
|
||||
let pollBackoff = 0
|
||||
|
||||
async function pollForChallenge() {
|
||||
if (!myBotId.value) return
|
||||
try {
|
||||
const res = await fetch(`/api/fights/${fightId.value}/challenge/${myBotId.value}`)
|
||||
if (res.status === 429) {
|
||||
// Back off on rate limit — skip next few polls
|
||||
pollBackoff = Math.min(pollBackoff + 2, 8)
|
||||
return
|
||||
}
|
||||
if (!res.ok) return
|
||||
pollBackoff = Math.max(0, pollBackoff - 1) // Recover gradually
|
||||
const data = await res.json()
|
||||
|
||||
if (data.pending) {
|
||||
@@ -130,8 +138,12 @@ export function useHumanChallenge(
|
||||
|
||||
function startHumanPolling() {
|
||||
if (!myBotId.value) return
|
||||
pollBackoff = 0
|
||||
void pollForChallenge()
|
||||
humanPollHandle = setInterval(() => { void pollForChallenge() }, 400)
|
||||
humanPollHandle = setInterval(() => {
|
||||
if (pollBackoff > 0) { pollBackoff--; return }
|
||||
void pollForChallenge()
|
||||
}, 800)
|
||||
}
|
||||
|
||||
function stopHumanPolling() {
|
||||
|
||||
@@ -197,19 +197,8 @@ function wireSSE() {
|
||||
})
|
||||
scrollLiveLog()
|
||||
},
|
||||
async onHumanChallenge(sseData) {
|
||||
if (myBot.value) {
|
||||
try {
|
||||
const res = await fetch(`/api/fights/${fightId.value}/challenge/${myBot.value.id}`)
|
||||
if (res.ok) {
|
||||
const fullData = await res.json()
|
||||
if (fullData.pending) {
|
||||
handleSSEChallenge(fullData)
|
||||
return
|
||||
}
|
||||
}
|
||||
} catch { /* fall through */ }
|
||||
}
|
||||
onHumanChallenge(sseData) {
|
||||
// SSE now includes choices directly — no secondary fetch needed
|
||||
handleSSEChallenge(sseData)
|
||||
},
|
||||
onRoundEnd(data) {
|
||||
@@ -539,6 +528,9 @@ async function fightAgain(botId: string) {
|
||||
liveHpA.value = 100
|
||||
liveHpB.value = 100
|
||||
liveCurrentRound.value = 0
|
||||
// Stop ALL old polling/connections before starting new fight
|
||||
stopHumanPolling()
|
||||
stopPolling()
|
||||
disconnectSSE()
|
||||
|
||||
try {
|
||||
|
||||
@@ -123,9 +123,15 @@ function handleKeyboard(e: KeyboardEvent) {
|
||||
}
|
||||
}
|
||||
|
||||
let pollBackoff = 0
|
||||
|
||||
function startPolling() {
|
||||
pollBackoff = 0
|
||||
pollForChallenge()
|
||||
pollHandle = setInterval(pollForChallenge, 600)
|
||||
pollHandle = setInterval(() => {
|
||||
if (pollBackoff > 0) { pollBackoff--; return }
|
||||
pollForChallenge()
|
||||
}, 800)
|
||||
}
|
||||
|
||||
function stopPolling() {
|
||||
@@ -172,7 +178,9 @@ async function pollForChallenge() {
|
||||
|
||||
try {
|
||||
const res = await fetch(`/api/fights/${fightId.value}/challenge/${myBotId.value}`)
|
||||
if (res.status === 429) { pollBackoff = Math.min(pollBackoff + 2, 8); return }
|
||||
if (!res.ok) return
|
||||
pollBackoff = Math.max(0, pollBackoff - 1)
|
||||
const data = await res.json()
|
||||
|
||||
// Reset error tracking on success
|
||||
|
||||
Reference in New Issue
Block a user