feat(specials): add launch and end dates for scheduling

This commit is contained in:
Hördle Bot
2025-11-23 02:09:49 +01:00
parent 69eb69b8cd
commit e9526918e1
7 changed files with 160 additions and 26 deletions

View File

@@ -8,6 +8,8 @@ interface Special {
name: string; name: string;
maxAttempts: number; maxAttempts: number;
unlockSteps: string; unlockSteps: string;
launchDate?: string;
endDate?: string;
_count?: { _count?: {
songs: number; songs: number;
}; };
@@ -62,10 +64,15 @@ export default function AdminPage() {
const [newSpecialName, setNewSpecialName] = useState(''); const [newSpecialName, setNewSpecialName] = useState('');
const [newSpecialMaxAttempts, setNewSpecialMaxAttempts] = useState(7); const [newSpecialMaxAttempts, setNewSpecialMaxAttempts] = useState(7);
const [newSpecialUnlockSteps, setNewSpecialUnlockSteps] = useState('[2,4,7,11,16,30,60]'); const [newSpecialUnlockSteps, setNewSpecialUnlockSteps] = useState('[2,4,7,11,16,30,60]');
const [newSpecialLaunchDate, setNewSpecialLaunchDate] = useState('');
const [newSpecialEndDate, setNewSpecialEndDate] = useState('');
const [editingSpecialId, setEditingSpecialId] = useState<number | null>(null); const [editingSpecialId, setEditingSpecialId] = useState<number | null>(null);
const [editSpecialName, setEditSpecialName] = useState(''); const [editSpecialName, setEditSpecialName] = useState('');
const [editSpecialMaxAttempts, setEditSpecialMaxAttempts] = useState(7); const [editSpecialMaxAttempts, setEditSpecialMaxAttempts] = useState(7);
const [editSpecialUnlockSteps, setEditSpecialUnlockSteps] = useState('[2,4,7,11,16,30,60]'); const [editSpecialUnlockSteps, setEditSpecialUnlockSteps] = useState('[2,4,7,11,16,30,60]');
const [editSpecialLaunchDate, setEditSpecialLaunchDate] = useState('');
const [editSpecialEndDate, setEditSpecialEndDate] = useState('');
// Edit state // Edit state
const [editingId, setEditingId] = useState<number | null>(null); const [editingId, setEditingId] = useState<number | null>(null);
@@ -177,12 +184,16 @@ export default function AdminPage() {
name: newSpecialName, name: newSpecialName,
maxAttempts: newSpecialMaxAttempts, maxAttempts: newSpecialMaxAttempts,
unlockSteps: newSpecialUnlockSteps, unlockSteps: newSpecialUnlockSteps,
launchDate: newSpecialLaunchDate || null,
endDate: newSpecialEndDate || null,
}), }),
}); });
if (res.ok) { if (res.ok) {
setNewSpecialName(''); setNewSpecialName('');
setNewSpecialMaxAttempts(7); setNewSpecialMaxAttempts(7);
setNewSpecialUnlockSteps('[2,4,7,11,16,30,60]'); setNewSpecialUnlockSteps('[2,4,7,11,16,30,60]');
setNewSpecialLaunchDate('');
setNewSpecialEndDate('');
fetchSpecials(); fetchSpecials();
} else { } else {
alert('Failed to create special'); alert('Failed to create special');
@@ -262,6 +273,8 @@ export default function AdminPage() {
setEditSpecialName(special.name); setEditSpecialName(special.name);
setEditSpecialMaxAttempts(special.maxAttempts); setEditSpecialMaxAttempts(special.maxAttempts);
setEditSpecialUnlockSteps(special.unlockSteps); setEditSpecialUnlockSteps(special.unlockSteps);
setEditSpecialLaunchDate(special.launchDate ? new Date(special.launchDate).toISOString().split('T')[0] : '');
setEditSpecialEndDate(special.endDate ? new Date(special.endDate).toISOString().split('T')[0] : '');
}; };
const saveEditedSpecial = async () => { const saveEditedSpecial = async () => {
@@ -274,6 +287,8 @@ export default function AdminPage() {
name: editSpecialName, name: editSpecialName,
maxAttempts: editSpecialMaxAttempts, maxAttempts: editSpecialMaxAttempts,
unlockSteps: editSpecialUnlockSteps, unlockSteps: editSpecialUnlockSteps,
launchDate: editSpecialLaunchDate || null,
endDate: editSpecialEndDate || null,
}), }),
}); });
if (res.ok) { if (res.ok) {
@@ -671,11 +686,28 @@ export default function AdminPage() {
<div className="admin-card" style={{ marginBottom: '2rem' }}> <div className="admin-card" style={{ marginBottom: '2rem' }}>
<h2 style={{ fontSize: '1.25rem', fontWeight: 'bold', marginBottom: '1rem' }}>Manage Specials</h2> <h2 style={{ fontSize: '1.25rem', fontWeight: 'bold', marginBottom: '1rem' }}>Manage Specials</h2>
<form onSubmit={handleCreateSpecial} style={{ marginBottom: '1rem' }}> <form onSubmit={handleCreateSpecial} style={{ marginBottom: '1rem' }}>
<div style={{ display: 'flex', gap: '0.5rem', flexWrap: 'wrap' }}> <div style={{ display: 'flex', gap: '0.5rem', flexWrap: 'wrap', alignItems: 'flex-end' }}>
<input type="text" placeholder="Special name" value={newSpecialName} onChange={e => setNewSpecialName(e.target.value)} className="form-input" required /> <div style={{ display: 'flex', flexDirection: 'column' }}>
<input type="number" placeholder="Max attempts" value={newSpecialMaxAttempts} onChange={e => setNewSpecialMaxAttempts(Number(e.target.value))} className="form-input" min={1} /> <label style={{ fontSize: '0.75rem', color: '#666' }}>Name</label>
<input type="text" placeholder="Unlock steps JSON" value={newSpecialUnlockSteps} onChange={e => setNewSpecialUnlockSteps(e.target.value)} className="form-input" /> <input type="text" placeholder="Special name" value={newSpecialName} onChange={e => setNewSpecialName(e.target.value)} className="form-input" required />
<button type="submit" className="btn-primary">Add Special</button> </div>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<label style={{ fontSize: '0.75rem', color: '#666' }}>Max Attempts</label>
<input type="number" placeholder="Max attempts" value={newSpecialMaxAttempts} onChange={e => setNewSpecialMaxAttempts(Number(e.target.value))} className="form-input" min={1} style={{ width: '80px' }} />
</div>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<label style={{ fontSize: '0.75rem', color: '#666' }}>Unlock Steps</label>
<input type="text" placeholder="Unlock steps JSON" value={newSpecialUnlockSteps} onChange={e => setNewSpecialUnlockSteps(e.target.value)} className="form-input" style={{ width: '200px' }} />
</div>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<label style={{ fontSize: '0.75rem', color: '#666' }}>Launch Date</label>
<input type="date" value={newSpecialLaunchDate} onChange={e => setNewSpecialLaunchDate(e.target.value)} className="form-input" />
</div>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<label style={{ fontSize: '0.75rem', color: '#666' }}>End Date</label>
<input type="date" value={newSpecialEndDate} onChange={e => setNewSpecialEndDate(e.target.value)} className="form-input" />
</div>
<button type="submit" className="btn-primary" style={{ height: '38px' }}>Add Special</button>
</div> </div>
</form> </form>
<div style={{ display: 'flex', flexWrap: 'wrap', gap: '0.5rem' }}> <div style={{ display: 'flex', flexWrap: 'wrap', gap: '0.5rem' }}>
@@ -691,6 +723,7 @@ export default function AdminPage() {
}}> }}>
<span>{special.name} ({special._count?.songs || 0})</span> <span>{special.name} ({special._count?.songs || 0})</span>
<a href={`/admin/specials/${special.id}`} className="btn-primary" style={{ marginRight: '0.5rem', textDecoration: 'none' }}>Curate</a> <a href={`/admin/specials/${special.id}`} className="btn-primary" style={{ marginRight: '0.5rem', textDecoration: 'none' }}>Curate</a>
<button onClick={() => startEditSpecial(special)} className="btn-secondary" style={{ marginRight: '0.5rem' }}>Edit</button>
<button onClick={() => handleDeleteSpecial(special.id)} className="btn-danger">Delete</button> <button onClick={() => handleDeleteSpecial(special.id)} className="btn-danger">Delete</button>
</div> </div>
))} ))}
@@ -698,12 +731,29 @@ export default function AdminPage() {
{editingSpecialId !== null && ( {editingSpecialId !== null && (
<div style={{ marginTop: '1rem', padding: '1rem', background: '#f9fafb', borderRadius: '0.5rem' }}> <div style={{ marginTop: '1rem', padding: '1rem', background: '#f9fafb', borderRadius: '0.5rem' }}>
<h3>Edit Special</h3> <h3>Edit Special</h3>
<div style={{ display: 'flex', gap: '0.5rem', flexWrap: 'wrap' }}> <div style={{ display: 'flex', gap: '0.5rem', flexWrap: 'wrap', alignItems: 'flex-end' }}>
<input type="text" value={editSpecialName} onChange={e => setEditSpecialName(e.target.value)} className="form-input" /> <div style={{ display: 'flex', flexDirection: 'column' }}>
<input type="number" value={editSpecialMaxAttempts} onChange={e => setEditSpecialMaxAttempts(Number(e.target.value))} className="form-input" min={1} /> <label style={{ fontSize: '0.75rem', color: '#666' }}>Name</label>
<input type="text" value={editSpecialUnlockSteps} onChange={e => setEditSpecialUnlockSteps(e.target.value)} className="form-input" /> <input type="text" value={editSpecialName} onChange={e => setEditSpecialName(e.target.value)} className="form-input" />
<button onClick={saveEditedSpecial} className="btn-primary">Save</button> </div>
<button onClick={() => setEditingSpecialId(null)} className="btn-secondary">Cancel</button> <div style={{ display: 'flex', flexDirection: 'column' }}>
<label style={{ fontSize: '0.75rem', color: '#666' }}>Max Attempts</label>
<input type="number" value={editSpecialMaxAttempts} onChange={e => setEditSpecialMaxAttempts(Number(e.target.value))} className="form-input" min={1} style={{ width: '80px' }} />
</div>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<label style={{ fontSize: '0.75rem', color: '#666' }}>Unlock Steps</label>
<input type="text" value={editSpecialUnlockSteps} onChange={e => setEditSpecialUnlockSteps(e.target.value)} className="form-input" style={{ width: '200px' }} />
</div>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<label style={{ fontSize: '0.75rem', color: '#666' }}>Launch Date</label>
<input type="date" value={editSpecialLaunchDate} onChange={e => setEditSpecialLaunchDate(e.target.value)} className="form-input" />
</div>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<label style={{ fontSize: '0.75rem', color: '#666' }}>End Date</label>
<input type="date" value={editSpecialEndDate} onChange={e => setEditSpecialEndDate(e.target.value)} className="form-input" />
</div>
<button onClick={saveEditedSpecial} className="btn-primary" style={{ height: '38px' }}>Save</button>
<button onClick={() => setEditingSpecialId(null)} className="btn-secondary" style={{ height: '38px' }}>Cancel</button>
</div> </div>
</div> </div>
)} )}

View File

@@ -43,14 +43,16 @@ export async function PUT(
try { try {
const { id } = await params; const { id } = await params;
const specialId = parseInt(id); const specialId = parseInt(id);
const { name, maxAttempts, unlockSteps } = await request.json(); const { name, maxAttempts, unlockSteps, launchDate, endDate } = await request.json();
const special = await prisma.special.update({ const special = await prisma.special.update({
where: { id: specialId }, where: { id: specialId },
data: { data: {
name, name,
maxAttempts, maxAttempts,
unlockSteps: JSON.stringify(unlockSteps) unlockSteps: typeof unlockSteps === 'string' ? unlockSteps : JSON.stringify(unlockSteps),
launchDate: launchDate ? new Date(launchDate) : null,
endDate: endDate ? new Date(endDate) : null,
} }
}); });

View File

@@ -11,7 +11,7 @@ export async function GET() {
} }
export async function POST(request: Request) { export async function POST(request: Request) {
const { name, maxAttempts = 7, unlockSteps = '[2,4,7,11,16,30,60]' } = await request.json(); const { name, maxAttempts = 7, unlockSteps = '[2,4,7,11,16,30,60]', launchDate, endDate } = await request.json();
if (!name) { if (!name) {
return NextResponse.json({ error: 'Name is required' }, { status: 400 }); return NextResponse.json({ error: 'Name is required' }, { status: 400 });
} }
@@ -20,6 +20,8 @@ export async function POST(request: Request) {
name, name,
maxAttempts: Number(maxAttempts), maxAttempts: Number(maxAttempts),
unlockSteps, unlockSteps,
launchDate: launchDate ? new Date(launchDate) : null,
endDate: endDate ? new Date(endDate) : null,
}, },
}); });
return NextResponse.json(special); return NextResponse.json(special);
@@ -35,7 +37,7 @@ export async function DELETE(request: Request) {
} }
export async function PUT(request: Request) { export async function PUT(request: Request) {
const { id, name, maxAttempts, unlockSteps } = await request.json(); const { id, name, maxAttempts, unlockSteps, launchDate, endDate } = await request.json();
if (!id) { if (!id) {
return NextResponse.json({ error: 'ID required' }, { status: 400 }); return NextResponse.json({ error: 'ID required' }, { status: 400 });
} }
@@ -45,6 +47,8 @@ export async function PUT(request: Request) {
...(name && { name }), ...(name && { name }),
...(maxAttempts && { maxAttempts: Number(maxAttempts) }), ...(maxAttempts && { maxAttempts: Number(maxAttempts) }),
...(unlockSteps && { unlockSteps }), ...(unlockSteps && { unlockSteps }),
launchDate: launchDate ? new Date(launchDate) : null,
endDate: endDate ? new Date(endDate) : null,
}, },
}); });
return NextResponse.json(updated); return NextResponse.json(updated);

View File

@@ -12,6 +12,18 @@ export default async function Home() {
const genres = await prisma.genre.findMany({ orderBy: { name: 'asc' } }); const genres = await prisma.genre.findMany({ orderBy: { name: 'asc' } });
const specials = await prisma.special.findMany({ orderBy: { name: 'asc' } }); const specials = await prisma.special.findMany({ orderBy: { name: 'asc' } });
const now = new Date();
const activeSpecials = specials.filter(s => {
const isStarted = !s.launchDate || s.launchDate <= now;
const isEnded = s.endDate && s.endDate < now;
return isStarted && !isEnded;
});
const upcomingSpecials = specials.filter(s => {
return s.launchDate && s.launchDate > now;
});
return ( return (
<> <>
<div style={{ textAlign: 'center', padding: '1rem', background: '#f3f4f6' }}> <div style={{ textAlign: 'center', padding: '1rem', background: '#f3f4f6' }}>
@@ -26,12 +38,12 @@ export default async function Home() {
))} ))}
{/* Separator if both exist */} {/* Separator if both exist */}
{genres.length > 0 && specials.length > 0 && ( {genres.length > 0 && activeSpecials.length > 0 && (
<span style={{ color: '#d1d5db' }}>|</span> <span style={{ color: '#d1d5db' }}>|</span>
)} )}
{/* Specials */} {/* Active Specials */}
{specials.map(s => ( {activeSpecials.map(s => (
<Link <Link
key={s.id} key={s.id}
href={`/special/${s.name}`} href={`/special/${s.name}`}
@@ -45,6 +57,17 @@ export default async function Home() {
</Link> </Link>
))} ))}
</div> </div>
{/* Upcoming Specials */}
{upcomingSpecials.length > 0 && (
<div style={{ marginTop: '0.5rem', fontSize: '0.875rem', color: '#666' }}>
Coming soon: {upcomingSpecials.map(s => (
<span key={s.id} style={{ marginLeft: '0.5rem' }}>
{s.name} ({s.launchDate?.toLocaleDateString()})
</span>
))}
</div>
)}
</div> </div>
<Game dailyPuzzle={dailyPuzzle} genre={null} /> <Game dailyPuzzle={dailyPuzzle} genre={null} />
</> </>

View File

@@ -14,10 +14,45 @@ interface PageProps {
export default async function SpecialPage({ params }: PageProps) { export default async function SpecialPage({ params }: PageProps) {
const { name } = await params; const { name } = await params;
const decodedName = decodeURIComponent(name); const decodedName = decodeURIComponent(name);
const currentSpecial = await prisma.special.findUnique({
where: { name: decodedName }
});
const now = new Date();
const isStarted = currentSpecial && (!currentSpecial.launchDate || currentSpecial.launchDate <= now);
const isEnded = currentSpecial && (currentSpecial.endDate && currentSpecial.endDate < now);
if (!currentSpecial || !isStarted) {
return (
<div style={{ textAlign: 'center', padding: '2rem' }}>
<h1>Special Not Available</h1>
<p>This special has not launched yet or does not exist.</p>
<Link href="/">Go Home</Link>
</div>
);
}
if (isEnded) {
return (
<div style={{ textAlign: 'center', padding: '2rem' }}>
<h1>Special Ended</h1>
<p>This special event has ended.</p>
<Link href="/">Go Home</Link>
</div>
);
}
const dailyPuzzle = await getOrCreateSpecialPuzzle(decodedName); const dailyPuzzle = await getOrCreateSpecialPuzzle(decodedName);
const genres = await prisma.genre.findMany({ orderBy: { name: 'asc' } }); const genres = await prisma.genre.findMany({ orderBy: { name: 'asc' } });
const specials = await prisma.special.findMany({ orderBy: { name: 'asc' } }); const specials = await prisma.special.findMany({ orderBy: { name: 'asc' } });
const activeSpecials = specials.filter(s => {
const sStarted = !s.launchDate || s.launchDate <= now;
const sEnded = s.endDate && s.endDate < now;
return sStarted && !sEnded;
});
return ( return (
<> <>
<div style={{ textAlign: 'center', padding: '1rem', background: '#fce7f3' }}> <div style={{ textAlign: 'center', padding: '1rem', background: '#fce7f3' }}>
@@ -39,12 +74,12 @@ export default async function SpecialPage({ params }: PageProps) {
))} ))}
{/* Separator if both exist */} {/* Separator if both exist */}
{genres.length > 0 && specials.length > 0 && ( {genres.length > 0 && activeSpecials.length > 0 && (
<span style={{ color: '#d1d5db' }}>|</span> <span style={{ color: '#d1d5db' }}>|</span>
)} )}
{/* Specials */} {/* Specials */}
{specials.map(s => ( {activeSpecials.map(s => (
<Link <Link
key={s.id} key={s.id}
href={`/special/${s.name}`} href={`/special/${s.name}`}

View File

@@ -0,0 +1,18 @@
-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_Special" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"maxAttempts" INTEGER NOT NULL DEFAULT 7,
"unlockSteps" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"launchDate" DATETIME,
"endDate" DATETIME
);
INSERT INTO "new_Special" ("createdAt", "id", "maxAttempts", "name", "unlockSteps") SELECT "createdAt", "id", "maxAttempts", "name", "unlockSteps" FROM "Special";
DROP TABLE "Special";
ALTER TABLE "new_Special" RENAME TO "Special";
CREATE UNIQUE INDEX "Special_name_key" ON "Special"("name");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;

View File

@@ -30,13 +30,15 @@ model Genre {
} }
model Special { model Special {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
name String @unique name String @unique
maxAttempts Int @default(7) maxAttempts Int @default(7)
unlockSteps String // JSON array: "[2,4,7,11,16,30,60]" unlockSteps String // JSON string: e.g. "[2, 4, 7, 11, 16, 30]"
createdAt DateTime @default(now()) createdAt DateTime @default(now())
launchDate DateTime?
endDate DateTime?
songs SpecialSong[] songs SpecialSong[]
dailyPuzzles DailyPuzzle[] puzzles DailyPuzzle[]
} }
model SpecialSong { model SpecialSong {