From b9901c133c1de5e2f6fc0493ce47fb7f1fde4e59 Mon Sep 17 00:00:00 2001 From: Didictateur Date: Wed, 25 Feb 2026 18:25:06 +0100 Subject: [PATCH] rajout de polling --- backend/src/game/game.py | 9 ++++ frontend/pages/riichi/chap5.html | 78 ++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 28 deletions(-) diff --git a/backend/src/game/game.py b/backend/src/game/game.py index e1fa00f..b451b70 100644 --- a/backend/src/game/game.py +++ b/backend/src/game/game.py @@ -15,6 +15,9 @@ class Game: self.discards_bots = [[] for _ in range(3)] self.hidden_discards_bots = [[] for _ in range(3)] + self.has_draw = False + self.has_played = True + self.scores = [25000] * 4 self.winds = [ @@ -57,6 +60,12 @@ class Game: self.discards_bots = [[] for _ in range(3)] self.hidden_discards_bots = [[] for _ in range(3)] + def play_draw(self): + if self.turn == 0: + self.hand_player.draw(self.wall.draw()) + self.has_draw = True + print(f"player {self.turn} has draw") + def draw_tile(self, hand: Hand): tile = self.wall.draw() hand.draw_tile(tile) diff --git a/frontend/pages/riichi/chap5.html b/frontend/pages/riichi/chap5.html index 2899168..373aace 100644 --- a/frontend/pages/riichi/chap5.html +++ b/frontend/pages/riichi/chap5.html @@ -141,43 +141,20 @@ .then(html => document.getElementById('menu').innerHTML = html); // Afficher la mascotte et le texte - const mascolteEl = document.getElementById('mascotte'); - mascolteEl.innerHTML = 'Tilineau'; + const mascotteEl = document.getElementById('mascotte'); + mascotteEl.innerHTML = 'Tilineau'; document.getElementById('speech-text').innerHTML = texts.riichi.chap5; // Toggle bulle de dialogue au clic sur la mascotte const speechBubble = document.querySelector('.speech-bubble'); - mascolteEl.addEventListener('click', () => { + mascotteEl.addEventListener('click', () => { speechBubble.classList.toggle('visible'); - mascolteEl.innerHTML = speechBubble.classList.contains('visible') + mascotteEl.innerHTML = speechBubble.classList.contains('visible') ? 'Tilineau' : 'Tilineau'; }); - // Initialiser une partie au chargement du chapitre 5 - fetch('/api/game/start', { method: 'POST' }) - .then(r => r.json()) - .then(data => { - console.log('Réponse API', data); - if (data.state && data.state.hand_player) { - displayHand(data.state.hand_player, 'hand-0'); - } - // Affichage des mains bots (1, 2, 3) - [1, 2, 3].forEach(id => { - fetch(`/api/game/hand/${id}`) - .then(r => r.json()) - .then(botData => { - if (botData.hand) { - displayHand(botData.hand, `hand-${id}`); - } else { - console.error(`Main vide pour joueur ${id}`); - } - }) - .catch(e => console.error(`Erreur main ${id}`, e)); - }); - }) - .catch(e => console.error('Erreur initialisation partie', e)); - + // Fonction d'affichage d'une main function displayHand(hand, displayId) { const display = document.getElementById(displayId); display.innerHTML = ''; @@ -192,6 +169,51 @@ display.appendChild(tileDiv); }); } + + // Polling pour récupérer la main d'un bot + function fetchBotHandWithRetry(id, tries = 8, delay = 300) { + fetch(`/api/game/hand/${id}`) + .then(r => r.json()) + .then(botData => { + if (botData.hand) { + displayHand(botData.hand, `hand-${id}`); + } else if (tries > 0) { + setTimeout(() => fetchBotHandWithRetry(id, tries - 1, delay), delay); + } else { + console.error(`Main vide pour joueur ${id}`); + } + }) + .catch(e => { + if (tries > 0) { + setTimeout(() => fetchBotHandWithRetry(id, tries - 1, delay), delay); + } else { + console.error(`Erreur main ${id}`, e); + } + }); + } + + // Initialiser une partie au chargement du chapitre 5 + fetch('/api/game/start', { method: 'POST' }) + .then(r => r.json()) + .then(data => { + console.log('Réponse API', data); + // Si has_draw est False, on appelle play_draw et on attend le reload + if (data.state && data.state.has_draw === false) { + fetch('/api/play_draw', { method: 'POST' }) + .then(() => { + location.reload(); + }); + return; + } + // Afficher la main du joueur + if (data.state && data.state.hand_player) { + displayHand(data.state.hand_player, 'hand-0'); + // Affichage des mains bots (1, 2, 3) avec polling + [1, 2, 3].forEach(id => { + fetchBotHandWithRetry(id); + }); + } + }); \ No newline at end of file