Fix rejoining #13

Merged
marco merged 5 commits from fix-rejoining into master 2024-05-21 21:56:36 +00:00
4 changed files with 102 additions and 76 deletions
Showing only changes of commit 2a2e219c80 - Show all commits

View File

@ -59,10 +59,11 @@ class ServerConnection {
broadcast.listen(handleIncomingData);
}
void disconnectExistingConnection() {
Future disconnectExistingConnection() async {
if (channel == null) return;
channel!.sink.close();
await channel!.sink.close();
channel = null;
broadcast = const Stream.empty();
}

View File

@ -19,8 +19,8 @@ class ConnectionCubit extends Cubit<ConnectionCubitState> {
ServerConnection.getInstance().connect(playerID, passphrase);
}
void disonnect() {
ServerConnection.getInstance().disconnectExistingConnection();
Future disonnect() async {
return ServerConnection.getInstance().disconnectExistingConnection();
}
void opponentConnected() {

View File

@ -24,13 +24,16 @@ class CreateGameWidget extends StatefulWidget {
class _CreateGameWidgetState extends State<CreateGameWidget> {
late Future<GameInfo?> registerResponse;
late Future disconnectFuture;
late ChessGameArguments chessGameArgs;
@override
void initState() {
super.initState();
ConnectionCubit().disonnect();
disconnectFuture = ConnectionCubit().disonnect();
disconnectFuture.then((val) {
registerResponse = createPrivateGame();
});
}
@override
@ -50,7 +53,13 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
return Scaffold(
floatingActionButton: fltnBtn,
body: Center(
child: FutureBuilder<GameInfo?>(
child: FutureBuilder(
future: disconnectFuture,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Container();
} else {
return FutureBuilder<GameInfo?>(
future: registerResponse,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
@ -60,14 +69,16 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
child: CircularProgressIndicator(),
);
} else {
var passphrase = snapshot.data?.passphrase ?? "no passphrase";
var passphrase =
snapshot.data?.passphrase ?? "no passphrase";
ConnectionCubit().connect(
snapshot.data!.playerID.toString(),
snapshot.data!.passphrase,
);
return BlocListener<ConnectionCubit, ConnectionCubitState>(
return BlocListener<ConnectionCubit,
ConnectionCubitState>(
listener: (context, state) {
// We wait for our opponent to connect
if (state.opponentConnected) {
@ -91,7 +102,8 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
children: [
SelectableText(
passphrase,
style: const TextStyle(fontWeight: FontWeight.bold),
style: const TextStyle(
fontWeight: FontWeight.bold),
),
IconButton(
icon: const Icon(Icons.copy),
@ -109,7 +121,9 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
);
}
},
),
);
}
}),
),
);
}

View File

@ -19,16 +19,25 @@ class JoinGameHandleWidget extends StatefulWidget {
class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
late Future<GameInfo?> joinGameFuture;
late Future disconnectFuture;
@override
void initState() {
super.initState();
ConnectionCubit().disonnect();
disconnectFuture = ConnectionCubit().disonnect();
disconnectFuture.then((val) {
joinGameFuture = joinPrivateGame(widget.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) {
@ -47,6 +56,8 @@ class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
}
});
}
});
}
Future<GameInfo?> joinPrivateGame(String phrase) async {
http.Response response;