Files
farolero/lib/servicios/identidad_dispositivo.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

48 lines
1.4 KiB
Dart

import 'dart:math';
import 'package:shared_preferences/shared_preferences.dart';
/// Identificador estable de este dispositivo.
///
/// Nearby Connections asigna un `endpointId` nuevo en cada conexión, así que no
/// sirve para reconocer a un móvil que se reconecta. Este id se guarda en disco
/// y sobrevive a caídas de conexión, cierres de la app y reinicios.
class IdentidadDispositivo {
static const _clave = 'dispositivo.id';
static String? _cache;
/// Devuelve el id del dispositivo, creándolo la primera vez.
static Future<String> obtener() async {
final cacheado = _cache;
if (cacheado != null) return cacheado;
final prefs = await SharedPreferences.getInstance();
final guardado = prefs.getString(_clave);
if (guardado != null && guardado.isNotEmpty) {
_cache = guardado;
return guardado;
}
final nuevo = _generar();
await prefs.setString(_clave, nuevo);
_cache = nuevo;
return nuevo;
}
/// Id ya cargado en memoria, si existe. Útil donde no se puede esperar.
static String? get cacheado => _cache;
static String _generar() {
final rng = Random.secure();
final bytes = List<int>.generate(8, (_) => rng.nextInt(256));
final hex = bytes
.map((b) => b.toRadixString(16).padLeft(2, '0'))
.join();
return 'dev-$hex';
}
/// Solo para pruebas: fija el id en memoria sin tocar disco.
static void fijarParaPruebas(String? id) => _cache = id;
}