11 string format_timestamp(uint64_t pos);
13 EventsModel::EventsModel(sqlite3 *db, int match_id) : db(db), match_id(match_id)
18 QVariant EventsModel::headerData(int section, Qt::Orientation orientation, int role) const
20 if (role != Qt::DisplayRole) {
23 if (orientation == Qt::Horizontal) {
26 } else if (section == 1) {
36 QVariant EventsModel::data(const QModelIndex &index, int role) const
38 if (role != Qt::DisplayRole) {
41 const Event &e = events[index.row()];
42 if (index.column() == 0) {
43 return QString::fromUtf8(format_timestamp(e.t));
44 } else if (index.column() == 1) {
45 optional<int> player_id = e.player_id;
46 optional<int> formation_id = e.formation_id;
48 auto p_it = players.find(*player_id);
49 const Player &p = p_it->second;
50 return QString::fromUtf8(p.name + " (" + p.number + ")");
51 } else if (formation_id) {
52 auto f_it = formations.find(*formation_id);
53 const Formation &f = f_it->second;
54 return QString::fromUtf8(f.name);
55 } else if (e.type == "formation_offense" || e.type == "formation_defense") {
56 return "(None/unknown)";
60 } else if (index.column() == 2) {
61 return QString::fromUtf8(events[index.row()].type);
66 void EventsModel::load_data()
71 // Read the players. (The ordering is used to build the order map.)
73 int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player ORDER BY gender, (number+0), number", -1, &stmt, 0);
74 if (ret != SQLITE_OK) {
75 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
80 ret = sqlite3_step(stmt);
81 if (ret == SQLITE_ROW) {
83 p.player_id = sqlite3_column_int(stmt, 0);
84 p.number = (const char *)sqlite3_column_text(stmt, 1);
85 p.name = (const char *) sqlite3_column_text(stmt, 2);
86 players[p.player_id] = std::move(p);
87 player_ordering[p.player_id] = order++;
88 } else if (ret == SQLITE_DONE) {
91 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
95 ret = sqlite3_finalize(stmt);
96 if (ret != SQLITE_OK) {
97 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
101 // Read the formations.
102 ret = sqlite3_prepare_v2(db, "SELECT formation, name FROM formation", -1, &stmt, 0);
103 if (ret != SQLITE_OK) {
104 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
108 ret = sqlite3_step(stmt);
109 if (ret == SQLITE_ROW) {
111 f.formation_id = sqlite3_column_int(stmt, 0);
112 f.name = (const char *) sqlite3_column_text(stmt, 1);
113 formations[f.formation_id] = std::move(f);
114 } else if (ret == SQLITE_DONE) {
117 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
121 ret = sqlite3_finalize(stmt);
122 if (ret != SQLITE_OK) {
123 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
128 ret = sqlite3_prepare_v2(db, "SELECT event, t, player, formation, type FROM event WHERE match=? ORDER BY t", -1, &stmt, 0);
129 if (ret != SQLITE_OK) {
130 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
133 sqlite3_bind_int64(stmt, 1, match_id);
135 ret = sqlite3_step(stmt);
136 if (ret == SQLITE_ROW) {
138 e.event_id = sqlite3_column_int(stmt, 0);
139 e.t = sqlite3_column_int(stmt, 1);
140 if (sqlite3_column_type(stmt, 2) == SQLITE_INTEGER) { // Non-NULL.
141 e.player_id = sqlite3_column_int(stmt, 2);
143 if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) { // Non-NULL.
144 e.formation_id = sqlite3_column_int(stmt, 3);
146 e.type = (const char *)sqlite3_column_text(stmt, 4);
147 events.push_back(std::move(e));
148 } else if (ret == SQLITE_DONE) {
151 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
155 ret = sqlite3_finalize(stmt);
156 if (ret != SQLITE_OK) {
157 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
162 unsigned EventsModel::insert_event(uint64_t t, optional<int> player_id, optional<int> formation_id, const string &type)
164 auto it = lower_bound(events.begin(), events.end(), t,
165 [](const Event &e, uint64_t t) { return e.t < t; });
166 unsigned pos = distance(events.begin(), it);
167 beginInsertRows(QModelIndex(), pos, pos);
171 e.player_id = player_id;
172 e.formation_id = formation_id;
174 events.insert(events.begin() + pos, e);
178 // Insert the new row into the database.
180 int ret = sqlite3_prepare_v2(db, "INSERT INTO event (match, t, player, formation, type) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0);
181 if (ret != SQLITE_OK) {
182 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
186 sqlite3_bind_int64(stmt, 1, match_id);
187 sqlite3_bind_int64(stmt, 2, t);
189 sqlite3_bind_int64(stmt, 3, *player_id);
191 sqlite3_bind_null(stmt, 3);
194 sqlite3_bind_int64(stmt, 4, *formation_id);
196 sqlite3_bind_null(stmt, 4);
198 sqlite3_bind_text(stmt, 5, type.data(), type.size(), SQLITE_STATIC);
200 ret = sqlite3_step(stmt);
201 if (ret == SQLITE_ROW) {
202 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
206 ret = sqlite3_finalize(stmt);
207 if (ret != SQLITE_OK) {
208 fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
212 events[pos].event_id = sqlite3_last_insert_rowid(db);
216 void EventsModel::delete_event(unsigned pos)
218 int event_id = events[pos].event_id;
220 beginRemoveRows(QModelIndex(), pos, pos);
221 events.erase(events.begin() + pos);
224 // Delete the row from the database.
226 int ret = sqlite3_prepare_v2(db, "DELETE FROM event WHERE event=?", -1, &stmt, 0);
227 if (ret != SQLITE_OK) {
228 fprintf(stderr, "DELETE prepare: %s\n", sqlite3_errmsg(db));
232 sqlite3_bind_int64(stmt, 1, event_id);
234 ret = sqlite3_step(stmt);
235 if (ret == SQLITE_ROW) {
236 fprintf(stderr, "DELETE step: %s\n", sqlite3_errmsg(db));
240 ret = sqlite3_finalize(stmt);
241 if (ret != SQLITE_OK) {
242 fprintf(stderr, "DELETE finalize: %s\n", sqlite3_errmsg(db));
247 void EventsModel::set_event_type(unsigned pos, const string &type)
249 events[pos].type = type;
250 emit dataChanged(createIndex(pos, 0), createIndex(pos, 2));
253 int ret = sqlite3_prepare_v2(db, "UPDATE event SET type=? WHERE event=?", -1, &stmt, 0);
254 if (ret != SQLITE_OK) {
255 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
259 sqlite3_bind_text(stmt, 1, type.data(), type.size(), SQLITE_STATIC);
260 sqlite3_bind_int64(stmt, 2, events[pos].event_id);
262 ret = sqlite3_step(stmt);
263 if (ret == SQLITE_ROW) {
264 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
268 ret = sqlite3_finalize(stmt);
269 if (ret != SQLITE_OK) {
270 fprintf(stderr, "UPDATE finalize: %s\n", sqlite3_errmsg(db));
275 unsigned EventsModel::get_last_event_pos(uint64_t t) const
277 // upper_bound() gives first where e.t > t,
278 // and the one before that is the one we want.
279 auto it = upper_bound(events.begin(), events.end(), t,
280 [](uint64_t t, const Event &e) { return t < e.t; });
281 if (it == events.begin()) {
284 return distance(events.begin(), it - 1);
288 EventsModel::Status EventsModel::get_status_at(uint64_t t)
293 s.attack_state = Status::NOT_STARTED;
295 s.pull_state = Status::SHOULD_PULL;
296 uint64_t last_gained_possession = 0;
297 uint64_t last_stoppage = 0;
298 uint64_t time_spent_in_stoppage = 0;
299 unsigned num_touches = 0;
301 auto set_offense = [&s] { s.attack_state = Status::OFFENSE; };
302 auto set_defense = [&s] { s.attack_state = Status::DEFENSE; };
304 for (const Event &e : events) {
309 if (e.type == "goal" || e.type == "their_goal") {
310 s.pull_state = Status::SHOULD_PULL;
311 } 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") {
312 // No effect on pull status.
313 } else if (e.type == "pull") {
314 s.pull_state = Status::PULL_IN_AIR;
316 s.pull_state = Status::NOT_PULLING; // Includes pull_landed and pull_oob.
319 if (e.type == "set_offense") {
321 } else if (e.type == "set_defense") {
325 if (e.type == "goal") {
330 if (e.type == "their_goal") {
335 if (e.type == "catch") {
336 if (num_touches == 0) { // Pick up.
337 last_gained_possession = e.t;
338 time_spent_in_stoppage = 0;
342 if (e.type == "interception") {
345 last_gained_possession = e.t;
346 time_spent_in_stoppage = 0;
348 if (e.type == "defense" || e.type == "their_throwaway") {
351 time_spent_in_stoppage = 0;
353 if (e.type == "drop" || e.type == "throwaway") {
357 if (e.type == "stoppage") {
361 if (e.type == "restart") {
363 if (last_stoppage != 0) {
364 time_spent_in_stoppage += (e.t - last_stoppage);
369 if (s.stoppage && last_stoppage != 0) {
370 time_spent_in_stoppage += (t - last_stoppage);
373 s.num_passes = (num_touches == 0) ? 0 : num_touches - 1;
374 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;
375 s.stoppage_sec = (s.attack_state == Status::OFFENSE && last_gained_possession != 0 && num_touches != 0) ? time_spent_in_stoppage / 1000 : 0;
379 set<int> EventsModel::get_team_at(uint64_t t)
382 for (const Event &e : events) {
386 if (e.type == "in") {
387 team.insert(*e.player_id);
389 if (e.type == "out") {
390 team.erase(*e.player_id);
396 void EventsModel::set_team_at(uint64_t t, const set<int> &new_team)
398 // Backdate to the last goal or stoppage, _or_ the last time someone
399 // going out is mentioned. (We don't really track injuries yet;
400 // do we want an explicit injury type? If we had one, it would probably
402 uint64_t backdate_point = 0;
403 for (const Event &e : events) {
407 if (e.type == "goal" || e.type == "their_goal" || e.type == "stoppage" || e.type == "reset" || e.type == "set_offense" || e.type == "set_defense") {
408 backdate_point = e.t + 1;
410 if (e.player_id.has_value() && !new_team.count(*e.player_id)) {
411 backdate_point = e.t + 1;
415 // Delete all in/outs already at the backdate point.
416 for (unsigned i = 0; i < events.size(); ) {
417 if (events[i].t > backdate_point) {
420 if (events[i].t == backdate_point && (events[i].type == "in" || events[i].type == "out")) {
427 // Finally make the subs we need.
428 set<int> old_team = get_team_at(backdate_point);
429 for (int player_id : old_team) {
430 if (!new_team.count(player_id)) {
431 insert_event(backdate_point, player_id, nullopt, "out");
434 for (int player_id : new_team) {
435 if (!old_team.count(player_id)) {
436 insert_event(backdate_point, player_id, nullopt, "in");
441 void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
443 // If there's another goal/stoppage no more than 20 seconds ago,
444 // we assume that the formation started at that point (it just took
445 // the operator a bit of time to see it). If not, we assume we
446 // changed in the middle of a point.
447 uint64_t backdate_point = 0;
448 for (const Event &e : events) {
452 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" || e.type == "pull" || e.type == "their_pull") {
453 backdate_point = e.t + 1;
455 if (e.type == "formation_offense" || e.type == "formation_defense") {
459 if (backdate_point != 0 && t - backdate_point < 20000) {
463 insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_offense");
465 insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_defense");
469 vector<int> EventsModel::sort_team(const set<int> &team) const
471 vector<int> ret(team.begin(), team.end());
472 std::sort(ret.begin(), ret.end(), [this](int a, int b) {
473 return player_ordering.find(a)->second < player_ordering.find(b)->second;