mchess-client/lib/pages/create_game_widget.dart

148 lines
4.7 KiB
Dart
Raw Normal View History

2023-06-29 23:49:18 +00:00
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2023-06-29 23:49:18 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart';
2024-05-19 19:18:47 +00:00
import 'package:mchess/api/game_info.dart';
2023-06-29 23:49:18 +00:00
import 'package:mchess/connection_cubit/connection_cubit.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:mchess/pages/chess_game.dart';
import 'package:mchess/utils/config.dart' as config;
2024-05-19 19:18:47 +00:00
import 'package:mchess/utils/passphrase.dart';
import 'package:universal_platform/universal_platform.dart';
2023-06-29 23:49:18 +00:00
2024-05-19 19:18:47 +00:00
class CreateGameWidget extends StatefulWidget {
const CreateGameWidget({super.key});
2023-06-29 23:49:18 +00:00
@override
2024-05-19 19:18:47 +00:00
State<CreateGameWidget> createState() => _CreateGameWidgetState();
2023-06-29 23:49:18 +00:00
}
2024-05-19 19:18:47 +00:00
class _CreateGameWidgetState extends State<CreateGameWidget> {
late Future<GameInfo?> registerResponse;
2023-06-29 23:49:18 +00:00
late ChessGameArguments chessGameArgs;
@override
void initState() {
super.initState();
ConnectionCubit().disonnect();
registerResponse = createPrivateGame();
2023-06-29 23:49:18 +00:00
}
@override
Widget build(BuildContext context) {
2024-05-19 19:18:47 +00:00
FloatingActionButton? fltnBtn;
if (UniversalPlatform.isLinux ||
UniversalPlatform.isMacOS ||
UniversalPlatform.isWindows) {
fltnBtn = FloatingActionButton(
child: const Icon(Icons.cancel),
onPressed: () {
context.pop();
},
);
}
2023-06-29 23:49:18 +00:00
return Scaffold(
2024-05-19 19:18:47 +00:00
floatingActionButton: fltnBtn,
2023-06-29 23:49:18 +00:00
body: Center(
2024-05-19 19:18:47 +00:00
child: FutureBuilder<GameInfo?>(
2023-06-29 23:49:18 +00:00
future: registerResponse,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const SizedBox(
height: 100,
width: 100,
child: CircularProgressIndicator(),
);
} else {
var passphrase = snapshot.data?.passphrase ?? "no passphrase";
ConnectionCubit().connect(
snapshot.data!.playerID.toString(),
snapshot.data!.passphrase,
);
2023-06-29 23:49:18 +00:00
return BlocListener<ConnectionCubit, ConnectionCubitState>(
listener: (context, state) {
// We wait for our opponent to connect
if (state.opponentConnected) {
2024-05-19 19:18:47 +00:00
//TODO: is goNamed the correct way to navigate?
context.goNamed('game', pathParameters: {
'phrase': passphrase.toURL(),
});
2023-06-29 23:49:18 +00:00
}
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Give this phrase to your friend and sit tight:',
style: TextStyle(
color: Theme.of(context).colorScheme.primary),
),
const SizedBox(height: 25),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SelectableText(
passphrase,
style: const TextStyle(fontWeight: FontWeight.bold),
),
IconButton(
icon: const Icon(Icons.copy),
onPressed: () async {
await Clipboard.setData(
ClipboardData(text: passphrase));
},
)
],
2023-06-29 23:49:18 +00:00
),
const SizedBox(height: 25),
const CircularProgressIndicator()
],
),
);
}
},
),
),
);
}
Future<GameInfo?> createPrivateGame() async {
2023-06-29 23:49:18 +00:00
Response response;
try {
response = await http.get(Uri.parse(config.getCreateGameURL()),
headers: {"Accept": "application/json"});
2023-06-29 23:49:18 +00:00
} catch (e) {
2024-05-19 19:18:47 +00:00
log('Exception: ${e.toString()}');
2023-06-29 23:49:18 +00:00
const snackBar = SnackBar(
backgroundColor: Colors.amberAccent,
content: Text("mChess server is not responding. Try again or give up"),
);
Future.delayed(const Duration(seconds: 2), () {
2024-05-19 19:18:47 +00:00
if (!context.mounted) return null;
2023-06-29 23:49:18 +00:00
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(snackBar);
2023-12-27 14:46:15 +00:00
context.goNamed('lobbySelector'); // We go back to the lobby selector
2023-06-29 23:49:18 +00:00
});
return null;
}
if (response.statusCode == 200) {
2024-05-19 19:18:47 +00:00
var info = GameInfo.fromJson(jsonDecode(response.body));
info.store();
return info;
2023-06-29 23:49:18 +00:00
}
return null;
}
}