import 'dart:convert'; import 'dart:developer'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:http/http.dart' as http; import 'package:mchess/api/register.dart'; import 'package:mchess/connection_cubit/connection_cubit.dart'; import 'package:mchess/pages/chess_game.dart'; import 'package:shared_preferences/shared_preferences.dart'; class LobbySelector extends StatefulWidget { const LobbySelector({super.key}); @override State createState() => _LobbySelectorState(); } class _LobbySelectorState extends State { final buttonStyle = const ButtonStyle(); final myController = TextEditingController(); late Future joinGameFuture; @override Widget build(BuildContext context) { SharedPreferences.getInstance().then((prefs) { final playerID = prefs.getString("playerID"); final lobbyID = prefs.getString("lobbyID"); final passphrase = prefs.getString("passphrase"); log("lobbyID: $lobbyID and playerID: $playerID and passphrase: $passphrase"); }); return Scaffold( body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ ElevatedButton( onPressed: () { buildJoinOrHostDialog(context); }, child: const Row( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.lock), SizedBox( width: 10, ), Text('Private game') ], ), ), ], ), ), ); } Future buildJoinOrHostDialog(BuildContext context) { return showDialog( context: context, builder: (BuildContext context) { return Scaffold( body: AlertDialog( title: const Text('Host or join?'), actions: [ TextButton( child: const Text('Cancel'), onPressed: () { context.pop(); }, ), TextButton( child: const Text('Host'), onPressed: () { context.push('/host'); //replaces joinOrHostDialog }, ), TextButton( child: const Text('Join'), onPressed: () { buildEnterPassphraseDialog(context); }, ), ], ), ); }, ); } Future buildEnterPassphraseDialog(BuildContext context) { return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: const Text('Enter the passphrase here:'), content: TextField( controller: myController, decoration: InputDecoration( hintText: 'Enter passphrase here', suffixIcon: IconButton( onPressed: () { joinGameFuture = joinPrivateGame(); joinGameFuture.then((value) { if (value == null) return; switchToGame(value); }); }, icon: const Icon(Icons.check), )), ), actions: [ TextButton( child: const Text('Cancel'), onPressed: () { context.pop(); }, ), ], ); }, ); } void switchToGame(PlayerInfo info) { var chessGameArgs = ChessGameArguments( lobbyID: info.lobbyID!, playerID: info.playerID!, passphrase: info.passphrase); ConnectionCubit.getInstance().connect( info.playerID!.uuid, info.lobbyID!.uuid, info.passphrase, ); if (!chessGameArgs.isValid()) { context.goNamed('lobbySelector'); const snackBar = SnackBar( backgroundColor: Colors.amberAccent, content: Text("Game information is corrupted"), ); ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar(snackBar); } context.pushReplacement('/game', extra: chessGameArgs); } Future joinPrivateGame() async { String addr; http.Response response; // server expects us to send the passphrase var info = PlayerInfo( playerID: null, lobbyID: null, passphrase: myController.text); if (kDebugMode) { addr = 'http://localhost:8080/api/joinPrivate'; } else { addr = 'https://chess.sw-gross.de:9999/api/joinPrivate'; } try { response = await http.post(Uri.parse(addr), body: jsonEncode(info), headers: {"Accept": "application/json"}); } catch (e) { log(e.toString()); if (!context.mounted) return null; const snackBar = SnackBar( backgroundColor: Colors.amberAccent, content: Text("mChess server is not responding. Try again or give up"), ); ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar(snackBar); return null; } if (response.statusCode == 200) { var info = PlayerInfo.fromJson(jsonDecode(response.body)); log('Player info received from server: '); log('lobbyID: ${info.lobbyID}'); log('playerID: ${info.playerID}'); log('passphrase: ${info.passphrase}'); return info; } return null; } }