From 6f24cfad30f27b6ee80346bf29a6b3ebe45f3e5d Mon Sep 17 00:00:00 2001 From: Josue Zamudio Date: Fri, 20 Mar 2026 18:38:33 +0000 Subject: [PATCH] Add unready toggle in lobby - Server: Handle unready event to set player ready state to false - Client: Ready button toggles between READY and UNREADY - Client: Unready sends unready socket event to server Co-Authored-By: Claude Opus 4.6 --- public/index.html | 4 ++-- public/js/network.js | 5 +++++ public/js/ui.js | 12 +++++++++--- server/index.js | 23 +++++++++++++++++------ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/public/index.html b/public/index.html index fc10e87..7abe997 100644 --- a/public/index.html +++ b/public/index.html @@ -62,10 +62,10 @@ - + - + diff --git a/public/js/network.js b/public/js/network.js index 241b47c..0a866be 100644 --- a/public/js/network.js +++ b/public/js/network.js @@ -110,6 +110,11 @@ class NetworkManager { this.socket.emit('ready'); } + unready() { + if (!this.socket) return; + this.socket.emit('unready'); + } + sendMove(direction) { if (!this.socket || !this.currentPlayerId) return; this.socket.emit('player-move', { playerId: this.currentPlayerId, direction }); diff --git a/public/js/ui.js b/public/js/ui.js index aec214d..cd4178c 100644 --- a/public/js/ui.js +++ b/public/js/ui.js @@ -62,9 +62,15 @@ class UIManager { } handleReady() { - network.ready(); - this.buttons.ready.textContent = 'READY!'; - this.buttons.ready.disabled = true; + if (this.buttons.ready.textContent === 'READY') { + network.ready(); + this.buttons.ready.textContent = 'UNREADY'; + this.buttons.ready.disabled = false; + } else { + network.unready(); + this.buttons.ready.textContent = 'READY'; + this.buttons.ready.disabled = false; + } } handleLeave() { diff --git a/server/index.js b/server/index.js index 75a69ca..d420c11 100644 --- a/server/index.js +++ b/server/index.js @@ -118,6 +118,19 @@ io.on('connection', (socket) => { } }); + socket.on('unready', () => { + const player = lobby.players.get(socket.id); + if (!player) return; + + player.ready = false; + + // Broadcast only to players in the lobby room + io.to(LOBBY_ROOM).emit('player-joined', { + player: { id: player.id, name: player.name, ready: player.ready }, + players: getPlayersList() + }); + }); + socket.on('player-move', ({ playerId, direction }) => { if (!lobby.gameStarted) return; @@ -367,14 +380,12 @@ function clearRows(player) { } function sendGarbage(sender, rowsCleared) { - const garbageRows = rowsCleared >= 4 ? rowsCleared : Math.max(1, rowsCleared - 1); + // Number of garbage rows equals number of lines cleared const opponents = Array.from(lobby.players.values()).filter(p => p.id !== sender.id && !p.eliminated); if (opponents.length === 0) return; - // Send all garbage rows to each opponent - for (const opponent of opponents) { - for (let i = 0; i < garbageRows; i++) { - addGarbageToPlayer(opponent); - } + for (let i = 0; i < rowsCleared; i++) { + const target = opponents[Math.floor(Math.random() * opponents.length)]; + addGarbageToPlayer(target); } }