diff --git a/server/src/db/schema.ts b/server/src/db/schema.ts index 9bb9c8f..2f61691 100644 --- a/server/src/db/schema.ts +++ b/server/src/db/schema.ts @@ -109,3 +109,39 @@ export const bets = sqliteTable('bets', { createdAt: text('created_at').notNull(), settledAt: text('settled_at'), }) + +export const tournaments = sqliteTable('tournaments', { + id: text('id').primaryKey(), + name: text('name').notNull(), + format: text('format', { enum: ['single_elim', 'round_robin'] }).notNull(), + size: integer('size').notNull(), + entrySats: integer('entry_sats').notNull().default(0), + prizeSats: integer('prize_sats').notNull().default(0), + status: text('status', { enum: ['open', 'active', 'finished'] }).notNull().default('open'), + currentRound: integer('current_round').notNull().default(0), + seasonId: text('season_id'), + createdAt: text('created_at').notNull(), + startedAt: text('started_at'), + finishedAt: text('finished_at'), +}) + +export const tournamentEntries = sqliteTable('tournament_entries', { + id: text('id').primaryKey(), + tournamentId: text('tournament_id').notNull().references(() => tournaments.id), + botId: text('bot_id').notNull().references(() => bots.id), + seed: integer('seed').notNull().default(0), + eliminated: integer('eliminated', { mode: 'boolean' }).notNull().default(false), + createdAt: text('created_at').notNull(), +}) + +export const tournamentMatches = sqliteTable('tournament_matches', { + id: text('id').primaryKey(), + tournamentId: text('tournament_id').notNull().references(() => tournaments.id), + round: integer('round').notNull(), + matchIndex: integer('match_index').notNull(), + botAId: text('bot_a_id').references(() => bots.id), + botBId: text('bot_b_id').references(() => bots.id), + fightId: text('fight_id').references(() => fights.id), + winnerId: text('winner_id').references(() => bots.id), + status: text('status', { enum: ['pending', 'live', 'finished'] }).notNull().default('pending'), +}) diff --git a/server/src/db/startup.ts b/server/src/db/startup.ts index 00f09a3..b012cd0 100644 --- a/server/src/db/startup.ts +++ b/server/src/db/startup.ts @@ -1,4 +1,5 @@ import { sqlite } from './index.js' +import { logger } from '../lib/logger.js' export function runMigrations() { sqlite.exec(` @@ -86,6 +87,45 @@ export function runMigrations() { ); `) + + sqlite.exec(` + CREATE TABLE IF NOT EXISTS tournaments ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + format TEXT NOT NULL DEFAULT 'single_elim', + size INTEGER NOT NULL, + entry_sats INTEGER NOT NULL DEFAULT 0, + prize_sats INTEGER NOT NULL DEFAULT 0, + status TEXT NOT NULL DEFAULT 'open', + current_round INTEGER NOT NULL DEFAULT 0, + season_id TEXT, + created_at TEXT NOT NULL, + started_at TEXT, + finished_at TEXT + ); + + CREATE TABLE IF NOT EXISTS tournament_entries ( + id TEXT PRIMARY KEY, + tournament_id TEXT NOT NULL REFERENCES tournaments(id), + bot_id TEXT NOT NULL REFERENCES bots(id), + seed INTEGER NOT NULL DEFAULT 0, + eliminated INTEGER NOT NULL DEFAULT 0, + created_at TEXT NOT NULL + ); + + CREATE TABLE IF NOT EXISTS tournament_matches ( + id TEXT PRIMARY KEY, + tournament_id TEXT NOT NULL REFERENCES tournaments(id), + round INTEGER NOT NULL, + match_index INTEGER NOT NULL, + bot_a_id TEXT REFERENCES bots(id), + bot_b_id TEXT REFERENCES bots(id), + fight_id TEXT REFERENCES fights(id), + winner_id TEXT REFERENCES bots(id), + status TEXT NOT NULL DEFAULT 'pending' + ); + `) + // Column migrations for existing databases const migrations = [ "ALTER TABLE bots ADD COLUMN archetype TEXT NOT NULL DEFAULT 'standard'", @@ -120,5 +160,5 @@ export function runMigrations() { sqlite.exec("UPDATE bots SET bot_type = 'classic' WHERE webhook_url LIKE 'http://classic.local%' AND bot_type = 'regular'") } catch { /* ok */ } - console.log('[botfights] database migrated') + logger.info('db', 'database migrated') }