Files
esports/scripts/seed-dev-superset.ts

88 lines
3.7 KiB
TypeScript

/** A small, labeled dev-only showcase for features absent from the live archive. */
import { SQL } from 'bun';
import { PRESETS } from '../src/lib/broadcast/layout';
const databaseUrl = process.env.DB_URL || process.env.DATABASE_URL;
if (!databaseUrl) throw new Error('DB_URL or DATABASE_URL is required');
const sql = new SQL(databaseUrl, { max: 1 });
const ids = {
tournament: '00000000-0000-4000-8000-00000000d101',
round: '00000000-0000-4000-8000-00000000d102',
registration: [
'00000000-0000-4000-8000-00000000d111',
'00000000-0000-4000-8000-00000000d112',
'00000000-0000-4000-8000-00000000d113',
'00000000-0000-4000-8000-00000000d114'
],
match: ['00000000-0000-4000-8000-00000000d121', '00000000-0000-4000-8000-00000000d122'],
prediction: ['00000000-0000-4000-8000-00000000d131', '00000000-0000-4000-8000-00000000d132'],
channel: '00000000-0000-4000-8000-00000000d141'
} as const;
try {
const [{ name }] = await sql`SELECT current_database() AS name`;
if (name !== 'vash_dev' && name !== process.env.SEED_TEST_DATABASE)
throw new Error(`Refusing to seed ${name}; only a dev database is allowed`);
await sql.begin(async (tx) => {
const teams = await tx`
SELECT t.id AS team_id, t.owner_id AS user_id
FROM team t JOIN "user" u ON u.id = t.owner_id
WHERE t.is_personal AND t.owner_id IS NOT NULL
ORDER BY t.owner_id, t.id LIMIT 5
`;
if (teams.length < 5 || new Set(teams.map((team) => team.user_id)).size < 5)
throw new Error('The dev showcase needs five distinct users with personal teams');
const [pool] = await tx`SELECT id FROM mappool ORDER BY created_at, id LIMIT 1`;
if (!pool) throw new Error('The dev showcase needs one copied mappool');
await tx`
INSERT INTO tournament (id, name, description, format, state, config, max_slots, created_by, tagline, accent_color)
VALUES (
${ids.tournament}, 'Dev Showcase',
'A dev-only bracket built from copied players and a copied mappool.',
'single_elim', 'IN_PROGRESS',
${{ teamSize: 1, scoringType: 'score_v2', bans: 1, bestOf: 7 }},
4, ${teams[0].user_id}, 'Explore the bracket and predictions', '#ff66aa'
) ON CONFLICT (id) DO NOTHING
`;
await tx`
INSERT INTO tournament_round (id, tournament_id, name, abbreviation, round_order, best_of, mappool_id, bracket_type)
VALUES (${ids.round}, ${ids.tournament}, 'Semifinals', 'SF', 1, 7, ${pool.id}, 'winners')
ON CONFLICT (id) DO NOTHING
`;
for (let i = 0; i < 4; i++) {
await tx`
INSERT INTO tournament_registration (id, tournament_id, team_id, user_id, status, seed)
VALUES (${ids.registration[i]}, ${ids.tournament}, ${teams[i].team_id}, ${teams[i].user_id}, 'registered', ${i + 1})
ON CONFLICT (id) DO NOTHING
`;
}
for (let i = 0; i < 2; i++) {
await tx`
INSERT INTO tournament_match (id, tournament_id, round_id, bracket_position, team1_id, team2_id, state)
VALUES (${ids.match[i]}, ${ids.tournament}, ${ids.round}, ${i + 1},
${teams[i * 2].team_id}, ${teams[i * 2 + 1].team_id}, 'PENDING')
ON CONFLICT (id) DO NOTHING
`;
await tx`
INSERT INTO prediction (id, user_id, tournament_match_id, winner_team_id, winner_score, loser_score)
VALUES (${ids.prediction[i]}, ${teams[4].user_id}, ${ids.match[i]}, ${teams[i * 2].team_id}, 4, 2)
ON CONFLICT (id) DO NOTHING
`;
}
await tx`
INSERT INTO broadcast_channel (id, name, mode, tournament_id, layout, created_by, live, record)
VALUES (${ids.channel}, 'Dev Showcase TV', 'tournament', ${ids.tournament},
${PRESETS.tourney.make()}, ${teams[0].user_id}, false, true)
ON CONFLICT (id) DO NOTHING
`;
});
console.log(
'Seeded dev showcase: 1 tournament, 1 round, 4 registrations, 2 bracket matches, 2 predictions, 1 broadcast channel'
);
} finally {
await sql.end();
}