Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 인게임 점수 획득시 효과음, 라운드시작 카운트다운 효과음 #321

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added .yarn/cache/fsevents-patch-6b67494872-10c0.zip
Binary file not shown.
Binary file added src/assets/audio/countDown.mp3
Binary file not shown.
Binary file added src/assets/audio/soundEffect.mp3
Binary file not shown.
5 changes: 5 additions & 0 deletions src/pages/GamePage/GameCode/GameCode.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useCallback, useMemo, useRef } from 'react';
import IngameHeader from '@/common/Ingame/IngameHeader';
import IngameRank from '@/common/Ingame/IngameRank';
import playSoundEffect, {
SOUND_SCORE,
} from '@/pages/GamePage/common/playSoundEffect';
import useIngameStore from '@/store/useIngameStore';
import CanvasTrack from '../common/CanvasTrack';
import TrackLine from '../common/TrackLine';
Expand Down Expand Up @@ -91,6 +94,8 @@ const GameCode = ({ publishIngame, userId }: GameCodeProps) => {
const handleUpdateScore = useCallback(() => {
const newScore = currentScore + scorePerSubmit;
publishIngame('/info', { currentScore: newScore });
const sound = playSoundEffect(SOUND_SCORE);
sound.then((audio) => audio.play());
}, [currentScore, scorePerSubmit]);

return (
Expand Down
8 changes: 7 additions & 1 deletion src/pages/GamePage/GameSentence/GameSentence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import Dashboard from '@/common/Ingame/Dashboard';
import IngameHeader from '@/common/Ingame/IngameHeader';
import IngameRank from '@/common/Ingame/IngameRank';
import { SentenceNext } from '@/common/Ingame/SentenceBlocks';
import playSoundEffect, {
SOUND_SCORE,
} from '@/pages/GamePage/common/playSoundEffect';
import useIngameStore from '@/store/useIngameStore';
import CanvasTrack from '../common/CanvasTrack';
import TrackLine from '../common/TrackLine';
Expand All @@ -26,6 +29,8 @@ export interface I_RankInfoList {
}

const GameSentence = ({ publishIngame, userId }: GameSentenceProps) => {
const sound = playSoundEffect(SOUND_SCORE);

const { ingameRoomRes, isRoundWaiting } = useIngameStore();

const currentScore = ingameRoomRes.allMembers.find(
Expand All @@ -40,6 +45,7 @@ const GameSentence = ({ publishIngame, userId }: GameSentenceProps) => {
const handleNextRound = useCallback(() => {
sentenceList.current = ingameRoomRes.questions!;
setSentenceIdx(0);
sound.then((audio) => audio.play());
}, [ingameRoomRes.questions]);

const {
Expand Down Expand Up @@ -87,12 +93,12 @@ const GameSentence = ({ publishIngame, userId }: GameSentenceProps) => {
);

const scorePerTrankLength = Math.ceil((1 / TotalSpacedWord) * 100);

const handleUpdateScore: UpdateScoreType = useCallback(() => {
const newScore = currentScore + scorePerTrankLength;
publishIngame('/info', {
currentScore: newScore,
});
sound.then((audio) => audio.play());
}, [currentScore, scorePerTrankLength]);

return (
Expand Down
3 changes: 3 additions & 0 deletions src/pages/GamePage/GameWord/WordGameLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
I_IngameWsResponse,
PublishIngameType,
} from '../../GamePage/types/websocketType';
import playSoundEffect, { SOUND_SCORE } from '../common/playSoundEffect';
import useFocusInput from '../hooks/useFocusInput';
import WordCell from './WordCell';
import WordRankContainer from './WordRankContainer';
Expand Down Expand Up @@ -65,6 +66,7 @@ const WordGameLayout = ({
useEffect(() => {
onInputChange(0, 100, 150); //동기화..
}, [ingameRoomRes]);
const sound = playSoundEffect(SOUND_SCORE);

return (
<>
Expand Down Expand Up @@ -99,6 +101,7 @@ const WordGameLayout = ({
setValue('wordInput', '');
initializeTyping();
submitCount.current += 1;
sound.then((audio) => audio.play());
})}>
<input
autoFocus
Expand Down
8 changes: 6 additions & 2 deletions src/pages/GamePage/common/RoundWaitModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Dialog from '@radix-ui/react-dialog';
import { useEffect } from 'react';
import { convertTime } from '@/common/Timer/utils/convertTime';
import useTimer from '@/hooks/useTimer';
import playSoundEffect, { SOUND_COUNTDOWN } from './playSoundEffect';

interface RoundWaitModalProps {
isOpen: boolean;
Expand All @@ -17,9 +18,12 @@ const RoundWaitModal = ({ isOpen, onTimeFinish }: RoundWaitModalProps) => {
},
autoStart: false,
});

const sound = playSoundEffect(SOUND_COUNTDOWN);
useEffect(() => {
isOpen && startTimer();
if (isOpen) {
startTimer();
sound.then((audio) => audio.play());
}
}, [isOpen]);

return (
Expand Down
17 changes: 17 additions & 0 deletions src/pages/GamePage/common/playSoundEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const SOUND_SCORE = 'SCORE';
export const SOUND_COUNTDOWN = 'COUNTDOWN';
type SoundEffectType = typeof SOUND_SCORE | typeof SOUND_COUNTDOWN;

const mappedSoundFiles: Record<SoundEffectType, () => Promise<string>> = {
SCORE: () =>
import('@/assets/audio/soundEffect.mp3').then((module) => module.default),
COUNTDOWN: () =>
import('@/assets/audio/countDown.mp3').then((module) => module.default),
};

const playSoundEffect = async (type: SoundEffectType) => {
const src = await mappedSoundFiles[type]();
const audio = new Audio(src);
return audio;
};
export default playSoundEffect;