make games rejoinable

This commit is contained in:
Marco 2024-05-15 14:27:48 +02:00
parent 618102dd67
commit 2b6e62ae32
3 changed files with 48 additions and 19 deletions

View File

@ -12,6 +12,10 @@ class PlayerInfo {
required this.passphrase,
});
factory PlayerInfo.empty() {
return const PlayerInfo(playerID: null, lobbyID: null, passphrase: null);
}
factory PlayerInfo.fromJson(Map<String, dynamic> json) {
final playerid = UuidValue.fromString(json['playerID']);
final lobbyid = UuidValue.fromString(json['lobbyID']);
@ -21,11 +25,24 @@ class PlayerInfo {
playerID: playerid, lobbyID: lobbyid, passphrase: passphrase);
}
Map<String, dynamic> toJson() => {
'playerID': playerID,
'lobbyID': lobbyID,
'passphrase': passphrase,
};
Map<String, dynamic> toJson() {
String? pid;
String? lid;
if (playerID != null) {
pid = playerID.toString();
}
if (lobbyID != null) {
lid = lobbyID.toString();
}
return {
'playerID': pid,
'lobbyID': lid,
'passphrase': passphrase,
};
}
void store() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
@ -42,7 +59,7 @@ class PlayerInfo {
await prefs.setBool("contains", false);
}
Future<PlayerInfo?> get() async {
static Future<PlayerInfo?> get() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
var contains = prefs.getBool("contains");
var playerID = prefs.getString("playerID");

View File

@ -6,10 +6,10 @@ 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/ws_connection.dart';
import 'package:mchess/connection_cubit/connection_cubit.dart';
import 'package:mchess/pages/chess_game.dart';
import 'package:mchess/utils/config.dart' as config;
import 'package:shared_preferences/shared_preferences.dart';
class LobbySelector extends StatefulWidget {
const LobbySelector({super.key});
@ -25,13 +25,6 @@ class _LobbySelectorState extends State<LobbySelector> {
@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(
@ -57,6 +50,9 @@ class _LobbySelectorState extends State<LobbySelector> {
}
Future<void> buildJoinOrHostDialog(BuildContext context) {
//TODO: find a better place to disconnect old websocket connection
ServerConnection.getInstance().disconnectExistingConnection();
return showDialog<void>(
context: context,
builder: (BuildContext context) {
@ -170,13 +166,28 @@ class _LobbySelectorState extends State<LobbySelector> {
Future<PlayerInfo?> joinPrivateGame(BuildContext context) async {
http.Response response;
// server expects us to send the passphrase
var info = PlayerInfo(
playerID: null, lobbyID: null, passphrase: phraseController.text);
var existingInfo = await PlayerInfo.get();
log("lobbyID: ${existingInfo?.lobbyID} and playerID: ${existingInfo?.playerID} and passphrase: ${existingInfo?.passphrase}");
PlayerInfo info;
if (existingInfo?.passphrase == phraseController.text) {
// We have player info for this exact passphrase
info = PlayerInfo(
playerID: existingInfo?.playerID,
lobbyID: existingInfo?.lobbyID,
passphrase: phraseController.text);
} else {
info = PlayerInfo(
playerID: null, lobbyID: null, passphrase: phraseController.text);
}
var decodedInfo = jsonEncode(info);
log("decodedInfo: $decodedInfo");
try {
response = await http.post(Uri.parse(config.getJoinURL()),
body: jsonEncode(info), headers: {"Accept": "application/json"});
body: decodedInfo, headers: {"Accept": "application/json"});
} catch (e) {
log(e.toString());
@ -206,6 +217,7 @@ class _LobbySelectorState extends State<LobbySelector> {
if (response.statusCode == HttpStatus.ok) {
var info = PlayerInfo.fromJson(jsonDecode(response.body));
info.store();
log('Player info received from server: ');
log('lobbyID: ${info.lobbyID}');
log('playerID: ${info.playerID}');

View File

@ -1,7 +1,7 @@
const prodURL = 'chess.sw-gross.de:9999';
const debugURL = 'localhost:8080';
const useDbgUrl = false;
const useDbgUrl = true;
String getHostURL() {
var prot = 'https';