Skip to content
Snippets Groups Projects
Commit ef698fd7 authored by De Brettes Thomas's avatar De Brettes Thomas
Browse files

Timer

parent 1baad40a
Branches
No related tags found
No related merge requests found
......@@ -26,7 +26,16 @@ app.use(express.static(path.join(__dirname, 'htdocs')));
http.listen(port , ()=>{
console.log("Listening on http://localhost${port}/");
});
let rooms = [];
let gameState = {}
io.on('connection', (socket) => {
console.log(`[connection] ${socket.id}`);
......@@ -54,7 +63,13 @@ io.on('connection', (socket) => {
io.to(socket.id).emit('join room', room.id);
if (room.players.length === 2) {
gameState = {
player1: { time: 300, isTurn: false }, // 5 minutes pour chaque joueur
player2: { time: 300, isTurn: false },
};
io.to(room.id).emit('start game', room.players);
gameState.player1.isTurn = true
room.timer = setInterval(() => updateTimers(gameState, room.id), 1000);
}
});
......@@ -64,6 +79,19 @@ io.on('connection', (socket) => {
socket.on('play', (data) => {
console.log(`[Server] Play reçu : ${JSON.stringify(data)}`); // Debugging
// Change le tour du joueur
if (gameState.player1.isTurn) {
gameState.player1.isTurn = false;
gameState.player2.isTurn = true;
} else {
gameState.player1.isTurn = true;
gameState.player2.isTurn = false;
}
// Diffuser l'update des timers et du tour aux clients
io.to(data.roomId).emit('update-timer', gameState); // Diffuser les timers mis à jour
socket.broadcast.emit('play', data); // On diffuse à tous les clients
});
socket.on('roque', (data) => {
......@@ -115,3 +143,26 @@ function createRoom(player) {
function roomId() {
return Math.random().toString(36).substr(2, 9);
}
// Fonction qui met à jour les timers toutes les secondes
function updateTimers(gameState) {
// Si c'est le tour du joueur 1, décrémenter son temps
if (gameState.player1.isTurn) {
gameState.player1.time--;
io.emit('update-timer', gameState); // Diffuser l'état des timers aux clients
}
// Si c'est le tour du joueur 2, décrémenter son temps
if (gameState.player2.isTurn) {
gameState.player2.time--;
io.emit('update-timer', gameState);
}
// Si un joueur atteint 0 seconde, fin du jeu ou message d'alerte
if (gameState.player1.time <= 0 || gameState.player2.time <= 0) {
io.emit('game-over', 'Temps écoulé !');
}
}
// Mettre à jour chaque seconde
.board.black-perspective {
transform: rotate(180deg);
transform-origin: center;
}
.btn-primary {
background-color: rgb(243, 8, 149) !important; /* Change la couleur des boutons primaires */
border-radius: 10px; /* Arrondi les bords */
}
body {
font-family: 'Libre Baskerville', serif; /* Utilisation de la police de style rétro */
background-image: url('fond-ecran.jpg') !important; /* Remplace par le nom de ton fichier */
background-size: cover !important; /* Ajuste l'image pour qu'elle couvre tout l'écran */
background-position: center !important; /* Centre l'image */
background-repeat: no-repeat !important; /* Empêche la répétition */
background-color:rgb(0, 0, 0)
}
#echec-message {
font-family: 'Anton', sans-serif;
font-weight: bold;
font-size: 2rem; /* Taille du texte pour bien le voir */
color: red; /* Ou une autre couleur qui attire l'attention */
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment