-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmw_database.cpp
More file actions
200 lines (155 loc) · 4.91 KB
/
Copy pathmw_database.cpp
File metadata and controls
200 lines (155 loc) · 4.91 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDateTime>
#include <QDesktopServices>
#include <QUrl>
#include "pathAppData.h"
#include "db_settings.h"
QString textQueryResuls() {
return
"select"
" dateTime(t1.dateTime, 'localtime') as dateTime,"
" t2.countPassedSteps as steps,"
" t2.countErrors as err,"
" round(t2.score, 3) as SCORE,"
" round("
" 1. - "
" cast(t2.countErrors as double) /"
" cast(t2.countPassedSteps as double)"
" , 2) as probability"
" from ("
" select"
" t1.id as idStart,"
" max(t2.id) as idLastStep"
" from"
" STARTS as t1"
" inner join STEPS as t2"
" on t1.id = t2.idStart"
" where"
" (:level = 0 or t2.queueLength = :level)"
" and t2.countPassedSteps >= :minCount"
" and t1.gameType = :gameType"
" group by"
" t1.id"
" ) as t0"
" inner join STARTS as t1 on t1.id = t0.idStart"
" inner join STEPS as t2 on t2.id = t0.idLastStep"
" order by t1.dateTime desc";
}
void MainWindow::setQueryResults() {
QSqlQuery& qu = query_results;
if (!qu.isActive()) {
qu = QSqlQuery();
qu.prepare(textQueryResuls());
}
qu.bindValue(":level", m_current_level);
qu.bindValue(":minCount", m_current_minCount);
qu.bindValue(":gameType", state_game_type);
DbTools::execWithCheck(qu);
model_results.setQuery(qu);
}
void MainWindow::initTableResults() {
auto* tb = ui->tb_results;
tb->setModel(&model_results);
setQueryResults();
tb->setColumnWidth(0, 200);
}
static bool table_exists(const QString& name) {
QSqlQuery qu;
qu.prepare(
"select name from sqlite_master"
" where type = 'table' and name = ?");
qu.addBindValue(name);
DbTools::execWithCheck(qu);
return qu.next();
}
static int db_version() {
QString v = DbSettings::getSettings("db_version");
if (!v.isEmpty())
return v.toInt();
if (table_exists("STARTS"))
return 1;
return 0;
}
static void set_db_version(int v) {
DbSettings::setSettings("db_version", QString::number(v));
}
void MainWindow::initDatabase() {
const QString driver = "QSQLITE";
if (!QSqlDatabase::isDriverAvailable(driver))
return;
auto db = QSqlDatabase::addDatabase(driver);
QString s = pathAppData() + "/database.sqlite";
ui->lb_AppData->setText(s);
db.setDatabaseName(s);
connect(ui->bt_open_path_with_database, &QPushButton::clicked,
[this] { QDesktopServices::openUrl(pathAppData()); });
if (!db.open())
return;
if (db_version() == 1) {
DbTools::execWithCheck("alter table STARTS add gameType integer");
DbTools::execWithCheck("update STARTS set gameType = 1");
DbTools::execWithCheck("alter table STEPS rename probability to score");
DbTools::execWithCheck("alter table STEPS add asTimeMarker integer");
DbTools::execWithCheck("update STEPS set asTimeMarker = 0");
}
set_db_version(2);
DbTools::execWithCheck(
"create table if not exists"
" STARTS ("
" id integer primary key,"
" gameType integer,"
" dateTime text"
" )");
DbTools::execWithCheck(
"create table if not exists"
" STEPS ("
" id integer primary key,"
" idStart integer not null,"
" asTimeMarker integer,"
" dateTime text,"
" queueLength integer,"
" hasError bool,"
" hasSkipping bool,"
" currentSymbol integer,"
" pressedSymbol integer,"
" score double,"
" countPassedSteps integer,"
" countSkippedSteps integer,"
" countErrors integer"
" )");
}
void MainWindow::insert_start() {
QSqlQuery q("insert into STARTS (dateTime,gameType) values (?,?)");
q.addBindValue(QDateTime::currentDateTimeUtc());
q.addBindValue(state_game_type);
DbTools::execWithCheck(q);
id_current_game = q.lastInsertId().toUInt();
}
void MainWindow::insert_step(uint asTimeMarker) {
uint szQu = list_remember_queue.size();
const auto& lev = map_stat_levels[szQu];
QSqlQuery q;
q.prepare(
"insert into STEPS ("
" idStart, dateTime, queueLength, hasError, hasSkipping,"
" currentSymbol, pressedSymbol, score,"
" countPassedSteps, countSkippedSteps, countErrors,"
" asTimeMarker"
" ) values (?,?,?,?,?,?,?,?,?,?,?,?)");
q.addBindValue(id_current_game);
q.addBindValue(QDateTime::currentDateTimeUtc());
q.addBindValue(szQu);
q.addBindValue(state_error);
q.addBindValue(state_skipping);
q.addBindValue(current_symbol.unicode());
q.addBindValue(pressed_symbol.unicode());
q.addBindValue(lev.score);
q.addBindValue(lev.c_passed_steps);
q.addBindValue(lev.c_skipped_steps);
q.addBindValue(lev.c_errors);
q.addBindValue(asTimeMarker);
DbTools::execWithCheck(q);
}