import { GameDto, GameRow, GameStatus } from '../models/game.model'; import { isRegisteredImplementationKey, listRegisteredModules } from '../games/registry'; import * as gameRepository from '../repositories/gameRepository'; function toGameDto(row: GameRow): GameDto { return { id: row.id, slug: row.slug, title: row.title, description: row.description, category: row.category, thumbnailUrl: row.thumbnail_url, accentColor: row.accent_color ?? '#D6B25E', status: row.status, sortOrder: row.sort_order, isFeatured: row.is_featured, implementationKey: row.implementation_key, createdAt: row.created_at.toISOString(), updatedAt: row.updated_at.toISOString(), }; } export async function listGames(options?: { featured?: boolean }): Promise { const rows = await gameRepository.listActiveGames(options?.featured === true); return rows.map(toGameDto); } export async function listAllGamesAdmin(): Promise { const rows = await gameRepository.listAllGames(); return rows.map(toGameDto); } export async function getGame(slug: string): Promise { const row = await gameRepository.getGameBySlug(slug); return row ? toGameDto(row) : null; } const VALID_STATUS: GameStatus[] = ['active', 'coming_soon', 'maintenance']; export type GameImplementationOption = { key: string; defaultSlug: string; label: string; }; export function listRegisteredImplementations(): GameImplementationOption[] { return listRegisteredModules().map((m) => ({ key: m.implementationKey, defaultSlug: m.slug, label: m.displayName, })); } function assertValidImplementationKey(key: string | null | undefined): void { if (key == null || key === '') return; if (!isRegisteredImplementationKey(key)) { throw new Error(`Unknown implementation key: ${key}`); } } export function parseGameInput(body: Record, partial = false) { const slug = typeof body.slug === 'string' ? body.slug.trim() : ''; const title = typeof body.title === 'string' ? body.title.trim() : ''; if (!partial && (!slug || !title)) { throw new Error('slug and title are required'); } if (partial && !slug && !title && body.slug === undefined && body.title === undefined) { // ok for partial updates with other fields } const status = typeof body.status === 'string' && VALID_STATUS.includes(body.status as GameStatus) ? (body.status as GameStatus) : undefined; return { slug: slug || undefined, title: title || undefined, description: typeof body.description === 'string' ? body.description : body.description === null ? null : undefined, category: typeof body.category === 'string' ? body.category.trim() : undefined, thumbnailUrl: typeof body.thumbnailUrl === 'string' ? body.thumbnailUrl : body.thumbnailUrl === null ? null : undefined, accentColor: typeof body.accentColor === 'string' ? body.accentColor : undefined, status, sortOrder: typeof body.sortOrder === 'number' ? body.sortOrder : undefined, isFeatured: typeof body.isFeatured === 'boolean' ? body.isFeatured : undefined, implementationKey: typeof body.implementationKey === 'string' ? body.implementationKey.trim() || null : body.implementationKey === null ? null : undefined, }; } export async function createGame(body: Record): Promise { const input = parseGameInput(body); if (!input.slug || !input.title) throw new Error('slug and title are required'); assertValidImplementationKey(input.implementationKey); const existing = await gameRepository.getGameBySlug(input.slug); if (existing) throw new Error('A game with this slug already exists'); const row = await gameRepository.createGame({ slug: input.slug, title: input.title, description: input.description, category: input.category, thumbnailUrl: input.thumbnailUrl, accentColor: input.accentColor, status: input.status, sortOrder: input.sortOrder, isFeatured: input.isFeatured, implementationKey: input.implementationKey, }); return toGameDto(row); } export async function updateGame(id: string, body: Record): Promise { const input = parseGameInput(body, true); assertValidImplementationKey(input.implementationKey); if (input.slug) { const clash = await gameRepository.getGameBySlug(input.slug); if (clash && clash.id !== id) throw new Error('A game with this slug already exists'); } const row = await gameRepository.updateGame(id, input); if (!row) throw new Error('Game not found'); return toGameDto(row); } export async function deleteGame(id: string): Promise { const ok = await gameRepository.deleteGame(id); if (!ok) throw new Error('Game not found'); } export async function listImplementationsAdmin(): Promise { return listRegisteredImplementations(); }