-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbmanager.cpp
More file actions
832 lines (682 loc) · 25.3 KB
/
Copy pathdbmanager.cpp
File metadata and controls
832 lines (682 loc) · 25.3 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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#include "dbmanager.h"
#include "qdatetime.h"
#include <QDebug>
DbManager::DbManager(const QString &path)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName(path);
allTables = new tablesAndColumns();
if (!m_db.open())
{
qDebug() << "Error: connection with database fail";
}
else
{
qDebug() << "Database: connection ok";
QSqlQuery query;
query.prepare("CREATE TABLE games(id INTEGER PRIMARY KEY AUTOINCREMENT,gameName TEXT, gameImage TEXT, gameExePath TEXT, gameExe TEXT, running BOOLEAN, timePlayed INTEGER, createdAt TEXT, updatedAt TEXT);");
if (!query.exec())
{
qDebug() << "Table already games created.";
}
QSqlQuery queryGameHistorical;
queryGameHistorical.prepare("CREATE TABLE game_historical(id INTEGER PRIMARY KEY AUTOINCREMENT,gameId INTEGER, timePlayed INTEGER, createdAt TEXT, updatedAt TEXT);");
if (!queryGameHistorical.exec())
{
qDebug() << "Table already game_historical created.";
}
QSqlQuery queryAchivements;
queryAchivements.prepare("CREATE TABLE achivements(id INTEGER PRIMARY KEY AUTOINCREMENT, achivement TEXT, unlocked BOOLEAN, active BOOLEAN, unlockedAt TEXT);");
if (!queryAchivements.exec())
{
qDebug() << "Table already achivements created.";
QVector<DbManager::Achivements> getAchivements = DbManager::getAchivements();
if(getAchivements.count() == 0){
// Create achievements data
QSqlQuery queryAdd;
queryAdd.prepare("INSERT INTO achivements (achivement, unlocked, active, unlockedAt) VALUES "
"('crono', false, true, ''), "
"('silver', false, false, ''), "
"('nova', false, false,''), "
"('platinum', false, false,''), "
"('diamond', false, false,'')");
if (queryAdd.exec())
{
qDebug() << "Data added to achivements";
}
else
{
qDebug() << "Could add data to achivements: " << queryAdd.lastError();
}
}else{
qDebug() << "There is already data in achivements";
}
}else{
}
}
}
DbManager::~DbManager()
{
if (m_db.isOpen())
{
m_db.close();
}
}
bool DbManager::insertGame(const QString gameImage, const QString &gameName, const QString &gameExePath, const QString &gameExe)
{
bool success = false;
if (!gameName.isEmpty())
{
QSqlQuery queryAdd;
queryAdd.prepare("INSERT INTO games (gameImage,gameName,gameExePath,gameExe,running,timePlayed,createdAt, updatedAt) VALUES (:gameImage,:gameName,:gameExePath,:gameExe,:running,:timePlayed,:createdAt,:updatedAt)");
queryAdd.bindValue(":gameImage", gameImage);
queryAdd.bindValue(":gameName", gameName);
queryAdd.bindValue(":gameExePath", gameExePath);
queryAdd.bindValue(":gameExe", gameExe);
queryAdd.bindValue(":running", false);
queryAdd.bindValue(":timePlayed", 0);
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
queryAdd.bindValue(":createdAt", formattedDate);
queryAdd.bindValue(":updatedAt", formattedDate);
if (queryAdd.exec())
{
success = true;
}
else
{
qDebug() << "Could not game: " << queryAdd.lastError();
}
}
else
{
qDebug() << "Data is required to add.";
}
return success;
}
bool DbManager::updateGame(const QString gameImage, const QString &gameName, const QString &gameExePath, const QString &gameExe, const int &gameId)
{
bool success = false;
if (!gameName.isEmpty())
{
QSqlQuery queryAdd;
queryAdd.prepare("UPDATE games SET gameImage = :gameImage, gameName = :gameName, gameExePath = :gameExePath, gameExe = :gameExe, updatedAt = :updatedAt WHERE id = :gameId");
queryAdd.bindValue(":gameImage", gameImage);
queryAdd.bindValue(":gameName", gameName);
queryAdd.bindValue(":gameExePath", gameExePath);
queryAdd.bindValue(":gameExe", gameExe);
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
queryAdd.bindValue(":updatedAt", formattedDate);
queryAdd.bindValue(":gameId", gameId);
if (queryAdd.exec())
{
success = true;
}
else
{
qDebug() << "Could update game: " << queryAdd.lastError();
}
}
else
{
qDebug() << "Data is required to update.";
}
return success;
}
// Games struct
struct Games
{
int id;
QString gameImage;
QString gameName;
QString gameExePath;
QString gameExe;
bool running;
int timePlayed;
QString createdAt;
QString updatedAt;
};
QVector<DbManager::Games> DbManager::getGames()
{
QVector<Games> games;
QSqlQuery query("SELECT * FROM games ORDER BY id DESC");
int idIndex = query.record().indexOf("id");
int gameImageIndex = query.record().indexOf("gameImage");
int gameNameIndex = query.record().indexOf("gameName");
int gameExePathIndex = query.record().indexOf("gameExePath");
int gameExeIndex = query.record().indexOf("gameExe");
int runningIndex = query.record().indexOf("running");
int timePlayedIndex = query.record().indexOf("timePlayed");
int createdAtIndex = query.record().indexOf("createdAt");
int updatedAtIndex = query.record().indexOf("updatedAt");
while (query.next())
{
int id = query.value(idIndex).toInt();
QString gameImage = query.value(gameImageIndex).toString();
QString gameName = query.value(gameNameIndex).toString();
QString gameExePath = query.value(gameExePathIndex).toString();
QString gameExe = query.value(gameExeIndex).toString();
bool running = query.value(runningIndex).toBool();
int timePlayed = query.value(timePlayedIndex).toInt();
QString createdAt = query.value(createdAtIndex).toString();
QString updatedAt = query.value(updatedAtIndex).toString();
games.push_back(Games{id, gameImage, gameName, gameExePath, gameExe, running, timePlayed, createdAt, updatedAt});
}
return games;
}
QVector<DbManager::Games> DbManager::getGameById(int gameId)
{
QVector<Games> games;
QSqlQuery query;
query.prepare("SELECT * FROM games WHERE id = :gameId ORDER BY id DESC");
query.bindValue(":gameId", gameId);
if (query.exec())
{
int idIndex = query.record().indexOf("id");
int gameImageIndex = query.record().indexOf("gameImage");
int gameNameIndex = query.record().indexOf("gameName");
int gameExePathIndex = query.record().indexOf("gameExePath");
int gameExeIndex = query.record().indexOf("gameExe");
bool runningIndex = query.record().indexOf("running");
int timePlayedIndex = query.record().indexOf("timePlayed");
int createdAtIndex = query.record().indexOf("createdAt");
int updatedAtIndex = query.record().indexOf("updatedAt");
while (query.next())
{
int id = query.value(idIndex).toInt();
QString gameImage = query.value(gameImageIndex).toString();
QString gameName = query.value(gameNameIndex).toString();
QString gameExePath = query.value(gameExePathIndex).toString();
QString gameExe = query.value(gameExeIndex).toString();
bool running = query.value(runningIndex).toBool();
int timePlayed = query.value(timePlayedIndex).toInt();
QString createdAt = query.value(createdAtIndex).toString();
QString updatedAt = query.value(updatedAtIndex).toString();
games.push_back(Games{id, gameImage, gameName, gameExePath, gameExe, running, timePlayed, createdAt, updatedAt});
}
}
else
{
qDebug() << "Error executing query:" << query.lastError().text();
}
return games;
}
bool DbManager::updateTimePlayed(int gameId, int timePlayed)
{
bool success = false;
if (timePlayed != 0)
{
QSqlQuery queryAdd;
queryAdd.prepare("UPDATE games SET timePlayed = :timePlayed, updatedAt = :updatedAt WHERE id = :id");
queryAdd.bindValue(":timePlayed", timePlayed);
queryAdd.bindValue(":id", gameId);
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
queryAdd.bindValue(":updatedAt", formattedDate);
if (queryAdd.exec())
{
success = true;
}
else
{
qDebug() << "Error: " << queryAdd.lastError();
}
}
return success;
}
bool DbManager::updateGameRunning(int gameId, bool running)
{
bool success = false;
if (gameId != 0)
{
QSqlQuery queryAdd;
queryAdd.prepare("UPDATE games SET running = :running, updatedAt = :updatedAt WHERE id = :id");
queryAdd.bindValue(":running", running);
queryAdd.bindValue(":id", gameId);
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
queryAdd.bindValue(":updatedAt", formattedDate);
if (queryAdd.exec())
{
success = true;
}
else
{
qDebug() << "Error: " << queryAdd.lastError();
}
}
return success;
}
void DbManager::updateAllGameRunning()
{
QSqlQuery queryAdd;
queryAdd.prepare("UPDATE games SET running = false");
if (queryAdd.exec())
{
qDebug() << "Success";
}
else
{
qDebug() << "Error: " << queryAdd.lastError();
}
}
int DbManager::totalTimePlayed()
{
int totalTimePlayedResult = 0;
QSqlQuery query;
query.prepare("SELECT SUM(timePlayed) as totalTimePlayed FROM games");
if (query.exec())
{
int totalTimePlayedIndex = query.record().indexOf("totalTimePlayed");
while (query.next())
{
int totalTimePlayed = query.value(totalTimePlayedIndex).toInt();
totalTimePlayedResult = totalTimePlayed;
}
}
else
{
qDebug() << "Error executing query:" << query.lastError().text();
}
return totalTimePlayedResult;
}
// Gets total play time for specific game
int DbManager::totalPlayTime(int gameId)
{
int totalTimePlayedResult = 0;
QSqlQuery query;
query.prepare("SELECT timePlayed FROM games WHERE id = :gameId");
query.bindValue(":gameId", gameId);
if (query.exec())
{
int totalTimePlayedIndex = query.record().indexOf("timePlayed");
while (query.next())
{
int totalTimePlayed = query.value(totalTimePlayedIndex).toInt();
totalTimePlayedResult = totalTimePlayed;
}
}
else
{
qDebug() << "Error executing query:" << query.lastError().text();
}
return totalTimePlayedResult;
}
bool DbManager::insertGameHistorical(const int gameId)
{
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
bool success = false;
if (gameId != 0)
{
QSqlQuery queryAdd;
queryAdd.prepare("INSERT INTO game_historical (gameId,timePlayed,createdAt,updatedAt) VALUES (:gameId,:timePlayed,:createdAt,:updatedAt)");
queryAdd.bindValue(":gameId", gameId);
queryAdd.bindValue(":timePlayed", 0);
queryAdd.bindValue(":createdAt", formattedDate);
queryAdd.bindValue(":updatedAt", formattedDate);
if (queryAdd.exec())
{
success = true;
}
else
{
qDebug() << "Could not insert into game_historical: " << queryAdd.lastError();
}
}
else
{
qDebug() << "Data is required to add.";
}
return success;
}
DbManager::GameHistorical DbManager::getGameHistoricalToday(int gameId)
{
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
DbManager::GameHistorical gameH;
QSqlQuery query;
query.prepare("SELECT id, timePlayed FROM game_historical WHERE gameId = :gameId AND createdAt = :createdAt");
query.bindValue(":gameId", gameId);
query.bindValue(":createdAt", formattedDate);
if (query.exec())
{
int id = query.record().indexOf("id");
int result = query.record().indexOf("timePlayed");
while (query.next())
{
int idResult = query.value(id).toInt();
int totalResult = query.value(result).toInt();
gameH = {idResult, gameId, totalResult, "", ""};
}
}
else
{
qDebug() << "Error executing query:" << query.lastError().text();
}
return gameH;
}
int DbManager::getTodayGameHistorical(int gameId)
{
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
int total = 0;
QSqlQuery query;
query.prepare("SELECT COUNT(id) AS total FROM game_historical WHERE gameId = :gameId AND createdAt = :createdAt");
query.bindValue(":gameId", gameId);
query.bindValue(":createdAt", formattedDate);
if (query.exec())
{
int result = query.record().indexOf("total");
while (query.next())
{
int totalResult = query.value(result).toInt();
total = totalResult;
}
}
else
{
qDebug() << "Error executing query:" << query.lastError().text();
}
return total;
}
void DbManager::updateGameHistoricalTimePlayed(int gameHistoricalId, int timePlayed)
{
QSqlQuery queryAdd;
queryAdd.prepare("UPDATE game_historical SET timePlayed = :timePlayed WHERE id = :gameHistoricalId");
queryAdd.bindValue(":timePlayed", timePlayed);
queryAdd.bindValue(":gameHistoricalId", gameHistoricalId);
if (queryAdd.exec())
{
qDebug() << "Success";
}
else
{
qDebug() << "Error: " << queryAdd.lastError();
}
}
// Gets total play time for today
int DbManager::totalPlayTimeToday(int gameId)
{
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
int totalTimePlayedResult = 0;
QSqlQuery query;
query.prepare("SELECT SUM(timePlayed) AS timePlayed FROM game_historical WHERE createdAt = :createdAt AND gameId = :gameId");
query.bindValue(":createdAt", formattedDate);
query.bindValue(":gameId", gameId);
if (query.exec())
{
int totalTimePlayedIndex = query.record().indexOf("timePlayed");
while (query.next())
{
int totalTimePlayed = query.value(totalTimePlayedIndex).toInt();
totalTimePlayedResult = totalTimePlayed;
}
}
else
{
qDebug() << "Error executing query:" << query.lastError().text();
}
return totalTimePlayedResult;
}
// Gets total play time for this week
int DbManager::totalPlayTimeThisWeek(int gameId)
{
QDate currentDate = QDate::currentDate();
QDate lastWeekDate = currentDate.addDays(-7);
int lastWeekYear = lastWeekDate.year();
int lastWeekMonth = lastWeekDate.month();
int lastWeekDay = lastWeekDate.day();
QString lastWeek = QString("%1-%2-%3").arg(lastWeekYear).arg(lastWeekMonth, 2, 10, QChar('0')).arg(lastWeekDay, 2, 10, QChar('0'));
QDate tomorrowDate = currentDate.addDays(1);
int year = tomorrowDate.year();
int month = tomorrowDate.month();
int day = tomorrowDate.day();
QString today = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
int totalTimePlayedResult = 0;
QSqlQuery query;
query.prepare("SELECT SUM(timePlayed) AS total FROM game_historical WHERE gameId = :gameId AND createdAt > :lastWeek AND createdAt < :today");
query.bindValue(":lastWeek", lastWeek);
query.bindValue(":today", today);
query.bindValue(":gameId", gameId);
if (query.exec())
{
int totalTimePlayedIndex = query.record().indexOf("total");
while (query.next())
{
int totalTimePlayed = query.value(totalTimePlayedIndex).toInt();
totalTimePlayedResult = totalTimePlayed;
}
}
else
{
qDebug() << "Error executing query:" << query.lastError().text();
}
return totalTimePlayedResult;
}
DbManager::MostPlayGame DbManager::mostPlayedGame()
{
DbManager::MostPlayGame gameResult;
QSqlQuery query;
query.prepare("SELECT gameName, MAX(timePlayed) AS total FROM games");
if (query.exec())
{
int gameName = query.record().indexOf("gameName");
int total = query.record().indexOf("total");
while (query.next())
{
QString gameNameResult = query.value(gameName).toString();
int totalResult = query.value(total).toInt();
gameResult = {gameNameResult, totalResult};
}
}
else
{
qDebug() << "Error executing query int mostPlayedGame func:" << query.lastError().text();
}
return gameResult;
}
int DbManager::totalTimePlayedToday()
{
int totalResponse = 0;
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
QSqlQuery query;
query.prepare("SELECT SUM(timePlayed) AS total FROM game_historical WHERE createdAt = :createdAt");
query.bindValue(":createdAt", formattedDate);
if (query.exec())
{
int total = query.record().indexOf("total");
while (query.next())
{
int totalResult = query.value(total).toInt();
totalResponse = totalResult;
}
}
else
{
qDebug() << "Error executing query int mostPlayedGame func:" << query.lastError().text();
}
return totalResponse;
}
// Gets total play time filter by days
int DbManager::timePlayedFilter(int days)
{
QDate currentDate = QDate::currentDate();
QDate lastWeekDate = currentDate.addDays(-days);
int lastWeekYear = lastWeekDate.year();
int lastWeekMonth = lastWeekDate.month();
int lastWeekDay = lastWeekDate.day();
QString lastWeek = QString("%1-%2-%3").arg(lastWeekYear).arg(lastWeekMonth, 2, 10, QChar('0')).arg(lastWeekDay, 2, 10, QChar('0'));
QDate tomorrowDate = currentDate.addDays(1);
int year = tomorrowDate.year();
int month = tomorrowDate.month();
int day = tomorrowDate.day();
QString today = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
int totalTimePlayedResult = 0;
QSqlQuery query;
query.prepare("SELECT SUM(timePlayed) AS total FROM game_historical WHERE createdAt > :lastWeek AND createdAt < :today");
query.bindValue(":lastWeek", lastWeek);
query.bindValue(":today", today);
if (query.exec())
{
int totalTimePlayedIndex = query.record().indexOf("total");
while (query.next())
{
int totalTimePlayed = query.value(totalTimePlayedIndex).toInt();
totalTimePlayedResult = totalTimePlayed;
}
}
else
{
qDebug() << "Error executing query:" << query.lastError().text();
}
return totalTimePlayedResult;
}
bool DbManager::deleteGame(int gameId)
{
QSqlQuery query;
query.prepare("DELETE FROM games WHERE id = :gameId");
query.bindValue(":gameId", gameId);
if (query.exec())
{
while (query.next())
{
return true;
}
}
else
{
qDebug() << "Error executing query int DeleteGame func:" << query.lastError().text();
return false;
}
return false;
}
bool DbManager::deleteGameHistorical(int gameId)
{
QSqlQuery query;
query.prepare("DELETE FROM game_historical WHERE gameId = :gameId");
query.bindValue(":gameId", gameId);
if (query.exec())
{
while (query.next())
{
return true;
}
}
else
{
qDebug() << "Error executing query int DeleteGameHistorical func:" << query.lastError().text();
return false;
}
return false;
}
QVector<DbManager::HoursPlayedPerDayOfTheLastWeekData> DbManager::hoursPlayedPerDayOfTheLastWeek(int gameId)
{
QVector<DbManager::HoursPlayedPerDayOfTheLastWeekData> hoursPlayedPerDayOfTheLastWeekResult;
QSqlQuery query;
query.prepare("WITH DateRange AS (SELECT DATE('now', '-' || (row_number() OVER ()) || ' days') AS day FROM (SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7) UNION SELECT DATE('now', 'localtime')) SELECT DateRange.day AS day, STRFTIME('%w', DateRange.day) AS day_of_week, COALESCE(SUM(CASE WHEN game_historical.gameId = :gameId THEN game_historical.timePlayed ELSE 0 END), 0) AS total_time_played FROM DateRange LEFT JOIN game_historical ON DATE(game_historical.updatedAt) = DateRange.day AND game_historical.gameId = :gameId WHERE DateRange.day > DATE('now', '-8 days', 'localtime') GROUP BY DateRange.day ORDER BY DateRange.day;");
query.bindValue(":gameId", gameId);
if (query.exec())
{
int day = query.record().indexOf("day");
int dayOfWeek = query.record().indexOf("day_of_week");
int totalTimePlayed = query.record().indexOf("total_time_played");
while (query.next())
{
QString dayResult = query.value(day).toString();
int dayOfWeekResult = query.value(dayOfWeek).toInt();
int totalTimePlayedResult = query.value(totalTimePlayed).toInt();
hoursPlayedPerDayOfTheLastWeekResult.push_back(DbManager::HoursPlayedPerDayOfTheLastWeekData{dayResult, dayOfWeekResult, totalTimePlayedResult});
}
}
else
{
qDebug() << "Error executing query int hoursPlayedPerDayOfTheLastWeek func:" << query.lastError().text();
}
return hoursPlayedPerDayOfTheLastWeekResult;
}
QVector<DbManager::Achivements> DbManager::getAchivements()
{
QVector<Achivements> achivements;
QSqlQuery query("SELECT * FROM achivements");
int idIndex = query.record().indexOf("id");
int achivementIndex = query.record().indexOf("achivement");
int unlockedIndex = query.record().indexOf("unlocked");
int activeIndex = query.record().indexOf("active");
int unlockedAtIndex = query.record().indexOf("unlockedAt");
while (query.next())
{
int id = query.value(idIndex).toInt();
QString achivement = query.value(achivementIndex).toString();
bool unlocked = query.value(unlockedIndex).toBool();
bool active = query.value(activeIndex).toBool();
QString unlockedAt = query.value(unlockedAtIndex).toString();
achivements.push_back(DbManager::Achivements{id, achivement, unlocked, active, unlockedAt});
}
return achivements;
}
bool DbManager::updateAchivement(int id)
{
QSqlQuery queryAdd;
queryAdd.prepare("UPDATE achivements SET unlocked = true, unlockedAt = :unlockedAt WHERE id = :id");
QDate currentDate = QDate::currentDate();
int year = currentDate.year();
int month = currentDate.month();
int day = currentDate.day();
QString formattedDate = QString("%1-%2-%3").arg(year).arg(month, 2, 10, QChar('0')).arg(day, 2, 10, QChar('0'));
queryAdd.bindValue(":unlockedAt", formattedDate);
queryAdd.bindValue(":id", id);
if (queryAdd.exec())
{
qDebug() << "Success updating achivements";
return true;
}
else
{
qDebug() << "Error updating achivements: " << queryAdd.lastError();
return false;
}
}
void DbManager::updateActiveTheme(int id){
QSqlQuery queryAdd;
queryAdd.prepare("UPDATE achivements SET active = CASE WHEN id IN (:id) THEN true ELSE false END;");
queryAdd.bindValue(":id", id);
if (queryAdd.exec())
{
qDebug() << "Success updating achivements theme";
}
else
{
qDebug() << "Error updating achivements theme: " << queryAdd.lastError();
}
}