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';
|
|
|
|
|
2024-05-19 19:18:47 +00:00
|
|
|
class GameInfo {
|
2023-06-29 23:49:18 +00:00
|
|
|
final UuidValue? playerID;
|
|
|
|
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,
|
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() {
|
2024-05-20 13:34:20 +00:00
|
|
|
return const GameInfo(playerID: null, passphrase: null);
|
2024-05-15 17:42:18 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 19:18:47 +00:00
|
|
|
factory GameInfo.fromJson(Map<String, dynamic> json) {
|
2023-12-09 19:34:52 +00:00
|
|
|
final playerid = UuidValue.fromString(json['playerID']);
|
2023-06-29 23:49:18 +00:00
|
|
|
final passphrase = json['passphrase'];
|
2023-06-02 21:28:40 +00:00
|
|
|
|
2024-05-20 13:34:20 +00:00
|
|
|
return GameInfo(playerID: playerid, 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;
|
|
|
|
|
|
|
|
if (playerID != null) {
|
|
|
|
pid = playerID.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'playerID': pid,
|
|
|
|
'passphrase': passphrase,
|
|
|
|
};
|
|
|
|
}
|
2023-12-23 15:44:23 +00:00
|
|
|
|
|
|
|
void store() async {
|
|
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
|
|
|
2024-05-20 13:34:20 +00:00
|
|
|
await prefs.setString(passphrase!, playerID.toString());
|
2023-12-23 15:44:23 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 13:34:20 +00:00
|
|
|
static Future<GameInfo?> get(String phrase) async {
|
2023-12-23 15:44:23 +00:00
|
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
2024-05-20 13:34:20 +00:00
|
|
|
var playerID = prefs.getString(phrase);
|
2023-12-23 15:44:23 +00:00
|
|
|
|
2024-05-20 13:34:20 +00:00
|
|
|
if (playerID == null) return null;
|
2023-12-23 15:44:23 +00:00
|
|
|
|
2024-05-19 19:18:47 +00:00
|
|
|
return GameInfo(
|
2024-05-20 13:34:20 +00:00
|
|
|
playerID: UuidValue.fromString(playerID!), passphrase: phrase);
|
2023-12-23 15:44:23 +00:00
|
|
|
}
|
2023-06-02 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class WebsocketMessageIdentifyPlayer {
|
|
|
|
final String playerID;
|
2023-06-29 23:49:18 +00:00
|
|
|
final String? passphrase;
|
2023-06-02 21:28:40 +00:00
|
|
|
|
|
|
|
const WebsocketMessageIdentifyPlayer({
|
|
|
|
required this.playerID,
|
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() =>
|
2024-05-20 13:34:20 +00:00
|
|
|
{'playerID': playerID, 'passphrase': passphrase};
|
2023-06-02 21:28:40 +00:00
|
|
|
}
|