mchess-client/lib/api/game_info.dart

66 lines
1.5 KiB
Dart
Raw Permalink 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 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() {
return const GameInfo(playerID: null, passphrase: null);
}
2024-05-19 19:18:47 +00:00
factory GameInfo.fromJson(Map<String, dynamic> json) {
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
return GameInfo(playerID: playerid, passphrase: passphrase);
2023-06-02 21:28:40 +00:00
}
2023-06-29 23:49:18 +00:00
Map<String, dynamic> toJson() {
String? pid;
if (playerID != null) {
pid = playerID.toString();
}
return {
'playerID': pid,
'passphrase': passphrase,
};
}
void store() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString(passphrase!, playerID.toString());
}
static Future<GameInfo?> get(String phrase) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
var playerID = prefs.getString(phrase);
if (playerID == null) return null;
2024-05-19 19:18:47 +00:00
return GameInfo(
2024-05-21 16:48:06 +00:00
playerID: UuidValue.fromString(playerID), passphrase: phrase);
}
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() =>
{'playerID': playerID, 'passphrase': passphrase};
2023-06-02 21:28:40 +00:00
}