Repository navigation
Expand file tree
/
Copy pathstorage.cpp
More file actions
411 lines (359 loc) · 10.9 KB
/
Copy pathstorage.cpp
File metadata and controls
411 lines (359 loc) · 10.9 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
#include <cassert>
#include <iostream>
#include "storage.hpp"
static inline
unsigned int atou(const char *s) {
return (unsigned int) atol(s);
}
namespace Storage {
Inst::Inst(const std::string _filename) : filename(_filename) {
int rc;
rc = sqlite3_open(filename.c_str(), &db);
if (rc) {
std::cerr << "Can't open " << filename << std::endl;
assert(0 && "Can't open filename");
}
}
Inst::~Inst() {
sqlite3_close(db);
}
void
Inst::insert(const char *cmd) {
char *errMsg = 0;
int rc = sqlite3_exec(db, cmd, NULL, 0, &errMsg);
if (rc != SQLITE_OK) {
std::cerr << errMsg << std::endl;
assert(0 && "SQL Error");
}
}
void
Inst::fetch(const char *cmd,
SQLITECALLBACK cb,
void *ctx) {
char *errMsg;
int rc = sqlite3_exec(db, cmd, cb, ctx, &errMsg);
if (rc != SQLITE_OK) {
std::cerr << errMsg << std::endl;
sqlite3_free(errMsg);
assert(0 && "SQL ERROR");
}
}
void
Inst::initialize_schema() {
const char* createTables = R"(
CREATE TABLE IF NOT EXISTS `Games` (
`gid` INTEGER PRIMARY KEY AUTOINCREMENT,
`maxPlayers` INTEGER NOT NULL,
`turnIndex` INTEGER NOT NULL,
`wordsMade` INTEGER NOT NULL DEFAULT 0,
`passesMade` INTEGER NOT NULL DEFAULT 0,
`gameOver` INTEGER NOT NULL DEFAULT 0,
`tiles` TEXT
);
CREATE TABLE IF NOT EXISTS `Players` (
`pid` INTEGER PRIMARY KEY AUTOINCREMENT,
`gid` INTEGER NOT NULL,
`plIdx` INTEGER NOT NULL,
`playerName` TEXT NOT NULL,
`secretKey` INTEGER NOT NULL,
`score` INTEGER NOT NULL DEFAULT 0,
`hand` TEXT,
`state` INTEGER NOT NULL,
`turnKey` TEXT,
`endScoreAdj` INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS `Moves` (
`mid` INTEGER PRIMARY KEY AUTOINCREMENT,
`gid` INTEGER NOT NULL,
`pid` INTEGER NOT NULL,
`moveType` INTEGER NOT NULL,
`moveDetail` TEXT,
`longestWord` TEXT,
`score` INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS `BoardTiles` (
`bid` INTEGER PRIMARY KEY AUTOINCREMENT,
`gid` INTEGER NOT NULL,
`row` INTEGER NOT NULL,
`col` INTEGER NOT NULL,
`letter` INTEGER NOT NULL,
`score` INTEGER NOT NULL
);
)";
insert(createTables);
}
// Helpers used to fetch state
typedef struct {
unsigned int pid;
} player_id_data;
int
get_sql_player_id_cb(void *data, int argc, char **argv, char **argName) {
assert(argc == 1);
player_id_data *ctx = (player_id_data *) data;
ctx->pid = atol(argv[0]);
return 0;
}
unsigned int
Inst::get_sql_player_id(unsigned int gid, unsigned int plIdx) {
char *cmd = sqlite3_mprintf("select pid from Players where gid=%u and plIdx=%u;",
gid, plIdx);
player_id_data data;
fetch(cmd, get_sql_player_id_cb, &data);
sqlite3_free(cmd);
return data.pid;
}
int
get_sql_game_list_cb(void *data, int argc, char **argv, char **argName) {
StorageGameList *gl = (StorageGameList *) data;
StorageGame sg = {
atou(argv[0]), // gid
atou(argv[1]), // maxPlayers
atou(argv[2]), // turnIndex
atou(argv[3]), // wordsMade
atou(argv[4]), // passesMade
atou(argv[5]), // gameOver
std::string(argv[6]) // tiles
};
gl->push_back(sg);
std::cout << "Found game=" << sg.gid
<< " maxPlayers=" << sg.maxPlayers
<< " gameOver=" << sg.gameOver
<< std::endl;
return 0;
}
StorageGameList
Inst::game_list() {
StorageGameList gl;
char *cmd = sqlite3_mprintf("select "
"gid, maxPlayers, turnIndex, wordsMade, passesMade, gameOver, tiles "
"from Games;");
fetch(cmd, get_sql_game_list_cb, &gl);
sqlite3_free(cmd);
return gl;
}
int
get_sql_game_board_tile_list_cb(void *data, int argc, char **argv, char **argName) {
StorageBoardTileList *btl = (StorageBoardTileList *) data;
StorageBoardTile bt = {
atou(argv[0]), // row
atou(argv[1]), // col
argv[2][0], // letter
atoi(argv[3]) // score
};
btl->push_back(bt);
return 0;
}
StorageBoardTileList
Inst::game_board_tile_list(unsigned int gid) {
StorageBoardTileList btl;
char *cmd = sqlite3_mprintf("select "
"row, col, letter, score "
"from BoardTiles where gid=%u;",
gid);
fetch(cmd, get_sql_game_board_tile_list_cb, &btl);
sqlite3_free(cmd);
std::cout << "Loaded " << btl.size() << " tiles" << std::endl;
return btl;
}
int
get_sql_game_player_list_cb(void *data, int argc, char **argv, char **argName) {
StoragePlayerList *pl = (StoragePlayerList *) data;
StoragePlayer player = {
atou(argv[0]), // pid
atou(argv[1]), // plIdx
std::string(argv[2]), // playerName
std::string(argv[3]), // secretKey
atoi(argv[4]), // score
argv[5] ? std::string(argv[5]) : "", // hand
atou(argv[6]), // state
argv[7] ? std::string(argv[7]) : "", // turnKey
atoi(argv[8]) // endScoreAdj
};
pl->push_back(player);
std::cout << "Found player " << player.playerName << " score " << player.score << std::endl;
return 0;
}
StoragePlayerList
Inst::game_player_list(unsigned int gid) {
StoragePlayerList pl;
char *cmd = sqlite3_mprintf("select "
"pid, plIdx, playerName, secretKey, score, hand, state, "
"turnKey, endScoreAdj "
"from Players where gid=%u;",
gid);
fetch(cmd, get_sql_game_player_list_cb, &pl);
sqlite3_free(cmd);
return pl;
}
int
get_sql_game_move_list_cb(void *data, int argc, char **argv, char **argName) {
StorageMoveList *ml = (StorageMoveList *) data;
StorageMove move = {
atou(argv[0]), // moveType
(argv[1]) ? std::string(argv[1]) : std::string(""), // longestWord
atoi(argv[2]) // score
};
ml->push_back(move);
return 0;
}
StorageMoveList
Inst::game_move_list(unsigned int gid) {
StorageMoveList ml;
char *cmd = sqlite3_mprintf("select "
"moveType, longestWord, score "
"from Moves where gid=%u;",
gid);
fetch(cmd, get_sql_game_move_list_cb, &ml);
sqlite3_free(cmd);
return ml;
}
// Helpers used by Game::Inst
void
Inst::add_game(unsigned int gid,
unsigned int maxPlayers, unsigned int turnIndex,
const std::string tiles) {
char *cmd = sqlite3_mprintf("insert into Games "
"(maxPlayers, turnIndex, tiles) values "
"(%u, %u, '%q');",
maxPlayers, turnIndex, tiles.c_str());
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_game_tiles(unsigned int gid,
const std::string &tiles) {
char *cmd = sqlite3_mprintf("update Games set tiles='%q' where gid=%u;", tiles.c_str(), gid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_game_words_made(unsigned int gid,
unsigned int wordsMade) {
char *cmd = sqlite3_mprintf("update Games set wordsMade=%u where gid=%u;",
wordsMade, gid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_game_passes_made(unsigned int gid,
unsigned int passesMade) {
char *cmd = sqlite3_mprintf("update Games set passesMade=%u where gid=%u;",
passesMade, gid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_game_turn_index(unsigned int gid,
unsigned int turnIndex) {
char *cmd = sqlite3_mprintf("update Games set turnIndex=%u where gid=%u;", turnIndex, gid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_game_over(unsigned int gid) {
char *cmd = sqlite3_mprintf("update Games set gameOver=1 where gid=%u;", gid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::add_board_tile(unsigned int gid,
unsigned int row, unsigned int col,
char letter, int score) {
char *cmd = sqlite3_mprintf("insert into BoardTiles "
"(gid, row, col, letter, score) "
"values (%u, %u, %u, '%c', %d);",
gid, row, col, letter, score);
insert(cmd);
sqlite3_free(cmd);
}
// Players
unsigned int
Inst::add_player(unsigned int gid,
unsigned int plIdx,
const std::string &playerName,
const std::string &secretKey,
unsigned int state) {
char *cmd = sqlite3_mprintf("insert into Players "
"(gid, plIdx, playerName, secretKey, state) "
"values (%u, %u, '%q', '%q', %u);",
gid, plIdx,
playerName.c_str(), secretKey.c_str(),
state);
insert(cmd);
sqlite3_free(cmd);
return get_sql_player_id(gid, plIdx);
}
void
Inst::update_player_hand(unsigned int pid,
const std::string &hand) {
char *cmd = sqlite3_mprintf("update Players set hand='%q' where pid=%u;", hand.c_str(), pid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_player_state(unsigned int pid,
unsigned int state) {
char *cmd = sqlite3_mprintf("update Players set state=%u where pid=%u;", state, pid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_player_score(unsigned int pid,
int score) {
char *cmd = sqlite3_mprintf("update Players set score=%d where pid=%u;", score, pid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_player_end_adj_score(unsigned int pid,
int deltaScore) {
char *cmd = sqlite3_mprintf("update Players set endScoreAdj=%d where pid=%u;",
deltaScore, pid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::update_player_turnkey(unsigned int pid,
const std::string &turnKey) {
char *cmd = sqlite3_mprintf("update Players set turnKey='%q' where pid=%u;",
turnKey.c_str(), pid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::add_player_move_pass(unsigned int gid,
unsigned int pid) {
char *cmd = sqlite3_mprintf("insert into Moves "
"(gid, pid, moveType, score) values "
"(%u, %u, 0, 0);",
gid, pid);
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::add_player_move_exch(unsigned int gid,
unsigned int pid,
const std::string &removedLetters) {
char *cmd = sqlite3_mprintf("insert into Moves "
"(gid, pid, moveType, longestWord, score) values "
"(%u, %u, 1, '%q', 0);",
gid, pid, removedLetters.c_str());
insert(cmd);
sqlite3_free(cmd);
}
void
Inst::add_player_move_play(unsigned int gid,
unsigned int pid,
const std::string &strJsonMove,
const std::string &longest_word,
int total_score) {
char *cmd = sqlite3_mprintf("insert into Moves "
"(gid, pid, moveType, moveDetail, longestWord, score) values "
"(%u, %u, 2, '%q', '%q', %d);",
gid, pid,
strJsonMove.c_str(), longest_word.c_str(),
total_score);
insert(cmd);
sqlite3_free(cmd);
}
}; // namespace Storage