Files
farolero/lib/modelos/partida.dart
T
FreeTLab 863690168c 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.
2026-07-25 20:36:32 +02:00

95 lines
2.6 KiB
Dart

import 'jugador.dart';
/// Configuración de una partida
class ConfigPartida {
final bool modoMultimovil;
final String categoria; // 'todas' o nombre de categoría
final int numImpostores;
final bool pistaImpostor;
final int? tiempoDebateSegundos; // null = sin límite
/// Cuando hay más de un impostor, cada impostor ve los nombres del resto.
final bool impostoresSeConocen;
const ConfigPartida({
this.modoMultimovil = false,
this.categoria = 'todas',
this.numImpostores = 1,
this.pistaImpostor = false,
this.tiempoDebateSegundos,
this.impostoresSeConocen = true,
});
}
/// Fases del juego
enum FaseJuego {
configuracion,
verPalabra,
debate,
votacion,
resultado,
adivinanza, // El impostor intenta adivinar la palabra
finPartida,
}
/// Resultado de una ronda de votación
class ResultadoVotacion {
final String eliminadoId;
final String eliminadoNombre;
final bool eraImpostor;
final Map<String, String> votos; // votante -> votado
const ResultadoVotacion({
required this.eliminadoId,
required this.eliminadoNombre,
required this.eraImpostor,
required this.votos,
});
}
/// Estado completo de una partida
class Partida {
final ConfigPartida config;
final List<Jugador> jugadores;
final String palabraSecreta;
final String categoriaReal;
/// Pista que ve el impostor. Es específica de la palabra cuando el banco la
/// aporta; si no, cae al nombre de la categoría.
final String pistaImpostor;
FaseJuego fase;
int rondaActual;
final List<ResultadoVotacion> historialVotaciones;
String? ganador; // 'jugadores' | 'impostores' | null
Partida({
required this.config,
required this.jugadores,
required this.palabraSecreta,
required this.categoriaReal,
String? pistaImpostor,
this.fase = FaseJuego.verPalabra,
this.rondaActual = 1,
List<ResultadoVotacion>? historialVotaciones,
this.ganador,
}) : pistaImpostor = pistaImpostor ?? categoriaReal,
historialVotaciones = historialVotaciones ?? [];
/// Nombres de los impostores, para que cada impostor sepa quiénes son sus
/// compañeros cuando la partida lo permite.
List<String> get nombresImpostores =>
jugadores.where((j) => j.esImpostor).map((j) => j.nombre).toList();
List<Jugador> get jugadoresActivos =>
jugadores.where((j) => !j.eliminado).toList();
List<Jugador> get impostoresActivos =>
jugadoresActivos.where((j) => j.esImpostor).toList();
List<Jugador> get jugadoresNormalesActivos =>
jugadoresActivos.where((j) => !j.esImpostor).toList();
int get impostoresTotales =>
jugadores.where((j) => j.esImpostor).length;
}