Repository navigation
Expand file tree
/
Copy pathdao.c
More file actions
525 lines (460 loc) · 14.1 KB
/
Copy pathdao.c
File metadata and controls
525 lines (460 loc) · 14.1 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// ```
// FSFSDao dao;
// daoInit(&dao);
//
// daoInsertReplayOverview(&dao);
// daoInsertReplayInput(&dao, 1, 0x45);
// daoMarkReplayComplete(&dao);
// ```
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include "core.h"
#include "dao.h"
#include "engine.h"
#include "log.h"
#include "option.h" // for FileExists
// For mkdir
#ifdef __linux__
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#endif
#define DAO_FILENAME "fs.db"
static void setupHiscoreTable(FSDao *dao);
static void setupReplayOverviewTable(FSDao *dao);
static void setupReplayInputTable(FSDao *dao);
// Resolves the db file to load.
//
// Load priority is as follows:
// - fs.db (only if already exists)
// - $XDG_DATA_HOME/faststack/database.db (linux, created if doesn't exist)
// - fs.db (created if doesn't exist)
const char* daoGetDatabasePath(void)
{
if (fileExists(DAO_FILENAME)) {
return DAO_FILENAME;
}
#ifdef __linux__
static char dbPath[128];
const char *fsDatabase = "faststack";
char *dataHome = getenv("XDG_DATA_HOME");
if (dataHome) {
snprintf(dbPath, sizeof(dbPath), "%s/%s", dataHome, fsDatabase);
} else {
char *homePath = getenv("HOME");
if (!homePath) {
homePath = "~";
}
snprintf(dbPath, sizeof(dbPath), "%s/%s/%s",
homePath, ".local/share", fsDatabase);
}
// sqlite3 will create the database for us, but it won't create any
// leading directories, so perform that ourselves.
//
// Assumes you have a .local/share created and does not exist as a
// normal file.
errno = 0;
if (mkdir(dbPath, 0777) != 0) {
if (errno != EEXIST) {
fsLogFatal("mkdir '%s' returned %s", dbPath, strerror(errno));
exit(1);
}
} else {
fsLogInfo("created new database directory %s", dbPath);
}
strncat(dbPath, "/database.db", sizeof(dbPath) - strlen(dbPath) - 1);
return dbPath;
#else
return DAO_FILENAME;
#endif
}
// Need to keep track of the replay foreign key.
void daoInit(FSDao *dao)
{
const char *path = daoGetDatabasePath();
fsLogInfo("using database at %s", path);
if (sqlite3_open(path, &dao->db) != SQLITE_OK) {
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
setupHiscoreTable(dao);
setupReplayOverviewTable(dao);
setupReplayInputTable(dao);
}
void daoDeinit(FSDao *dao)
{
if (sqlite3_finalize(dao->hiscore_stmt) != SQLITE_OK) {
fsLogWarning("%s", sqlite3_errmsg(dao->db));
}
if (sqlite3_finalize(dao->replay_overview_stmt) != SQLITE_OK) {
fsLogWarning("%s", sqlite3_errmsg(dao->db));
}
if (sqlite3_finalize(dao->replay_overview_complete_stmt) != SQLITE_OK) {
fsLogWarning("%s", sqlite3_errmsg(dao->db));
}
if (sqlite3_finalize(dao->replay_input_stmt) != SQLITE_OK) {
fsLogWarning("%s", sqlite3_errmsg(dao->db));
}
if (sqlite3_close(dao->db) != SQLITE_OK) {
fsLogWarning("%s", sqlite3_errmsg(dao->db));
}
}
// If new hiscore fields are added in the future, previously unknown fields
// will simply have NULL values and will not have any associated data. This
// is handled on the date retrieval side.
static void setupHiscoreTable(FSDao *dao)
{
const char create_stmt[] =
"create table if not exists hiscore"
"("
"id INTEGER PRIMARY KEY,"
"replay_id INTEGER REFERENCES replay_overview(id),"
"time FLOAT,"
"tps FLOAT,"
"kpt FLOAT,"
"goal INTEGER,"
"date DATETIME"
");";
if (sqlite3_exec(dao->db, create_stmt, NULL, NULL, NULL) != SQLITE_OK) {
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
const char insert_stmt[] =
"insert into hiscore (replay_id, time, tps, kpt, goal, date) "
"values (?, ?, ?, ?, ?, datetime(\"now\"));";
if (sqlite3_prepare_v2(
dao->db,
insert_stmt,
sizeof(insert_stmt),
&dao->hiscore_stmt,
NULL
) != SQLITE_OK)
{
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
}
void daoSaveHiscore(FSDao *dao, const FSEngine *f)
{
sqlite3_stmt *s = dao->hiscore_stmt;
const int msElapsed = f->msPerTick * f->totalTicks;
sqlite3_bind_int(s, 1, dao->replay_overview_row_id);
sqlite3_bind_double(s, 2, (double) msElapsed / 1000);
sqlite3_bind_double(s, 3, (double) f->blocksPlaced / ((double) msElapsed / 1000));
sqlite3_bind_double(s, 4, (double) f->totalKeysPressed / f->blocksPlaced);
sqlite3_bind_int(s, 5, f->goal);
sqlite3_step(s);
sqlite3_clear_bindings(s);
sqlite3_reset(s);
}
static void setupReplayOverviewTable(FSDao *dao)
{
const char create_stmt[] =
"create table if not exists replay_overview"
"("
"id INTEGER PRIMARY KEY,"
"version INTEGER,"
"date DATETIME,"
"complete INT2,"
"seed INTEGER,"
"goal INTEGER,"
"field_width INTEGER,"
"field_height INTEGER,"
"field_hidden INTEGER,"
"initial_action_style INTEGER,"
"das_speed INTEGER,"
"das_delay INTEGER,"
"ms_per_tick INTEGER,"
"ticks_per_draw INTEGER,"
"are_delay INTEGER,"
"are_cancellable INTEGER,"
"lock_style INTEGER,"
"lock_delay INTEGER,"
"floorkick_limit INTEGER,"
"one_shot_soft_drop INTEGER,"
"rotation_system INTEGER,"
"gravity INTEGER,"
"soft_drop_gravity INTEGER,"
"randomizer INTEGER,"
"ready_phase_length INTEGER,"
"go_phase_length INTEGER,"
"infinite_ready_go_hold INTEGER,"
"next_piece_count INTEGER"
");";
if (sqlite3_exec(dao->db, create_stmt, NULL, NULL, NULL) != SQLITE_OK) {
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
const char insert_stmt[] =
"insert into replay_overview"
"("
"date,"
"version,"
"complete,"
"seed,"
"goal,"
"field_width,"
"field_height,"
"field_hidden,"
"initial_action_style,"
"das_speed,"
"das_delay,"
"ms_per_tick,"
"ticks_per_draw,"
"are_delay,"
"are_cancellable,"
"lock_style,"
"lock_delay,"
"floorkick_limit,"
"one_shot_soft_drop,"
"rotation_system,"
"gravity,"
"soft_drop_gravity,"
"randomizer,"
"ready_phase_length,"
"go_phase_length,"
"infinite_ready_go_hold,"
"next_piece_count"
") "
"values"
"("
"datetime(\"now\"),"
"1,"
"0,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?,"
"?"
");";
if (sqlite3_prepare_v2(
dao->db,
insert_stmt,
sizeof(insert_stmt),
&dao->replay_overview_stmt,
NULL
) != SQLITE_OK)
{
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
const char select_stmt[] =
"select * from replay_overview where id = ?;";
if (sqlite3_prepare_v2(
dao->db,
select_stmt,
sizeof(select_stmt),
&dao->replay_overview_select_stmt,
NULL
) != SQLITE_OK)
{
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
const char update_stmt[] =
"update replay_overview set complete=1 where id = ?;";
if (sqlite3_prepare_v2(
dao->db,
update_stmt,
sizeof(update_stmt),
&dao->replay_overview_complete_stmt,
NULL
) != SQLITE_OK)
{
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
}
// NOTE: Current storage means reads every frame to check if the tick
// is accessed.
//
// These could be buffered however to perform only every second or so easily
// so don't worry about changing format right now.
static void setupReplayInputTable(FSDao *dao)
{
const char create_stmt[] =
"create table if not exists replay_input"
"("
"id INTEGER PRIMARY KEY,"
"replay_id INTEGER REFERENCES replay_overview(id),"
"tick INTEGER,"
"keystate INTEGER"
");";
if (sqlite3_exec(dao->db, create_stmt, NULL, NULL, NULL) != SQLITE_OK) {
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
const char insert_stmt[] =
"insert into replay_input"
"("
"replay_id,"
"tick,"
"keystate"
")"
"values"
"("
"?,"
"?,"
"?"
");";
if (sqlite3_prepare_v2(
dao->db,
insert_stmt,
sizeof(insert_stmt),
&dao->replay_input_stmt,
NULL
) != SQLITE_OK)
{
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
const char get_stmt[] =
"select keystate from replay_input "
"where replay_id = ? and tick = ?;";
if (sqlite3_prepare_v2(
dao->db,
get_stmt,
sizeof(get_stmt),
&dao->replay_output_stmt,
NULL
) != SQLITE_OK)
{
fsLogFatal("%s", sqlite3_errmsg(dao->db));
exit(1);
}
dao->last_input_keystate = 0;
dao->last_output_keystate = 0;
}
void daoInsertReplayOverview(FSDao *dao, const FSEngine *f)
{
sqlite3_stmt *s = dao->replay_overview_stmt;
sqlite3_bind_int(s, 1, f->seed);
sqlite3_bind_int(s, 2, f->goal);
sqlite3_bind_int(s, 3, f->fieldWidth);
sqlite3_bind_int(s, 4, f->fieldHeight);
sqlite3_bind_int(s, 5, f->fieldHidden);
sqlite3_bind_int(s, 6, f->initialActionStyle);
sqlite3_bind_int(s, 7, f->dasSpeed);
sqlite3_bind_int(s, 8, f->dasDelay);
sqlite3_bind_int(s, 9, f->msPerTick);
sqlite3_bind_int(s, 10, f->ticksPerDraw);
sqlite3_bind_int(s, 11, f->areDelay);
sqlite3_bind_int(s, 12, f->areCancellable);
sqlite3_bind_int(s, 13, f->lockStyle);
sqlite3_bind_int(s, 14, f->lockDelay);
sqlite3_bind_int(s, 15, f->floorkickLimit);
sqlite3_bind_int(s, 16, f->oneShotSoftDrop);
sqlite3_bind_int(s, 17, f->rotationSystem);
sqlite3_bind_int(s, 18, f->gravity);
sqlite3_bind_int(s, 19, f->softDropGravity);
sqlite3_bind_int(s, 20, f->randomizer);
sqlite3_bind_int(s, 21, f->readyPhaseLength);
sqlite3_bind_int(s, 22, f->goPhaseLength);
sqlite3_bind_int(s, 23, f->infiniteReadyGoHold);
sqlite3_bind_int(s, 24, f->nextPieceCount);
sqlite3_step(s);
sqlite3_clear_bindings(s);
sqlite3_reset(s);
dao->replay_overview_row_id = sqlite3_last_insert_rowid(dao->db);
}
void daoInsertReplayInput(FSDao *dao, u32 ticks, u32 keystate)
{
// Only store deltas and not each state.
if (dao->last_input_keystate == keystate) {
return;
}
sqlite3_stmt *s = dao->replay_input_stmt;
sqlite3_bind_int(s, 1, dao->replay_overview_row_id);
sqlite3_bind_int(s, 2, ticks);
sqlite3_bind_int(s, 3, keystate);
sqlite3_step(s);
sqlite3_clear_bindings(s);
sqlite3_reset(s);
dao->last_input_keystate = keystate;
}
static void daoLoadReplayOverview(FSDao *dao, FSEngine *f, u32 replay_id)
{
sqlite3_stmt *s = dao->replay_overview_select_stmt;
sqlite3_bind_int(s, 1, replay_id);
if (sqlite3_step(s) != SQLITE_ROW) {
fsLogFatal("no replay found with id: %d", replay_id);
exit(1);
}
if (sqlite3_column_int(s, 3) == 0) {
fsLogWarning("incomplete replay being played!");
}
// Skip id, version, date and complete
f->seed = sqlite3_column_int(s, 4);
f->goal = sqlite3_column_int(s, 5);
f->fieldWidth = sqlite3_column_int(s, 6);
f->fieldHeight = sqlite3_column_int(s, 7);
f->fieldHidden = sqlite3_column_int(s, 8);
f->initialActionStyle = sqlite3_column_int(s, 9);
f->dasSpeed = sqlite3_column_int(s, 10);
f->dasDelay = sqlite3_column_int(s, 11);
f->msPerTick = sqlite3_column_int(s, 12);
f->ticksPerDraw = sqlite3_column_int(s, 13);
f->areDelay = sqlite3_column_int(s, 14);
f->areCancellable = sqlite3_column_int(s, 15);
f->lockStyle = sqlite3_column_int(s, 16);
f->lockDelay = sqlite3_column_int(s, 17);
f->floorkickLimit = sqlite3_column_int(s, 18);
f->oneShotSoftDrop = sqlite3_column_int(s, 19);
f->rotationSystem = sqlite3_column_int(s, 20);
f->gravity = sqlite3_column_int(s, 21);
f->softDropGravity = sqlite3_column_int(s, 22);
f->randomizer = sqlite3_column_int(s, 23);
f->readyPhaseLength = sqlite3_column_int(s, 24);
f->goPhaseLength = sqlite3_column_int(s, 25);
f->infiniteReadyGoHold = sqlite3_column_int(s, 26);
f->nextPieceCount = sqlite3_column_int(s, 27);
sqlite3_clear_bindings(s);
sqlite3_reset(s);
}
void daoLoadReplay(FSDao *dao, FSEngine *f, u32 replay_id)
{
daoLoadReplayOverview(dao, f, replay_id);
dao->output_replay_id = replay_id;
dao->last_output_keystate = 0;
}
u32 daoGetReplayInput(FSDao *dao, u32 tick)
{
sqlite3_stmt *s = dao->replay_output_stmt;
sqlite3_bind_int(s, 1, dao->output_replay_id);
sqlite3_bind_int(s, 2, tick);
if (sqlite3_step(s) == SQLITE_ROW) {
dao->last_output_keystate = sqlite3_column_int(s, 0);
}
sqlite3_step(s);
sqlite3_clear_bindings(s);
sqlite3_reset(s);
return dao->last_output_keystate;
}
void daoMarkReplayComplete(FSDao *dao)
{
sqlite3_stmt *s = dao->replay_overview_complete_stmt;
sqlite3_bind_int(s, 1, dao->replay_overview_row_id);
sqlite3_step(s);
sqlite3_clear_bindings(s);
sqlite3_reset(s);
}