2023-12-23 15:44:23 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2024-05-15 17:42:18 +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) {
|
2023-12-09 19:34:52 +00:00
|
|
|
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
|
|
|
|
2024-05-15 17:42: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,
|
|
|
|
};
|
|
|
|
}
|
2023-12-23 15:44:23 +00:00
|
|
|
|
|
|
|
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-15 17:42:18 +00:00
|
|
|
static Future<PlayerInfo?> get() async {
|
2023-12-23 15:44:23 +00:00
|
|
|
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
|
|
|
}
|