File size: 4,698 Bytes
9580dce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 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(),
});
});
|