Add client leaving host notification

This commit is contained in:
Joey Yakimowich-Payne 2026-01-19 15:46:17 -07:00
commit 3c54a0f4d9
No known key found for this signature in database
GPG key ID: DDF6AF5B21B407D4
3 changed files with 164 additions and 0 deletions

View file

@ -966,6 +966,23 @@ export const useGame = () => {
showScoreboard();
}
}
if (data.type === 'LEAVE') {
const playerId = conn.peer;
const updatedPlayers = playersRef.current.filter(p => p.id !== playerId);
playersRef.current = updatedPlayers;
setPlayers(updatedPlayers);
connectionsRef.current.delete(playerId);
if (presenterIdRef.current === playerId) {
const realPlayers = updatedPlayers.filter(p => p.id !== 'host');
const newPresenter = realPlayers.length > 0 ? realPlayers[0].id : null;
setPresenterId(newPresenter);
broadcast({ type: 'PRESENTER_CHANGED', payload: { presenterId: newPresenter } });
}
broadcast({ type: 'PLAYER_LEFT', payload: { playerId } });
}
};
useEffect(() => {
@ -1587,6 +1604,7 @@ export const useGame = () => {
if (role !== 'CLIENT') return;
if (hostConnectionRef.current?.open) {
hostConnectionRef.current.send({ type: 'LEAVE', payload: {} });
hostConnectionRef.current.close();
}
hostConnectionRef.current = null;