mchess-client/lib/api/game_info.dart

98 lines
2.5 KiB
Dart
Raw Normal View History

import 'package:shared_preferences/shared_preferences.dart';
2023-06-02 21:28:40 +00:00
import 'package:uuid/uuid.dart';
2024-05-19 19:18:47 +00:00
class GameInfo {
2023-06-29 23:49:18 +00:00
final UuidValue? playerID;
final UuidValue? lobbyID;
final String? passphrase;
2023-06-02 21:28:40 +00:00
2024-05-19 19:18:47 +00:00
const GameInfo({
2023-06-02 21:28:40 +00:00
required this.playerID,
required this.lobbyID,
2023-06-29 23:49:18 +00:00
required this.passphrase,
2023-06-02 21:28:40 +00:00
});
2024-05-19 19:18:47 +00:00
factory GameInfo.empty() {
return const GameInfo(playerID: null, lobbyID: null, passphrase: null);
}
2024-05-19 19:18:47 +00:00
factory GameInfo.fromJson(Map<String, dynamic> json) {
final playerid = UuidValue.fromString(json['playerID']);
final lobbyid = UuidValue.fromString(json['lobbyID']);
2023-06-29 23:49:18 +00:00
final passphrase = json['passphrase'];
2023-06-02 21:28:40 +00:00
2024-05-19 19:18:47 +00:00
return GameInfo(
2023-06-29 23:49:18 +00:00
playerID: playerid, lobbyID: lobbyid, passphrase: passphrase);
2023-06-02 21:28:40 +00:00
}
2023-06-29 23:49:18 +00:00
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();
await prefs.setBool("contains", true);
await prefs.setString("playerID", playerID.toString());
await prefs.setString("lobbyID", lobbyID.toString());
await prefs.setString("passphrase", passphrase.toString());
}
void delete() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool("contains", false);
}
2024-05-19 19:18:47 +00:00
static Future<GameInfo?> get() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
var contains = prefs.getBool("contains");
var playerID = prefs.getString("playerID");
var lobbyID = prefs.getString("lobbyID");
var passphrase = prefs.getString("passphrase");
if (contains == null ||
!contains ||
playerID == null ||
lobbyID == null ||
passphrase == null) {
return null;
}
2024-05-19 19:18:47 +00:00
return GameInfo(
playerID: UuidValue.fromString(playerID),
lobbyID: UuidValue.fromString(lobbyID),
passphrase: passphrase);
}
2023-06-02 21:28:40 +00:00
}
class WebsocketMessageIdentifyPlayer {
final String playerID;
final String lobbyID;
2023-06-29 23:49:18 +00:00
final String? passphrase;
2023-06-02 21:28:40 +00:00
const WebsocketMessageIdentifyPlayer({
required this.playerID,
required this.lobbyID,
2023-06-29 23:49:18 +00:00
required this.passphrase,
2023-06-02 21:28:40 +00:00
});
2023-06-29 23:49:18 +00:00
Map<String, dynamic> toJson() =>
{'lobbyID': lobbyID, 'playerID': playerID, 'passphrase': passphrase};
2023-06-02 21:28:40 +00:00
}