rajout de polling

This commit is contained in:
Didictateur 2026-02-25 18:25:06 +01:00
parent c6aa103a7f
commit b9901c133c
2 changed files with 59 additions and 28 deletions

View file

@ -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)

View file

@ -141,43 +141,20 @@
.then(html => document.getElementById('menu').innerHTML = html);
// Afficher la mascotte et le texte
const mascolteEl = document.getElementById('mascotte');
mascolteEl.innerHTML = '<img src="/img/tilineau/idle.png" alt="Tilineau">';
const mascotteEl = document.getElementById('mascotte');
mascotteEl.innerHTML = '<img src="/img/tilineau/idle.png" alt="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')
? '<img class="explaining" src="/img/tilineau/explaining.png" alt="Tilineau">'
: '<img src="/img/tilineau/idle.png" alt="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);
});
}
});
</script>
</body>
</html>