Files
front-hokm/lib/feature/game/presentation/bloc/game_state.dart
T
2026-06-19 23:18:51 +03:30

106 lines
3.0 KiB
Dart

import 'package:equatable/equatable.dart';
import '../../../../core/network/ws_client.dart';
import '../../domain/entities/game_entities.dart';
/// یک بازیکن در اتاق انتظارِ میز خصوصی.
class LobbyPlayer {
final String name;
final bool host;
final int seat; // جایگاهِ مطلق (۰..۳)
const LobbyPlayer(this.name, this.host, this.seat);
}
/// وضعیت اتاق انتظارِ میز خصوصی (دورهمی).
class TableLobby {
final String code;
final List<LobbyPlayer> players;
final bool isHost;
final int youSeat; // جایگاهِ مطلقِ خودِ کاربر (برای چیدمانِ صحنه)
final int remaining;
final bool unlimited;
const TableLobby({
required this.code,
required this.players,
required this.isHost,
required this.youSeat,
required this.remaining,
required this.unlimited,
});
factory TableLobby.fromJson(Map<String, dynamic> j) => TableLobby(
code: (j['code'] ?? '') as String,
players: ((j['players'] as List?) ?? [])
.map((e) => LobbyPlayer(
(e['name'] ?? '') as String,
(e['host'] ?? false) as bool,
(e['seat'] ?? 0) as int,
))
.toList(),
isHost: (j['host'] ?? false) as bool,
youSeat: (j['you_seat'] ?? 0) as int,
remaining: (j['remaining'] ?? 0) as int,
unlimited: (j['unlimited'] ?? false) as bool,
);
String get sig => '$code|$isHost|$youSeat|$remaining|$unlimited|'
'${players.map((p) => '${p.name}${p.host ? '*' : ''}@${p.seat}').join(',')}';
}
class GameUiState extends Equatable {
final WsStatus connection;
final GameState? state;
final HandResult? handResult;
final GameOver? gameOver;
final String? notice;
final TableLobby? lobby;
final int? countdown;
final bool tableClosed;
const GameUiState({
this.connection = WsStatus.connecting,
this.state,
this.handResult,
this.gameOver,
this.notice,
this.lobby,
this.countdown,
this.tableClosed = false,
});
GameUiState copyWith({
WsStatus? connection,
GameState? state,
HandResult? handResult,
GameOver? gameOver,
String? notice,
TableLobby? lobby,
int? countdown,
bool? tableClosed,
bool clearHandResult = false,
bool clearNotice = false,
}) =>
GameUiState(
connection: connection ?? this.connection,
state: state ?? this.state,
handResult: clearHandResult ? null : (handResult ?? this.handResult),
gameOver: gameOver ?? this.gameOver,
notice: clearNotice ? null : (notice ?? this.notice),
lobby: lobby ?? this.lobby,
countdown: countdown ?? this.countdown,
tableClosed: tableClosed ?? this.tableClosed,
);
@override
List<Object?> get props => [
connection,
state,
handResult,
gameOver,
notice,
lobby?.sig,
countdown,
tableClosed,
];
}