RotNot / src /app /api /logic /generate /route.ts
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
4.7 kB
import { NextRequest, NextResponse } from "next/server";
import { generateSudoku, type SudokuDifficulty } from "@/lib/puzzles/sudoku";
import {
generateSequence,
type SequenceDifficulty,
type SequenceType,
} from "@/lib/puzzles/sequences";
import {
generatePattern,
type PatternDifficulty,
} from "@/lib/puzzles/patterns";
import {
generateWordScramble,
type WordScrambleDifficulty,
} from "@/lib/puzzles/wordscramble";
import {
generateConstraintPuzzle,
type ConstraintDifficulty,
} from "@/lib/puzzles/constraint";
import {
generateMathEquation,
type MathDifficulty,
} from "@/lib/puzzles/mathequation";
import { withApiError } from "@/lib/api-error";
const VALID_DIFFICULTIES = ["easy", "medium", "hard"] as const;
type Difficulty = (typeof VALID_DIFFICULTIES)[number];
const VALID_PUZZLE_TYPES = ["sudoku", "sequence", "pattern", "wordscramble", "constraint", "mathequation"] as const;
type PuzzleType = (typeof VALID_PUZZLE_TYPES)[number];
/**
* GET /api/logic/generate?type=sudoku&difficulty=medium
*
* Returns a fresh puzzle. Supported types:
* - sudoku (default): 9×9 Sudoku grid
* - sequence: number sequence with hidden terms
* - pattern: visual pattern matrix (pick the missing piece)
* - wordscramble: unscramble letters to form a word
* - constraint: 4×4 Killer-Sudoku-lite with cage sum constraints
*
* Optional `sequenceType` param for sequences: arithmetic, geometric, fibonacci, etc.
* Optional `wordCategory` param for wordscramble: nature, food, science, etc.
*/
export const GET = withApiError(async (request: NextRequest) => {
const { searchParams } = new URL(request.url);
const rawType = searchParams.get("type") ?? "sudoku";
const rawDiff = searchParams.get("difficulty") ?? "medium";
if (!VALID_DIFFICULTIES.includes(rawDiff as Difficulty)) {
return NextResponse.json(
{
error: `Invalid difficulty. Must be one of: ${VALID_DIFFICULTIES.join(", ")}`,
status: 400,
},
{ status: 400 },
);
}
if (!VALID_PUZZLE_TYPES.includes(rawType as PuzzleType)) {
return NextResponse.json(
{
error: `Invalid type. Must be one of: ${VALID_PUZZLE_TYPES.join(", ")}`,
status: 400,
},
{ status: 400 },
);
}
const difficulty = rawDiff as Difficulty;
const puzzleType = rawType as PuzzleType;
if (puzzleType === "sequence") {
const rawSeqType = searchParams.get("sequenceType") ?? undefined;
const puzzle = generateSequence(
difficulty as SequenceDifficulty,
rawSeqType as SequenceType | undefined,
);
return NextResponse.json({
puzzleType: "sequence",
...puzzle,
generatedAt: new Date().toISOString(),
});
}
if (puzzleType === "pattern") {
const puzzle = generatePattern(difficulty as PatternDifficulty);
return NextResponse.json({
puzzleType: "pattern",
grid: puzzle.grid,
choices: puzzle.choices,
correctIndex: puzzle.correctIndex,
activeAttributes: puzzle.activeAttributes,
hint: puzzle.hint,
difficulty,
generatedAt: new Date().toISOString(),
});
}
if (puzzleType === "wordscramble") {
const rawWordCategory = searchParams.get("wordCategory") ?? undefined;
const puzzle = generateWordScramble(
difficulty as WordScrambleDifficulty,
rawWordCategory,
);
return NextResponse.json({
puzzleType: "wordscramble",
scrambled: puzzle.scrambled,
answer: puzzle.answer,
hintIndices: puzzle.hintIndices,
hint: puzzle.hint,
category: puzzle.category,
length: puzzle.length,
difficulty,
generatedAt: new Date().toISOString(),
});
}
if (puzzleType === "constraint") {
const puzzle = generateConstraintPuzzle(difficulty as ConstraintDifficulty);
return NextResponse.json({
puzzleType: "constraint",
solution: puzzle.solution,
given: puzzle.given,
cages: puzzle.cages,
hint: puzzle.hint,
difficulty,
generatedAt: new Date().toISOString(),
});
}
if (puzzleType === "mathequation") {
const puzzle = generateMathEquation(difficulty as MathDifficulty);
return NextResponse.json({
puzzleType: "mathequation",
equation: puzzle.equation,
answer: puzzle.answer,
hint: puzzle.hint,
operationCount: puzzle.operationCount,
difficulty,
generatedAt: new Date().toISOString(),
});
}
// Default: Sudoku
const { puzzle, solution } = generateSudoku(difficulty as SudokuDifficulty);
return NextResponse.json({
puzzleType: "sudoku",
puzzle,
solution,
difficulty,
generatedAt: new Date().toISOString(),
});
});