]> git.sesse.net Git - pkanalytics/blobdiff - main.cpp
Fix crash if there are no matches (database is empty).
[pkanalytics] / main.cpp
index 4654a928e4d84117148bd6d6ad704cf7caffcdb4..ade653a72b4e1f1cdd31aac389ece506a1a12953 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -33,7 +33,7 @@ sqlite3 *open_db(const char *filename)
        )", nullptr, nullptr, nullptr);  // Ignore errors.
 
        sqlite3_exec(db, R"(
-               CREATE TABLE IF NOT EXISTS match (match INTEGER PRIMARY KEY, description VARCHAR);
+               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);
        )", nullptr, nullptr, nullptr);  // Ignore errors.
 
        sqlite3_exec(db, R"(
@@ -96,7 +96,9 @@ int get_match_id(sqlite3 *db, QWidget *parent, int requested_match)
                dialog.setWindowTitle("Open game");
                dialog.setLabelText("Choose game to analyze:");
                dialog.setComboBoxItems(items);
-               dialog.setTextValue(items[items.size() - 2]);
+               if (items.size() >= 2) {
+                       dialog.setTextValue(items[items.size() - 2]);
+               }
                dialog.setOption(QInputDialog::UseListViewForComboBoxItems, true);
                if (!dialog.exec()) {
                        return -1;
@@ -118,7 +120,7 @@ int get_match_id(sqlite3 *db, QWidget *parent, int requested_match)
        }
 
        // Insert the new row into the database.
-       ret = sqlite3_prepare_v2(db, "INSERT INTO match (description) VALUES (?)", -1, &stmt, 0);
+       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);
        if (ret != SQLITE_OK) {
                fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
                abort();
@@ -147,9 +149,6 @@ int main(int argc, char *argv[])
        QApplication app(argc, argv);
        sqlite3 *db = open_db("ultimate.db");
 
-       // TODO: do this on-demand instead, from a menu
-       export_to_json(db, "ultimate.json");
-
        int requested_match = -1;
        if (argc >= 2) {
                requested_match = atoi(argv[1]);
@@ -161,10 +160,12 @@ int main(int argc, char *argv[])
        }
 
        MainWindow mainWindow(new EventsModel(db, match_id), new PlayersModel(db),
-                             new FormationsModel(db, true), new FormationsModel(db, false));
+                             new FormationsModel(db, true), new FormationsModel(db, false),
+                             db, match_id);
        mainWindow.resize(QSize(1280, 720));
        mainWindow.show();
 
-       return app.exec();
+       int ret = app.exec();
 
+       return ret;
 }