2022-12-25 18:57:47 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2023-05-28 15:44:22 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
2022-12-25 18:57:47 +00:00
|
|
|
import 'package:mchess/chess_bloc/chess_bloc.dart';
|
|
|
|
|
|
|
|
import 'package:mchess/chess/chess_board.dart';
|
2023-07-01 07:29:43 +00:00
|
|
|
import 'package:mchess/chess_bloc/promotion_bloc.dart';
|
2024-01-05 21:59:31 +00:00
|
|
|
import 'package:mchess/chess_bloc/tap_bloc.dart';
|
2023-07-01 07:29:43 +00:00
|
|
|
import 'package:mchess/utils/chess_utils.dart';
|
|
|
|
import 'package:mchess/utils/widgets/promotion_dialog.dart';
|
2023-12-27 14:46:15 +00:00
|
|
|
import 'package:universal_platform/universal_platform.dart';
|
2023-05-28 15:44:22 +00:00
|
|
|
import 'package:uuid/uuid.dart';
|
2022-12-25 18:57:47 +00:00
|
|
|
|
2023-05-28 12:54:46 +00:00
|
|
|
class ChessGame extends StatefulWidget {
|
2023-05-28 15:44:22 +00:00
|
|
|
final UuidValue playerID;
|
2023-06-02 21:28:40 +00:00
|
|
|
final UuidValue lobbyID;
|
2023-06-29 23:49:18 +00:00
|
|
|
final String? passphrase;
|
|
|
|
const ChessGame(
|
|
|
|
{required this.playerID,
|
|
|
|
required this.lobbyID,
|
|
|
|
required this.passphrase,
|
|
|
|
super.key});
|
2022-12-25 18:57:47 +00:00
|
|
|
|
2023-05-28 12:54:46 +00:00
|
|
|
@override
|
|
|
|
State<ChessGame> createState() => _ChessGameState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChessGameState extends State<ChessGame> {
|
2024-01-05 21:59:31 +00:00
|
|
|
ChessCoordinate? tappedSquare;
|
2023-05-28 12:54:46 +00:00
|
|
|
|
2022-12-25 18:57:47 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-12-27 14:46:15 +00:00
|
|
|
FloatingActionButton? fltnBtn;
|
|
|
|
if (UniversalPlatform.isLinux ||
|
|
|
|
UniversalPlatform.isMacOS ||
|
|
|
|
UniversalPlatform.isWindows) {
|
|
|
|
fltnBtn = FloatingActionButton(
|
|
|
|
child: const Icon(Icons.cancel),
|
|
|
|
onPressed: () {
|
|
|
|
context.pop();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2022-12-25 18:57:47 +00:00
|
|
|
return Scaffold(
|
2023-12-27 14:46:15 +00:00
|
|
|
floatingActionButton: fltnBtn,
|
2023-07-01 07:29:43 +00:00
|
|
|
body: Center(
|
2023-08-22 02:24:30 +00:00
|
|
|
child: Container(
|
|
|
|
margin: const EdgeInsets.all(10),
|
2024-01-05 21:59:31 +00:00
|
|
|
child: BlocListener<TapBloc, TapState>(
|
|
|
|
listener: (context, state) {
|
|
|
|
setState(() {
|
|
|
|
tappedSquare = state.firstSquareTapped;
|
|
|
|
});
|
2023-08-22 02:24:30 +00:00
|
|
|
},
|
2024-01-05 21:59:31 +00:00
|
|
|
child: BlocListener<PromotionBloc, PromotionState>(
|
|
|
|
listener: (listenerContext, state) {
|
|
|
|
if (state.showPromotionDialog) {
|
|
|
|
promotionDialogBuilder(listenerContext, state.colorMoved);
|
|
|
|
}
|
2023-08-22 02:24:30 +00:00
|
|
|
},
|
2024-01-05 21:59:31 +00:00
|
|
|
child: BlocBuilder<ChessBloc, ChessBoardState>(
|
|
|
|
builder: (context, state) {
|
|
|
|
return ChessBoard(
|
|
|
|
boardState: state,
|
|
|
|
tappedSquare: tappedSquare,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2023-08-22 02:24:30 +00:00
|
|
|
),
|
2023-07-01 07:29:43 +00:00
|
|
|
),
|
2022-12-25 19:18:50 +00:00
|
|
|
),
|
2022-12-25 18:57:47 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2023-07-01 07:29:43 +00:00
|
|
|
|
|
|
|
Future<void> promotionDialogBuilder(BuildContext context, ChessColor color) {
|
|
|
|
return showDialog<void>(
|
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
return PromotionDialog(
|
|
|
|
sideColor: color,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2022-12-25 18:57:47 +00:00
|
|
|
}
|
2023-06-29 23:49:18 +00:00
|
|
|
|
|
|
|
class ChessGameArguments {
|
|
|
|
final UuidValue lobbyID;
|
|
|
|
final UuidValue playerID;
|
|
|
|
final String? passphrase;
|
|
|
|
|
|
|
|
ChessGameArguments({
|
|
|
|
required this.lobbyID,
|
|
|
|
required this.playerID,
|
|
|
|
required this.passphrase,
|
|
|
|
});
|
2023-12-23 15:44:23 +00:00
|
|
|
|
|
|
|
bool isValid() {
|
|
|
|
try {
|
|
|
|
lobbyID.validate();
|
|
|
|
playerID.validate();
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (passphrase == null) return false;
|
|
|
|
if (passphrase!.isEmpty) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-06-29 23:49:18 +00:00
|
|
|
}
|