-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_service.cpp
146 lines (126 loc) · 5.04 KB
/
database_service.cpp
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
#include "database_service.h"
DatabaseService::DatabaseService(QObject *parent) : QObject(parent) {
if (QSqlDatabase::contains("jewel_game_save_connection")) {
db = new QSqlDatabase(QSqlDatabase::database("jewel_game_save_connection"));
} else {
db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE", "jewel_game_save_connection"));
db->setDatabaseName("data.db");
}
if (!db->open()) {
qDebug() << db->lastError();
} else {
QSqlQuery sqlQuery(*db);
sqlQuery.exec(
"CREATE TABLE Normal (end_time TEXT NOT NULL, score INTEGER NOT NULL, hint_times INTEGER NOT NULL);");
sqlQuery.exec(
"CREATE TABLE Hard (end_time TEXT NOT NULL, score INTEGER NOT NULL, hint_times INTEGER NOT NULL);");
sqlQuery.exec("CREATE TABLE Challenge (end_time TEXT NOT NULL, level INTEGER NOT NULL, hint_times INTEGER NOT "
"NULL, duration INTEGER NOT NULL);");
}
}
bool DatabaseService::addNewScore(QString mode, int score, int hintTimes) {
if (!db->open()) {
qDebug() << db->lastError();
return false;
} else {
QSqlQuery sqlQuery(*db);
bool isSucceed = false;
if (mode == "Normal") {
sqlQuery.prepare("INSERT INTO Normal (end_time, score, hint_times) VALUES (?, ?, ?)");
} else if (mode == "Hard") {
sqlQuery.prepare("INSERT INTO Hard (end_time, score, hint_times) VALUES (?, ?, ?)");
} else {
isSucceed = false;
db->close();
return isSucceed;
}
sqlQuery.addBindValue(QDateTime::currentDateTime().toString("yy/MM/dd hh:mm:ss"));
sqlQuery.addBindValue(score);
sqlQuery.addBindValue(hintTimes);
if (sqlQuery.exec()) {
isSucceed = true;
} else {
qDebug() << db->lastError();
isSucceed = false;
}
db->close();
return isSucceed;
}
}
bool DatabaseService::addNewLevel(QString mode, int level, int hintTimes, int duration) {
if (!db->open()) {
qDebug() << db->lastError();
return false;
} else {
QSqlQuery sqlQuery(*db);
bool isSucceed = false;
if (mode == "Challenge") {
sqlQuery.prepare("INSERT INTO Challenge (end_time, level, hint_times, duration) VALUES (?, ?, ?, ?)");
sqlQuery.addBindValue(QDateTime::currentDateTime().toString("yy/MM/dd hh:mm:ss"));
sqlQuery.addBindValue(level);
sqlQuery.addBindValue(hintTimes);
sqlQuery.addBindValue(duration);
} else {
isSucceed = false;
db->close();
return isSucceed;
}
if (sqlQuery.exec()) {
isSucceed = true;
} else {
qDebug() << db->lastError();
isSucceed = false;
}
db->close();
return isSucceed;
}
}
QList<QObject *> DatabaseService::getDataByMode(QString mode) {
auto list = QList<QObject *>();
if (!db->open()) {
qDebug() << db->lastError();
return list;
} else {
QSqlQuery sqlQuery(*db);
if (mode == "Normal") {
sqlQuery.prepare("select end_time, score, hint_times from Normal");
} else if (mode == "Hard") {
sqlQuery.prepare("select end_time, score, hint_times from Hard");
} else if (mode == "Challenge") {
sqlQuery.prepare("select end_time, level, hint_times, duration from Challenge");
} else {
return list;
}
if (!sqlQuery.exec()) {
qDebug() << sqlQuery.lastError();
} else {
while (sqlQuery.next()) {
GameRoundDataObj *newData = new GameRoundDataObj;
newData->mode = mode;
newData->endTime = sqlQuery.value(0).toString();
if (mode == "Challenge") {
newData->level = sqlQuery.value(1).toInt();
newData->duration = sqlQuery.value(3).toInt();
} else
newData->score = sqlQuery.value(1).toInt();
;
newData->hintTimes = sqlQuery.value(2).toInt();
list.append(newData);
}
}
}
return list;
}
GameRoundDataObj::GameRoundDataObj(QObject *parent) {}
int GameRoundDataObj::getDuration() const { return duration; }
void GameRoundDataObj::setDuration(const int &value) { duration = value; }
QString GameRoundDataObj::getEndTime() const { return endTime; }
void GameRoundDataObj::setEndTime(const QString &value) { endTime = value; }
int GameRoundDataObj::getScore() const { return score; }
void GameRoundDataObj::setScore(int value) { score = value; }
int GameRoundDataObj::getLevel() const { return level; }
void GameRoundDataObj::setLevel(int value) { level = value; }
int GameRoundDataObj::getHintTimes() const { return hintTimes; }
void GameRoundDataObj::setHintTimes(int value) { hintTimes = value; }
QString GameRoundDataObj::getMode() const { return mode; }
void GameRoundDataObj::setMode(const QString &value) { mode = value; }