duel tetris fonctionnel mais galere a tester en solo

This commit is contained in:
2026-02-19 16:45:54 +01:00
parent 0f69f4fb6f
commit a4210af235
3 changed files with 42 additions and 7 deletions
@@ -482,6 +482,17 @@ function setupSocketIO(io)
_tetrisRelayToOpponent(socket, 'tetris:lines-cleared', data);
});
// start-duel → relayé aux DEUX joueurs de la room (inclut l'émetteur)
socket.on('tetris:start-duel', () => {
const code = socket.tetrisRoomCode;
if (!code) return;
const room = tetrisRooms.get(code);
if (!room || room.size < 2) return;
for (const s of room.values()) {
s.emit('tetris:start-duel');
}
});
// game-over → relayé en opponent-game-over chez l'adversaire
socket.on('tetris:game-over', (data) => {
_tetrisRelayToOpponent(socket, 'tetris:opponent-game-over', data);
+11 -1
View File
@@ -3,10 +3,11 @@
// ─────────────────────────────────────────────
class Duel {
constructor(socket, tetrisGame, onStatusChange) {
constructor(socket, tetrisGame, onStatusChange, onStart) {
this.socket = socket;
this.tetrisGame = tetrisGame;
this.onStatusChange = onStatusChange; // (status, opponentName) => void
this.onStart = onStart; // () => void — déclenche le début du jeu local
this.action_queue = [];
this.opponentGrid = this._emptyGrid();
@@ -24,6 +25,11 @@ class Duel {
this.socket.emit('tetris:join', { roomCode });
}
startDuel() {
if (!this.isReady) return;
this.socket.emit('tetris:start-duel');
}
leave() {
if (!this.roomCode) return;
this.socket.emit('tetris:leave');
@@ -112,6 +118,10 @@ class Duel {
this.socket.on('tetris:opponent-game-over', (data) => {
this.action_queue.push({ type: 'OPPONENT_GAME_OVER', score: data.score });
});
this.socket.on('tetris:start-duel', () => {
if (this.onStart) this.onStart();
});
}
// ─── Utilitaires ─────────────────────────
+20 -6
View File
@@ -41,7 +41,13 @@ function hideOverlay() {
// SOCKET + DUEL
// ─────────────────────────────────────────────
const socket = io({ auth: { token: localStorage.getItem('token') } });
const socket = io({
auth: { token: localStorage.getItem('auth_token') },
reconnection: true,
reconnectionAttempts: 5,
reconnectionDelay: 1000,
transports: ['websocket', 'polling']
});
let duel = null;
@@ -63,11 +69,18 @@ function updateDuelStatus(status, opponentName) {
}
}
function startLocalGame() {
hideOverlay();
game.start();
updateButtons();
render();
}
btnJoinDuel.addEventListener('click', () => {
const code = inputRoomCode.value.trim().toUpperCase();
if (!code) return;
if (duel) { duel.leave(); }
duel = new Duel(socket, game, updateDuelStatus);
duel = new Duel(socket, game, updateDuelStatus, startLocalGame);
duel.join(code);
btnJoinDuel.disabled = true;
btnLeaveDuel.disabled = false;
@@ -112,10 +125,11 @@ const game = new Tetris(
);
btnStart.addEventListener('click', () => {
hideOverlay();
game.start();
updateButtons();
render();
if (duel && duel.isReady) {
duel.startDuel(); // déclenche les deux parties via le serveur
} else {
startLocalGame(); // solo
}
});
btnPause.addEventListener('click', () => {