Skip to content

Commit

Permalink
Added create levels script
Browse files Browse the repository at this point in the history
  • Loading branch information
John Davison committed Jul 9, 2023
1 parent ec45baf commit eca8ce1
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 43 deletions.
Binary file modified prisma/dev.db
Binary file not shown.
76 changes: 76 additions & 0 deletions scripts/createLevels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Difficulty } from "../src/app/play/[id]/types";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const getSolutions = async () => {
return await prisma.solution.findMany({
include: {
solutionPieces: true,
},
});
};

/**
* Create levels from all the solutions
*/
getSolutions().then((solutions) => {
solutions.map(async (solution) => {
const difficulty = getDifficulty(solution.id);
return await prisma.level.create({
data: {
id: solution.id,
difficulty,
solutionId: solution.id,
solutionPieces: {
connect: getRandomSolutionPieces(difficulty).map((id) => ({
id,
})),
},
},
});
});
});

const getDifficulty = (id: number): Difficulty => {
const modulo = (id % 4) as 1 | 2 | 3 | 0;
switch (modulo) {
case 1:
return "EASY";
case 2:
return "INTERMEDIATE";
case 3:
return "EXPERT";
case 0:
return "WIZARD";
}
};

/**
* Returns an array of 12 numbers in random order
*/
const shuffle = () => {
return [...Array(12)]
.map((_, i) => i + 1)
.map((value) => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value);
};

/**
* Return the first x number of ids from the shuffled array
*/
const getRandomSolutionPieces = (difficulty: Difficulty): number[] => {
switch (difficulty) {
case "EASY":
return shuffle().slice(0, 8);
case "INTERMEDIATE":
return shuffle().slice(0, 6);
case "EXPERT":
return shuffle().slice(0, 4);
case "WIZARD":
return shuffle().slice(0, 2);
default:
throw new Error("Invalid difficulty");
}
};
21 changes: 21 additions & 0 deletions scripts/getUniqueSolutions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const getSolutions = async () => {
return await prisma.solution.findMany({
include: {
solutionPieces: true,
},
});
};

getSolutions().then((solutions) => {
const noIdSolutions = solutions.map(({ id, ...rest }) =>
JSON.stringify(rest)
);
console.log(noIdSolutions);

const uniqueSolutions = [...new Set(noIdSolutions)];
console.log("Unique Solutions:", uniqueSolutions.length);
});
41 changes: 0 additions & 41 deletions src/app/play/[id]/types.js

This file was deleted.

2 changes: 2 additions & 0 deletions src/app/play/[id]/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ export type GameState = {
grid: number[][];
complete: boolean;
};

export type Difficulty = "EASY" | "INTERMEDIATE" | "EXPERT" | "WIZARD";
Empty file added src/app/solution/[id]/page.tsx
Empty file.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es6",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.scripts.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"baseUrl": ".",
"noEmit": false
},
"include": ["./scripts/*.ts", "./src/app/play/[id]/sharedUtils.ts"]
"include": ["./scripts/*.ts", "./src/app/play/[id]/sharedUtils.ts", "./src/app/play/[id]/types.ts"]
}

0 comments on commit eca8ce1

Please sign in to comment.