"use client" import { CommonStatusDataMap } from "@rahoot/common/types/game/status" import AnswerButton from "@rahoot/web/components/AnswerButton" import QuestionMedia from "@rahoot/web/components/game/QuestionMedia" import { useEvent, useSocket } from "@rahoot/web/contexts/socketProvider" import { usePlayerStore } from "@rahoot/web/stores/player" import { ANSWERS_COLORS, ANSWERS_ICONS, SFX_ANSWERS_MUSIC, SFX_ANSWERS_SOUND, } from "@rahoot/web/utils/constants" import clsx from "clsx" import { useParams } from "next/navigation" import { useEffect, useState } from "react" import useSound from "use-sound" type Props = { data: CommonStatusDataMap["SELECT_ANSWER"] } const Answers = ({ data: { question, answers, image, media, time, totalPlayer }, }: Props) => { const { gameId }: { gameId?: string } = useParams() const { socket } = useSocket() const { player } = usePlayerStore() const [cooldown, setCooldown] = useState(time) const [paused, setPaused] = useState(false) const [totalAnswer, setTotalAnswer] = useState(0) const [isMediaPlaying, setIsMediaPlaying] = useState(false) const [sfxPop] = useSound(SFX_ANSWERS_SOUND, { volume: 0.1, }) const [playMusic, { stop: stopMusic, sound: answersMusic }] = useSound( SFX_ANSWERS_MUSIC, { volume: 0.2, interrupt: true, loop: true, }, ) const handleAnswer = (answerKey: number) => () => { if (!player) { return } socket?.emit("player:selectedAnswer", { gameId, data: { answerKey, }, }) sfxPop() } useEffect(() => { playMusic() return () => { stopMusic() } }, [playMusic]) useEffect(() => { if (!answersMusic) { return } answersMusic.volume(isMediaPlaying ? 0.05 : 0.2) }, [answersMusic, isMediaPlaying]) useEvent("game:cooldown", (sec) => { setCooldown(sec) }) useEvent("game:cooldownPause", (isPaused) => { setPaused(isPaused) }) useEvent("game:playerAnswer", (count) => { setTotalAnswer(count) sfxPop() }) return (

{question}

setIsMediaPlaying(playing)} />
Time {cooldown} {paused && ( Paused )}
Answers {totalAnswer}/{totalPlayer}
{answers.map((answer, key) => ( {answer} ))}
) } export default Answers