Skip to content

Commit

Permalink
Question implementation- player must answer question correctly to mov…
Browse files Browse the repository at this point in the history
…e between rooms

Created LocalDoor type for improved type safety
  • Loading branch information
hopperelec committed Jan 25, 2024
1 parent 9d2b800 commit 0ce8ebc
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Question = { id: number; question: string };
export type LocalDoor = { room1_id: number; room2_id: number };
25 changes: 23 additions & 2 deletions src/routes/(app)/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { PageData } from "./$types";
import SVGMap from "$lib/SVGMap.svelte";
import { error } from "@sveltejs/kit";
import type { Question } from "$lib/types";
export let data: PageData;
if (!data.map) throw error(403, "You do not have access to any maps!");
Expand Down Expand Up @@ -37,8 +38,28 @@
return doors[room1_id] && doors[room1_id].includes(room2_id);
}
function onClickRoom(clickedRoom: number) {
if (canMoveTo(clickedRoom)) position = clickedRoom;
async function askQuestion(question: Question): Promise<boolean> {
const answer = prompt(question.question);
const res = await fetch(`/check-answer?id=${question.id}&answer=${answer}`);
console.log(res);
return (await res.json()).correct;
}
async function getNextQuestion(): Promise<Question> {
const res: { id: number; definition: string } = await (await fetch("/get-definition")).json();
return {id: res.id, question: "What vocabulary is being defined: "+res.definition}
}
async function onClickRoom(clickedRoom: number) {
if (canMoveTo(clickedRoom)) {
const question = await getNextQuestion();
let askAgain = true;
while (askAgain) {
console.log(2);
if (await askQuestion(question)) askAgain = false;
}
position = clickedRoom;
}
}
function onSuccess() {
Expand Down
24 changes: 24 additions & 0 deletions src/routes/(app)/check-answer/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { error, type RequestHandler } from "@sveltejs/kit";
import prisma from "$lib/prisma";

export const GET: RequestHandler = async ({ url }) => {
const id = url.searchParams.get("id");
if (!id) throw error(400, "Question ID not provided");
const answer = url.searchParams.get("answer");
if (!answer) throw error(400, "Answer not provided");
const res = await prisma.definition.findUnique({
where: {
id: +id,
},
select: {
answer_regex: true,
},
});
if (!res) throw error(400, "Invalid question ID");
const correct = new RegExp("^" + res.answer_regex + "$").test(answer);
return new Response(JSON.stringify({ correct }), {
headers: {
"Content-Type": "application/json",
},
});
};
17 changes: 17 additions & 0 deletions src/routes/(app)/get-definition/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import prisma from "$lib/prisma";
import type { RequestHandler } from "@sveltejs/kit";

export const GET: RequestHandler = async () => {
const definition: { id: bigint; definition: string }[] =
await prisma.$queryRaw`SELECT id,definition FROM Definition ORDER BY rand() LIMIT 1`;
return new Response(
JSON.stringify(definition[0], (_, v) =>
typeof v === "bigint" ? Number(v) : v,
),
{
headers: {
"Content-Type": "application/json",
},
},
);
};
7 changes: 2 additions & 5 deletions src/routes/(app)/teacher/door-mapper/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import SVGMap from "$lib/SVGMap.svelte";
import { SVG_NS } from "$lib/constants";
import { error } from "@sveltejs/kit";
import type { LocalDoor } from "$lib/types";
export let data: PageData;
if (!data.map) throw error(403, "You do not have access to any maps!");
Expand All @@ -11,11 +12,7 @@
let firstRoom: number;
let firstRoomCenter: DOMPoint | undefined;
function drawLine(
door: { room1_id: number; room2_id: number },
firstRoomCenter?: DOMPoint,
secondRoomCenter?: DOMPoint,
) {
function drawLine(door: LocalDoor, firstRoomCenter?: DOMPoint, secondRoomCenter?: DOMPoint) {
if (firstRoomCenter && secondRoomCenter) {
const line = map
.getSVG()
Expand Down
9 changes: 5 additions & 4 deletions src/routes/(app)/teacher/door-mapper/add-door/+server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { error, type RequestHandler } from "@sveltejs/kit";
import prisma from "$lib/prisma";
import { getMapFor } from "$lib/get-map-for";
import type { LocalDoor } from "$lib/types";

export const POST: RequestHandler = async ({ request, locals }) => {
const map_id = (await getMapFor(locals.user))?.id;
if (!map_id) throw error(403, "You do not have access to any maps!");
const params = await request.json();
if (params.room1_id === params.room2_id)
const door: LocalDoor = await request.json();
if (door.room1_id === door.room2_id)
throw error(400, "Cannot add a door between a room and itself!");
await prisma.door.create({
data: {
Expand All @@ -18,15 +19,15 @@ export const POST: RequestHandler = async ({ request, locals }) => {
room1: {
connect: {
id_map_id: {
id: Math.min(params.room1_id, params.room2_id),
id: Math.min(door.room1_id, door.room2_id),
map_id,
},
},
},
room2: {
connect: {
id_map_id: {
id: Math.max(params.room1_id, params.room2_id),
id: Math.max(door.room1_id, door.room2_id),
map_id,
},
},
Expand Down
7 changes: 4 additions & 3 deletions src/routes/(app)/teacher/door-mapper/remove-door/+server.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { error, type RequestHandler } from "@sveltejs/kit";
import prisma from "$lib/prisma";
import { getMapFor } from "$lib/get-map-for";
import type { LocalDoor } from "$lib/types";

export const POST: RequestHandler = async ({ request, locals }) => {
const params = await request.json();
const door: LocalDoor = await request.json();
const map_id = (await getMapFor(locals.user))?.id;
if (!map_id) throw error(403, "You do not have access to any maps!");
await prisma.door.delete({
where: {
map_id_room1_id_room2_id: {
map_id,
room1_id: params.room1_id,
room2_id: params.room2_id,
room1_id: door.room1_id,
room2_id: door.room2_id,
},
},
});
Expand Down

0 comments on commit 0ce8ebc

Please sign in to comment.