Add chess games over channel data
Play and watch chess on a channel via the Chess games entry in the channel overflow menu. Moves travel as GRP_DATA packets (type 0x0100) carrying the full position and a hash-chain value for authentication. Includes translations for all locales.
This commit is contained in:
1 parent
9ac915bdf2
commit
185b026a0d
66 files changed
+15314
-25
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import '../../storage/prefs_manager.dart';
|
||||
import 'chess_game.dart';
|
||||
|
||||
/// Per-radio list of chess games in preferences, keyed by the first 10 hex
|
||||
/// chars of the radio's public key.
|
||||
class ChessStore {
|
||||
static const String _keyPrefix = 'chess_games_';
|
||||
|
||||
Future<void> _pending = Future.value();
|
||||
|
||||
static String keyFor(String scope) => '$_keyPrefix$scope';
|
||||
|
||||
/// Games saved for [scope]; records that fail to decode are skipped.
|
||||
List<ChessGame> load(String scope) {
|
||||
if (scope.isEmpty) return [];
|
||||
try {
|
||||
final raw = PrefsManager.instance.getString(keyFor(scope));
|
||||
if (raw == null || raw.isEmpty) return [];
|
||||
final decoded = jsonDecode(raw);
|
||||
if (decoded is! List) return [];
|
||||
return decoded.map(ChessGame.fromJson).whereType<ChessGame>().toList();
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// Saves [games] for [scope]. Writes are serialized.
|
||||
Future<void> save(String scope, List<ChessGame> games) {
|
||||
if (scope.isEmpty) return Future.value();
|
||||
final json = jsonEncode(games.map((g) => g.toJson()).toList());
|
||||
return _pending = _pending.then((_) async {
|
||||
try {
|
||||
await PrefsManager.instance.setString(keyFor(scope), json);
|
||||
} catch (_) {}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> flush() => _pending;
|
||||
}
|
||||
Reference in new issue
Block a user