Debian Codegraph @

src/game/database.cpp

raw symbols SCIP
1 // This file is part of Dust Racing 2D.
2 // Copyright (C) 2020 Jussi Lind <[email protected]>
3 //
4 // Dust Racing 2D is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // Dust Racing 2D is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>.
15
16 #include "database.hpp"
17 #include "../common/config.hpp"
18 #include "../contrib/SimpleLogger/src/simple_logger.hpp"
19 #include "track.hpp"
20 #include "trackdata.hpp"
21
22 #include <stdexcept>
23
24 #include <QtSql/QSqlDatabase>
25 #include <QtSql/QSqlError>
26 #include <QtSql/QSqlQuery>
27 #include <QtSql/QSqlRecord>
28
29 #include <QDir>
30 #include <QFileInfo>
31 #include <QStandardPaths>
32 #include <QVariant>
33
34 using juzzlin::L;
35
36 namespace {
37
38 const QString BEST_POSITION_TABLE = "best_position";
39
40 const QString BEST_POSITION_FILTER = "track_name = :track_name AND version = :version AND lap_count = :lap_count AND difficulty = :difficulty";
41
42 const QString LAP_RECORD_TABLE = "lap_record";
43
44 const QString LAP_RECORD_FILTER = "track_name = :track_name AND version = :version";
45
46 const QString RACE_RECORD_TABLE = "race_record";
47
48 const QString RACE_RECORD_FILTER = "track_name = :track_name AND version = :version AND lap_count = :lap_count AND difficulty = :difficulty";
49
50 const QString TRACK_UNLOCK_TABLE = "track_unlock";
51
52 const QString TRACK_UNLOCK_FILTER = "track_name = :track_name AND version = :version AND lap_count = :lap_count AND difficulty = :difficulty";
53
54 const int TRACK_SET_VERSION = 1;
55
56 QString getAppDataPath()
57 {
58 const auto dataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
59 if (const QFileInfo path(dataPath); !path.exists())
60 {
61 QDir().mkpath(dataPath);
62 }
63 return dataPath;
64 }
65
66 QString getDbFilePath()
67 {
68 return getAppDataPath() + QDir::separator() + Config::Game::SQLITE_DATABASE_FILE_NAME;
69 }
70
71 void printError(QSqlQuery & query)
72 {
73 L().error() << "SQL Error: '" << query.lastQuery().toStdString() << "': '" << query.lastError().text().toStdString() << "'";
74 }
75 } // namespace
76
77 Database * Database::m_instance = nullptr;
78
79 Database::Database()
80 {
81 if (!Database::m_instance)
82 {
83 Database::m_instance = this;
84 }
85 else
86 {
87 throw std::runtime_error("Database already instantiated!");
88 }
89
90 initialize();
91 }
92
93 void Database::initialize()
94 {
95 L().info() << "Creating SQLite database file at " << getDbFilePath().toStdString();
96
97 QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
98 db.setDatabaseName(getDbFilePath());
99 db.open();
100
101 QSqlQuery query;
102 if (!query.exec("CREATE TABLE IF NOT EXISTS " + BEST_POSITION_TABLE + " (track_name TEXT, version INTEGER, lap_count INTEGER, difficulty INTEGER, position INTEGER)"))
103 {
104 printError(query);
105 }
106
107 if (!query.exec("CREATE TABLE IF NOT EXISTS " + LAP_RECORD_TABLE + " (track_name TEXT, version INTEGER, time INTEGER)"))
108 {
109 printError(query);
110 }
111
112 if (!query.exec("CREATE TABLE IF NOT EXISTS " + RACE_RECORD_TABLE + " (track_name TEXT, version INTEGER, lap_count INTEGER, difficulty INTEGER, time INTEGER)"))
113 {
114 printError(query);
115 }
116
117 if (!query.exec("CREATE TABLE IF NOT EXISTS " + TRACK_UNLOCK_TABLE + " (track_name TEXT, version INTEGER, lap_count INTEGER, difficulty INTEGER)"))
118 {
119 printError(query);
120 }
121 }
122
123 Database & Database::instance()
124 {
125 return *m_instance;
126 }
127
128 void Database::saveLapRecord(const Track & track, int msecs)
129 {
130 std::lock_guard<std::mutex> lock { m_mutex };
131
132 QSqlQuery query;
133 query.prepare("SELECT time FROM " + LAP_RECORD_TABLE + " WHERE " + LAP_RECORD_FILTER);
134 query.bindValue(":version", TRACK_SET_VERSION);
135 query.bindValue(":track_name", track.trackData().name());
136 if (!query.exec())
137 {
138 printError(query);
139 return;
140 }
141
142 if (!query.first())
143 {
144 L().debug() << "New lap record database entry added for " << track.trackData().name().toStdString();
145
146 QSqlQuery query;
147 query.prepare("INSERT INTO " + LAP_RECORD_TABLE + " (track_name, version, time) VALUES (:track_name, :version, :time)");
148 query.bindValue(":version", TRACK_SET_VERSION);
149 query.bindValue(":track_name", track.trackData().name());
150 query.bindValue(":time", msecs);
151 if (!query.exec())
152 {
153 printError(query);
154 }
155 }
156 else
157 {
158 L().debug() << "Updated lap record database entry for " << track.trackData().name().toStdString();
159
160 QSqlQuery query;
161 query.prepare("UPDATE " + LAP_RECORD_TABLE + " SET track_name = :track_name, version = :version, time = :time WHERE " + LAP_RECORD_FILTER);
162 query.bindValue(":version", TRACK_SET_VERSION);
163 query.bindValue(":track_name", track.trackData().name());
164 query.bindValue(":time", msecs);
165 if (!query.exec())
166 {
167 printError(query);
168 }
169 }
170 }
171
172 std::pair<int, bool> Database::loadLapRecord(const Track & track) const
173 {
174 std::lock_guard<std::mutex> lock { m_mutex };
175
176 QSqlQuery query;
177 query.prepare("SELECT time FROM " + LAP_RECORD_TABLE + " WHERE " + LAP_RECORD_FILTER);
178 query.bindValue(":version", TRACK_SET_VERSION);
179 query.bindValue(":track_name", track.trackData().name());
180 if (!query.exec())
181 {
182 printError(query);
183 return {};
184 }
185
186 if (!query.first())
187 {
188 return {};
189 }
190
191 const auto record = query.record();
192 const auto timeCol = record.indexOf("time");
193 return { query.value(timeCol).toInt(), true };
194 }
195
196 void Database::resetLapRecords()
197 {
198 std::lock_guard<std::mutex> lock { m_mutex };
199
200 QSqlQuery query;
201 if (!query.exec("DELETE FROM " + LAP_RECORD_TABLE))
202 {
203 printError(query);
204 }
205 }
206
207 void Database::saveRaceRecord(const Track & track, int msecs, int lapCount, DifficultyProfile::Difficulty difficulty)
208 {
209 std::lock_guard<std::mutex> lock { m_mutex };
210
211 QSqlQuery query;
212 query.prepare("SELECT time FROM " + RACE_RECORD_TABLE + " WHERE " + RACE_RECORD_FILTER);
213 query.bindValue(":version", TRACK_SET_VERSION);
214 query.bindValue(":track_name", track.trackData().name());
215 query.bindValue(":lap_count", lapCount);
216 query.bindValue(":difficulty", static_cast<int>(difficulty));
217 if (!query.exec())
218 {
219 printError(query);
220 return;
221 }
222
223 if (!query.first())
224 {
225 L().debug() << "New race record database entry added for " << track.trackData().name().toStdString();
226
227 QSqlQuery query;
228 query.prepare("INSERT INTO " + RACE_RECORD_TABLE + " (track_name, version, lap_count, difficulty, time) VALUES (:track_name, :version, :lap_count, :difficulty, :time)");
229 query.bindValue(":version", TRACK_SET_VERSION);
230 query.bindValue(":track_name", track.trackData().name());
231 query.bindValue(":lap_count", lapCount);
232 query.bindValue(":difficulty", static_cast<int>(difficulty));
233 query.bindValue(":time", msecs);
234 if (!query.exec())
235 {
236 printError(query);
237 }
238 }
239 else
240 {
241 L().debug() << "Updated race record database entry for " << track.trackData().name().toStdString();
242
243 QSqlQuery query;
244 query.prepare("UPDATE " + RACE_RECORD_TABLE + " SET track_name = :track_name, version = :version, time = :time WHERE " + RACE_RECORD_FILTER);
245 query.bindValue(":version", TRACK_SET_VERSION);
246 query.bindValue(":track_name", track.trackData().name());
247 query.bindValue(":lap_count", lapCount);
248 query.bindValue(":difficulty", static_cast<int>(difficulty));
249 query.bindValue(":time", msecs);
250 if (!query.exec())
251 {
252 printError(query);
253 }
254 }
255 }
256
257 std::pair<int, bool> Database::loadRaceRecord(const Track & track, int lapCount, DifficultyProfile::Difficulty difficulty) const
258 {
259 std::lock_guard<std::mutex> lock { m_mutex };
260
261 QSqlQuery query;
262 query.prepare("SELECT time FROM " + RACE_RECORD_TABLE + " WHERE " + RACE_RECORD_FILTER);
263 query.bindValue(":version", TRACK_SET_VERSION);
264 query.bindValue(":track_name", track.trackData().name());
265 query.bindValue(":lap_count", lapCount);
266 query.bindValue(":difficulty", static_cast<int>(difficulty));
267 if (!query.exec())
268 {
269 printError(query);
270 return {};
271 }
272
273 if (!query.first())
274 {
275 return {};
276 }
277
278 const auto record = query.record();
279 const auto timeCol = record.indexOf("time");
280 return { query.value(timeCol).toInt(), true };
281 }
282
283 void Database::resetRaceRecords()
284 {
285 std::lock_guard<std::mutex> lock { m_mutex };
286
287 QSqlQuery query;
288 if (!query.exec("DELETE FROM " + RACE_RECORD_TABLE))
289 {
290 printError(query);
291 }
292 }
293
294 void Database::saveBestPos(const Track & track, int pos, int lapCount, DifficultyProfile::Difficulty difficulty)
295 {
296 std::lock_guard<std::mutex> lock { m_mutex };
297
298 QSqlQuery query;
299 query.prepare("SELECT position FROM " + BEST_POSITION_TABLE + " WHERE " + BEST_POSITION_FILTER);
300 query.bindValue(":version", TRACK_SET_VERSION);
301 query.bindValue(":track_name", track.trackData().name());
302 query.bindValue(":lap_count", lapCount);
303 query.bindValue(":difficulty", static_cast<int>(difficulty));
304 if (!query.exec())
305 {
306 printError(query);
307 return;
308 }
309
310 if (!query.first())
311 {
312 L().debug() << "New best position database entry added for " << track.trackData().name().toStdString();
313
314 QSqlQuery query;
315 query.prepare("INSERT INTO " + BEST_POSITION_TABLE + " (track_name, version, lap_count, difficulty, position) VALUES (:track_name, :version, :lap_count, :difficulty, :position)");
316 query.bindValue(":version", TRACK_SET_VERSION);
317 query.bindValue(":track_name", track.trackData().name());
318 query.bindValue(":lap_count", lapCount);
319 query.bindValue(":difficulty", static_cast<int>(difficulty));
320 query.bindValue(":position", pos);
321 if (!query.exec())
322 {
323 printError(query);
324 }
325 }
326 else
327 {
328 L().debug() << "Updated best position database entry for " << track.trackData().name().toStdString();
329
330 QSqlQuery query;
331 query.prepare("UPDATE " + BEST_POSITION_TABLE + " SET track_name = :track_name, version = :version, position = :position WHERE " + BEST_POSITION_FILTER);
332 query.bindValue(":version", TRACK_SET_VERSION);
333 query.bindValue(":track_name", track.trackData().name());
334 query.bindValue(":lap_count", lapCount);
335 query.bindValue(":difficulty", static_cast<int>(difficulty));
336 query.bindValue(":position", pos);
337 if (!query.exec())
338 {
339 printError(query);
340 }
341 }
342 }
343
344 std::pair<int, bool> Database::loadBestPos(const Track & track, int lapCount, DifficultyProfile::Difficulty difficulty) const
345 {
346 std::lock_guard<std::mutex> lock { m_mutex };
347
348 QSqlQuery query;
349 query.prepare("SELECT position FROM " + BEST_POSITION_TABLE + " WHERE " + BEST_POSITION_FILTER);
350 query.bindValue(":version", TRACK_SET_VERSION);
351 query.bindValue(":track_name", track.trackData().name());
352 query.bindValue(":lap_count", lapCount);
353 query.bindValue(":difficulty", static_cast<int>(difficulty));
354 if (!query.exec())
355 {
356 printError(query);
357 return {};
358 }
359
360 if (!query.first())
361 {
362 return {};
363 }
364
365 const auto record = query.record();
366 const auto timeCol = record.indexOf("position");
367 return { query.value(timeCol).toInt(), true };
368 }
369
370 void Database::resetBestPos()
371 {
372 std::lock_guard<std::mutex> lock { m_mutex };
373
374 QSqlQuery query;
375 if (!query.exec("DELETE FROM " + BEST_POSITION_TABLE))
376 {
377 printError(query);
378 }
379 }
380
381 void Database::saveTrackUnlockStatus(const Track & track, int lapCount, DifficultyProfile::Difficulty difficulty)
382 {
383 std::lock_guard<std::mutex> lock { m_mutex };
384
385 QSqlQuery query;
386 query.prepare("SELECT * FROM " + TRACK_UNLOCK_TABLE + " WHERE " + TRACK_UNLOCK_FILTER);
387 query.bindValue(":version", TRACK_SET_VERSION);
388 query.bindValue(":track_name", track.trackData().name());
389 query.bindValue(":lap_count", lapCount);
390 query.bindValue(":difficulty", static_cast<int>(difficulty));
391 if (!query.exec())
392 {
393 printError(query);
394 return;
395 }
396
397 if (!query.first())
398 {
399 L().debug() << "New track unlock database entry added for " << track.trackData().name().toStdString();
400
401 QSqlQuery query;
402 query.prepare("INSERT INTO " + TRACK_UNLOCK_TABLE + " (track_name, version, lap_count, difficulty) VALUES (:track_name, :version, :lap_count, :difficulty)");
403 query.bindValue(":version", TRACK_SET_VERSION);
404 query.bindValue(":track_name", track.trackData().name());
405 query.bindValue(":lap_count", lapCount);
406 query.bindValue(":difficulty", static_cast<int>(difficulty));
407 if (!query.exec())
408 {
409 printError(query);
410 }
411 }
412 }
413
414 bool Database::loadTrackUnlockStatus(const Track & track, int lapCount, DifficultyProfile::Difficulty difficulty) const
415 {
416 std::lock_guard<std::mutex> lock { m_mutex };
417
418 QSqlQuery query;
419 query.prepare("SELECT * FROM " + TRACK_UNLOCK_TABLE + " WHERE " + TRACK_UNLOCK_FILTER);
420 query.bindValue(":version", TRACK_SET_VERSION);
421 query.bindValue(":track_name", track.trackData().name());
422 query.bindValue(":lap_count", lapCount);
423 query.bindValue(":difficulty", static_cast<int>(difficulty));
424 if (!query.exec())
425 {
426 printError(query);
427 return false;
428 }
429
430 return query.first();
431 }
432
433 void Database::resetTrackUnlockStatuses()
434 {
435 std::lock_guard<std::mutex> lock { m_mutex };
436
437 QSqlQuery query;
438 if (!query.exec("DELETE FROM " + TRACK_UNLOCK_TABLE))
439 {
440 printError(query);
441 }
442 }