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:
@@ -42,6 +42,36 @@ class EstadoJuego extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Máximo de impostores admitido para un número de jugadores dado.
|
||||
/// Es la misma regla en modo un solo móvil y en multidispositivo.
|
||||
static int maxImpostoresPara(int numJugadores) =>
|
||||
(numJugadores ~/ 3).clamp(1, 4);
|
||||
|
||||
/// Asigna impostores con un generador seguro y reparte la palabra al resto.
|
||||
void _repartirRoles(
|
||||
List<Jugador> jugadores,
|
||||
ConfigPartida config,
|
||||
String palabra,
|
||||
) {
|
||||
final rng = Random.secure();
|
||||
final numImpostores = config.numImpostores.clamp(
|
||||
1,
|
||||
maxImpostoresPara(jugadores.length),
|
||||
);
|
||||
final impostoresElegidos = <int>{};
|
||||
while (impostoresElegidos.length < numImpostores) {
|
||||
impostoresElegidos.add(rng.nextInt(jugadores.length));
|
||||
}
|
||||
for (final i in impostoresElegidos) {
|
||||
jugadores[i].esImpostor = true;
|
||||
}
|
||||
for (final jugador in jugadores) {
|
||||
if (!jugador.esImpostor) {
|
||||
jugador.palabra = palabra;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Crea una nueva partida con la configuración dada y lista de jugadores
|
||||
void crearPartida({
|
||||
required ConfigPartida config,
|
||||
@@ -60,29 +90,14 @@ class EstadoJuego extends ChangeNotifier {
|
||||
return Jugador(id: 'j${e.key}', nombre: e.value);
|
||||
}).toList();
|
||||
|
||||
// Asignar impostores usando Random seguro (no predecible)
|
||||
final rng = Random.secure();
|
||||
final numImpostores = config.numImpostores.clamp(1, jugadores.length ~/ 3);
|
||||
final impostoresElegidos = <int>{};
|
||||
while (impostoresElegidos.length < numImpostores) {
|
||||
impostoresElegidos.add(rng.nextInt(jugadores.length));
|
||||
}
|
||||
for (final i in impostoresElegidos) {
|
||||
jugadores[i].esImpostor = true;
|
||||
}
|
||||
|
||||
// Asignar palabras
|
||||
for (final j in jugadores) {
|
||||
if (!j.esImpostor) {
|
||||
j.palabra = palabra;
|
||||
}
|
||||
}
|
||||
_repartirRoles(jugadores, config, palabra);
|
||||
|
||||
_partida = Partida(
|
||||
config: config,
|
||||
jugadores: jugadores,
|
||||
palabraSecreta: palabra,
|
||||
categoriaReal: categoriaReal,
|
||||
pistaImpostor: _banco!.pistaDePalabra(palabra, categoria: categoriaReal),
|
||||
);
|
||||
|
||||
_votos.clear();
|
||||
@@ -117,27 +132,14 @@ class EstadoJuego extends ChangeNotifier {
|
||||
);
|
||||
}).toList();
|
||||
|
||||
final rng = Random.secure();
|
||||
final numImpostores = config.numImpostores.clamp(1, jugadores.length ~/ 3);
|
||||
final impostoresElegidos = <int>{};
|
||||
while (impostoresElegidos.length < numImpostores) {
|
||||
impostoresElegidos.add(rng.nextInt(jugadores.length));
|
||||
}
|
||||
for (final i in impostoresElegidos) {
|
||||
jugadores[i].esImpostor = true;
|
||||
}
|
||||
|
||||
for (final jugador in jugadores) {
|
||||
if (!jugador.esImpostor) {
|
||||
jugador.palabra = palabra;
|
||||
}
|
||||
}
|
||||
_repartirRoles(jugadores, config, palabra);
|
||||
|
||||
_partida = Partida(
|
||||
config: config,
|
||||
jugadores: jugadores,
|
||||
palabraSecreta: palabra,
|
||||
categoriaReal: categoriaReal,
|
||||
pistaImpostor: _banco!.pistaDePalabra(palabra, categoria: categoriaReal),
|
||||
);
|
||||
|
||||
_votos.clear();
|
||||
@@ -145,6 +147,22 @@ class EstadoJuego extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Nombres del resto de impostores para un jugador dado.
|
||||
///
|
||||
/// Devuelve `null` cuando no hay nada que mostrar (el jugador no es impostor
|
||||
/// o la partida no permite que se conozcan) y una lista —posiblemente vacía,
|
||||
/// si es el único impostor— cuando sí procede mostrarlo.
|
||||
List<String>? companerosImpostoresDe(String jugadorId) {
|
||||
final partida = _partida;
|
||||
if (partida == null || !partida.config.impostoresSeConocen) return null;
|
||||
final indice = partida.jugadores.indexWhere((j) => j.id == jugadorId);
|
||||
if (indice < 0 || !partida.jugadores[indice].esImpostor) return null;
|
||||
return partida.jugadores
|
||||
.where((j) => j.esImpostor && j.id != jugadorId)
|
||||
.map((j) => j.nombre)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Avanza a la fase de debate
|
||||
void iniciarDebate() {
|
||||
if (_partida == null) return;
|
||||
|
||||
Reference in New Issue
Block a user