-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
131 lines (111 loc) · 4.04 KB
/
main.cpp
File metadata and controls
131 lines (111 loc) · 4.04 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
#include "mainwindow.h"
#include "FileWordSource.h"
#include "SqlWordSource.h"
#include "GameManager.h"
#include "GameConfig.h"
#include <QApplication>
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QFile>
#include <QTextStream>
#include <QDebug>
namespace {
// Basit bir tablo şeması yaratır (yoksa) ve boşsa words.txt'den seed eder.
void initializeDatabaseIfNeeded(QSqlDatabase& db, const QString& wordsFilePath)
{
if (!db.isOpen()) {
qWarning() << "initializeDatabaseIfNeeded: database is not open";
return;
}
// Tabloyu oluştur (yoksa)
QSqlQuery createQuery(db);
const char* createSql =
"CREATE TABLE IF NOT EXISTS words ("
" id INTEGER PRIMARY KEY AUTOINCREMENT,"
" text TEXT NOT NULL,"
" length INTEGER NOT NULL,"
" language TEXT NOT NULL DEFAULT 'tr',"
" is_answer INTEGER NOT NULL DEFAULT 1"
");";
if (!createQuery.exec(QLatin1String(createSql))) {
qWarning() << "Failed to create words table:" << createQuery.lastError().text();
return;
}
// İlgili uzunlukta kelime var mı kontrol et
QSqlQuery countQuery(db);
countQuery.prepare("SELECT COUNT(*) FROM words WHERE length = :len");
countQuery.bindValue(":len", GameConfig::WORD_LENGTH);
if (!countQuery.exec() || !countQuery.next()) {
qWarning() << "Failed to count words:" << countQuery.lastError().text();
return;
}
int existingCount = countQuery.value(0).toInt();
if (existingCount > 0) {
// Zaten veri var, seed etmeye gerek yok.
return;
}
// words.txt'den seed et
QFile file(wordsFilePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Could not open words file for seeding DB:" << wordsFilePath;
return;
}
QTextStream in(&file);
db.transaction();
QSqlQuery insertQuery(db);
insertQuery.prepare(
"INSERT INTO words (text, length, language, is_answer) "
"VALUES (:text, :length, :language, 1)"
);
int inserted = 0;
while (!in.atEnd()) {
QString line = in.readLine().trimmed().toUpper();
if (line.length() != GameConfig::WORD_LENGTH)
continue;
insertQuery.bindValue(":text", line);
insertQuery.bindValue(":length", GameConfig::WORD_LENGTH);
insertQuery.bindValue(":language", QStringLiteral("tr"));
if (!insertQuery.exec()) {
qWarning() << "Failed to insert word" << line << ":" << insertQuery.lastError().text();
// Hata olsa da diğerlerine devam edebiliriz.
continue;
}
++inserted;
}
if (!db.commit()) {
qWarning() << "Failed to commit word seeding transaction:" << db.lastError().text();
} else {
qDebug() << "Seeded" << inserted << "words into database from" << wordsFilePath;
}
}
} // namespace
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
const QString baseDir = QCoreApplication::applicationDirPath() + "/..";
const QString wordsFilePath = baseDir + "/words.txt";
const QString dbPath = baseDir + "/words.db";
// SQLite veritabanı bağlantısını kur
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbPath);
if (!db.open()) {
qWarning() << "Failed to open database, falling back to FileWordSource:"
<< db.lastError().text();
// Fallback: Eski davranış, direkt dosyadan oku
FileWordSource wordSource(wordsFilePath);
GameManager gameManager(wordSource);
MainWindow w(gameManager, wordSource);
w.show();
return a.exec();
}
// Veritabanını oluştur ve gerekirse words.txt'den seed et
initializeDatabaseIfNeeded(db, wordsFilePath);
// Artık kelimeleri veritabanından okuyan SqlWordSource'u kullanıyoruz
SqlWordSource wordSource(db);
GameManager gameManager(wordSource);
MainWindow w(gameManager, wordSource);
w.show();
return a.exec();
}