From 358e8a6041fe640a9a9283680eaa273d54848fb5 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 21 May 2024 18:48:06 +0200 Subject: [PATCH] more changes because it's fun --- lib/api/game_info.dart | 2 +- lib/connection/ws_connection.dart | 28 ++++++----- lib/connection_cubit/connection_cubit.dart | 49 +++++++++++++++++-- lib/pages/create_game_widget.dart | 12 ++--- lib/pages/join_game_handle_widget.dart | 56 ++++++++++++---------- 5 files changed, 97 insertions(+), 50 deletions(-) diff --git a/lib/api/game_info.dart b/lib/api/game_info.dart index 9247df4..5b10048 100644 --- a/lib/api/game_info.dart +++ b/lib/api/game_info.dart @@ -47,7 +47,7 @@ class GameInfo { if (playerID == null) return null; return GameInfo( - playerID: UuidValue.fromString(playerID!), passphrase: phrase); + playerID: UuidValue.fromString(playerID), passphrase: phrase); } } diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 0e5f10c..ceaed3e 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -40,23 +40,27 @@ class ServerConnection { channel!.sink.add(message); } - void connect(String playerID, String? passphrase) { - if (channel != null) return; + Future? connect(String playerID, String? passphrase) { + if (channel != null) return null; channel = WebSocketChannel.connect(Uri.parse(config.getWebsocketURL())); - send( - jsonEncode( - WebsocketMessageIdentifyPlayer( - playerID: (playerID), - passphrase: (passphrase), + channel!.ready.then((val) { + send( + jsonEncode( + WebsocketMessageIdentifyPlayer( + playerID: (playerID), + passphrase: (passphrase), + ), ), - ), - ); + ); - log(channel!.closeCode.toString()); - broadcast = channel!.stream.asBroadcastStream(); - broadcast.listen(handleIncomingData); + log(channel!.closeCode.toString()); + broadcast = channel!.stream.asBroadcastStream(); + broadcast.listen(handleIncomingData); + }); + + return channel!.ready; } Future disconnectExistingConnection() async { diff --git a/lib/connection_cubit/connection_cubit.dart b/lib/connection_cubit/connection_cubit.dart index 2f877b8..10babd6 100644 --- a/lib/connection_cubit/connection_cubit.dart +++ b/lib/connection_cubit/connection_cubit.dart @@ -16,24 +16,63 @@ class ConnectionCubit extends Cubit { } void connect(String playerID, String? passphrase) { - ServerConnection.getInstance().connect(playerID, passphrase); + var connectedFuture = + ServerConnection.getInstance().connect(playerID, passphrase); + + connectedFuture?.then((val) { + emit(ConnectionCubitState( + iAmConnected: true, + connectedToPhrase: passphrase, + opponentConnected: false)); + }); + } + + void connectIfNotConnected(String playerID, String? passphrase) { + if (state.iAmConnected && state.connectedToPhrase == passphrase) { + return; + } + if (state.iAmConnected && state.connectedToPhrase != passphrase) { + disonnect().then((val) { + connect(playerID, passphrase); + }); + } + + connect(playerID, passphrase); } Future disonnect() async { - return ServerConnection.getInstance().disconnectExistingConnection(); + var disconnectFuture = + ServerConnection.getInstance().disconnectExistingConnection(); + + disconnectFuture.then( + (val) { + emit(ConnectionCubitState.init()); + }, + ); + + return disconnectFuture; } void opponentConnected() { - emit(ConnectionCubitState(true)); + emit(ConnectionCubitState( + iAmConnected: state.iAmConnected, + connectedToPhrase: state.connectedToPhrase, + opponentConnected: true)); } } class ConnectionCubitState { + final bool iAmConnected; + final String? connectedToPhrase; final bool opponentConnected; - ConnectionCubitState(this.opponentConnected); + ConnectionCubitState( + {required this.iAmConnected, + required this.connectedToPhrase, + required this.opponentConnected}); factory ConnectionCubitState.init() { - return ConnectionCubitState(false); + return ConnectionCubitState( + iAmConnected: false, connectedToPhrase: null, opponentConnected: false); } } diff --git a/lib/pages/create_game_widget.dart b/lib/pages/create_game_widget.dart index e31f723..199e149 100644 --- a/lib/pages/create_game_widget.dart +++ b/lib/pages/create_game_widget.dart @@ -33,6 +33,12 @@ class _CreateGameWidgetState extends State { disconnectFuture = ConnectionCubit().disonnect(); disconnectFuture.then((val) { registerResponse = createPrivateGame(); + registerResponse.then((val) { + ConnectionCubit().connectIfNotConnected( + val!.playerID.toString(), + val.passphrase, + ); + }); }); } @@ -71,12 +77,6 @@ class _CreateGameWidgetState extends State { } else { var passphrase = snapshot.data?.passphrase ?? "no passphrase"; - - ConnectionCubit().connect( - snapshot.data!.playerID.toString(), - snapshot.data!.passphrase, - ); - return BlocListener( listener: (context, state) { diff --git a/lib/pages/join_game_handle_widget.dart b/lib/pages/join_game_handle_widget.dart index 28f3c66..7cd0724 100644 --- a/lib/pages/join_game_handle_widget.dart +++ b/lib/pages/join_game_handle_widget.dart @@ -3,6 +3,7 @@ import 'dart:developer'; import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:http/http.dart' as http; import 'package:mchess/api/game_info.dart'; import 'package:mchess/connection_cubit/connection_cubit.dart'; @@ -19,44 +20,47 @@ class JoinGameHandleWidget extends StatefulWidget { class _JoinGameHandleWidgetState extends State { late Future joinGameFuture; - late Future disconnectFuture; @override void initState() { super.initState(); - disconnectFuture = ConnectionCubit().disonnect(); - disconnectFuture.then((val) { - joinGameFuture = joinPrivateGame(widget.passphrase); + joinGameFuture = joinPrivateGame(widget.passphrase); + joinGameFuture.then((val) { + ConnectionCubit.getInstance().connectIfNotConnected( + val!.playerID!.uuid, + val.passphrase, + ); }); } @override Widget build(BuildContext context) { - return FutureBuilder( - future: disconnectFuture, - builder: (context, snapshot) { - if (snapshot.connectionState != ConnectionState.done) { - return Container(); - } else { - return FutureBuilder( - future: joinGameFuture, - builder: (context, snapshot) { - if (snapshot.connectionState != ConnectionState.done) { - return const SizedBox( - height: 100, - width: 100, - child: CircularProgressIndicator(), - ); - } else { - ConnectionCubit.getInstance().connect( - snapshot.data!.playerID!.uuid, - snapshot.data!.passphrase, - ); + var loadingIndicator = const SizedBox( + height: 100, + width: 100, + child: CircularProgressIndicator(), + ); + + return Scaffold( + body: Center( + child: FutureBuilder( + future: joinGameFuture, + builder: (context, snapshot) { + if (snapshot.connectionState != ConnectionState.done) { + return loadingIndicator; + } else { + return BlocBuilder( + builder: (context, state) { + if (state.iAmConnected) { return const ChessGame(); + } else { + return loadingIndicator; } }); - } - }); + } + }), + ), + ); } Future joinPrivateGame(String phrase) async {