Fix routing and move handling
This commit is contained in:
parent
17ac437f5b
commit
ba947ae5e4
@ -44,7 +44,7 @@ class ApiWebsocketMessage {
|
|||||||
case MessageType.boardState:
|
case MessageType.boardState:
|
||||||
ret = ApiWebsocketMessage(
|
ret = ApiWebsocketMessage(
|
||||||
type: type,
|
type: type,
|
||||||
move: null,
|
move: ApiMove.fromJson(json['move']),
|
||||||
turnColor: ApiColor.fromJson(json['turnColor']),
|
turnColor: ApiColor.fromJson(json['turnColor']),
|
||||||
reason: null,
|
reason: null,
|
||||||
position: json['position'],
|
position: json['position'],
|
||||||
|
@ -27,7 +27,7 @@ class ChessApp extends StatelessWidget {
|
|||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
routerConfig: ChessAppRouter.getInstance().router,
|
routerConfig: ChessAppRouter.getInstance().router,
|
||||||
title: 'mChess',
|
title: 'mChess 0.1339',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,7 @@ class ChessSquareOuterDragTarget extends StatelessWidget {
|
|||||||
final ChessPiece containedPiece;
|
final ChessPiece containedPiece;
|
||||||
|
|
||||||
const ChessSquareOuterDragTarget(
|
const ChessSquareOuterDragTarget(
|
||||||
{super.key,
|
{super.key, required this.coordinate, required this.containedPiece});
|
||||||
required this.coordinate,
|
|
||||||
required this.containedPiece});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
@ -67,12 +67,9 @@ class ChessBloc extends Bloc<ChessEvent, ChessBoardState> {
|
|||||||
|
|
||||||
void moveAndPositionHandler(
|
void moveAndPositionHandler(
|
||||||
ReceivedBoardState event, Emitter<ChessBoardState> emit) {
|
ReceivedBoardState event, Emitter<ChessBoardState> emit) {
|
||||||
turnColor = state.newTurnColor == ChessColor.white
|
|
||||||
? ChessColor.black
|
|
||||||
: ChessColor.white;
|
|
||||||
|
|
||||||
ChessMove? move;
|
ChessMove? move;
|
||||||
if (event.startSquare != null && event.endSquare != null) {
|
if (event.startSquare != ChessCoordinate.none() &&
|
||||||
|
event.endSquare != ChessCoordinate.none()) {
|
||||||
move = ChessMove(from: event.startSquare!, to: event.endSquare!);
|
move = ChessMove(from: event.startSquare!, to: event.endSquare!);
|
||||||
ChessPositionManager.getInstance()
|
ChessPositionManager.getInstance()
|
||||||
.recordMove(event.startSquare, event.endSquare, event.position);
|
.recordMove(event.startSquare, event.endSquare, event.position);
|
||||||
|
@ -95,6 +95,15 @@ class ChessPositionManager {
|
|||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ChessPosition _getDebugPostion() {
|
||||||
|
ChessPosition pos = {};
|
||||||
|
|
||||||
|
pos[ChessCoordinate(7, 7)] =
|
||||||
|
ChessPiece(ChessPieceClass.pawn, ChessColor.white);
|
||||||
|
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
static ChessPosition _getStartingPosition() {
|
static ChessPosition _getStartingPosition() {
|
||||||
ChessPosition pos = {};
|
ChessPosition pos = {};
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ class ServerConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handleIncomingData(dynamic data) {
|
void handleIncomingData(dynamic data) {
|
||||||
log("Data received:");
|
log('${DateTime.now()}: Data received:');
|
||||||
log(data);
|
log(data);
|
||||||
var apiMessage = ApiWebsocketMessage.fromJson(jsonDecode(data));
|
var apiMessage = ApiWebsocketMessage.fromJson(jsonDecode(data));
|
||||||
|
|
||||||
@ -82,7 +82,7 @@ class ServerConnection {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case MessageType.move:
|
case MessageType.move:
|
||||||
handleIncomingMoveMessage(apiMessage);
|
log('ERROR: move message received');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MessageType.invalidMove:
|
case MessageType.invalidMove:
|
||||||
@ -119,24 +119,6 @@ class ServerConnection {
|
|||||||
myColor: ChessColor.fromApiColor(apiMessage.playerColor!)));
|
myColor: ChessColor.fromApiColor(apiMessage.playerColor!)));
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleIncomingMoveMessage(ApiWebsocketMessage apiMessage) {
|
|
||||||
if (apiMessage.position != null) {
|
|
||||||
var move = ChessMove.fromApiMove(apiMessage.move!);
|
|
||||||
ChessBloc.getInstance().add(
|
|
||||||
ReceivedBoardState(
|
|
||||||
startSquare: move.from,
|
|
||||||
endSquare: move.to,
|
|
||||||
position: ChessPositionManager.getInstance()
|
|
||||||
.fromPGNString(apiMessage.position!),
|
|
||||||
squareInCheck: ChessCoordinate.fromApiCoordinate(
|
|
||||||
apiMessage.squareInCheck ?? const ApiCoordinate(col: 0, row: 0)),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
log('Error: no position received');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void handleInvalidMoveMessage(ApiWebsocketMessage apiMessage) {
|
void handleInvalidMoveMessage(ApiWebsocketMessage apiMessage) {
|
||||||
log("invalid move message received, with move: ${apiMessage.move.toString()}");
|
log("invalid move message received, with move: ${apiMessage.move.toString()}");
|
||||||
ChessBloc.getInstance().add(
|
ChessBloc.getInstance().add(
|
||||||
|
@ -37,9 +37,9 @@ class _ChessGameState extends State<ChessGame> {
|
|||||||
child: Container(
|
child: Container(
|
||||||
margin: const EdgeInsets.all(10),
|
margin: const EdgeInsets.all(10),
|
||||||
child: BlocListener<PromotionBloc, PromotionState>(
|
child: BlocListener<PromotionBloc, PromotionState>(
|
||||||
listener: (context, state) {
|
listener: (listenerContext, state) {
|
||||||
if (state.showPromotionDialog) {
|
if (state.showPromotionDialog) {
|
||||||
promotionDialogBuilder(context, state.colorMoved);
|
promotionDialogBuilder(listenerContext, state.colorMoved);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: BlocBuilder<ChessBloc, ChessBoardState>(
|
child: BlocBuilder<ChessBloc, ChessBoardState>(
|
||||||
@ -55,7 +55,7 @@ class _ChessGameState extends State<ChessGame> {
|
|||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
ServerConnection.getInstance().disconnectExistingConnection();
|
ServerConnection.getInstance().disconnectExistingConnection();
|
||||||
context.push('/');
|
GoRouter.of(context).goNamed('lobbySelector');
|
||||||
},
|
},
|
||||||
child: const Icon(Icons.cancel),
|
child: const Icon(Icons.cancel),
|
||||||
),
|
),
|
||||||
|
@ -70,7 +70,7 @@ class _HostGameWidgetState extends State<HostGameWidget> {
|
|||||||
listener: (context, state) {
|
listener: (context, state) {
|
||||||
// We wait for our opponent to connect
|
// We wait for our opponent to connect
|
||||||
if (state.opponentConnected) {
|
if (state.opponentConnected) {
|
||||||
context.push('/game', extra: chessGameArgs);
|
context.goNamed('game', extra: chessGameArgs);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: Column(
|
child: Column(
|
||||||
@ -98,7 +98,7 @@ class _HostGameWidgetState extends State<HostGameWidget> {
|
|||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
child: const Icon(Icons.cancel),
|
child: const Icon(Icons.cancel),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
context.push('/');
|
context.pop();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -74,7 +74,7 @@ class _LobbySelectorState extends State<LobbySelector> {
|
|||||||
TextButton(
|
TextButton(
|
||||||
child: const Text('Host'),
|
child: const Text('Host'),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
context.push('/host');
|
context.push('/host'); //replaces joinOrHostDialog
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
@ -137,7 +137,7 @@ class _LobbySelectorState extends State<LobbySelector> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!chessGameArgs.isValid()) {
|
if (!chessGameArgs.isValid()) {
|
||||||
context.push('/');
|
context.goNamed('lobbySelector');
|
||||||
const snackBar = SnackBar(
|
const snackBar = SnackBar(
|
||||||
backgroundColor: Colors.amberAccent,
|
backgroundColor: Colors.amberAccent,
|
||||||
content: Text("Game information is corrupted"),
|
content: Text("Game information is corrupted"),
|
||||||
@ -146,7 +146,7 @@ class _LobbySelectorState extends State<LobbySelector> {
|
|||||||
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.push('/game', extra: chessGameArgs);
|
context.pushReplacement('/game', extra: chessGameArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<PlayerInfo?> joinPrivateGame() async {
|
Future<PlayerInfo?> joinPrivateGame() async {
|
||||||
|
@ -1,91 +0,0 @@
|
|||||||
import 'dart:developer';
|
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:go_router/go_router.dart';
|
|
||||||
import 'package:http/http.dart';
|
|
||||||
import 'package:mchess/api/register.dart';
|
|
||||||
import 'package:mchess/pages/chess_game.dart';
|
|
||||||
import 'package:http/http.dart' as http;
|
|
||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
class PrepareRandomGameWidget extends StatefulWidget {
|
|
||||||
const PrepareRandomGameWidget({super.key});
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<PrepareRandomGameWidget> createState() =>
|
|
||||||
_PrepareRandomGameWidgetState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _PrepareRandomGameWidgetState extends State<PrepareRandomGameWidget> {
|
|
||||||
late Future randomGameResponse;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
randomGameResponse = registerForRandomGame();
|
|
||||||
goToGameWhenResponseIsHere(randomGameResponse as Future<PlayerInfo?>);
|
|
||||||
super.initState();
|
|
||||||
}
|
|
||||||
|
|
||||||
void goToGameWhenResponseIsHere(Future<PlayerInfo?> resp) {
|
|
||||||
resp.then((value) {
|
|
||||||
if (value == null) return;
|
|
||||||
context.push(
|
|
||||||
'/game',
|
|
||||||
extra: ChessGameArguments(
|
|
||||||
lobbyID: value.lobbyID!,
|
|
||||||
playerID: value.playerID!,
|
|
||||||
passphrase: value.passphrase),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return const Scaffold(
|
|
||||||
body: Center(
|
|
||||||
child: SizedBox(
|
|
||||||
height: 100,
|
|
||||||
width: 100,
|
|
||||||
child: CircularProgressIndicator(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<PlayerInfo?> registerForRandomGame() async {
|
|
||||||
String addr;
|
|
||||||
Response response;
|
|
||||||
|
|
||||||
if (kDebugMode) {
|
|
||||||
addr = 'http://localhost:8080/api/random';
|
|
||||||
} else {
|
|
||||||
addr = 'https://chess.sw-gross.de:9999/api/random';
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
response = await http
|
|
||||||
.get(Uri.parse(addr), headers: {"Accept": "application/json"});
|
|
||||||
} catch (e) {
|
|
||||||
if (!context.mounted) return null;
|
|
||||||
|
|
||||||
const snackBar = SnackBar(
|
|
||||||
backgroundColor: Colors.amberAccent,
|
|
||||||
content: Text("mChess server is not responding. Try again or give up"),
|
|
||||||
);
|
|
||||||
Future.delayed(const Duration(seconds: 2), () {
|
|
||||||
ScaffoldMessenger.of(context).clearSnackBars();
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
|
||||||
context.pop();
|
|
||||||
});
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
log(response.body);
|
|
||||||
return PlayerInfo.fromJson(jsonDecode(response.body));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,7 +2,6 @@ import 'package:go_router/go_router.dart';
|
|||||||
import 'package:mchess/pages/chess_game.dart';
|
import 'package:mchess/pages/chess_game.dart';
|
||||||
import 'package:mchess/pages/lobby_selector.dart';
|
import 'package:mchess/pages/lobby_selector.dart';
|
||||||
import 'package:mchess/pages/host_game.dart';
|
import 'package:mchess/pages/host_game.dart';
|
||||||
import 'package:mchess/pages/prepare_random_game.dart';
|
|
||||||
|
|
||||||
class ChessAppRouter {
|
class ChessAppRouter {
|
||||||
static final ChessAppRouter _instance = ChessAppRouter._internal();
|
static final ChessAppRouter _instance = ChessAppRouter._internal();
|
||||||
@ -22,12 +21,6 @@ class ChessAppRouter {
|
|||||||
return const LobbySelector();
|
return const LobbySelector();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
GoRoute(
|
|
||||||
path: '/prepareRandom',
|
|
||||||
name: 'prepareRandom',
|
|
||||||
builder: (context, state) {
|
|
||||||
return const PrepareRandomGameWidget();
|
|
||||||
}),
|
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/host',
|
path: '/host',
|
||||||
name: 'host',
|
name: 'host',
|
||||||
|
Loading…
Reference in New Issue
Block a user