feat: Implement multiplayer game session management
- Add models for managing player assignments and game session initialization in `inicio_partida_multijugador.dart`. - Create a multiplayer room state management system in `sala_multijugador.dart`, including user registration, selection, and session validation. - Develop a UI screen for displaying player words sequentially in `pantalla_palabras_cliente.dart`. - Implement unit tests for the multiplayer session management and player assignment logic in `inicio_partida_multijugador_test.dart` and `sala_multijugador_test.dart`.
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:flutter/foundation.dart';
|
||||
import '../modelos/jugador.dart';
|
||||
import '../modelos/partida.dart';
|
||||
import '../modelos/palabra.dart';
|
||||
import '../modelos/sala_multijugador.dart';
|
||||
import '../servicios/servicio_notas.dart';
|
||||
|
||||
/// Estado global del juego gestionado con Provider
|
||||
@@ -89,6 +90,61 @@ class EstadoJuego extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Crea una partida multi-dispositivo usando los usuarios seleccionados de la
|
||||
/// sala como jugadores reales. La identidad de jugador se conserva por id y
|
||||
/// cada jugador queda asociado al endpoint del cliente que lo controla.
|
||||
void crearPartidaDesdeSala({
|
||||
required ConfigPartida config,
|
||||
required EstadoSalaMultijugador sala,
|
||||
}) {
|
||||
if (_banco == null) return;
|
||||
final usuariosSeleccionados = sala.usuariosSeleccionados;
|
||||
if (usuariosSeleccionados.length < 3) return;
|
||||
|
||||
final palabra = _banco!.palabraAleatoria(config.categoria);
|
||||
final categoriaReal =
|
||||
_banco!.categoriaDepalabra(palabra) ?? config.categoria;
|
||||
|
||||
final jugadores = usuariosSeleccionados.map((usuario) {
|
||||
final clienteId = usuario.clienteIdSeleccionado;
|
||||
final endpointId = clienteId == null
|
||||
? null
|
||||
: sala.clientes[clienteId]?.endpointId;
|
||||
return Jugador(
|
||||
id: usuario.id,
|
||||
nombre: usuario.nombre,
|
||||
endpointId: endpointId,
|
||||
);
|
||||
}).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;
|
||||
}
|
||||
}
|
||||
|
||||
_partida = Partida(
|
||||
config: config,
|
||||
jugadores: jugadores,
|
||||
palabraSecreta: palabra,
|
||||
categoriaReal: categoriaReal,
|
||||
);
|
||||
|
||||
_votos.clear();
|
||||
ServicioNotas.limpiarNotas();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
/// Avanza a la fase de debate
|
||||
void iniciarDebate() {
|
||||
if (_partida == null) return;
|
||||
|
||||
Reference in New Issue
Block a user