From: Steinar H. Gunderson Date: Tue, 2 May 2023 19:15:20 +0000 (+0200) Subject: Make offense/defense a tristate-enum. X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=fd47dc764ce17096c227d03af4e8965056fc0bfb;p=pkanalytics Make offense/defense a tristate-enum. --- diff --git a/events.cpp b/events.cpp index 0e3a62d..231d6ee 100644 --- a/events.cpp +++ b/events.cpp @@ -230,8 +230,7 @@ EventsModel::Status EventsModel::get_status_at(uint64_t t) Status s; s.our_score = 0; s.their_score = 0; - s.offense = false; - s.defense = false; + s.attack_state = Status::NOT_STARTED; s.stoppage = false; s.should_pull = true; uint64_t last_gained_possession = 0; @@ -239,8 +238,8 @@ EventsModel::Status EventsModel::get_status_at(uint64_t t) uint64_t time_spent_in_stoppage = 0; unsigned num_touches = 0; - auto set_offense = [&s] { s.offense = true; s.defense = false; }; - auto set_defense = [&s] { s.offense = false; s.defense = true; }; + auto set_offense = [&s] { s.attack_state = Status::OFFENSE; }; + auto set_defense = [&s] { s.attack_state = Status::DEFENSE; }; for (const Event &e : events) { if (e.t > t) { @@ -310,8 +309,8 @@ EventsModel::Status EventsModel::get_status_at(uint64_t t) } s.num_passes = (num_touches == 0) ? 0 : num_touches - 1; - s.possession_sec = (s.offense && last_gained_possession != 0 && num_touches != 0) ? (t - last_gained_possession - time_spent_in_stoppage) / 1000 : 0; - s.stoppage_sec = (s.offense && last_gained_possession != 0 && num_touches != 0) ? time_spent_in_stoppage / 1000 : 0; + s.possession_sec = (s.attack_state == Status::OFFENSE && last_gained_possession != 0 && num_touches != 0) ? (t - last_gained_possession - time_spent_in_stoppage) / 1000 : 0; + s.stoppage_sec = (s.attack_state == Status::OFFENSE && last_gained_possession != 0 && num_touches != 0) ? time_spent_in_stoppage / 1000 : 0; return s; } diff --git a/events.h b/events.h index d3651e5..00c5f1d 100644 --- a/events.h +++ b/events.h @@ -33,8 +33,7 @@ public: struct Status { unsigned our_score, their_score; - bool offense; - bool defense; + enum { NOT_STARTED, OFFENSE, DEFENSE } attack_state; bool stoppage; bool should_pull; unsigned num_passes; diff --git a/main.cpp b/main.cpp index 2caa63f..74aff9b 100644 --- a/main.cpp +++ b/main.cpp @@ -169,7 +169,7 @@ void MainWindow::insert_event(int button_id) EventsModel::Status s = events->get_status_at(t); ui->event_view->selectionModel()->blockSignals(true); - if (s.offense) { + if (s.attack_state == EventsModel::Status::OFFENSE) { // TODO: Perhaps not if that player already did the last catch? ui->event_view->selectRow(events->insert_event(t, player_id, "catch")); } else { @@ -237,10 +237,9 @@ void MainWindow::update_status(uint64_t t) EventsModel::Status s = events->get_status_at(t); char buf[256]; const char *offense = "not started"; - if (s.offense) { - assert(!s.defense); + if (s.attack_state == EventsModel::Status::OFFENSE) { offense = "offense"; - } else if (s.defense) { + } else if (s.attack_state == EventsModel::Status::DEFENSE) { offense = "defense"; } @@ -328,25 +327,25 @@ void MainWindow::update_action_buttons(uint64_t t) ui->stoppage->setShortcut(QCoreApplication::translate("MainWindow", "V", nullptr)); } - ui->catch_->setEnabled(s.offense && has_selection_with_player); - ui->throwaway->setEnabled(s.offense && has_selection_with_player); - ui->drop->setEnabled(s.offense && has_selection_with_player); - ui->goal->setEnabled(s.offense && has_selection_with_player); - ui->offensive_soft_plus->setEnabled(s.offense && has_selection_with_player); - ui->offensive_soft_minus->setEnabled(s.offense && has_selection_with_player); + ui->catch_->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player); + ui->throwaway->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player); + ui->drop->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player); + ui->goal->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player); + ui->offensive_soft_plus->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player); + ui->offensive_soft_minus->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && has_selection_with_player); // TODO: be stricter - ui->pull->setEnabled(s.defense && s.should_pull && has_selection_with_player); - ui->pull_landed->setEnabled(s.defense && has_selection_with_player); - - ui->interception->setEnabled(s.defense && has_selection_with_player); - ui->their_throwaway->setEnabled(s.defense); - ui->our_defense->setEnabled(s.defense && has_selection_with_player); - ui->their_goal->setEnabled(s.defense); - ui->defensive_soft_plus->setEnabled(s.defense && has_selection_with_player); - ui->defensive_soft_minus->setEnabled(s.defense && has_selection_with_player); - ui->their_pull->setEnabled(s.offense && s.should_pull); - ui->our_foul->setEnabled(s.defense && has_selection_with_player); + ui->pull->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && s.should_pull && has_selection_with_player); + ui->pull_landed->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player); + + ui->interception->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player); + ui->their_throwaway->setEnabled(s.attack_state == EventsModel::Status::DEFENSE); + ui->our_defense->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player); + ui->their_goal->setEnabled(s.attack_state == EventsModel::Status::DEFENSE); + ui->defensive_soft_plus->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player); + ui->defensive_soft_minus->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player); + ui->their_pull->setEnabled(s.attack_state == EventsModel::Status::OFFENSE && s.should_pull); + ui->our_foul->setEnabled(s.attack_state == EventsModel::Status::DEFENSE && has_selection_with_player); } sqlite3 *open_db(const char *filename)