From 2f55a1096cdcf915d978965834b3e142f7f2b571 Mon Sep 17 00:00:00 2001 From: Burton Jong Date: Tue, 30 Jul 2024 00:43:41 -0600 Subject: [PATCH] created score board and temp start game button yawn --- components/artist/audio.tsx | 6 +-- components/artist/guesscontainer.tsx | 74 ++++++++++++++++------------ components/artist/score.tsx | 42 ++++++++++++++++ components/card.tsx | 2 +- util/game.ts | 4 +- 5 files changed, 90 insertions(+), 38 deletions(-) create mode 100644 components/artist/score.tsx diff --git a/components/artist/audio.tsx b/components/artist/audio.tsx index 99b17ce..6b51166 100644 --- a/components/artist/audio.tsx +++ b/components/artist/audio.tsx @@ -19,7 +19,7 @@ export default function Audio({ song }: { song: Song }) { className="pb-2" /> - {song.images && ( + {/* {song.images && ( album cover - )} + )} */}

diff --git a/components/artist/guesscontainer.tsx b/components/artist/guesscontainer.tsx index e95b3d3..1a4080d 100644 --- a/components/artist/guesscontainer.tsx +++ b/components/artist/guesscontainer.tsx @@ -1,14 +1,15 @@ "use client"; import Options from "@/components/artist/options"; -import type { Image, Song } from "@/util/types"; +import Score from "@/components/artist/score"; import Audio from "@/components/artist/audio"; import { useParams } from "next/navigation"; import { useQuery } from "@tanstack/react-query"; -import { useEffect, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import { Game } from "@/util/game"; +import type { Image, Song } from "@/util/types"; export default function GuessContainer({ token }: { token: string }) { const params = useParams(); @@ -112,21 +113,23 @@ export default function GuessContainer({ token }: { token: string }) { }); }, [gameState.game, gameState.currentRound]); - const handleAnswer = (chosenSong: Song) => { - console.log(gameState.currentRound); - if (gameState.game && gameState.currentRound) { - const isCorrect = gameState.game.checkAnswer( - chosenSong, - gameState.currentRound.currentSong - ); - console.log( - isCorrect - ? "Correct!" - : `Wrong! The correct answer was: ${gameState.currentRound.currentSong.name}` - ); - setGameState((prevState) => ({ ...prevState, currentRound: null })); - } - }; + const handleAnswer = useCallback( + (chosenSong: Song) => { + if (gameState.game && gameState.currentRound) { + const isCorrect = gameState.game.checkAnswer( + chosenSong, + gameState.currentRound.currentSong + ); + console.log( + isCorrect + ? "Correct!" + : `Wrong! The correct answer was: ${gameState.currentRound.currentSong.name}` + ); + setGameState((prevState) => ({ ...prevState, currentRound: null })); + } + }, + [gameState.game, gameState.currentRound] + ); return (
@@ -141,26 +144,33 @@ export default function GuessContainer({ token }: { token: string }) { ) : ( <>
-
+
-
- +
+ {gameState.game ? ( +
+ +
+ ) : ( +

+ Options will appear here. +

+ )}
+
)} diff --git a/components/artist/score.tsx b/components/artist/score.tsx new file mode 100644 index 0000000..1f7a925 --- /dev/null +++ b/components/artist/score.tsx @@ -0,0 +1,42 @@ +import { Card } from "@/components/card"; + +interface ScoreProps { + currentRound: number; + start: () => void; + score: number; +} + +export default function Score({ currentRound, start, score }: ScoreProps) { + return ( + +
+ {currentRound && ( +

+ Your Score: {score} / 5 +

+ )} + +

+ Current Round: {currentRound} +

+ {!currentRound && ( + + )} + + {/*
+

+ Read more +

+
*/} +
+
+ ); +} diff --git a/components/card.tsx b/components/card.tsx index c3f175d..2ae39c4 100644 --- a/components/card.tsx +++ b/components/card.tsx @@ -19,7 +19,7 @@ export const Card: React.FC = ({ children }) => { return (
diff --git a/util/game.ts b/util/game.ts index 7d4f60c..d369316 100644 --- a/util/game.ts +++ b/util/game.ts @@ -17,7 +17,7 @@ export class Game { startNewRound() { if (this.currentRound >= this.totalRounds) { - return null; // Game over + return null; } this.currentRound++; @@ -44,7 +44,7 @@ export class Game { getWrongAnswers(correctSong: Song) { let wrongAnswers: Song[] = []; - while (wrongAnswers.length < 2) { + while (wrongAnswers.length < 3) { let randomSong = this.getUnusedRandomSong(); if ( randomSong.name !== correctSong.name &&