]> git.sesse.net Git - pkanalytics/commitdiff
Make offense/defense a tristate-enum.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 May 2023 19:15:20 +0000 (21:15 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Tue, 2 May 2023 19:15:20 +0000 (21:15 +0200)
events.cpp
events.h
main.cpp

index 0e3a62def6ffaa0248bfc24f4af79d254c9caf62..231d6ee125d5c2205604e724c5fd868d177da52d 100644 (file)
@@ -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;
 }
 
index d3651e52b7043990ce000335caaf8f987112329e..00c5f1d97f52556ae186cd38fd54d46a61687ef5 100644 (file)
--- 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;
index 2caa63fbf3fdb6190dc1de55d3a47b2ac76bb868..74aff9ba95d11cd84595a94d5901a58524177886 100644 (file)
--- 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)