Marco
7a51e71767
Additionally, we set some groundwork for storing the game data (lobby id, player id, passphrase) in permanent storage in order to reconnect with it later.
144 lines
4.2 KiB
Dart
144 lines
4.2 KiB
Dart
import 'dart:developer';
|
|
|
|
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:http/http.dart';
|
|
import 'package:mchess/api/register.dart';
|
|
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';
|
|
|
|
class HostGameWidget extends StatefulWidget {
|
|
const HostGameWidget({super.key});
|
|
|
|
@override
|
|
State<HostGameWidget> createState() => _HostGameWidgetState();
|
|
}
|
|
|
|
class _HostGameWidgetState extends State<HostGameWidget> {
|
|
late Future<PlayerInfo?> registerResponse;
|
|
late ChessGameArguments chessGameArgs;
|
|
|
|
@override
|
|
void initState() {
|
|
registerResponse = hostPrivateGame();
|
|
registerResponse.then((value) {
|
|
value?.store();
|
|
});
|
|
connectToWebsocket(registerResponse);
|
|
|
|
super.initState();
|
|
}
|
|
|
|
void connectToWebsocket(Future<PlayerInfo?> resp) {
|
|
resp.then((value) {
|
|
if (value == null) return;
|
|
|
|
chessGameArgs = ChessGameArguments(
|
|
lobbyID: value.lobbyID!,
|
|
playerID: value.playerID!,
|
|
passphrase: value.passphrase);
|
|
|
|
ConnectionCubit.getInstance().connect(
|
|
value.playerID!.uuid,
|
|
value.lobbyID!.uuid,
|
|
value.passphrase,
|
|
);
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: Center(
|
|
child: FutureBuilder<PlayerInfo?>(
|
|
future: registerResponse,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState != ConnectionState.done) {
|
|
return const SizedBox(
|
|
height: 100,
|
|
width: 100,
|
|
child: CircularProgressIndicator(),
|
|
);
|
|
} else {
|
|
String passphrase = snapshot.data?.passphrase ?? "no passphrase";
|
|
return BlocListener<ConnectionCubit, ConnectionCubitState>(
|
|
listener: (context, state) {
|
|
// We wait for our opponent to connect
|
|
if (state.opponentConnected) {
|
|
context.push('/game', extra: chessGameArgs);
|
|
}
|
|
},
|
|
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),
|
|
SelectableText(
|
|
passphrase,
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
),
|
|
const SizedBox(height: 25),
|
|
const CircularProgressIndicator()
|
|
],
|
|
),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
child: const Icon(Icons.cancel),
|
|
onPressed: () {
|
|
context.push('/');
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<PlayerInfo?> hostPrivateGame() async {
|
|
String addr;
|
|
Response response;
|
|
|
|
if (kDebugMode) {
|
|
addr = 'http://localhost:8080/api/hostPrivate';
|
|
} else {
|
|
addr = 'https://chess.sw-gross.de:9999/api/hostPrivate';
|
|
}
|
|
|
|
try {
|
|
response = await http
|
|
.get(Uri.parse(addr), 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"),
|
|
);
|
|
Future.delayed(const Duration(seconds: 2), () {
|
|
ScaffoldMessenger.of(context).clearSnackBars();
|
|
ScaffoldMessenger.of(context).showSnackBar(snackBar);
|
|
context.push('/'); // We go back to the lobby selector
|
|
});
|
|
return null;
|
|
}
|
|
|
|
if (response.statusCode == 200) {
|
|
log(response.body);
|
|
return PlayerInfo.fromJson(jsonDecode(response.body));
|
|
}
|
|
return null;
|
|
}
|
|
}
|