Fix rejoining #13
@ -47,7 +47,7 @@ class GameInfo {
|
|||||||
if (playerID == null) return null;
|
if (playerID == null) return null;
|
||||||
|
|
||||||
return GameInfo(
|
return GameInfo(
|
||||||
playerID: UuidValue.fromString(playerID!), passphrase: phrase);
|
playerID: UuidValue.fromString(playerID), passphrase: phrase);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,11 +40,12 @@ class ServerConnection {
|
|||||||
channel!.sink.add(message);
|
channel!.sink.add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect(String playerID, String? passphrase) {
|
Future? connect(String playerID, String? passphrase) {
|
||||||
if (channel != null) return;
|
if (channel != null) return null;
|
||||||
|
|
||||||
channel = WebSocketChannel.connect(Uri.parse(config.getWebsocketURL()));
|
channel = WebSocketChannel.connect(Uri.parse(config.getWebsocketURL()));
|
||||||
|
|
||||||
|
channel!.ready.then((val) {
|
||||||
send(
|
send(
|
||||||
jsonEncode(
|
jsonEncode(
|
||||||
WebsocketMessageIdentifyPlayer(
|
WebsocketMessageIdentifyPlayer(
|
||||||
@ -57,6 +58,9 @@ class ServerConnection {
|
|||||||
log(channel!.closeCode.toString());
|
log(channel!.closeCode.toString());
|
||||||
broadcast = channel!.stream.asBroadcastStream();
|
broadcast = channel!.stream.asBroadcastStream();
|
||||||
broadcast.listen(handleIncomingData);
|
broadcast.listen(handleIncomingData);
|
||||||
|
});
|
||||||
|
|
||||||
|
return channel!.ready;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future disconnectExistingConnection() async {
|
Future disconnectExistingConnection() async {
|
||||||
|
@ -16,24 +16,63 @@ class ConnectionCubit extends Cubit<ConnectionCubitState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void connect(String playerID, String? passphrase) {
|
void connect(String playerID, String? passphrase) {
|
||||||
|
var connectedFuture =
|
||||||
ServerConnection.getInstance().connect(playerID, passphrase);
|
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 {
|
Future disonnect() async {
|
||||||
return ServerConnection.getInstance().disconnectExistingConnection();
|
var disconnectFuture =
|
||||||
|
ServerConnection.getInstance().disconnectExistingConnection();
|
||||||
|
|
||||||
|
disconnectFuture.then(
|
||||||
|
(val) {
|
||||||
|
emit(ConnectionCubitState.init());
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return disconnectFuture;
|
||||||
}
|
}
|
||||||
|
|
||||||
void opponentConnected() {
|
void opponentConnected() {
|
||||||
emit(ConnectionCubitState(true));
|
emit(ConnectionCubitState(
|
||||||
|
iAmConnected: state.iAmConnected,
|
||||||
|
connectedToPhrase: state.connectedToPhrase,
|
||||||
|
opponentConnected: true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConnectionCubitState {
|
class ConnectionCubitState {
|
||||||
|
final bool iAmConnected;
|
||||||
|
final String? connectedToPhrase;
|
||||||
final bool opponentConnected;
|
final bool opponentConnected;
|
||||||
|
|
||||||
ConnectionCubitState(this.opponentConnected);
|
ConnectionCubitState(
|
||||||
|
{required this.iAmConnected,
|
||||||
|
required this.connectedToPhrase,
|
||||||
|
required this.opponentConnected});
|
||||||
|
|
||||||
factory ConnectionCubitState.init() {
|
factory ConnectionCubitState.init() {
|
||||||
return ConnectionCubitState(false);
|
return ConnectionCubitState(
|
||||||
|
iAmConnected: false, connectedToPhrase: null, opponentConnected: false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,12 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
|
|||||||
disconnectFuture = ConnectionCubit().disonnect();
|
disconnectFuture = ConnectionCubit().disonnect();
|
||||||
disconnectFuture.then((val) {
|
disconnectFuture.then((val) {
|
||||||
registerResponse = createPrivateGame();
|
registerResponse = createPrivateGame();
|
||||||
|
registerResponse.then((val) {
|
||||||
|
ConnectionCubit().connectIfNotConnected(
|
||||||
|
val!.playerID.toString(),
|
||||||
|
val.passphrase,
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,12 +77,6 @@ class _CreateGameWidgetState extends State<CreateGameWidget> {
|
|||||||
} else {
|
} else {
|
||||||
var passphrase =
|
var passphrase =
|
||||||
snapshot.data?.passphrase ?? "no passphrase";
|
snapshot.data?.passphrase ?? "no passphrase";
|
||||||
|
|
||||||
ConnectionCubit().connect(
|
|
||||||
snapshot.data!.playerID.toString(),
|
|
||||||
snapshot.data!.passphrase,
|
|
||||||
);
|
|
||||||
|
|
||||||
return BlocListener<ConnectionCubit,
|
return BlocListener<ConnectionCubit,
|
||||||
ConnectionCubitState>(
|
ConnectionCubitState>(
|
||||||
listener: (context, state) {
|
listener: (context, state) {
|
||||||
|
@ -3,6 +3,7 @@ import 'dart:developer';
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:http/http.dart' as http;
|
import 'package:http/http.dart' as http;
|
||||||
import 'package:mchess/api/game_info.dart';
|
import 'package:mchess/api/game_info.dart';
|
||||||
import 'package:mchess/connection_cubit/connection_cubit.dart';
|
import 'package:mchess/connection_cubit/connection_cubit.dart';
|
||||||
@ -19,44 +20,47 @@ class JoinGameHandleWidget extends StatefulWidget {
|
|||||||
|
|
||||||
class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
|
class _JoinGameHandleWidgetState extends State<JoinGameHandleWidget> {
|
||||||
late Future<GameInfo?> joinGameFuture;
|
late Future<GameInfo?> joinGameFuture;
|
||||||
late Future disconnectFuture;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return FutureBuilder(
|
var loadingIndicator = const SizedBox(
|
||||||
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,
|
height: 100,
|
||||||
width: 100,
|
width: 100,
|
||||||
child: CircularProgressIndicator(),
|
child: CircularProgressIndicator(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: FutureBuilder(
|
||||||
|
future: joinGameFuture,
|
||||||
|
builder: (context, snapshot) {
|
||||||
|
if (snapshot.connectionState != ConnectionState.done) {
|
||||||
|
return loadingIndicator;
|
||||||
} else {
|
} else {
|
||||||
ConnectionCubit.getInstance().connect(
|
return BlocBuilder<ConnectionCubit, ConnectionCubitState>(
|
||||||
snapshot.data!.playerID!.uuid,
|
builder: (context, state) {
|
||||||
snapshot.data!.passphrase,
|
if (state.iAmConnected) {
|
||||||
);
|
|
||||||
return const ChessGame();
|
return const ChessGame();
|
||||||
|
} else {
|
||||||
|
return loadingIndicator;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<GameInfo?> joinPrivateGame(String phrase) async {
|
Future<GameInfo?> joinPrivateGame(String phrase) async {
|
||||||
|
Loading…
Reference in New Issue
Block a user