toggle automatic pioche

This commit is contained in:
Didictateur 2025-11-23 01:48:50 +01:00
parent 7188c57215
commit a73e07f510
2 changed files with 64 additions and 3 deletions

View file

@ -19,7 +19,14 @@
<div>Room: <strong id="roomId">-</strong></div>
<div>Status: <strong id="status">-</strong></div>
<div>Joueurs: <strong id="playerCount">0/2</strong></div>
<div><span id="youAreHost" style="display:none;font-weight:bold">Vous êtes l'hôte</span></div>
<div id="youAreHost" style="display:none;color:green;font-weight:bold;">(Vous êtes l'hôte)</div>
<div style="margin-top:8px">
<label id="autoDrawLabel" style="display:none;align-items:center;gap:8px;">
<input type="checkbox" id="autoDrawToggle" />
Pioche automatique
</label>
</div>
<div style="margin-top:8px">
<button id="startBtn" style="display:none" class="btn-cta">Commencer la partie</button>
@ -45,6 +52,8 @@
let roomHostId = null;
const hostLabelEl = document.getElementById('hostLabel');
const youAreHostEl = document.getElementById('youAreHost');
const autoDrawLabelEl = document.getElementById('autoDrawLabel');
const autoDrawToggleEl = document.getElementById('autoDrawToggle');
function log(...args){ console.log(...args); }
@ -91,6 +100,20 @@
startBtn.style.display = 'none';
}
}
// update auto-draw UI if server provides it
try{
if(typeof data.autoDraw !== 'undefined' && autoDrawLabelEl && autoDrawToggleEl){
// show the control only to the host
if(myPlayerId && roomHostId && myPlayerId === roomHostId){
autoDrawLabelEl.style.display = 'inline-flex';
autoDrawToggleEl.checked = !!data.autoDraw;
autoDrawToggleEl.disabled = false;
} else {
// hide control for non-hosts
autoDrawLabelEl.style.display = 'none';
}
}
}catch(_){ }
});
// register game started handler early so we don't miss the broadcast
@ -129,6 +152,17 @@
}
// after join, fetch room state to avoid missing a recent 'playing' state
fetch(`/rooms/${encodeURIComponent(roomId)}`).then(r => r.json()).then(roomInfo => {
try{
if(roomInfo && typeof roomInfo.autoDraw !== 'undefined'){
if(myPlayerId && roomHostId && myPlayerId === roomHostId){
// host: show control and set state
if(autoDrawLabelEl && autoDrawToggleEl){ autoDrawLabelEl.style.display = 'inline-flex'; autoDrawToggleEl.checked = !!roomInfo.autoDraw; autoDrawToggleEl.disabled = false; }
} else {
// non-host: hide control
if(autoDrawLabelEl) autoDrawLabelEl.style.display = 'none';
}
}
}catch(_){ }
if(roomInfo && roomInfo.status === 'playing'){
// server already marked room as playing; redirect immediately
if(myPlayerId){
@ -152,6 +186,18 @@
});
});
// Auto-draw toggle (host only)
if(autoDrawToggleEl){
autoDrawToggleEl.addEventListener('change', (ev)=>{
if(!socket) return;
const enabled = !!autoDrawToggleEl.checked;
socket.emit('room:auto_draw:set', { roomId, enabled }, (resp)=>{
if(resp && resp.error){ log('auto-draw error', resp); alert(resp.error); return; }
log('auto-draw updated', resp);
});
});
}
// Copy invite link (hidden link, no display)
document.getElementById('copyInviteBtn').addEventListener('click', async ()=>{
const link = `${window.location.origin}/waiting.html?roomId=${encodeURIComponent(roomId)}`;

View file

@ -27,6 +27,7 @@ function sendRoomUpdate(room){
hostId: room.hostId,
size: (room.boardState && room.boardState.width) || room.size,
cards: Object.keys(room.cards || {}),
autoDraw: !!room.autoDraw,
deckCount: (room.deck && room.deck.length) || 0,
discardCount: (room.discard && room.discard.length) || 0
};
@ -624,7 +625,7 @@ app.post('/rooms', (req, res) => {
// hostId will be set when the first player joins
// initialize a deck for this room
const deck = buildDefaultDeck();
rooms.set(id, { id, boardState, players: [], status: 'waiting', hostId: null, size, cards: {}, playedCards: [], removalTimers: new Map(), deck, hands: {} });
rooms.set(id, { id, boardState, players: [], status: 'waiting', hostId: null, size, cards: {}, playedCards: [], removalTimers: new Map(), deck, hands: {}, autoDraw: false });
res.json({ roomId: id, size });
});
@ -633,7 +634,7 @@ app.get('/rooms/:id', (req, res) => {
if (!room) return res.status(404).json({ error: 'room not found' });
const boardState = room.boardState || null;
const size = (room.boardState && room.boardState.width) || room.size;
res.json({ id: room.id, boardState, size: size, players: room.players.map(p => ({ id: p.id, color: p.color })), status: room.status, hostId: room.hostId, cards: Object.keys(room.cards || {}) });
res.json({ id: room.id, boardState, size: size, players: room.players.map(p => ({ id: p.id, color: p.color })), status: room.status, hostId: room.hostId, cards: Object.keys(room.cards || {}), autoDraw: !!room.autoDraw });
});
io.on('connection', (socket) => {
@ -692,6 +693,20 @@ io.on('connection', (socket) => {
cb && cb({ ok: true, color, roomId, playerId: assignedId, hostId: room.hostId });
});
// Host can toggle automatic drawing in the waiting room. Only the host may change this setting.
socket.on('room:auto_draw:set', ({ roomId, enabled }, cb) => {
const room = rooms.get(roomId);
if(!room) return cb && cb({ error: 'room not found' });
const sender = socket.data.playerId;
if(!sender) return cb && cb({ error: 'not joined' });
if(room.hostId !== sender) return cb && cb({ error: 'only the host can change auto-draw' });
room.autoDraw = !!enabled;
// Broadcast updated room state to all participants
try{ sendRoomUpdate(room); }catch(_){ }
try{ io.to(room.id).emit('room:auto_draw:changed', { roomId: room.id, enabled: room.autoDraw }); }catch(_){ }
return cb && cb({ ok: true, autoDraw: room.autoDraw });
});
socket.on('game:move', ({ roomId, from, to, promotion }, cb) => {
const room = rooms.get(roomId);
if (!room) return cb && cb({ error: 'room not found' });