-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgamedb.dart
235 lines (204 loc) · 6.69 KB
/
gamedb.dart
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/// handle sqlite database of games
///
///
///
import 'dart:io';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'io.dart';
import 'utils.dart';
class GameDB {
static const String dbFile = 'games.db';
static const int _version = 1;
static Directory? _dbDir;
static late Database _db;
static Map<String, dynamic> lastGame = {};
static final GameDB _singleton = GameDB._privateConstructor();
static bool handledStart = false;
GameDB._privateConstructor();
factory GameDB() {
return _singleton;
}
sendDBToServer() async {
return await IO().sendDB(await getDbFile());
}
Future setLastGame() async {
List<Map<String, dynamic>> results = await _db.query(
'Games',
where: 'live = 1',
columns: ['gameID', 'summary', 'json'],
orderBy: 'summary DESC',
limit: 1,
);
lastGame = (results.length == 1) ? results[0] : {};
}
Future _createTables(Database db) async {
db.execute("CREATE TABLE Games ("
"gameID TEXT PRIMARY KEY ON CONFLICT REPLACE, "
"live BOOL, "
"summary TEXT NOT NULL, "
"json TEXT NOT NULL, "
"lastUpdated TEXT "
") WITHOUT ROWID;");
db.execute("CREATE INDEX live_idx ON Games (live, summary DESC);");
db.execute(
"""CREATE TRIGGER lastGameUpdate AFTER UPDATE OF gameID, json, live, summary ON Games
BEGIN
UPDATE Games SET lastUpdated=CURRENT_TIMESTAMP WHERE id=id;
END;""");
db.execute("CREATE TABLE Users ("
"id INTEGER PRIMARY KEY ON CONFLICT REPLACE, "
"name TEXT NOT NULL, "
"pin TEXT, "
"lastUpdated TEXT "
") WITHOUT ROWID;");
// will be used for if-modified-since header
db.execute("CREATE TABLE Updates ("
"what TEXT PRIMARY KEY ON CONFLICT REPLACE, "
"utc TEXT) WITHOUT ROWID;");
db.execute(
"""CREATE TRIGGER lastUserUpdate AFTER UPDATE OF id, name ON Users
BEGIN
UPDATE Users SET lastUpdated=CURRENT_TIMESTAMP WHERE id=id;
END;""");
}
Future<Map<String, dynamic>> addUser(Map<String, String> user, {updateServer: false}) async {
if (updateServer) {
user = Map.from(await IO().createPlayer(user));
}
_db.insert('Users', {'id': user['id'], 'name': user['name'], 'pin': user['pin']},
conflictAlgorithm: ConflictAlgorithm.replace);
return user;
}
Future<void> setPin(int id, String pin) async {
// TODO not used anywhere yet. Will be used when an existing player,
// saved locally, is registered on server too, and we add a PIN.
// Save in db, write to IO
}
Future<dynamic> rebuildDatabase() async {
try {
await _db.close();
} catch (e) {
print(e.toString());
}
await File(await getDbFile()).delete();
bool test = await initDB();
if (test != true) {
return test;
}
return updatePlayersFromServer();
}
Future<String> getLastUpdated(String table) async {
List results = await _db.query(
'Updates',
where: 'what = ?',
whereArgs: [table],
columns: ['utc'],
);
return results.length == 1 ? results[0]['utc'] : null;
}
Future setLastUpdated(String table, DateTime utc, [Batch? batch]) async {
if (batch == null) {
return _db.insert('Updates', {'utc': HttpDate.format(utc), 'what': table},
conflictAlgorithm: ConflictAlgorithm.replace);
} else {
return batch.insert(
'Updates', {'utc': HttpDate.format(utc), 'what': table},
conflictAlgorithm: ConflictAlgorithm.replace);
}
}
Future<String> getDbFile() async {
if (_dbDir == null) {
_dbDir = await getApplicationDocumentsDirectory();
}
return p.join(_dbDir!.path, dbFile);
}
Future<bool> tablesOK() async {
List<Map<String,dynamic>> tables = await _db.query('sqlite_master',
columns: ['name'],
where: 'type = ? and (name = ? or name = ?)',
whereArgs: ['table', 'Games', 'Users'],
);
return tables.length==2;
}
Future<dynamic> initDB() async {
try {
_db = await openDatabase(await getDbFile(),
version: _version,
onOpen: (Database db) {},
onUpgrade: (Database db, int oldVersion, int newVersion) {
if (oldVersion < 2) {}
},
onCreate: (Database db, int version) async => await _createTables(db));
if (await tablesOK()) {
return true;
} else {
return {
'exception': 'Missing table from database - recreate it',
'stack': 'tablesOK => initDB'
};
}
} catch (crash, stackTrace) {
return {'exception': crash, 'stack': stackTrace};
}
}
Future<Map> putGame(Map<String, dynamic> record) async {
record['lastUpdated'] = DateTime
.now()
.millisecondsSinceEpoch;
_db.insert('Games', record, conflictAlgorithm: ConflictAlgorithm.replace);
return await IO().updateGame(record['gameID'], record);
}
void deleteGame(String gameID) {
_db.delete('Games', where: 'gameID = ?', whereArgs: [gameID]);
}
Future<String> getGame(String gameID) async {
List<Map<String, dynamic>> out = await _db.query(
'Games',
columns: ['json'],
where: 'gameID = ?',
whereArgs: [gameID],
);
return out.length == 1 ? out[0]['json'] : null;
}
Future<List<Map<String, dynamic>>> listGames(
{bool live=true, int limit=0, int offset=0}) async {
return _db.query(
'Games',
where: 'live = ?',
whereArgs: [live ? 1 : 0],
columns: ['gameID', 'summary'],
limit: limit,
offset: offset,
);
}
Future listPlayers() async {
// select from the newly-updated local db
List<Map<String, dynamic>> players = await _db.query(
'Users',
columns: ['id', 'name'],
);
// strip away the dbQuery objects, leaving just a list of Maps
players.forEach((Map<String, dynamic> player) {
GLOBAL.allPlayers.add({'id': player['id'], 'name': player['name']});
});
GLOBAL.playersListUpdated = true;
}
Future updatePlayersFromServer() async {
// get players from the server. Get all of them, if we think there's not many to deal with
GLOBAL.playersListUpdated = false;
String lastUpdated =
GLOBAL.allPlayers.length < 50 ? '' : await getLastUpdated('Users');
List<dynamic> newPlayers = await IO().listPlayers(lastUpdated);
// update local db with updates received from the server
Batch batch = _db.batch();
newPlayers.forEach((player) {
batch.insert('Users', {'id': player[0], 'name': player[1]},
conflictAlgorithm: ConflictAlgorithm.replace);
});
setLastUpdated('Users', DateTime.now(), batch);
batch.commit(noResult: true);
listPlayers();
}
}