diff --git a/.env.example b/.env.example index 9fcf7eb..81e6f3f 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,6 @@ NEXTAUTH_SECRET="your-secret-goes-here" NEXTAUTH_URL="http://localhost:3000" DATABASE_URL=file:./dev.db INGEST_CHALLENGES_AT_STARTUP=true -CHALLENGES_DIR="./challenges" \ No newline at end of file +CHALLENGES_DIR="./challenges" +GAME_START_TIME="" +GAME_END_TIME="" diff --git a/README.md b/README.md index 855adc6..84bf10c 100644 --- a/README.md +++ b/README.md @@ -132,9 +132,12 @@ NEXTAUTH_SECRET="your-secret-here" NEXTAUTH_URL="http://localhost:3000" INGEST_CHALLENGES_AT_STARTUP=true CHALLENGES_DIR="./challenges" +GAME_START_TIME="" +GAME_END_TIME="" ``` Set `INGEST_CHALLENGES_AT_STARTUP` to `true` if you want challenges in `CHALLENGES_DIR` automatically imported when the server starts. +`GAME_START_TIME` and `GAME_END_TIME` accept ISO 8601 date strings. When set, these values replace the existing start and end times in the database during server startup. ## 📝 License diff --git a/src/app/api/game-config/route.ts b/src/app/api/game-config/route.ts index dfd9fa4..7b47bae 100644 --- a/src/app/api/game-config/route.ts +++ b/src/app/api/game-config/route.ts @@ -16,7 +16,7 @@ export async function GET() { isActive: false, startTime: null, endTime: null, - hasEndTime: true + hasEndTime: true, }); } @@ -25,7 +25,7 @@ export async function GET() { ...gameConfig, startTime: gameConfig.startTime.toISOString(), endTime: gameConfig.endTime?.toISOString() || null, - hasEndTime: gameConfig.endTime !== null + hasEndTime: gameConfig.endTime !== null, }); } catch (error) { console.error('Error fetching game config:', error); diff --git a/src/instrumentation.ts b/src/instrumentation.ts index 1f06541..937080e 100644 --- a/src/instrumentation.ts +++ b/src/instrumentation.ts @@ -1,7 +1,40 @@ +import { prisma } from '@/lib/prisma'; + export async function register() { if (process.env.NEXT_RUNTIME === 'nodejs') { console.log('Running startup tasks...'); + if (process.env.GAME_START_TIME || process.env.GAME_END_TIME) { + const envStart = process.env.GAME_START_TIME; + const envEnd = process.env.GAME_END_TIME; + + const startDate = envStart ? new Date(envStart) : undefined; + const endDate = envEnd ? new Date(envEnd) : undefined; + + if ((startDate && isNaN(startDate.getTime())) || (endDate && isNaN(endDate.getTime()))) { + console.error('Invalid GAME_START_TIME or GAME_END_TIME values'); + } else { + const existingConfig = await prisma.gameConfig.findFirst(); + if (existingConfig) { + await prisma.gameConfig.update({ + where: { id: existingConfig.id }, + data: { + ...(startDate ? { startTime: startDate } : {}), + ...(envEnd !== undefined ? { endTime: endDate ?? null } : {}), + }, + }); + } else if (startDate) { + await prisma.gameConfig.create({ + data: { + startTime: startDate, + endTime: endDate ?? null, + isActive: true, + }, + }); + } + } + } + if (process.env.INGEST_CHALLENGES_AT_STARTUP === 'true') { // Run challenge ingestion const { ChallengeIngestionService } = await import('@/lib/challenge-ingestion');