mchess-client/lib/api/register.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';
2023-06-29 23:49:18 +00:00
class PlayerInfo {
final UuidValue? playerID;
final UuidValue? lobbyID;
final String? passphrase;
2023-06-02 21:28:40 +00:00
2023-06-29 23:49:18 +00:00
const PlayerInfo({
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
});
factory PlayerInfo.empty() {
return const PlayerInfo(playerID: null, lobbyID: null, passphrase: null);
}
2023-06-29 23:49:18 +00:00
factory PlayerInfo.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
2023-06-29 23:49:18 +00:00
return PlayerInfo(
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);
}
static Future<PlayerInfo?> 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;
}
return PlayerInfo(
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
}