Merge pull request 'Fix tapping on opponent's piece' (#8) from fix-tap-on-drag into master

Reviewed-on: #8
This commit is contained in:
marco 2024-02-01 11:42:18 +01:00
commit 33e79a90c9
5 changed files with 14 additions and 10 deletions

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mchess/chess/chess_square_outer_dragtarget.dart'; import 'package:mchess/chess/chess_square_outer_dragtarget.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/tap_bloc.dart'; import 'package:mchess/chess_bloc/tap_bloc.dart';
import '../utils/chess_utils.dart'; import '../utils/chess_utils.dart';
@ -66,7 +67,8 @@ class _ChessSquareState extends State<ChessSquare> {
containedPiece: widget.containedPiece ?? const ChessPiece.none()), containedPiece: widget.containedPiece ?? const ChessPiece.none()),
); );
if (widget.containedPiece == null) { if (widget.containedPiece == null ||
widget.containedPiece!.color != ChessBloc.getMyColor()) {
return GestureDetector( return GestureDetector(
child: dragTarget, child: dragTarget,
onTap: () { onTap: () {

View File

@ -30,13 +30,13 @@ class ChessSquareOuterDragTarget extends StatelessWidget {
if (isPromotionMove( if (isPromotionMove(
details.data.movedPiece!.pieceClass, details.data.movedPiece!.pieceClass,
ChessBloc.myColor!, ChessBloc.getMyColor(),
details.data.toSquare, details.data.toSquare,
)) { )) {
var move = ChessMove( var move = ChessMove(
from: details.data.fromSquare, to: details.data.toSquare); from: details.data.fromSquare, to: details.data.toSquare);
PromotionBloc().add(PawnMovedToPromotionField( PromotionBloc().add(PawnMovedToPromotionField(
move: move, colorMoved: ChessBloc.myColor!)); move: move, colorMoved: ChessBloc.myColor));
} else if (coordinate != details.data.fromSquare) { } else if (coordinate != details.data.fromSquare) {
ChessBloc().add(OwnPieceMoved( ChessBloc().add(OwnPieceMoved(
startSquare: details.data.fromSquare, startSquare: details.data.fromSquare,

View File

@ -11,9 +11,9 @@ import 'package:mchess/utils/chess_utils.dart';
class ChessBloc extends Bloc<ChessEvent, ChessBoardState> { class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
static final ChessBloc _instance = ChessBloc._internal(); static final ChessBloc _instance = ChessBloc._internal();
static ChessColor turnColor = ChessColor.white; static ChessColor turnColor = ChessColor.white;
static ChessColor? myColor = ChessColor.white; static ChessColor myColor = ChessColor.white;
static ChessColor? getSidesColor() { static ChessColor getMyColor() {
return myColor; return myColor;
} }
@ -126,7 +126,7 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
OwnPromotionPlayed event, Emitter<ChessBoardState> emit) { OwnPromotionPlayed event, Emitter<ChessBoardState> emit) {
var apiMove = event.move.toApiMove(); var apiMove = event.move.toApiMove();
var shorNameForPiece = chessPiecesShortName[ var shorNameForPiece = chessPiecesShortName[
ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor!)]!; ChessPieceAssetKey(pieceClass: event.pieceClass, color: myColor)]!;
apiMove.promotionToPiece = shorNameForPiece; apiMove.promotionToPiece = shorNameForPiece;
var message = ApiWebsocketMessage( var message = ApiWebsocketMessage(
type: MessageType.move, type: MessageType.move,

View File

@ -51,12 +51,12 @@ class TapBloc extends Bloc<TapEvent, TapState> {
state.firstSquareTapped != event.tapped) { state.firstSquareTapped != event.tapped) {
if (isPromotionMove( if (isPromotionMove(
state.pieceOnFirstTappedSquare!.pieceClass, state.pieceOnFirstTappedSquare!.pieceClass,
ChessBloc.myColor!, ChessBloc.myColor,
event.tapped, event.tapped,
)) { )) {
PromotionBloc().add(PawnMovedToPromotionField( PromotionBloc().add(PawnMovedToPromotionField(
move: ChessMove(from: state.firstSquareTapped!, to: event.tapped), move: ChessMove(from: state.firstSquareTapped!, to: event.tapped),
colorMoved: ChessBloc.myColor!)); colorMoved: ChessBloc.myColor));
emit(TapState.init()); emit(TapState.init());
return; return;
} else { } else {

View File

@ -168,8 +168,10 @@ class _LobbySelectorState extends State<LobbySelector> {
backgroundColor: Colors.amberAccent, backgroundColor: Colors.amberAccent,
content: Text("mChess server is not responding. Try again or give up"), content: Text("mChess server is not responding. Try again or give up"),
); );
ScaffoldMessenger.of(context).clearSnackBars(); if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(snackBar); ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
}
return null; return null;
} }