feat: botfights v1 — full fighting game with Kaplay engine

- Vue 3 + Vite + Tailwind 4 frontend with synthwave aesthetic
- Hono backend on port 9100 with SQLite/Drizzle
- Procedural pixel-art sprite generator (48x48, 8 animation states)
- Kaplay fight scene with punch/kick/special/knockback/KO animations
- 12 mock bots across 6 tiers with Elo rating system
- 9 challenge types, 10 fight arenas with modifiers
- Fight replay with staggered battle log and ~1 min timing
- Sprite preview page at /sprites

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Dorian
2026-03-06 16:27:54 +00:00
co-authored by Claude Opus 4.6
commit 335c148866
44 changed files with 7782 additions and 0 deletions
+135
View File
@@ -0,0 +1,135 @@
import { Hono } from 'hono'
import { nanoid } from 'nanoid'
import { createHash, randomBytes } from 'crypto'
import { db, schema } from '../db/index.js'
import { eq } from 'drizzle-orm'
export const botsRouter = new Hono()
function hashSecret(secret: string): string {
return createHash('sha256').update(secret).digest('hex')
}
// Register a new bot
botsRouter.post('/', async (c) => {
const body = await c.req.json()
const { name, webhook_url, avatar_seed } = body
if (!name || typeof name !== 'string' || name.length < 2 || name.length > 32) {
return c.json({ error: 'Name must be 2-32 characters.' }, 400)
}
if (!/^[a-zA-Z0-9_-]+$/.test(name)) {
return c.json({ error: 'Name must be alphanumeric, hyphens, or underscores.' }, 400)
}
if (!webhook_url || typeof webhook_url !== 'string') {
return c.json({ error: 'webhook_url is required.' }, 400)
}
try {
new URL(webhook_url)
} catch {
return c.json({ error: 'webhook_url must be a valid URL.' }, 400)
}
// Check for duplicate name
const existing = await db.select({ id: schema.bots.id })
.from(schema.bots)
.where(eq(schema.bots.name, name))
.limit(1)
if (existing.length > 0) {
return c.json({ error: 'A bot with that name already exists.' }, 409)
}
const id = nanoid(12)
const secret = randomBytes(32).toString('hex')
await db.insert(schema.bots).values({
id,
name,
webhookUrl: webhook_url,
avatarSeed: avatar_seed || name,
secretHash: hashSecret(secret),
createdAt: new Date().toISOString(),
})
return c.json({
id,
name,
secret,
message: 'Bot registered. Save your secret -- it will not be shown again.',
}, 201)
})
// List bots (public info only)
botsRouter.get('/', async (c) => {
const rows = await db.select({
id: schema.bots.id,
name: schema.bots.name,
avatarSeed: schema.bots.avatarSeed,
eloRating: schema.bots.eloRating,
wins: schema.bots.wins,
losses: schema.bots.losses,
winStreak: schema.bots.winStreak,
bestStreak: schema.bots.bestStreak,
tier: schema.bots.tier,
isActive: schema.bots.isActive,
createdAt: schema.bots.createdAt,
}).from(schema.bots).orderBy(schema.bots.eloRating)
return c.json(rows)
})
// Get single bot profile
botsRouter.get('/:name', async (c) => {
const name = c.req.param('name')
const rows = await db.select({
id: schema.bots.id,
name: schema.bots.name,
avatarSeed: schema.bots.avatarSeed,
eloRating: schema.bots.eloRating,
wins: schema.bots.wins,
losses: schema.bots.losses,
winStreak: schema.bots.winStreak,
bestStreak: schema.bots.bestStreak,
tier: schema.bots.tier,
isActive: schema.bots.isActive,
createdAt: schema.bots.createdAt,
}).from(schema.bots).where(eq(schema.bots.name, name)).limit(1)
if (rows.length === 0) {
return c.json({ error: 'Bot not found.' }, 404)
}
return c.json(rows[0])
})
// Health check a bot's webhook
botsRouter.post('/:name/health', async (c) => {
const name = c.req.param('name')
const rows = await db.select({
webhookUrl: schema.bots.webhookUrl,
}).from(schema.bots).where(eq(schema.bots.name, name)).limit(1)
if (rows.length === 0) {
return c.json({ error: 'Bot not found.' }, 404)
}
try {
const healthUrl = new URL('/health', rows[0].webhookUrl).toString()
const controller = new AbortController()
const timeout = setTimeout(() => controller.abort(), 5000)
const res = await fetch(healthUrl, { signal: controller.signal })
clearTimeout(timeout)
return c.json({
reachable: res.ok,
status: res.status,
})
} catch {
return c.json({ reachable: false, status: 0 })
}
})
+113
View File
@@ -0,0 +1,113 @@
import { Hono } from 'hono'
import { db, schema } from '../db/index.js'
import { eq, desc } from 'drizzle-orm'
import { ARENAS } from '../engine/arenas.js'
import { runMockFight } from '../engine/mock.js'
export const fightsRouter = new Hono()
// List recent fights (with bot names)
fightsRouter.get('/', async (c) => {
const rows = await db.select()
.from(schema.fights)
.orderBy(desc(schema.fights.createdAt))
.limit(20)
// Resolve bot names
const botIds = new Set<string>()
for (const f of rows) {
botIds.add(f.botAId)
botIds.add(f.botBId)
if (f.winnerId) botIds.add(f.winnerId)
}
const botMap = new Map<string, { name: string; avatarSeed: string; eloRating: number; tier: number }>()
for (const id of botIds) {
const bot = await db.select({
name: schema.bots.name,
avatarSeed: schema.bots.avatarSeed,
eloRating: schema.bots.eloRating,
tier: schema.bots.tier,
}).from(schema.bots).where(eq(schema.bots.id, id)).limit(1)
if (bot[0]) botMap.set(id, bot[0])
}
const enriched = rows.map(f => {
const arena = ARENAS.find(a => a.id === f.arena)
return {
...f,
botA: botMap.get(f.botAId) || null,
botB: botMap.get(f.botBId) || null,
winner: f.winnerId ? botMap.get(f.winnerId) || null : null,
arenaInfo: arena ? { name: arena.name, description: arena.description } : null,
}
})
return c.json(enriched)
})
// Get a single fight with rounds and bot details
fightsRouter.get('/:id', async (c) => {
const id = c.req.param('id')
const fightRows = await db.select()
.from(schema.fights)
.where(eq(schema.fights.id, id))
.limit(1)
if (fightRows.length === 0) {
return c.json({ error: 'Fight not found.' }, 404)
}
const fight = fightRows[0]
const [botARows, botBRows] = await Promise.all([
db.select({
id: schema.bots.id,
name: schema.bots.name,
avatarSeed: schema.bots.avatarSeed,
eloRating: schema.bots.eloRating,
wins: schema.bots.wins,
losses: schema.bots.losses,
tier: schema.bots.tier,
}).from(schema.bots).where(eq(schema.bots.id, fight.botAId)).limit(1),
db.select({
id: schema.bots.id,
name: schema.bots.name,
avatarSeed: schema.bots.avatarSeed,
eloRating: schema.bots.eloRating,
wins: schema.bots.wins,
losses: schema.bots.losses,
tier: schema.bots.tier,
}).from(schema.bots).where(eq(schema.bots.id, fight.botBId)).limit(1),
])
const roundRows = await db.select()
.from(schema.rounds)
.where(eq(schema.rounds.fightId, id))
.orderBy(schema.rounds.roundNumber)
const arena = ARENAS.find(a => a.id === fight.arena)
return c.json({
...fight,
botA: botARows[0] || null,
botB: botBRows[0] || null,
arenaInfo: arena || null,
rounds: roundRows,
})
})
// Trigger a mock fight between two random bots (dev/testing)
fightsRouter.post('/mock', async (c) => {
const allBots = await db.select({ id: schema.bots.id }).from(schema.bots)
if (allBots.length < 2) {
return c.json({ error: 'Need at least 2 registered bots.' }, 400)
}
const shuffled = [...allBots].sort(() => Math.random() - 0.5)
const fightId = await runMockFight(shuffled[0].id, shuffled[1].id)
return c.json({ fightId, message: 'Mock fight completed.' })
})