2 #include <QApplication>
5 #include <QInputDialog>
13 #include "mainwindow.h"
16 #include "formations.h"
21 sqlite3 *open_db(const char *filename)
24 int ret = sqlite3_open(filename, &db);
25 if (ret != SQLITE_OK) {
26 fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
31 CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR, gender VARCHAR(1));
32 )", nullptr, nullptr, nullptr); // Ignore errors.
35 CREATE TABLE IF NOT EXISTS match (match INTEGER PRIMARY KEY, description VARCHAR, video_filename VARCHAR, gender_rule_a BOOLEAN DEFAULT false, gender_pull_rule BOOLEAN DEFAULT false);
36 )", nullptr, nullptr, nullptr); // Ignore errors.
39 CREATE TABLE IF NOT EXISTS formation (formation INTEGER PRIMARY KEY, name VARCHAR, offense BOOLEAN NOT NULL);
40 )", nullptr, nullptr, nullptr); // Ignore errors.
43 CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, match INTEGER, t INTEGER, player INTEGER, type VARCHAR, formation INTEGER, FOREIGN KEY (player) REFERENCES player(player), FOREIGN KEY (match) REFERENCES match (match), FOREIGN KEY (formation) REFERENCES formation (formation));
44 )", nullptr, nullptr, nullptr); // Ignore errors.
46 sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr); // Ignore errors.
47 sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr); // Ignore errors.
48 sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr); // Ignore errors.
51 int get_match_id(sqlite3 *db, QWidget *parent, int requested_match)
55 bool requested_match_ok = false;
57 // Read the list of matches already in the database.
59 int ret = sqlite3_prepare_v2(db, "SELECT match, description FROM match ORDER BY match", -1, &stmt, 0);
60 if (ret != SQLITE_OK) {
61 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
65 ret = sqlite3_step(stmt);
66 if (ret == SQLITE_ROW) {
68 snprintf(buf, sizeof(buf), "%s (%d)", sqlite3_column_text(stmt, 1), sqlite3_column_int(stmt, 0));
69 ids.push_back(sqlite3_column_int(stmt, 0));
70 if (ids.back() == requested_match) {
71 requested_match_ok = true;
74 } else if (ret == SQLITE_DONE) {
77 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
81 ret = sqlite3_finalize(stmt);
82 if (ret != SQLITE_OK) {
83 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
86 items.push_back("Add new…");
88 if (requested_match_ok) {
89 return requested_match;
94 QInputDialog dialog(parent, Qt::WindowFlags());
95 dialog.setWindowTitle("Open game");
96 dialog.setLabelText("Choose game to analyze:");
97 dialog.setComboBoxItems(items);
98 if (items.size() >= 2) {
99 dialog.setTextValue(items[items.size() - 2]);
101 dialog.setOption(QInputDialog::UseListViewForComboBoxItems, true);
102 if (!dialog.exec()) {
105 chosen_str = dialog.textValue();
108 for (unsigned i = 0; i < ids.size(); ++i) {
109 if (chosen_str == items[i]) {
114 // Must be a new game. Get its name and insert it into the database.
116 QString new_game_str = QInputDialog::getText(parent, "New game", "Choose name for new game:", QLineEdit::Normal, "", &ok);
117 if (!ok || new_game_str.isEmpty()) {
121 // Insert the new row into the database.
122 ret = sqlite3_prepare_v2(db, "INSERT INTO match (description, gender_rule_a, gender_pull_rule) VALUES (?, COALESCE((SELECT gender_rule_a FROM match ORDER BY match DESC LIMIT 1),false), COALESCE((SELECT gender_pull_rule FROM match ORDER BY match DESC LIMIT 1),false))", -1, &stmt, 0);
123 if (ret != SQLITE_OK) {
124 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
128 QByteArray new_game_utf8 = new_game_str.toUtf8();
129 sqlite3_bind_text(stmt, 1, (const char *)new_game_utf8.data(), new_game_utf8.size(), SQLITE_STATIC);
131 ret = sqlite3_step(stmt);
132 if (ret == SQLITE_ROW) {
133 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
137 ret = sqlite3_finalize(stmt);
138 if (ret != SQLITE_OK) {
139 fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
143 return sqlite3_last_insert_rowid(db);
146 int main(int argc, char *argv[])
148 QApplication app(argc, argv);
149 sqlite3 *db = open_db("ultimate.db");
151 int requested_match = -1;
153 requested_match = atoi(argv[1]);
156 int match_id = get_match_id(db, nullptr, requested_match);
157 if (match_id <= 0) { // Cancel.
161 MainWindow mainWindow(new EventsModel(db, match_id), new PlayersModel(db),
162 new FormationsModel(db, true), new FormationsModel(db, false),
164 mainWindow.resize(QSize(1280, 720));
167 int ret = app.exec();