]> git.sesse.net Git - pkanalytics/blobdiff - events.cpp
Make Frame allocated on a freelist, in preparation for PBOs.
[pkanalytics] / events.cpp
index bb915b273b072ef4c4e5ec5eb9c348ac4d562f6b..88b0d4a7d0273dc7a9a727e41b49409afd981d54 100644 (file)
@@ -38,24 +38,52 @@ QVariant EventsModel::data(const QModelIndex &index, int role) const
        if (role != Qt::DisplayRole) {
                return QVariant();
        }
+       const Event &e = events[index.row()];
        if (index.column() == 0) {
-               return QString::fromUtf8(format_timestamp(events[index.row()].t));
+               return QString::fromUtf8(format_timestamp(e.t));
        } else if (index.column() == 1) {
-               optional<int> player_id = events[index.row()].player_id;
-               optional<int> formation_id = events[index.row()].formation_id;
+               optional<int> player_id = e.player_id;
+               optional<int> formation_id = e.formation_id;
                if (player_id) {
                        auto p_it = players.find(*player_id);
                        const Player &p = p_it->second;
                        return QString::fromUtf8(p.name + " (" + p.number + ")");
                } else if (formation_id) {
                        auto f_it = formations.find(*formation_id);
-                       const Formation &p = f_it->second;
-                       return QString::fromUtf8(p.name);
+                       const Formation &f = f_it->second;
+                       return QString::fromUtf8(f.name);
+               } else if (e.type == "formation_offense" || e.type == "formation_defense") {
+                       return "(None/unknown)";
                } else {
                        return QVariant();
                }
        } else if (index.column() == 2) {
-               return QString::fromUtf8(events[index.row()].type);
+               string type = e.type;
+               type[0] = toupper(e.type[0]);
+               for (char &ch : type) {
+                       if (ch == '_') {
+                               ch = ' ';
+                       }
+               }
+
+               // Various fixups.
+               if (type == "Pull oob") {
+                       type = "Pull OOB";
+               } else if (type == "Formation defense") {
+                       type = "Defensive formation";
+               } else if (type == "Formation offense") {
+                       type = "Offensive formation";
+               } else if (type == "Set offense") {
+                       type = "On offense";
+               } else if (type == "Set defense") {
+                       type = "On defense";
+               } else if (type == "Catch") {
+                       type = "Catch/take";
+               } else if (type == "Was d") {
+                       type = "Was d-ed";
+               }
+
+               return QString::fromUtf8(type);
        }
        return QVariant();
 }
@@ -166,6 +194,7 @@ unsigned EventsModel::insert_event(uint64_t t, optional<int> player_id, optional
        Event e;
        e.t = t;
        e.player_id = player_id;
+       e.formation_id = formation_id;
        e.type = type;
        events.insert(events.begin() + pos, e);
 
@@ -268,6 +297,34 @@ void EventsModel::set_event_type(unsigned pos, const string &type)
        }
 }
 
+void EventsModel::set_event_formation(unsigned pos, int formation_id)
+{
+       events[pos].formation_id = formation_id;
+       emit dataChanged(createIndex(pos, 0), createIndex(pos, 2));
+
+       sqlite3_stmt *stmt;
+       int ret = sqlite3_prepare_v2(db, "UPDATE event SET formation=? WHERE event=?", -1, &stmt, 0);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       sqlite3_bind_int64(stmt, 1, formation_id);
+       sqlite3_bind_int64(stmt, 2, events[pos].event_id);
+
+       ret = sqlite3_step(stmt);
+       if (ret == SQLITE_ROW) {
+               fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+
+       ret = sqlite3_finalize(stmt);
+       if (ret != SQLITE_OK) {
+               fprintf(stderr, "UPDATE finalize: %s\n", sqlite3_errmsg(db));
+               abort();
+       }
+}
+
 unsigned EventsModel::get_last_event_pos(uint64_t t) const
 {
        // upper_bound() gives first where e.t > t,
@@ -287,6 +344,8 @@ EventsModel::Status EventsModel::get_status_at(uint64_t t)
        s.our_score = 0;
        s.their_score = 0;
        s.attack_state = Status::NOT_STARTED;
+       s.offensive_formation = 0;
+       s.defensive_formation = 0;
        s.stoppage = false;
        s.pull_state = Status::SHOULD_PULL;
        uint64_t last_gained_possession = 0;
@@ -346,7 +405,7 @@ EventsModel::Status EventsModel::get_status_at(uint64_t t)
                        num_touches = 0;
                        time_spent_in_stoppage = 0;
                }
-               if (e.type == "drop" || e.type == "throwaway") {
+               if (e.type == "drop" || e.type == "was_d" || e.type == "throwaway") {
                        set_defense();
                        num_touches = 0;
                }
@@ -361,6 +420,20 @@ EventsModel::Status EventsModel::get_status_at(uint64_t t)
                                last_stoppage = 0;
                        }
                }
+               if (e.type == "formation_offense") {
+                       if (e.formation_id) {
+                               s.offensive_formation = *e.formation_id;
+                       } else {
+                               s.offensive_formation = 0;
+                       }
+               }
+               if (e.type == "formation_defense") {
+                       if (e.formation_id) {
+                               s.defensive_formation = *e.formation_id;
+                       } else {
+                               s.defensive_formation = 0;
+                       }
+               }
        }
        if (s.stoppage && last_stoppage != 0) {
                time_spent_in_stoppage += (t - last_stoppage);
@@ -436,7 +509,7 @@ void EventsModel::set_team_at(uint64_t t, const set<int> &new_team)
 
 void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
 {
-       // If there's another goal/stoppage no more than 20 seconds ago,
+       // If there's another goal/stoppage/turnover no more than 20 seconds ago,
        // we assume that the formation started at that point (it just took
        // the operator a bit of time to see it). If not, we assume we
        // changed in the middle of a point.
@@ -445,7 +518,14 @@ void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
                if (e.t > t) {
                        break;
                }
-               if (e.type == "goal" || e.type == "their_goal" || e.type == "stoppage" || e.type == "reset" || e.type == "set_offense" || e.type == "set_defense" || e.type == "in" || e.type == "out") {
+               if (e.type == "goal" || e.type == "their_goal" ||
+                   e.type == "in" || e.type == "out" ||
+                   e.type == "stoppage" || e.type == "reset" ||
+                   e.type == "set_defense" || e.type == "set_offense" ||
+                   e.type == "throwaway" || e.type == "their_throwaway" ||
+                   e.type == "drop" || e.type == "was_d" || e.type == "defense" || e.type == "interception" ||
+                   e.type == "pull" || e.type == "pull_landed" || e.type == "pull_oob" || e.type == "their_pull" ||
+                   e.type == "formation_offense" || e.type == "formation_defense") {
                        backdate_point = e.t + 1;
                }
                if (e.type == "formation_offense" || e.type == "formation_defense") {
@@ -456,9 +536,9 @@ void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
                t = backdate_point;
        }
        if (offense) {
-               insert_event(t, nullopt, formation, "formation_offense");
+               insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_offense");
        } else {
-               insert_event(t, nullopt, formation, "formation_defense");
+               insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_defense");
        }
 }