NUEVA GESTIÓN DE USUARIOS Y PARTIDAS
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../modelos/partida.dart';
|
||||
import '../modelos/snapshot_partida_online.dart';
|
||||
|
||||
class ResultadoPartidaGuardado {
|
||||
final String id;
|
||||
@@ -42,6 +43,25 @@ class ResultadoPartidaGuardado {
|
||||
);
|
||||
}
|
||||
|
||||
factory ResultadoPartidaGuardado.desdeSnapshotOnline(
|
||||
SnapshotPartidaOnline snapshot,
|
||||
) {
|
||||
final impostores = snapshot.impostores.isNotEmpty
|
||||
? snapshot.impostores.length
|
||||
: snapshot.jugadores.where((jugador) => jugador.esImpostor).length;
|
||||
return ResultadoPartidaGuardado(
|
||||
id: 'online-${snapshot.roomId ?? DateTime.now().microsecondsSinceEpoch}',
|
||||
fecha: DateTime.now(),
|
||||
modoMultimovil: true,
|
||||
jugadores: snapshot.jugadores.length,
|
||||
impostores: impostores,
|
||||
rondas: snapshot.ronda,
|
||||
ganador: snapshot.ganador ?? 'sin_resultado',
|
||||
palabra: snapshot.palabraSecreta ?? '',
|
||||
categoria: snapshot.categoria,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
'id': id,
|
||||
'fecha': fecha.toIso8601String(),
|
||||
@@ -99,6 +119,16 @@ class ServicioHistorialPartidas extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> guardarSnapshotOnline(SnapshotPartidaOnline snapshot) async {
|
||||
if (snapshot.ganador == null || snapshot.palabraSecreta == null) return;
|
||||
final guardado = ResultadoPartidaGuardado.desdeSnapshotOnline(snapshot);
|
||||
if (_partidas.any((partida) => partida.id == guardado.id)) return;
|
||||
_partidas.insert(0, guardado);
|
||||
if (_partidas.length > 100) _partidas.removeRange(100, _partidas.length);
|
||||
await _persistir();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> limpiar() async {
|
||||
_partidas.clear();
|
||||
await _persistir();
|
||||
|
||||
Reference in New Issue
Block a user