blob: 5ffdc3666ca1acfa8fa17bd88a3c7cfe70f50a38 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import 'dart:io';
import 'package:path/path.dart' as p;
/// Persistent store for the authoritative game-state snapshot.
///
/// Replaces the reference app's `shared_preferences`-backed persistence with a
/// small synchronous store: the server saves after every action and must be
/// able to read back the snapshot before broadcasting (Phase 2).
abstract class SaveStore {
/// Returns the stored snapshot, or null if nothing has been saved yet.
String? read();
/// Writes the snapshot.
void write(String data);
/// Removes the stored snapshot.
void clear();
}
/// In-memory [SaveStore] — used by tests.
class InMemorySaveStore implements SaveStore {
String? _data;
@override
String? read() => _data;
@override
void write(String data) => _data = data;
@override
void clear() => _data = null;
}
/// File-backed [SaveStore] writing a single JSON snapshot into [directory].
class FileSaveStore implements SaveStore {
final File _file;
FileSaveStore(String directory)
: _file = File(p.join(directory, 'game.json'));
@override
String? read() => _file.existsSync() ? _file.readAsStringSync() : null;
@override
void write(String data) {
final dir = _file.parent;
if (!dir.existsSync()) dir.createSync(recursive: true);
_file.writeAsString(data, flush: true);
}
@override
void clear() {
if (_file.existsSync()) _file.deleteSync();
}
}
|