Files
botfights/server/src/engine/background.ts
T

102 lines
3.2 KiB
TypeScript
Raw Normal View History

import { db, schema } from '../db/index.js'
import { runMockFight } from './mock.js'
// Background fight loop keeps mock bots fighting so the site always has fresh content.
// Configurable via env:
// FIGHT_LOOP_ENABLED=true (default: true in production, false in dev)
// FIGHT_LOOP_INTERVAL_MS=45000 (default: 45s between fights)
// FIGHT_LOOP_QUIET_HOURS=2-8 (default: 2am-8am UTC, slower fights)
const isProduction = process.env.NODE_ENV === 'production'
const enabled = process.env.FIGHT_LOOP_ENABLED
? process.env.FIGHT_LOOP_ENABLED === 'true'
: isProduction
const intervalMs = Number(process.env.FIGHT_LOOP_INTERVAL_MS) || 45_000
const quietStart = Number(process.env.FIGHT_LOOP_QUIET_START) || 2
const quietEnd = Number(process.env.FIGHT_LOOP_QUIET_END) || 8
let running = false
export function startBackgroundFights() {
if (!enabled) {
console.log('[background] fight loop disabled (set FIGHT_LOOP_ENABLED=true to enable)')
return
}
if (running) return
running = true
console.log(`[background] fight loop started — interval ${intervalMs}ms, quiet hours ${quietStart}-${quietEnd} UTC`)
loop().catch(err => {
console.error('[background] fight loop crashed:', err)
running = false
})
}
export function stopBackgroundFights() {
running = false
}
async function loop() {
// Small delay on startup to let everything settle
await sleep(5_000)
while (running) {
try {
await runOneBackgroundFight()
} catch (err) {
console.error('[background] fight error:', err)
}
// During quiet hours, fights run 4x slower
const hour = new Date().getUTCHours()
const isQuiet = quietStart < quietEnd
? hour >= quietStart && hour < quietEnd
: hour >= quietStart || hour < quietEnd
const delay = isQuiet ? intervalMs * 4 : intervalMs
// Add jitter (±25%) so fights don't feel robotic
const jitter = delay * (0.75 + Math.random() * 0.5)
await sleep(jitter)
}
console.log('[background] fight loop stopped')
}
async function runOneBackgroundFight() {
// Pick two mock bots with ELO-weighted matchmaking
const allBots = await db.select({
id: schema.bots.id,
webhookUrl: schema.bots.webhookUrl,
eloRating: schema.bots.eloRating,
name: schema.bots.name,
}).from(schema.bots)
const mockBots = allBots.filter(b => b.webhookUrl.startsWith('http://mock.local'))
if (mockBots.length < 2) return
// Mixed matchmaking: 70% close ELO, 30% wild card
const isWild = Math.random() < 0.3
const botA = mockBots[Math.floor(Math.random() * mockBots.length)]
const others = mockBots.filter(b => b.id !== botA.id)
let botB: typeof botA
if (isWild) {
botB = others[Math.floor(Math.random() * others.length)]
} else {
others.sort((a, b) => {
const diffA = Math.abs(a.eloRating - botA.eloRating) + Math.random() * 150
const diffB = Math.abs(b.eloRating - botA.eloRating) + Math.random() * 150
return diffA - diffB
})
botB = others[0]
}
const fightId = await runMockFight(botA.id, botB.id)
console.log(`[background] ${botA.name} vs ${botB.name} => fight ${fightId}`)
}
function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}