2026-03-08 01:24:26 +00:00
|
|
|
import { db, schema } from '../db/index.js'
|
2026-03-08 10:33:30 +00:00
|
|
|
import { eq, sql } from 'drizzle-orm'
|
2026-03-08 01:24:26 +00:00
|
|
|
import { runFightAsync, isInFight } from './orchestrator.js'
|
|
|
|
|
import { checkPaymentStatus, refundEntry } from './payments.js'
|
|
|
|
|
|
|
|
|
|
interface RankedQueueEntry {
|
|
|
|
|
botId: string
|
|
|
|
|
botName: string
|
|
|
|
|
webhookUrl: string
|
|
|
|
|
eloRating: number
|
|
|
|
|
joinedAt: number
|
|
|
|
|
paymentId: string
|
|
|
|
|
resolve: (fightId: string) => void
|
|
|
|
|
reject: (error: Error) => void
|
|
|
|
|
timeoutHandle: ReturnType<typeof setTimeout>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rankedQueue: RankedQueueEntry[] = []
|
|
|
|
|
const RANKED_TIMEOUT_MS = 60_000
|
|
|
|
|
|
|
|
|
|
// Post-fight cooldown tracking (shared with free queue)
|
|
|
|
|
const rankedCooldowns = new Map<string, number>()
|
|
|
|
|
const COOLDOWN_MS = 15_000
|
|
|
|
|
|
|
|
|
|
export function setRankedCooldown(botId: string) {
|
|
|
|
|
rankedCooldowns.set(botId, Date.now() + COOLDOWN_MS)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getRankedQueueStatus(): { waiting: number } {
|
|
|
|
|
return { waiting: rankedQueue.length }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Join the ranked queue. Requires a confirmed payment.
|
|
|
|
|
* Returns fightId when matched, or rejects with refund on timeout.
|
|
|
|
|
* NEVER matches against mock bots.
|
|
|
|
|
*/
|
|
|
|
|
export async function joinRankedQueue(botId: string, paymentId: string): Promise<string> {
|
|
|
|
|
// Verify payment is confirmed
|
|
|
|
|
const status = await checkPaymentStatus(paymentId)
|
|
|
|
|
if (status !== 'confirmed') {
|
|
|
|
|
throw new Error(`Payment not confirmed (status: ${status}). Cannot join ranked queue.`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check cooldown
|
|
|
|
|
const cooldownUntil = rankedCooldowns.get(botId)
|
|
|
|
|
if (cooldownUntil && Date.now() < cooldownUntil) {
|
|
|
|
|
const waitSec = Math.ceil((cooldownUntil - Date.now()) / 1000)
|
|
|
|
|
throw new Error(`Cooldown active. Wait ${waitSec}s.`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if already in a fight
|
|
|
|
|
if (isInFight(botId)) {
|
|
|
|
|
throw new Error('Bot is already in a fight.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load bot
|
|
|
|
|
const botRows = await db.select().from(schema.bots).where(eq(schema.bots.id, botId)).limit(1)
|
|
|
|
|
if (botRows.length === 0) throw new Error('Bot not found')
|
|
|
|
|
const bot = botRows[0]
|
|
|
|
|
|
|
|
|
|
if (!bot.isActive) {
|
|
|
|
|
throw new Error('Bot is deactivated due to webhook errors. Re-test your webhook to reactivate.')
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 10:33:30 +00:00
|
|
|
// NEVER allow mock/classic bots in ranked
|
|
|
|
|
if (bot.webhookUrl.startsWith('http://mock.local') || bot.webhookUrl.startsWith('http://classic.local')) {
|
|
|
|
|
throw new Error('Practice bots cannot join ranked fights.')
|
2026-03-08 01:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`[ranked-queue] joinRankedQueue botId=${botId} name=${bot.name} paymentId=${paymentId}`)
|
|
|
|
|
|
|
|
|
|
// Don't allow same bot twice
|
|
|
|
|
const existing = rankedQueue.findIndex(e => e.botId === botId)
|
|
|
|
|
if (existing !== -1) {
|
|
|
|
|
const old = rankedQueue.splice(existing, 1)[0]
|
|
|
|
|
clearTimeout(old.timeoutHandle)
|
|
|
|
|
old.reject(new Error('Rejoined ranked queue'))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if someone is already waiting — instant match by closest ELO
|
|
|
|
|
if (rankedQueue.length > 0) {
|
|
|
|
|
rankedQueue.sort((a, b) => {
|
|
|
|
|
const diffA = Math.abs(a.eloRating - bot.eloRating)
|
|
|
|
|
const diffB = Math.abs(b.eloRating - bot.eloRating)
|
|
|
|
|
return diffA - diffB
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const opponent = rankedQueue.shift()!
|
|
|
|
|
clearTimeout(opponent.timeoutHandle)
|
|
|
|
|
|
|
|
|
|
// Start ranked fight
|
|
|
|
|
const fightId = await runFightAsync(opponent.botId, botId, 'ranked')
|
|
|
|
|
opponent.resolve(fightId)
|
|
|
|
|
return fightId
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 10:33:30 +00:00
|
|
|
// Dev mode: auto-match against a random mock bot
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
|
const mockBots = await db.select().from(schema.bots)
|
|
|
|
|
.where(sql`${schema.bots.webhookUrl} LIKE 'http://mock.local%'`)
|
|
|
|
|
if (mockBots.length > 0) {
|
|
|
|
|
const mock = mockBots[Math.floor(Math.random() * mockBots.length)]
|
|
|
|
|
console.log(`[ranked-queue] dev: auto-matching ${bot.name} vs mock bot ${mock.name}`)
|
|
|
|
|
const fightId = await runFightAsync(botId, mock.id, 'ranked')
|
|
|
|
|
return fightId
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-08 01:24:26 +00:00
|
|
|
// Nobody waiting — join queue and wait up to 60s
|
|
|
|
|
return new Promise<string>((resolve, reject) => {
|
|
|
|
|
const timeoutHandle = setTimeout(async () => {
|
|
|
|
|
const idx = rankedQueue.findIndex(e => e.botId === botId)
|
|
|
|
|
if (idx !== -1) {
|
|
|
|
|
rankedQueue.splice(idx, 1)
|
|
|
|
|
// Refund the entry fee
|
|
|
|
|
try {
|
|
|
|
|
await refundEntry(paymentId)
|
|
|
|
|
reject(new Error('No ranked opponent found — entry fee refunded.'))
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reject(new Error('No ranked opponent found. Refund attempted but may have failed.'))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, RANKED_TIMEOUT_MS)
|
|
|
|
|
|
|
|
|
|
rankedQueue.push({
|
|
|
|
|
botId,
|
|
|
|
|
botName: bot.name,
|
|
|
|
|
webhookUrl: bot.webhookUrl,
|
|
|
|
|
eloRating: bot.eloRating,
|
|
|
|
|
joinedAt: Date.now(),
|
|
|
|
|
paymentId,
|
|
|
|
|
resolve,
|
|
|
|
|
reject,
|
|
|
|
|
timeoutHandle,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Leave the ranked queue. Triggers refund of entry payment.
|
|
|
|
|
*/
|
|
|
|
|
export async function leaveRankedQueue(botId: string): Promise<boolean> {
|
|
|
|
|
const idx = rankedQueue.findIndex(e => e.botId === botId)
|
|
|
|
|
if (idx === -1) return false
|
|
|
|
|
const entry = rankedQueue.splice(idx, 1)[0]
|
|
|
|
|
clearTimeout(entry.timeoutHandle)
|
|
|
|
|
|
|
|
|
|
// Refund entry fee
|
|
|
|
|
try {
|
|
|
|
|
await refundEntry(entry.paymentId)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(`[ranked-queue] refund failed for ${entry.paymentId}:`, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry.reject(new Error('Left ranked queue'))
|
|
|
|
|
return true
|
|
|
|
|
}
|