File size: 6,607 Bytes
811126d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
"use client";
import { useState, useEffect, useRef } from "react";
import { useParams, useRouter } from "next/navigation";
import { Button } from "@/components/ui/button";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import { ArrowLeft, Mic, MicOff } from "lucide-react";
import Link from "next/link";
import { useGameSocket } from "@/hooks/use-game-socket";
import "../../styles/background-pattern.css";
interface Player {
id: number;
name: string;
avatar: string;
isReady: boolean;
isSpeaking: boolean;
}
interface GameState {
id: number;
status: "PLAYING" | "FINISHED";
players: Player[];
currentRound: number;
totalRounds: number;
currentSpeaker?: Player;
imposteur?: Player;
}
export default function GamePage() {
const params = useParams();
const router = useRouter();
const [gameState, setGameState] = useState<GameState | null>(null);
const [isMuted, setIsMuted] = useState(true);
const [currentUserId, setCurrentUserId] = useState<number | null>(null);
const audioRef = useRef<HTMLAudioElement>(null);
const { startCall, handleIncomingAudio, socket } = useGameSocket(
params.inviteCode as string
);
useEffect(() => {
// Récupérer l'ID de l'utilisateur depuis les cookies
const userId = document.cookie.match(/userId=(\d+)/)?.[1];
if (!userId) {
router.push("/");
return;
}
setCurrentUserId(parseInt(userId));
// Récupérer l'état initial du jeu
fetchGameState();
}, []);
const fetchGameState = async () => {
try {
const response = await fetch(`/api/group/${params.inviteCode}`);
if (!response.ok)
throw new Error("Erreur lors de la récupération du jeu");
const data = await response.json();
setGameState(data);
} catch (error) {
console.error("Erreur:", error);
alert("Impossible de récupérer l'état du jeu");
}
};
const toggleMicrophone = async () => {
try {
if (isMuted) {
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
});
await startCall(stream);
} else {
// Arrêter le flux audio
const audioTracks = audioRef.current?.srcObject as MediaStream;
audioTracks?.getTracks().forEach((track) => track.stop());
}
setIsMuted(!isMuted);
} catch (error) {
console.error("Erreur lors de l'accès au microphone:", error);
alert("Impossible d'accéder au microphone");
}
};
useEffect(() => {
if (!socket) return;
// Gérer les événements de jeu
socket.on("gameStateUpdate", (newState: GameState) => {
setGameState(newState);
});
// Gérer l'audio entrant
handleIncomingAudio((stream) => {
if (audioRef.current) {
audioRef.current.srcObject = stream;
audioRef.current.play().catch(console.error);
}
});
return () => {
socket.off("gameStateUpdate");
};
}, [socket]);
if (!gameState) {
return (
<div className="min-h-screen bg-gray-100 flex items-center justify-center">
<div className="text-xl">Chargement...</div>
</div>
);
}
const currentPlayer = currentUserId
? gameState.players.find((p) => p.id === currentUserId)
: null;
const isImposteur = currentPlayer?.id === gameState.imposteur?.id;
return (
<div className="min-h-screen relative overflow-hidden">
<div className="background-pattern" />
<div className="background-overlay" />
<div className="background-vignette" />
<audio ref={audioRef} autoPlay />
<div className="relative z-10 p-4">
<div className="max-w-6xl mx-auto bg-gray-100 bg-opacity-90 backdrop-blur-sm rounded-lg shadow-xl p-8">
<div className="flex justify-between items-center mb-8">
<Link href="/">
<Button
variant="outline"
className="border-orange-400 text-orange-600 hover:bg-orange-100"
>
<ArrowLeft className="mr-2 h-4 w-4" />
Quitter la partie
</Button>
</Link>
<div className="text-center">
<h1 className="text-4xl font-grobold font-normal text-orange-600 mb-2">
Round {gameState.currentRound}/{gameState.totalRounds}
</h1>
{isImposteur && (
<div className="text-red-500 font-bold">
Vous êtes l'imposteur !
</div>
)}
</div>
<Button
onClick={toggleMicrophone}
className={`${
isMuted
? "bg-red-500 hover:bg-red-600"
: "bg-green-500 hover:bg-green-600"
} text-white`}
>
{isMuted ? (
<>
<MicOff className="mr-2 h-5 w-5" />
Micro désactivé
</>
) : (
<>
<Mic className="mr-2 h-5 w-5" />
Micro activé
</>
)}
</Button>
</div>
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-6 mb-8">
{gameState.players.map((player) => (
<div
key={player.id}
className={`flex items-center space-x-4 p-4 rounded-lg ${
player.isSpeaking ? "bg-green-200" : "bg-gray-200"
}`}
>
<Avatar className="w-16 h-16 border-4 border-orange-400">
<AvatarImage src={player.avatar} alt={player.name} />
<AvatarFallback>{player.name[0]}</AvatarFallback>
</Avatar>
<div>
<p className="font-grobold font-normal text-orange-800">
{player.name}
{player.id === currentUserId && " (Vous)"}
</p>
<p
className={`text-${
player.isSpeaking ? "green" : "gray"
}-600`}
>
{player.isSpeaking ? "Parle" : "Silencieux"}
</p>
</div>
</div>
))}
</div>
{gameState.currentSpeaker && (
<div className="text-center text-2xl font-grobold font-normal text-orange-600">
Au tour de {gameState.currentSpeaker.name} de parler !
</div>
)}
</div>
</div>
</div>
);
}
|