]> git.sesse.net Git - pkanalytics/blobdiff - main.cpp
Add a Win32 build.
[pkanalytics] / main.cpp
index b65895d38a278b2edff0bc9b614a231eac5db5d8..3889023fa6154a51ade93615a9242c261cb282fd 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -1,9 +1,9 @@
-#include <QMediaPlayer>
 #include <QMainWindow>
 #include <QApplication>
 #include <QGridLayout>
-#include <QVideoWidget>
 #include <QShortcut>
+#include <QInputDialog>
+#include <QTimer>
 #include <algorithm>
 #include <string>
 #include <map>
 #include <optional>
 #include <sqlite3.h>
 #include "mainwindow.h"
-#include "ui_mainwindow.h"
 #include "events.h"
+#include "players.h"
+#include "formations.h"
+#include "json.h"
 
 using namespace std;
 
-string format_timestamp(uint64_t pos)
-{
-       int ms = pos % 1000;
-       pos /= 1000;
-       int sec = pos % 60;
-       pos /= 60;
-       int min = pos % 60;
-       int hour = pos / 60;
-
-       char buf[256];
-       snprintf(buf, sizeof(buf), "%d:%02d:%02d.%03d", hour, min, sec, ms);
-       return buf;
-}
-
-MainWindow::MainWindow(EventsModel *events) : events(events)
+sqlite3 *open_db(const char *filename)
 {
-       player = new QMediaPlayer;
-       //player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv"));
-       player->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv"));
-       player->play();
-
-       ui = new Ui::MainWindow;
-       ui->setupUi(this);
-
-       ui->event_view->setModel(events);
-       connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged,
-               [this, events](const QModelIndex &current, const QModelIndex &previous) {
-                       player->setPosition(events->get_time(current.row()));
-               });
+       sqlite3 *db;
+       int ret = sqlite3_open(filename, &db);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db));
+               exit(1);
+       }
 
-       connect(player, &QMediaPlayer::positionChanged, [this](uint64_t pos) {
-               position_changed(pos);
-       });
+       sqlite3_exec(db, R"(
+               CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR, gender VARCHAR(1));
+       )", nullptr, nullptr, nullptr);  // Ignore errors.
 
-       player->setVideoOutput(ui->video);
+       sqlite3_exec(db, R"(
+               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.
 
-       connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); });
-       connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); });
+       sqlite3_exec(db, R"(
+               CREATE TABLE IF NOT EXISTS formation (formation INTEGER PRIMARY KEY, name VARCHAR, offense BOOLEAN NOT NULL);
+       )", nullptr, nullptr, nullptr);  // Ignore errors.
 
-       connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); });
-       connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); });
+       sqlite3_exec(db, R"(
+               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));
+       )", nullptr, nullptr, nullptr);  // Ignore errors.
 
-       // TODO: Would be nice to actually have a frame...
-       connect(ui->minus1f, &QPushButton::clicked, [this]() { seek(-20); });
-       connect(ui->plus1f, &QPushButton::clicked, [this]() { seek(20); });
+       sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr);  // Ignore errors.
+       sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr);  // Ignore errors.
+       sqlite3_exec(db, "PRAGMA foreign_keys=ON", nullptr, nullptr, nullptr);  // Ignore errors.
+       return db;
+}
+int get_match_id(sqlite3 *db, QWidget *parent, int requested_match)
+{
+       QStringList items;
+       vector<int> ids;
+       bool requested_match_ok = false;
 
-       connect(ui->play_pause, &QPushButton::clicked, [this]() {
-               if (playing) {
-                       player->pause();
-                       ui->play_pause->setText("Play (space)");
+       // Read the list of matches already in the database.
+       sqlite3_stmt *stmt;
+       int ret = sqlite3_prepare_v2(db, "SELECT match, description FROM match ORDER BY match", -1, &stmt, 0);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+       for ( ;; ) {
+               ret = sqlite3_step(stmt);
+               if (ret == SQLITE_ROW) {
+                       char buf[256];
+                       snprintf(buf, sizeof(buf), "%s (%d)", sqlite3_column_text(stmt, 1), sqlite3_column_int(stmt, 0));
+                       ids.push_back(sqlite3_column_int(stmt, 0));
+                       if (ids.back() == requested_match) {
+                               requested_match_ok = true;
+                       }
+                       items.push_back(buf);
+               } else if (ret == SQLITE_DONE) {
+                       break;
                } else {
-                       player->setPlaybackRate(1.0);
-                       player->play();
-                       ui->play_pause->setText("Pause (space)");
+                       fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
+                       abort();
                }
-               playing = !playing;
-
-               // Needs to be set anew when we modify setText(), evidently.
-               ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr));
-       });
-
-       connect(ui->player_1, &QPushButton::clicked, [this]() { insert_event(1); });
-       connect(ui->player_2, &QPushButton::clicked, [this]() { insert_event(2); });
-       connect(ui->player_3, &QPushButton::clicked, [this]() { insert_event(3); });
-       connect(ui->player_4, &QPushButton::clicked, [this]() { insert_event(4); });
-       connect(ui->player_5, &QPushButton::clicked, [this]() { insert_event(5); });
-       connect(ui->player_6, &QPushButton::clicked, [this]() { insert_event(6); });
-       connect(ui->player_7, &QPushButton::clicked, [this]() { insert_event(7); });
-
-       // TODO: disable if nothing is selected
-       connect(ui->catch_, &QPushButton::clicked, [this]() { set_current_event_type("catch"); });
-       connect(ui->throwaway, &QPushButton::clicked, [this]() { set_current_event_type("throwaway"); });
-       connect(ui->drop, &QPushButton::clicked, [this]() { set_current_event_type("drop"); });
-       connect(ui->goal, &QPushButton::clicked, [this]() { set_current_event_type("goal"); });
-       connect(ui->offensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_plus"); });
-       connect(ui->offensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("offensive_soft_minus"); });
-       connect(ui->pull, &QPushButton::clicked, [this]() { set_current_event_type("pull"); });
-       connect(ui->pull_landed, &QPushButton::clicked, [this]() { set_current_event_type("pull_landed"); });
-
-       QShortcut *key_delete = new QShortcut(QKeySequence(Qt::Key_Delete), this);
-       connect(key_delete, &QShortcut::activated, [this]() { ui->delete_->animateClick(); });
-       connect(ui->delete_, &QPushButton::clicked, [this]() { delete_current_event(); });
-}
-
-void MainWindow::position_changed(uint64_t pos)
-{
-       ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos)));
-       if (buffered_seek) {
-               player->setPosition(*buffered_seek);
-               buffered_seek.reset();
        }
-       if (!playing) {
-               player->pause();  // We only played to get a picture.
+       ret = sqlite3_finalize(stmt);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
+               abort();
        }
-       update_status();
-}
+       items.push_back("Add new…");
 
-void MainWindow::seek(int64_t delta_ms)
-{
-       int64_t current_pos = buffered_seek ? *buffered_seek : player->position();
-       uint64_t pos = max<int64_t>(current_pos + delta_ms, 0);
-       buffered_seek = pos;
-       if (!playing) {
-               player->setPlaybackRate(0.01);
-               player->play();  // Or Qt won't show the seek.
+       if (requested_match_ok) {
+               return requested_match;
        }
-}
 
-void MainWindow::insert_event(int button_id)
-{
-       ui->event_view->selectionModel()->blockSignals(true);
-       ui->event_view->selectRow(events->insert_event(player->position(), button_id));
-       ui->event_view->selectionModel()->blockSignals(false);
-}
-
-void MainWindow::set_current_event_type(const string &type)
-{
-       QItemSelectionModel *select = ui->event_view->selectionModel();
-       if (!select->hasSelection()) {
-               return;
+       QString chosen_str;
+       {
+               QInputDialog dialog(parent, Qt::WindowFlags());
+               dialog.setWindowTitle("Open game");
+               dialog.setLabelText("Choose game to analyze:");
+               dialog.setComboBoxItems(items);
+               if (items.size() >= 2) {
+                       dialog.setTextValue(items[items.size() - 2]);
+               }
+               dialog.setOption(QInputDialog::UseListViewForComboBoxItems, true);
+               if (!dialog.exec()) {
+                       return -1;
+               }
+               chosen_str = dialog.textValue();
        }
-       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
-       events->set_event_type(row, type);
-       update_status();
-}
 
-void MainWindow::delete_current_event()
-{
-       QItemSelectionModel *select = ui->event_view->selectionModel();
-       if (!select->hasSelection()) {
-               return;
+       for (unsigned i = 0; i < ids.size(); ++i) {
+               if (chosen_str == items[i]) {
+                       return ids[i];
+               }
        }
-       int row = select->selectedRows().front().row();  // Should only be one, due to our selection behavior.
-       ui->event_view->selectionModel()->blockSignals(true);
-       events->delete_event(row);
-       ui->event_view->selectionModel()->blockSignals(false);
-       update_status();
-}
 
-void MainWindow::update_status()
-{
-       EventsModel::Status s = events->get_status_at(player->position());
-       char buf[256];
-       snprintf(buf, sizeof(buf), "%d–%d | %s | %d passes, %d sec possession",
-               s.our_score, s.their_score, s.offense ? "offense" : "defense", s.num_passes, s.possession_sec);
-       ui->status->setText(buf);
-}
+       // Must be a new game. Get its name and insert it into the database.
+       bool ok;
+       QString new_game_str = QInputDialog::getText(parent, "New game", "Choose name for new game:", QLineEdit::Normal, "", &ok);
+       if (!ok || new_game_str.isEmpty()) {
+               return -1;
+       }
 
-sqlite3 *open_db(const char *filename)
-{
-       sqlite3 *db;
-       int ret = sqlite3_open(filename, &db);
+       // Insert the new row into the database.
+       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, "%s: %s\n", filename, sqlite3_errmsg(db));
-               exit(1);
+               fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
+               abort();
        }
 
-       sqlite3_exec(db, R"(
-               CREATE TABLE IF NOT EXISTS player (player INTEGER PRIMARY KEY, number VARCHAR, name VARCHAR);
-       )", nullptr, nullptr, nullptr);  // Ignore errors.
+       QByteArray new_game_utf8 = new_game_str.toUtf8();
+       sqlite3_bind_text(stmt, 1, (const char *)new_game_utf8.data(), new_game_utf8.size(), SQLITE_STATIC);
 
-       sqlite3_exec(db, R"(
-               CREATE TABLE IF NOT EXISTS event (event INTEGER PRIMARY KEY, t INTEGER, player INTEGER, type VARCHAR, FOREIGN KEY (player) REFERENCES player(player));
-       )", nullptr, nullptr, nullptr);  // Ignore errors.
+       ret = sqlite3_step(stmt);
+       if (ret == SQLITE_ROW) {
+               fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
 
-       sqlite3_exec(db, "PRAGMA journal_mode=WAL", nullptr, nullptr, nullptr);  // Ignore errors.
-       sqlite3_exec(db, "PRAGMA synchronous=NORMAL", nullptr, nullptr, nullptr);  // Ignore errors.
-       return db;
+       ret = sqlite3_finalize(stmt);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       return sqlite3_last_insert_rowid(db);
 }
 
 int main(int argc, char *argv[])
@@ -191,10 +148,23 @@ int main(int argc, char *argv[])
        QApplication app(argc, argv);
        sqlite3 *db = open_db("ultimate.db");
 
-       MainWindow mainWindow(new EventsModel(db));
+       int requested_match = -1;
+       if (argc >= 2) {
+               requested_match = atoi(argv[1]);
+       }
+
+       int match_id = get_match_id(db, nullptr, requested_match);
+       if (match_id <= 0) {  // Cancel.
+               return 0;
+       }
+
+       MainWindow mainWindow(new EventsModel(db, match_id), new PlayersModel(db),
+                             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;
 }