X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=main.cpp;h=6ca5ecdce930bd484ecba1486fd582e3d398d3c8;hb=63e1b9e8927c834ef8902f32b674898f0199a4f3;hp=95ecdb95ee83f11f53380dde71215c776ac2b62a;hpb=d9d1ea8099961060f8ec41c7009ba3d8d2c3d333;p=pkanalytics diff --git a/main.cpp b/main.cpp index 95ecdb9..6ca5ecd 100644 --- a/main.cpp +++ b/main.cpp @@ -2,8 +2,9 @@ #include #include #include -#include #include +#include +#include #include #include #include @@ -11,281 +12,134 @@ #include #include #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, PlayersModel *players) : events(events), players(players) +sqlite3 *open_db(const char *filename) { - video = new QMediaPlayer; - //video->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate.mkv")); - video->setSource(QUrl::fromLocalFile("/home/sesse/dev/stats/ultimate-prores.mkv")); - video->play(); - - ui = new Ui::MainWindow; - ui->setupUi(this); - - ui->event_view->setModel(events); - connect(ui->event_view->selectionModel(), &QItemSelectionModel::currentRowChanged, - [this, events](const QModelIndex ¤t, const QModelIndex &previous) { - video->setPosition(events->get_time(current.row())); - }); - - ui->player_view->setModel(players); - - connect(video, &QMediaPlayer::positionChanged, [this](uint64_t pos) { - position_changed(pos); - }); - - video->setVideoOutput(ui->video); - - connect(ui->minus10s, &QPushButton::clicked, [this]() { seek(-10000); }); - connect(ui->plus10s, &QPushButton::clicked, [this]() { seek(10000); }); - - connect(ui->minus2s, &QPushButton::clicked, [this]() { seek(-2000); }); - connect(ui->plus2s, &QPushButton::clicked, [this]() { seek(2000); }); - - // 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); }); - - connect(ui->play_pause, &QPushButton::clicked, [this]() { - if (playing) { - video->pause(); - ui->play_pause->setText("Play (space)"); - } else { - video->setPlaybackRate(1.0); - video->play(); - ui->play_pause->setText("Pause (space)"); - } - playing = !playing; - - // Needs to be set anew when we modify setText(), evidently. - ui->play_pause->setShortcut(QCoreApplication::translate("MainWindow", "Space", nullptr)); - }); + sqlite3 *db; + int ret = sqlite3_open(filename, &db); + if (ret != SQLITE_OK) { + fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db)); + exit(1); + } - 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); }); + 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. - // Offensive events - // 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]() { insert_noplayer_event("pull_landed"); }); + sqlite3_exec(db, R"( + CREATE TABLE IF NOT EXISTS match (match INTEGER PRIMARY KEY, description VARCHAR, video_filename VARCHAR); + )", nullptr, nullptr, nullptr); // Ignore errors. - // Defensive events (TODO add more) - connect(ui->their_throwaway, &QPushButton::clicked, [this]() { insert_noplayer_event("their_throwaway"); }); - connect(ui->their_goal, &QPushButton::clicked, [this]() { insert_noplayer_event("their_goal"); }); - connect(ui->their_pull, &QPushButton::clicked, [this]() { insert_noplayer_event("their_pull"); }); - connect(ui->our_defense, &QPushButton::clicked, [this]() { set_current_event_type("defense"); }); // TODO: player-connected - connect(ui->defensive_soft_plus, &QPushButton::clicked, [this]() { set_current_event_type("defensive_soft_plus"); }); // TODO: player-connected - connect(ui->defensive_soft_minus, &QPushButton::clicked, [this]() { set_current_event_type("defensive_soft_minus"); }); // TODO: player-connected + 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. - // Misc. events - connect(ui->substitution, &QPushButton::clicked, [this]() { make_substitution(); }); - connect(ui->stoppage, &QPushButton::clicked, [this]() { insert_noplayer_event("stoppage"); }); // FIXME needs a way to restart - connect(ui->unknown, &QPushButton::clicked, [this]() { insert_noplayer_event("unknown"); }); + 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. - 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(); }); + 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; } - -void MainWindow::position_changed(uint64_t pos) +int get_match_id(sqlite3 *db, QWidget *parent, int requested_match) { - ui->timestamp->setText(QString::fromUtf8(format_timestamp(pos))); - if (buffered_seek) { - video->setPosition(*buffered_seek); - buffered_seek.reset(); - } - if (!playing) { - video->pause(); // We only played to get a picture. - } - update_status(); -} + QStringList items; + vector ids; + bool requested_match_ok = false; -void MainWindow::seek(int64_t delta_ms) -{ - int64_t current_pos = buffered_seek ? *buffered_seek : video->position(); - uint64_t pos = max(current_pos + delta_ms, 0); - buffered_seek = pos; - if (!playing) { - video->setPlaybackRate(0.01); - video->play(); // Or Qt won't show the seek. + // 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(); } -} - -void MainWindow::insert_event(int button_id) -{ - uint64_t t = video->position(); - set team = events->get_team_at(t); - if (button_id > team.size()) { - return; + 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 { + fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db)); + abort(); + } } - int player_id = *next(team.begin(), button_id - 1); - - EventsModel::Status s = events->get_status_at(t); - - ui->event_view->selectionModel()->blockSignals(true); - if (s.offense) { - // TODO: Perhaps not if that player already did the last catch? - ui->event_view->selectRow(events->insert_event(t, player_id, "catch")); - } else { - ui->event_view->selectRow(events->insert_event(t, player_id)); + ret = sqlite3_finalize(stmt); + if (ret != SQLITE_OK) { + fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db)); + abort(); } - ui->event_view->selectionModel()->blockSignals(false); -} - -void MainWindow::insert_noplayer_event(const string &type) -{ - uint64_t t = video->position(); - - ui->event_view->selectionModel()->blockSignals(true); - ui->event_view->selectRow(events->insert_event(t, nullopt, type)); - ui->event_view->selectionModel()->blockSignals(false); -} + items.push_back("Add new…"); -void MainWindow::set_current_event_type(const string &type) -{ - QItemSelectionModel *select = ui->event_view->selectionModel(); - if (!select->hasSelection()) { - return; + if (requested_match_ok) { + return requested_match; } - 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; + QString chosen_str; + { + QInputDialog dialog(parent, Qt::WindowFlags()); + dialog.setWindowTitle("Open game"); + dialog.setLabelText("Choose game to analyze:"); + dialog.setComboBoxItems(items); + 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. - ui->event_view->selectionModel()->blockSignals(true); - events->delete_event(row); - ui->event_view->selectionModel()->blockSignals(false); - update_status(); -} -void MainWindow::make_substitution() -{ - QItemSelectionModel *select = ui->player_view->selectionModel(); - - // FIXME: we should backdate t to start of point (last goal, or 0) if: - // - no players we're removing have had actions yet - // - there have been no other in/out events - // - // ...but if so, we might need to modify in/out events that are already there - // (perhaps just overwrite them all?) - uint64_t t = video->position(); - - set old_team = events->get_team_at(t); - set new_team; - - for (QModelIndex row : select->selectedRows()) { - new_team.insert(players->get_player_id(row.row())); + for (unsigned i = 0; i < ids.size(); ++i) { + if (chosen_str == items[i]) { + return ids[i]; + } } - for (int player_id : old_team) { - if (!new_team.count(player_id)) { - events->insert_event(t, player_id, "out"); - } + // 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; } - for (int player_id : new_team) { - if (!old_team.count(player_id)) { - events->insert_event(t, player_id, "in"); - } + + // Insert the new row into the database. + ret = sqlite3_prepare_v2(db, "INSERT INTO match (description) VALUES (?)", -1, &stmt, 0); + if (ret != SQLITE_OK) { + fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db)); + abort(); } -} -void MainWindow::update_status() -{ - uint64_t t = video->position(); - EventsModel::Status s = events->get_status_at(t); - 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); + 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); - // FIXME: sort by number, instead of by internal ID - QPushButton *buttons[] = { - ui->player_1, - ui->player_2, - ui->player_3, - ui->player_4, - ui->player_5, - ui->player_6, - ui->player_7 - }; - const char shortcuts[] = "qweasdf"; - int num_players = 0; - for (int player_id : events->get_team_at(t)) { - QPushButton *btn = buttons[num_players]; - string label = players->get_player_name_by_id(player_id) + " (&" + shortcuts[num_players] + ")"; - char shortcut[2] = ""; - shortcut[0] = toupper(shortcuts[num_players]); - btn->setText(QString::fromUtf8(label)); - btn->setShortcut(QCoreApplication::translate("MainWindow", shortcut, nullptr)); - btn->setEnabled(true); - if (++num_players == 7) { - break; - } - } - for (int i = num_players; i < 7; ++i) { - QPushButton *btn = buttons[i]; - btn->setText("No player"); - btn->setEnabled(false); + ret = sqlite3_step(stmt); + if (ret == SQLITE_ROW) { + fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db)); + abort(); } -} -sqlite3 *open_db(const char *filename) -{ - sqlite3 *db; - int ret = sqlite3_open(filename, &db); + ret = sqlite3_finalize(stmt); if (ret != SQLITE_OK) { - fprintf(stderr, "%s: %s\n", filename, sqlite3_errmsg(db)); - exit(1); + fprintf(stderr, "INSERT finalize: %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. - - 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. - - 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; + return sqlite3_last_insert_rowid(db); } int main(int argc, char *argv[]) @@ -293,10 +147,26 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); sqlite3 *db = open_db("ultimate.db"); - MainWindow mainWindow(new EventsModel(db), new PlayersModel(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(); + + // TODO: do this on-demand instead, from a menu + export_to_json(db, "ultimate.json"); + return ret; }