feat(multidispositivo): reconnection, impostor awareness and protocol fixes

Fixes
- SnapshotPartidaOnline no longer serializes esImpostor unless the game is
  over. Every phase change was broadcasting the impostor roles in clear to
  all clients.
- The impostor clue travels in its own field and only when the game enables
  it. It used to be sent as the category key ("todas" when no category was
  picked) and was overwritten by each phase snapshot.
- Eliminated players can no longer vote nor be voted for, on both the client
  screen and the host listener.
- Nearby listeners are removed on dispose; votes are no longer dispatched
  twice; votacionResultado no longer triggers two navigations.
- _jugadoresHostControlados handed the secret word to the host's own
  impostors in the payload.
- Impostor cap unified as maxImpostoresPara(n) for both modes, and the
  silent clamp in multi-device now reports the effective number.
- palabraAleatoria uses Random.secure.

Impostors know each other
- ConfigPartida.impostoresSeConocen, on by default. companerosImpostores is
  nullable: null means nothing to show, an empty list means "you are the
  only impostor".

Reconnection
- IdentidadDispositivo persists a stable device id; the host uses it as the
  clientId so a phone that drops and returns is the same client. Falls back
  to the endpointId for clients that do not send one.
- Usuario.absorbidoDe records the original owner when the host takes over a
  disconnected player, and they are handed back automatically on return.
- New solicitarResync/resync messages: the host replies with the current
  phase plus that client's own players, and the client jumps straight to the
  running phase.
- Readiness is keyed by clientId instead of endpointId, and a disconnected
  client no longer blocks the round.
- The retry gives up after three minutes, only reconnects to the host it
  belonged to, and is cancelled correctly while shutting down.

Per-word clue support
- The word bank loader accepts both the old list-of-strings format and the
  new one carrying a clue per word, falling back to the category clue.

Not verified on real devices: Nearby reconnection timing still needs two
Android phones.
This commit is contained in:
2026-07-25 20:36:32 +02:00
parent bad641653c
commit 863690168c
23 changed files with 1284 additions and 235 deletions
+37
View File
@@ -154,10 +154,24 @@ class EstadoSalaMultijugador {
}
ResultadoOperacionSala registrarCliente(ClienteSala cliente) {
final existente = clientes[cliente.clientId];
if (existente != null) {
// Reconexión: el clientId es estable, el endpointId no. Conservamos lo
// que ya sabíamos del cliente y solo refrescamos por dónde se le habla.
clientes[cliente.clientId] = existente.copiar(
endpointId: cliente.endpointId,
nombre: cliente.nombre,
conectado: true,
);
return const ResultadoOperacionSala.ok();
}
clientes[cliente.clientId] = cliente;
return const ResultadoOperacionSala.ok();
}
/// True si ese cliente ya estuvo en la sala y vuelve tras una caída.
bool esReconexion(String clientId) => clientes.containsKey(clientId);
ResultadoOperacionSala crearUsuario(Usuario usuario) {
if (fase != FaseSalaMultijugador.lobby) {
return const ResultadoOperacionSala.error('sala_cerrada');
@@ -274,6 +288,8 @@ class EstadoSalaMultijugador {
if (entry.value.clienteIdSeleccionado == clientIdOrigen) {
usuarios[entry.key] = entry.value.copiar(
clienteIdSeleccionado: clientIdDestino,
// Se recuerda de quién eran para poder devolvérselos si vuelve.
absorbidoDe: entry.value.absorbidoDe ?? clientIdOrigen,
);
reasignados++;
}
@@ -281,6 +297,27 @@ class EstadoSalaMultijugador {
return reasignados;
}
/// Usuarios que el host absorbió de un cliente concreto.
List<Usuario> usuariosAbsorbidosDe(String clientId) => usuarios.values
.where((usuario) => usuario.absorbidoDe == clientId)
.toList();
/// Devuelve a su dueño original los usuarios que el host había absorbido.
/// Se usa cuando ese dispositivo se reconecta.
int devolverUsuariosAbsorbidos(String clientId) {
if (!clientes.containsKey(clientId)) return 0;
var devueltos = 0;
for (final entry in usuarios.entries.toList()) {
if (entry.value.absorbidoDe != clientId) continue;
usuarios[entry.key] = entry.value.copiar(
clienteIdSeleccionado: clientId,
limpiarAbsorbidoDe: true,
);
devueltos++;
}
return devueltos;
}
ResultadoOperacionSala validarInicio() {
if (fase != FaseSalaMultijugador.lobby) {
return const ResultadoOperacionSala.error('sala_cerrada');