2023-06-29 23:49:18 +00:00
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2024-01-17 21:50:02 +00:00
|
|
|
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';
|
2024-01-17 21:50:02 +00:00
|
|
|
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;
|
2024-05-20 15:21:25 +00:00
|
|
|
late Future disconnectFuture;
|
2023-06-29 23:49:18 +00:00
|
|
|
late ChessGameArguments chessGameArgs;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-05-20 15:21:25 +00:00
|
|
|
disconnectFuture = ConnectionCubit().disonnect();
|
|
|
|
disconnectFuture.then((val) {
|
|
|
|
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-20 15:21:25 +00:00
|
|
|
child: FutureBuilder(
|
|
|
|
future: disconnectFuture,
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState != ConnectionState.done) {
|
|
|
|
return Container();
|
|
|
|
} else {
|
|
|
|
return FutureBuilder<GameInfo?>(
|
|
|
|
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";
|
2024-05-20 13:34:20 +00:00
|
|
|
|
2024-05-20 15:21:25 +00:00
|
|
|
ConnectionCubit().connect(
|
|
|
|
snapshot.data!.playerID.toString(),
|
|
|
|
snapshot.data!.passphrase,
|
|
|
|
);
|
2024-05-20 13:34:20 +00:00
|
|
|
|
2024-05-20 15:21:25 +00:00
|
|
|
return BlocListener<ConnectionCubit,
|
|
|
|
ConnectionCubitState>(
|
|
|
|
listener: (context, state) {
|
|
|
|
// We wait for our opponent to connect
|
|
|
|
if (state.opponentConnected) {
|
|
|
|
//TODO: is goNamed the correct way to navigate?
|
|
|
|
context.goNamed('game', pathParameters: {
|
|
|
|
'phrase': passphrase.toURL(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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));
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 25),
|
|
|
|
const CircularProgressIndicator()
|
|
|
|
],
|
2024-01-17 21:50:02 +00:00
|
|
|
),
|
2024-05-20 15:21:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}),
|
2023-06-29 23:49:18 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-20 13:34:20 +00:00
|
|
|
Future<GameInfo?> createPrivateGame() async {
|
2023-06-29 23:49:18 +00:00
|
|
|
Response response;
|
|
|
|
|
|
|
|
try {
|
2024-05-20 13:34:20 +00:00
|
|
|
response = await http.get(Uri.parse(config.getCreateGameURL()),
|
2024-01-17 21:50:02 +00:00
|
|
|
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();
|
2024-05-15 17:42:18 +00:00
|
|
|
return info;
|
2023-06-29 23:49:18 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|