From b0d6f4002c3b3f1f0b31fea26e5699e820e27ac7 Mon Sep 17 00:00:00 2001 From: Marco Date: Sun, 28 May 2023 17:44:22 +0200 Subject: [PATCH] Change connection handling (Use playerID from registering to connect via websocket. --- lib/connection/ws_connection.dart | 3 ++- lib/connection_cubit/connection_cubit.dart | 4 +-- lib/models/models.dart | 17 +++++++++++++ lib/pages/chess_game.dart | 11 +++++--- lib/pages/lobby_selector.dart | 11 +------- lib/pages/prepare_chess_game.dart | 29 +++++++++++++++++----- lib/utils/chess_router.dart | 3 +-- pubspec.lock | 8 ++++++ pubspec.yaml | 1 + test/widget_test.dart | 5 +++- 10 files changed, 66 insertions(+), 26 deletions(-) create mode 100644 lib/models/models.dart diff --git a/lib/connection/ws_connection.dart b/lib/connection/ws_connection.dart index 7bd32ee..51319a9 100644 --- a/lib/connection/ws_connection.dart +++ b/lib/connection/ws_connection.dart @@ -31,7 +31,7 @@ class ServerConnection { counter++; } - void connect() { + void connect(String playerID) { if (wasConnected) channel.sink.close(); if (kDebugMode) { @@ -41,6 +41,7 @@ class ServerConnection { channel = WebSocketChannel.connect(Uri.parse('wss://chess.sw-gross.de:8080')); } + send(playerID); log(channel.closeCode.toString()); diff --git a/lib/connection_cubit/connection_cubit.dart b/lib/connection_cubit/connection_cubit.dart index cb1fb9c..81bdde7 100644 --- a/lib/connection_cubit/connection_cubit.dart +++ b/lib/connection_cubit/connection_cubit.dart @@ -15,8 +15,8 @@ class ConnectionCubit extends Cubit { return _instance; } - void connect() { - ServerConnection.getInstance().connect(); + void connect(String playerID) { + ServerConnection.getInstance().connect(playerID); emit(ConnectionCubitState(true)); } } diff --git a/lib/models/models.dart b/lib/models/models.dart new file mode 100644 index 0000000..ec6e9a5 --- /dev/null +++ b/lib/models/models.dart @@ -0,0 +1,17 @@ +import 'package:uuid/uuid.dart'; + +class ResponseFromRegisteringGame { + final UuidValue playerID; + + const ResponseFromRegisteringGame({ + required this.playerID, + }); + + factory ResponseFromRegisteringGame.fromJson(Map json) { + final uuid = UuidValue(json['playerID']); + + return ResponseFromRegisteringGame( + playerID: uuid, + ); + } +} diff --git a/lib/pages/chess_game.dart b/lib/pages/chess_game.dart index 6288792..a42702d 100644 --- a/lib/pages/chess_game.dart +++ b/lib/pages/chess_game.dart @@ -1,6 +1,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:go_router/go_router.dart'; import 'package:mchess/chess_bloc/chess_bloc.dart'; import 'package:mchess/chess/chess_board.dart'; @@ -9,9 +10,11 @@ import 'package:mchess/connection_cubit/connection_cubit.dart'; import 'package:mchess/connection/ws_connection.dart'; import 'package:mchess/utils/widgets/server_log_widget.dart'; +import 'package:uuid/uuid.dart'; class ChessGame extends StatefulWidget { - const ChessGame({super.key}); + final UuidValue playerID; + const ChessGame({required this.playerID, super.key}); @override State createState() => _ChessGameState(); @@ -21,7 +24,7 @@ class _ChessGameState extends State { @override void initState() { super.initState(); - ConnectionCubit.getInstance().connect(); + ConnectionCubit.getInstance().connect(widget.playerID.uuid); } @override @@ -59,9 +62,9 @@ class _ChessGameState extends State { ), floatingActionButton: FloatingActionButton( onPressed: () { - ConnectionCubit.getInstance().connect(); + context.goNamed('lobbySelector'); }, - child: const Icon(Icons.network_wifi), + child: const Icon(Icons.arrow_back), ), ); } diff --git a/lib/pages/lobby_selector.dart b/lib/pages/lobby_selector.dart index 41ddf81..88f9aa4 100644 --- a/lib/pages/lobby_selector.dart +++ b/lib/pages/lobby_selector.dart @@ -1,8 +1,5 @@ -import 'dart:developer'; - import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; -import 'package:http/http.dart' as http; class LobbySelector extends StatelessWidget { const LobbySelector({super.key}); @@ -12,16 +9,10 @@ class LobbySelector extends StatelessWidget { return Scaffold( body: TextButton( onPressed: () { - var randomGameFuture = registerForRandomGame(); - log(randomGameFuture.toString()); - context.goNamed('prepareChessGame', extra: randomGameFuture); + context.goNamed('prepareChessGame'); }, child: const Text('Random lobby'), ), ); } - - Future registerForRandomGame() { - return http.get(Uri.parse('http://localhost:8080/api/random')); - } } diff --git a/lib/pages/prepare_chess_game.dart b/lib/pages/prepare_chess_game.dart index 4240977..938847a 100644 --- a/lib/pages/prepare_chess_game.dart +++ b/lib/pages/prepare_chess_game.dart @@ -1,24 +1,41 @@ import 'dart:developer'; import 'package:flutter/material.dart'; +import 'package:mchess/models/models.dart'; import 'package:mchess/pages/chess_game.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; class PrepareChessGameWidget extends StatelessWidget { - final Future responseFutureForRegistering; - const PrepareChessGameWidget( - {required this.responseFutureForRegistering, super.key}); + const PrepareChessGameWidget({super.key}); @override Widget build(BuildContext context) { return FutureBuilder( - future: responseFutureForRegistering, + future: registerForRandomGame(), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.done) { - log('future done'); - return const ChessGame(); + log('future done ${snapshot.data?.playerID}'); + return ChessGame( + playerID: snapshot.data!.playerID, + ); } return const CircularProgressIndicator(); }, ); } + + Future registerForRandomGame() async { + final response = + await http.get(Uri.parse('http://localhost:8080/api/random')); + + if (response.statusCode == 200) { + log(response.body); + return ResponseFromRegisteringGame.fromJson(jsonDecode(response.body)); + } else { + // If the server did not return a 200 OK response, + // then throw an exception. + throw Exception('Failed to load album'); + } + } } diff --git a/lib/utils/chess_router.dart b/lib/utils/chess_router.dart index afaa40e..5f5e4fd 100644 --- a/lib/utils/chess_router.dart +++ b/lib/utils/chess_router.dart @@ -22,8 +22,7 @@ class ChessAppRouter { path: '/play', name: 'prepareChessGame', builder: (context, state) { - Future future = state.extra as Future; - return PrepareChessGameWidget(responseFutureForRegistering: future); + return const PrepareChessGameWidget(); }) ], ); diff --git a/pubspec.lock b/pubspec.lock index e1154f5..e341f79 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -301,6 +301,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" + uuid: + dependency: "direct main" + description: + name: uuid + sha256: "648e103079f7c64a36dc7d39369cabb358d377078a051d6ae2ad3aa539519313" + url: "https://pub.dev" + source: hosted + version: "3.0.7" vector_math: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 3467aad..11c131e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -43,6 +43,7 @@ dependencies: web_socket_channel: ^2.2.0 go_router: ^6.0.0 http: ^1.0.0 + uuid: ^3.0.7 dev_dependencies: flutter_test: diff --git a/test/widget_test.dart b/test/widget_test.dart index 721f7e3..eaee974 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -8,11 +8,14 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mchess/pages/chess_game.dart'; +import 'package:uuid/uuid.dart'; void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async { // Build our app and trigger a frame. - await tester.pumpWidget(const ChessGame()); + await tester.pumpWidget(ChessGame( + playerID: UuidValue("test"), + )); // Verify that our counter starts at 0. expect(find.text('0'), findsOneWidget);