From 228c980ffb4e5d06864c59a30fccd5ad1a9c01e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o?= <43091603+glazk0@users.noreply.github.com> Date: Sat, 22 Apr 2023 14:29:12 +0200 Subject: [PATCH] Adding verification on puzzle --- ui/Puzzle.tsx | 31 +++++++++++++++++++++++-------- ui/Puzzles.tsx | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/ui/Puzzle.tsx b/ui/Puzzle.tsx index 40ca5b0..777a012 100644 --- a/ui/Puzzle.tsx +++ b/ui/Puzzle.tsx @@ -20,8 +20,10 @@ type PuzzleData = { }; type Granted = { - tries: number; - score?: number; + tries: number | null; + score?: number | null; + message?: string | null; + success?: boolean | null; }; export default function Puzzle({ token, id }: { token: string; id: number }) { @@ -58,10 +60,14 @@ export default function Puzzle({ token, id }: { token: string; id: number }) { } }); - if (res.ok || res.status === 406) { - const data = (await res.json()) as { tries: number; score?: number }; - if (data.score) mutate(`puzzles/${puzzle?.id}`); - else setGranted(data); + if (res.ok || res.status === 406 || res.status === 423) { + const data = res.ok || res.status === 406 ? ((await res.json()) as Granted) : null; + if (data && data.score) mutate(`puzzles/${puzzle?.id}`); + else if (data && data.tries) setGranted(data); + else if (res.ok && data?.success) + setGranted({ tries: null, score: null, message: 'Réponse correcte' }); + else if (res.status === 423) + setGranted({ tries: null, score: null, message: 'Réponse incorrecte' }); } } @@ -99,8 +105,17 @@ export default function Puzzle({ token, id }: { token: string; id: number }) { /> {granted && (
-

Tentatives actuelles : {granted.tries}

- {granted.score &&

Score : {granted.score}

} + {granted.message && ( +

{granted.message}

+ )} + {granted.tries && ( +

+ Tentatives actuelles : {granted.tries} +

+ )} + {granted.score && ( +

Score : {granted.score}

+ )}
)} {/* now) { + if (now > startDate) { const minutes = 10 * 60 * 1000; if (startDate.getTime() + minutes < now.getTime()) { return true; @@ -90,7 +89,7 @@ export default function Puzzles({ token }: { token: string }) {

{chapter.name}

- {!isInEventGroup(chapter) && !isBetweenDates(chapter) && ( + {!isInEventGroup(chapter) && isBeforeStart(chapter) && (
@@ -211,15 +212,23 @@ export default function Puzzles({ token }: { token: string }) { } function PuzzleProp({ puzzle, chapter }: { puzzle: Puzzle; chapter: Chapter }) { - const isPuzzleAvailable = (chapter: Chapter) => { - const today = new Date(); - const startDate = new Date(chapter.startDate!); - const endDate = new Date(chapter.endDate!); - return ( - (chapter.startDate && chapter.endDate && today >= startDate && today <= endDate) || - (!chapter.startDate && !chapter.endDate) - ); - }; + function isBeforeStart(chapter: Chapter) { + if (!chapter.startDate || !chapter.endDate) { + return false; + } + + const startDate = new Date(chapter.startDate); + const now = new Date(); + + if (now > startDate) { + const minutes = 10 * 60 * 1000; + if (startDate.getTime() + minutes < now.getTime()) { + return true; + } + } + + return false; + } return (
  • tag.name.toLowerCase() === 'medium'), 'border-red-600/30': puzzle.tags?.find((tag) => tag.name.toLowerCase() === 'hard'), 'border-highlight-primary': !puzzle.tags?.length, - 'cursor-not-allowed': !isPuzzleAvailable(chapter) + 'cursor-not-allowed': !isBeforeStart(chapter) } )} > - {isPuzzleAvailable(chapter) ? ( + {isBeforeStart(chapter) ? (