string format_timestamp(uint64_t pos);
+EventsModel::EventsModel(sqlite3 *db, int match_id) : db(db), match_id(match_id)
+{
+ load_data();
+}
+
QVariant EventsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) {
if (section == 0) {
return "Time";
} else if (section == 1) {
- return "Player";
+ return "Who/what";
} else {
return "Type";
}
if (role != Qt::DisplayRole) {
return QVariant();
}
- refresh_if_needed();
+ 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> player_id = e.player_id;
+ optional<int> formation_id = e.formation_id;
if (player_id) {
- const Player &p = players[*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 &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";
+ }
+
+ return QString::fromUtf8(type);
}
return QVariant();
}
-void EventsModel::refresh_if_needed() const
+void EventsModel::load_data()
{
- if (!stale) {
- return;
- }
-
players.clear();
events.clear();
- stale = false;
- // Read the players.
+ // Read the players. (The ordering is used to build the order map.)
sqlite3_stmt *stmt;
- int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player", -1, &stmt, 0);
+ int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player ORDER BY gender, (number+0), number", -1, &stmt, 0);
if (ret != SQLITE_OK) {
fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
abort();
}
+ int order = 0;
for ( ;; ) {
ret = sqlite3_step(stmt);
if (ret == SQLITE_ROW) {
p.number = (const char *)sqlite3_column_text(stmt, 1);
p.name = (const char *) sqlite3_column_text(stmt, 2);
players[p.player_id] = std::move(p);
+ player_ordering[p.player_id] = order++;
+ } else if (ret == SQLITE_DONE) {
+ break;
+ } else {
+ fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+ }
+ ret = sqlite3_finalize(stmt);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+
+ // Read the formations.
+ ret = sqlite3_prepare_v2(db, "SELECT formation, name FROM formation", -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) {
+ Formation f;
+ f.formation_id = sqlite3_column_int(stmt, 0);
+ f.name = (const char *) sqlite3_column_text(stmt, 1);
+ formations[f.formation_id] = std::move(f);
} else if (ret == SQLITE_DONE) {
break;
} else {
}
// Read the events.
- ret = sqlite3_prepare_v2(db, "SELECT t, player, type FROM event", -1, &stmt, 0);
+ ret = sqlite3_prepare_v2(db, "SELECT event, t, player, formation, type FROM event WHERE match=? ORDER BY t", -1, &stmt, 0);
if (ret != SQLITE_OK) {
fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
abort();
}
+ sqlite3_bind_int64(stmt, 1, match_id);
for ( ;; ) {
ret = sqlite3_step(stmt);
if (ret == SQLITE_ROW) {
Event e;
- e.t = sqlite3_column_int(stmt, 0);
- e.player_id = sqlite3_column_int(stmt, 1);
- e.type = (const char *)sqlite3_column_text(stmt, 2);
+ e.event_id = sqlite3_column_int(stmt, 0);
+ e.t = sqlite3_column_int(stmt, 1);
+ if (sqlite3_column_type(stmt, 2) == SQLITE_INTEGER) { // Non-NULL.
+ e.player_id = sqlite3_column_int(stmt, 2);
+ }
+ if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) { // Non-NULL.
+ e.formation_id = sqlite3_column_int(stmt, 3);
+ }
+ e.type = (const char *)sqlite3_column_text(stmt, 4);
events.push_back(std::move(e));
} else if (ret == SQLITE_DONE) {
break;
fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
abort();
}
-
- // TODO what if data changes externally?
- //emit dataChanged(QModelIndex(
}
-int EventsModel::insert_event(uint64_t t, int player_id)
+unsigned EventsModel::insert_event(uint64_t t, optional<int> player_id, optional<int> formation_id, const string &type)
{
auto it = lower_bound(events.begin(), events.end(), t,
[](const Event &e, uint64_t t) { return e.t < t; });
- int pos = distance(events.begin(), it);
- beginInsertRows(QModelIndex(), pos, pos + 1);
+ unsigned pos = distance(events.begin(), it);
+ beginInsertRows(QModelIndex(), pos, pos);
Event e;
e.t = t;
e.player_id = player_id;
- e.type = "unknown";
+ e.formation_id = formation_id;
+ e.type = type;
events.insert(events.begin() + pos, e);
endInsertRows();
- // FIXME sqlite
+ // Insert the new row into the database.
+ sqlite3_stmt *stmt;
+ int ret = sqlite3_prepare_v2(db, "INSERT INTO event (match, t, player, formation, type) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+
+ sqlite3_bind_int64(stmt, 1, match_id);
+ sqlite3_bind_int64(stmt, 2, t);
+ if (player_id) {
+ sqlite3_bind_int64(stmt, 3, *player_id);
+ } else {
+ sqlite3_bind_null(stmt, 3);
+ }
+ if (formation_id) {
+ sqlite3_bind_int64(stmt, 4, *formation_id);
+ } else {
+ sqlite3_bind_null(stmt, 4);
+ }
+ sqlite3_bind_text(stmt, 5, type.data(), type.size(), SQLITE_STATIC);
+
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_ROW) {
+ fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+
+ ret = sqlite3_finalize(stmt);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+ events[pos].event_id = sqlite3_last_insert_rowid(db);
return pos;
}
+
+void EventsModel::delete_event(unsigned pos)
+{
+ int event_id = events[pos].event_id;
+
+ beginRemoveRows(QModelIndex(), pos, pos);
+ events.erase(events.begin() + pos);
+ endRemoveRows();
+
+ // Delete the row from the database.
+ sqlite3_stmt *stmt;
+ int ret = sqlite3_prepare_v2(db, "DELETE FROM event WHERE event=?", -1, &stmt, 0);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "DELETE prepare: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+
+ sqlite3_bind_int64(stmt, 1, event_id);
+
+ ret = sqlite3_step(stmt);
+ if (ret == SQLITE_ROW) {
+ fprintf(stderr, "DELETE step: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+
+ ret = sqlite3_finalize(stmt);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "DELETE finalize: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+}
+
+void EventsModel::set_event_type(unsigned pos, const string &type)
+{
+ events[pos].type = type;
+ emit dataChanged(createIndex(pos, 0), createIndex(pos, 2));
+
+ sqlite3_stmt *stmt;
+ int ret = sqlite3_prepare_v2(db, "UPDATE event SET type=? WHERE event=?", -1, &stmt, 0);
+ if (ret != SQLITE_OK) {
+ fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
+ abort();
+ }
+
+ sqlite3_bind_text(stmt, 1, type.data(), type.size(), SQLITE_STATIC);
+ 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();
+ }
+}
+
+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,
+ // and the one before that is the one we want.
+ auto it = upper_bound(events.begin(), events.end(), t,
+ [](uint64_t t, const Event &e) { return t < e.t; });
+ if (it == events.begin()) {
+ return 0;
+ } else {
+ return distance(events.begin(), it - 1);
+ }
+}
+
+EventsModel::Status EventsModel::get_status_at(uint64_t t)
+{
+ Status s;
+ 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;
+ uint64_t last_stoppage = 0;
+ uint64_t time_spent_in_stoppage = 0;
+ unsigned num_touches = 0;
+
+ 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) {
+ break;
+ }
+
+ if (e.type == "goal" || e.type == "their_goal") {
+ s.pull_state = Status::SHOULD_PULL;
+ } else if (e.type == "in" || e.type == "out" || e.type == "stoppage" || e.type == "restart" || e.type == "unknown" || e.type == "set_defense" || e.type == "set_offense") {
+ // No effect on pull status.
+ } else if (e.type == "pull") {
+ s.pull_state = Status::PULL_IN_AIR;
+ } else {
+ s.pull_state = Status::NOT_PULLING; // Includes pull_landed and pull_oob.
+ }
+
+ if (e.type == "set_offense") {
+ set_offense();
+ } else if (e.type == "set_defense") {
+ set_defense();
+ }
+
+ if (e.type == "goal") {
+ ++s.our_score;
+ set_defense();
+ num_touches = 0;
+ }
+ if (e.type == "their_goal") {
+ ++s.their_score;
+ set_offense();
+ num_touches = 0;
+ }
+ if (e.type == "catch") {
+ if (num_touches == 0) { // Pick up.
+ last_gained_possession = e.t;
+ time_spent_in_stoppage = 0;
+ }
+ ++num_touches;
+ }
+ if (e.type == "interception") {
+ num_touches = 1;
+ set_offense();
+ last_gained_possession = e.t;
+ time_spent_in_stoppage = 0;
+ }
+ if (e.type == "defense" || e.type == "their_throwaway") {
+ set_offense();
+ num_touches = 0;
+ time_spent_in_stoppage = 0;
+ }
+ if (e.type == "drop" || e.type == "throwaway") {
+ set_defense();
+ num_touches = 0;
+ }
+ if (e.type == "stoppage") {
+ s.stoppage = true;
+ last_stoppage = e.t;
+ }
+ if (e.type == "restart") {
+ s.stoppage = false;
+ if (last_stoppage != 0) {
+ time_spent_in_stoppage += (e.t - last_stoppage);
+ 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);
+ }
+
+ s.num_passes = (num_touches == 0) ? 0 : num_touches - 1;
+ 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;
+}
+
+set<int> EventsModel::get_team_at(uint64_t t)
+{
+ set<int> team;
+ for (const Event &e : events) {
+ if (e.t > t) {
+ break;
+ }
+ if (e.type == "in") {
+ team.insert(*e.player_id);
+ }
+ if (e.type == "out") {
+ team.erase(*e.player_id);
+ }
+ }
+ return team;
+}
+
+void EventsModel::set_team_at(uint64_t t, const set<int> &new_team)
+{
+ // Backdate to the last goal or stoppage, _or_ the last time someone
+ // going out is mentioned. (We don't really track injuries yet;
+ // do we want an explicit injury type? If we had one, it would probably
+ // be the simplest.)
+ uint64_t backdate_point = 0;
+ for (const Event &e : events) {
+ 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") {
+ backdate_point = e.t + 1;
+ }
+ if (e.player_id.has_value() && !new_team.count(*e.player_id)) {
+ backdate_point = e.t + 1;
+ }
+ }
+
+ // Delete all in/outs already at the backdate point.
+ for (unsigned i = 0; i < events.size(); ) {
+ if (events[i].t > backdate_point) {
+ break;
+ }
+ if (events[i].t == backdate_point && (events[i].type == "in" || events[i].type == "out")) {
+ delete_event(i);
+ } else {
+ ++i;
+ }
+ }
+
+ // Finally make the subs we need.
+ set<int> old_team = get_team_at(backdate_point);
+ for (int player_id : old_team) {
+ if (!new_team.count(player_id)) {
+ insert_event(backdate_point, player_id, nullopt, "out");
+ }
+ }
+ for (int player_id : new_team) {
+ if (!old_team.count(player_id)) {
+ insert_event(backdate_point, player_id, nullopt, "in");
+ }
+ }
+}
+
+void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
+{
+ // 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.
+ uint64_t backdate_point = 0;
+ for (const Event &e : events) {
+ if (e.t > t) {
+ break;
+ }
+ 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 == "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") {
+ backdate_point = 0;
+ }
+ }
+ if (backdate_point != 0 && t - backdate_point < 20000) {
+ t = backdate_point;
+ }
+ if (offense) {
+ insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_offense");
+ } else {
+ insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_defense");
+ }
+}
+
+vector<int> EventsModel::sort_team(const set<int> &team) const
+{
+ vector<int> ret(team.begin(), team.end());
+ std::sort(ret.begin(), ret.end(), [this](int a, int b) {
+ return player_ordering.find(a)->second < player_ordering.find(b)->second;
+ });
+ return ret;
+}