Introduce changes in API (register game first, then ws connection).

This commit is contained in:
Marco 2023-05-28 14:54:46 +02:00
parent cf12bc08c4
commit 9492644ac9
11 changed files with 109 additions and 34 deletions

View File

@ -31,4 +31,6 @@
android:name="flutterEmbedding"
android:value="2" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>

View File

@ -1,3 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
@ -71,6 +72,9 @@ class ChessSquare extends StatelessWidget {
}
},
builder: (context, candidateData, rejectedData) {
var maxDrags =
kDebugMode ? 1 : (ChessBloc.turnColor == ChessBloc.myColor ? 1 : 0);
return Container(
color: color,
width: ChessSquare.pieceWidth,
@ -78,8 +82,7 @@ class ChessSquare extends StatelessWidget {
child: Draggable<PieceMovedFrom>(
/* We create the move with the startSquare == endSquare. The receiving widget will give the correct value to end square. */
data: PieceMovedFrom(coordinate, containedPiece),
maxSimultaneousDrags:
ChessBloc.turnColor == ChessBloc.myColor ? 1 : 0,
maxSimultaneousDrags: maxDrags,
feedback: FractionalTranslation(
translation: const Offset(-0.5, -0.75),
child: SizedBox(

View File

@ -1,6 +1,4 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:mchess/utils/chess_utils.dart';
typedef ChessPositionType = Map<ChessCoordinate, ChessPiece>;

View File

@ -1,4 +1,5 @@
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:mchess/chess_bloc/chess_bloc.dart';
import 'package:mchess/chess_bloc/chess_events.dart';
import 'package:mchess/chess_bloc/chess_position.dart';
@ -9,13 +10,12 @@ class ServerConnection {
late WebSocketChannel channel;
late bool wasConnected = false;
late int counter = 0;
late Stream broadcast;
Stream broadcast = const Stream.empty();
static final ServerConnection _instance = ServerConnection._internal();
ServerConnection._internal() {
log("ServerConnection._internal constructor is called");
connect();
}
factory ServerConnection() {
@ -34,8 +34,13 @@ class ServerConnection {
void connect() {
if (wasConnected) channel.sink.close();
if (kDebugMode) {
channel =
WebSocketChannel.connect(Uri.parse('ws://localhost:8080/api/ws'));
} else {
channel =
WebSocketChannel.connect(Uri.parse('wss://chess.sw-gross.de:8080'));
}
log(channel.closeCode.toString());

View File

@ -15,7 +15,7 @@ class ConnectionCubit extends Cubit<ConnectionCubitState> {
return _instance;
}
void reconnect() {
void connect() {
ServerConnection.getInstance().connect();
emit(ConnectionCubitState(true));
}

View File

@ -10,9 +10,20 @@ import 'package:mchess/connection_cubit/connection_cubit.dart';
import 'package:mchess/connection/ws_connection.dart';
import 'package:mchess/utils/widgets/server_log_widget.dart';
class ChessGame extends StatelessWidget {
class ChessGame extends StatefulWidget {
const ChessGame({super.key});
@override
State<ChessGame> createState() => _ChessGameState();
}
class _ChessGameState extends State<ChessGame> {
@override
void initState() {
super.initState();
ConnectionCubit.getInstance().connect();
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -48,7 +59,7 @@ class ChessGame extends StatelessWidget {
),
floatingActionButton: FloatingActionButton(
onPressed: () {
ConnectionCubit.getInstance().reconnect();
ConnectionCubit.getInstance().connect();
},
child: const Icon(Icons.network_wifi),
),

View File

@ -1,5 +1,9 @@
import 'dart:developer';
import 'dart:io';
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});
@ -9,10 +13,16 @@ class LobbySelector extends StatelessWidget {
return Scaffold(
body: TextButton(
onPressed: () {
context.push("/play");
var randomGameFuture = registerForRandomGame();
log(randomGameFuture.toString());
context.goNamed('prepareChessGame', extra: randomGameFuture);
},
child: const Text('Random lobby'),
),
);
}
Future<http.Response> registerForRandomGame() {
return http.get(Uri.parse('http://localhost:8080/api/random'));
}
}

View File

@ -0,0 +1,24 @@
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:mchess/pages/chess_game.dart';
class PrepareChessGameWidget extends StatelessWidget {
final Future responseFutureForRegistering;
const PrepareChessGameWidget(
{required this.responseFutureForRegistering, super.key});
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: responseFutureForRegistering,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
log('future done');
return const ChessGame();
}
return const CircularProgressIndicator();
},
);
}
}

View File

@ -1,6 +1,7 @@
import 'package:go_router/go_router.dart';
import 'package:mchess/pages/chess_game.dart';
import 'package:mchess/pages/lobby_selector.dart';
import 'package:mchess/pages/prepare_chess_game.dart';
class ChessAppRouter {
static final ChessAppRouter _instance = ChessAppRouter._internal();
@ -15,12 +16,16 @@ class ChessAppRouter {
routes: [
GoRoute(
path: '/',
name: 'lobbySelector',
builder: (context, state) => const LobbySelector(),
),
GoRoute(
path: '/play',
builder: (context, state) => const ChessGame(),
)
name: 'prepareChessGame',
builder: (context, state) {
Future future = state.extra as Future;
return PrepareChessGameWidget(responseFutureForRegistering: future);
})
],
);
}

View File

@ -5,10 +5,10 @@ packages:
dependency: transitive
description:
name: async
sha256: bfe67ef28df125b7dddcea62755991f807aa39a2492a23e1550161692950bbe0
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
url: "https://pub.dev"
source: hosted
version: "2.10.0"
version: "2.11.0"
bloc:
dependency: transitive
description:
@ -29,10 +29,10 @@ packages:
dependency: transitive
description:
name: characters
sha256: e6a326c8af69605aec75ed6c187d06b349707a27fbff8222ca9cc2cff167975c
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
version: "1.3.0"
clock:
dependency: transitive
description:
@ -45,10 +45,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: cfc915e6923fe5ce6e153b0723c753045de46de1b4d63771530504004a45fae0
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
url: "https://pub.dev"
source: hosted
version: "1.17.0"
version: "1.17.2"
crypto:
dependency: transitive
description:
@ -120,14 +120,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "6.0.0"
http:
dependency: "direct main"
description:
name: http
sha256: "4c3f04bfb64d3efd508d06b41b825542f08122d30bda4933fb95c069d22a4fa3"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
http_parser:
dependency: transitive
description:
name: http_parser
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
url: "https://pub.dev"
source: hosted
version: "4.0.2"
js:
dependency: transitive
description:
name: js
sha256: "323b7c70073cccf6b9b8d8b334be418a3293cfb612a560dc2737160a37bf61bd"
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
url: "https://pub.dev"
source: hosted
version: "0.6.6"
version: "0.6.7"
lints:
dependency: transitive
description:
@ -148,26 +164,26 @@ packages:
dependency: transitive
description:
name: matcher
sha256: c94db23593b89766cda57aab9ac311e3616cf87c6fa4e9749df032f66f30dcb8
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
url: "https://pub.dev"
source: hosted
version: "0.12.14"
version: "0.12.15"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
version: "0.5.0"
meta:
dependency: transitive
description:
name: meta
sha256: "6c268b42ed578a53088d834796959e4a1814b5e9e164f147f580a386e5decf42"
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
url: "https://pub.dev"
source: hosted
version: "1.8.0"
version: "1.9.1"
nested:
dependency: transitive
description:
@ -233,10 +249,10 @@ packages:
dependency: transitive
description:
name: source_span
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
stack_trace:
dependency: transitive
description:
@ -273,10 +289,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "6182294da5abf431177fccc1ee02401f6df30f766bc6130a0852c6b6d7ee6b2d"
sha256: daadc9baabec998b062c9091525aa95786508b1c48e9c30f1f891b8bf6ff2e64
url: "https://pub.dev"
source: hosted
version: "0.4.18"
version: "0.5.2"
typed_data:
dependency: transitive
description:
@ -310,5 +326,5 @@ packages:
source: hosted
version: "6.2.2"
sdks:
dart: ">=2.19.0-392.0.dev <4.0.0"
dart: ">=3.0.0 <4.0.0"
flutter: ">=3.3.0"

View File

@ -42,6 +42,7 @@ dependencies:
quiver: ^3.1.0
web_socket_channel: ^2.2.0
go_router: ^6.0.0
http: ^1.0.0
dev_dependencies:
flutter_test: