diff --git a/client/src/pages/Game/Game.jsx b/client/src/pages/Game/Game.jsx
index 9e67f10..bbfdc36 100644
--- a/client/src/pages/Game/Game.jsx
+++ b/client/src/pages/Game/Game.jsx
@@ -19,6 +19,7 @@ export const Game = () => {
const [selectedSong, setSelectedSong] = useState(null);
const [guessResult, setGuessResult] = useState(null);
const [isHost, setIsHost] = useState(false);
+ const [hasGuessed, setHasGuessed] = useState(false);
const [messages, setMessages] = useState([]);
const [inputValue, setInputValue] = useState("");
@@ -48,9 +49,10 @@ export const Game = () => {
setTimeLeft(data.timeRemaining);
},
"guessing-phase-started": (data) => {
+ console.log("Guessing phase started:", data);
setPhase("guessing");
setTimeLeft(data.timeRemaining);
- setSongOptions(data.songOptions);
+ setSongOptions(data.songOptions || []);
},
"guess-result": (result) => {
setGuessResult(result.isCorrect);
@@ -94,6 +96,13 @@ export const Game = () => {
console.log("Received frequency update:", data.frequency);
setFrequency(data.frequency);
}
+ },
+ "phase-changed": (data) => {
+ console.log("Phase changed:", data);
+ setPhase(data.phase);
+ if (data.timeRemaining) {
+ setTimeLeft(data.timeRemaining);
+ }
}
};
@@ -143,8 +152,21 @@ export const Game = () => {
const handleSongSelect = useCallback((song) => {
setSelectedSong(song);
- send("submit-guess", { songId: song.id });
- }, [send]);
+ }, []);
+
+ const handleSubmitGuess = useCallback(() => {
+ if (!selectedSong || phase !== 'guessing') return;
+
+ console.log("Submitting guess:", selectedSong.id);
+ send("submit-guess", { songId: selectedSong.id });
+
+ setMessages(prev => [...prev, {
+ system: true,
+ text: `Du hast "${selectedSong.title}" von ${selectedSong.artist} gewählt.`
+ }]);
+
+ setHasGuessed(true);
+ }, [selectedSong, send, phase]);
const handleNextRound = useCallback(() => {
send("next-round");
@@ -212,23 +234,54 @@ export const Game = () => {
Die Rater versuchen nun, deinen Song zu erraten...
) : (
-
+
Welchen Song hat der Komponist gespielt?
-
- {songOptions.map(song => (
-
handleSongSelect(song)}
- >
-

-
-
{song.title}
-
{song.artist}
-
+
+ {songOptions.length === 0 ? (
+
+ ) : (
+ <>
+
+ {songOptions.map(song => (
+
!hasGuessed && handleSongSelect(song)}
+ >
+
+

+ {selectedSong?.id === song.id && (
+
+
+
+ )}
+
+
+
{song.title}
+
{song.artist}
+
+
+ ))}
- ))}
-
+
+
+ {hasGuessed ? (
+
Deine Antwort wurde eingereicht!
+ ) : (
+
+ )}
+
+ >
+ )}
)}
diff --git a/client/src/pages/Game/components/MusicSlider/MusicSlider.jsx b/client/src/pages/Game/components/MusicSlider/MusicSlider.jsx
index 4c18df8..802381a 100644
--- a/client/src/pages/Game/components/MusicSlider/MusicSlider.jsx
+++ b/client/src/pages/Game/components/MusicSlider/MusicSlider.jsx
@@ -187,6 +187,14 @@ export const MusicSlider = ({ isReadOnly = false, onFrequencyChange, frequency:
};
}, []);
+ useEffect(() => {
+ return () => {
+ if (isPlaying) {
+ stopAudio();
+ }
+ };
+ }, [isPlaying]);
+
return (
{
const advancePhase = (roomId) => {
const gameState = gameStates[roomId];
- if (!gameState) return false;
-
- if (gameState.phase === 'composing') {
+ if (!gameState) {
+ console.error(`Cannot advance phase: no game state for room ${roomId}`);
+ return false;
+ }
+
+ const currentPhase = gameState.phase;
+ console.log(`Advancing phase for room ${roomId} from ${currentPhase}`);
+
+ if (currentPhase === 'composing') {
gameState.phase = 'guessing';
gameState.roundStartTime = Date.now();
- return true;
- } else if (gameState.phase === 'guessing') {
+ console.log(`Room ${roomId} advanced to guessing phase`);
+ return { phase: 'guessing' };
+ }
+ else if (currentPhase === 'guessing') {
gameState.phase = 'results';
- return true;
- } else if (gameState.phase === 'results') {
+ console.log(`Room ${roomId} advanced to results phase`);
+ return { phase: 'results' };
+ }
+ else if (currentPhase === 'results') {
+ console.log(`Room ${roomId} starting new round from results phase`);
return startNewRound(roomId);
}
-
+
+ console.warn(`Cannot advance from unknown phase "${currentPhase}" in room ${roomId}`);
return false;
};
@@ -172,6 +184,12 @@ const cleanupGameState = (roomId) => {
delete gameStates[roomId];
};
+const getCurrentComposer = (roomId) => gameStates[roomId]?.currentComposer || null;
+
+const getGameState = (roomId) => {
+ return gameStates[roomId] || null;
+};
+
module.exports = {
initializeGameState,
startNewRound,
@@ -185,5 +203,7 @@ module.exports = {
getUserRole,
getSongOptions,
getSelectedSong,
- cleanupGameState
+ cleanupGameState,
+ getCurrentComposer,
+ getGameState
};
diff --git a/server/controller/room.js b/server/controller/room.js
index e1d209d..2c46cee 100644
--- a/server/controller/room.js
+++ b/server/controller/room.js
@@ -121,4 +121,15 @@ module.exports.validateRoomMembers = (io, roomId) => {
return validMembers;
}
-module.exports.setCleanupGameState = setCleanupGameState;
\ No newline at end of file
+module.exports.setCleanupGameState = setCleanupGameState;
+
+module.exports.getUserName = (userId) => {
+ for (const roomId in rooms) {
+ const room = rooms[roomId];
+ const member = room.members.find(m => m.id === userId);
+ if (member) {
+ return member.name;
+ }
+ }
+ return null;
+};
\ No newline at end of file
diff --git a/server/handler/connection.js b/server/handler/connection.js
index 96c1e32..85bd427 100644
--- a/server/handler/connection.js
+++ b/server/handler/connection.js
@@ -8,6 +8,7 @@ module.exports = (io) => (socket) => {
const clearRoomTimers = (roomId) => {
if (phaseTimers[roomId]) {
+ console.log(`Clearing timer for room ${roomId}`);
clearTimeout(phaseTimers[roomId]);
delete phaseTimers[roomId];
}
@@ -16,21 +17,33 @@ module.exports = (io) => (socket) => {
const startPhaseTimer = (roomId) => {
clearRoomTimers(roomId);
+ const gameState = gameController.getGameState(roomId);
+ if (!gameState) {
+ console.error(`Cannot start timer: no game state for room ${roomId}`);
+ return;
+ }
+
const timeRemaining = gameController.getTimeRemaining(roomId) * 1000;
+ console.log(`Starting ${gameState.phase} phase timer for room ${roomId} with ${timeRemaining}ms`);
phaseTimers[roomId] = setTimeout(() => {
+ console.log(`Timer expired for room ${roomId}, advancing phase from ${gameState.phase}`);
const advanced = gameController.advancePhase(roomId);
- if (!advanced) return;
+ if (!advanced) {
+ console.log(`Failed to advance phase for room ${roomId}`);
+ return;
+ }
if (typeof advanced === 'boolean') {
handleRoundStart(roomId);
} else {
- const currentPhase = advanced.phase || 'results';
+ const newPhase = gameController.getGameState(roomId).phase;
+ console.log(`Advanced to ${newPhase} phase in room ${roomId}`);
- if (currentPhase === 'guessing') {
+ if (newPhase === 'guessing') {
handleGuessingPhaseStart(roomId);
- } else if (currentPhase === 'results') {
+ } else if (newPhase === 'results') {
handleResultsPhaseStart(roomId);
}
}
@@ -59,10 +72,20 @@ module.exports = (io) => (socket) => {
};
const handleGuessingPhaseStart = (roomId) => {
+ const gameState = gameController.getGameState(roomId);
+ if (!gameState) return;
+
const roles = gameController.getRoles(roomId);
const songOptions = gameController.getSongOptions(roomId);
const timeLeft = gameController.getTimeRemaining(roomId);
+ console.log(`Starting guessing phase for room ${roomId} with ${Object.keys(roles).length} players`);
+
+ io.to(roomId).emit('phase-changed', {
+ phase: 'guessing',
+ timeRemaining: timeLeft
+ });
+
Object.entries(roles).forEach(([userId, role]) => {
if (role === 'guesser') {
io.to(userId).emit('guessing-phase-started', {
@@ -71,7 +94,7 @@ module.exports = (io) => (socket) => {
});
}
});
-
+
startPhaseTimer(roomId);
};
@@ -186,8 +209,28 @@ module.exports = (io) => (socket) => {
const roomId = roomController.getUserRoom(socket.id);
if (!roomId) return;
+ console.log(`User ${socket.id} submitted guess: Song ID ${songId}`);
+
+ const gamePhase = gameController.getGameState(roomId)?.phase;
+ if (gamePhase !== 'guessing') {
+ console.log(`Ignoring guess: room ${roomId} is in ${gamePhase} phase, not guessing`);
+ return;
+ }
+
const result = gameController.submitGuess(roomId, socket.id, songId);
- if (result) socket.emit("guess-result", result);
+ if (result) {
+ console.log(`Guess result for ${socket.id}:`, result);
+ socket.emit("guess-result", result);
+
+ const currentComposer = gameController.getCurrentComposer(roomId);
+ if (currentComposer) {
+ const guesserName = roomController.getUserName(socket.id) || "Someone";
+ io.to(currentComposer).emit("player-guessed", {
+ guesserName,
+ isCorrect: result.isCorrect
+ });
+ }
+ }
});
socket.on("next-round", () => {