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) {
62 type[0] = toupper(e.type[0]);
63 for (char &ch : type) {
70 if (type == "Pull oob") {
72 } else if (type == "Formation defense") {
73 type = "Defensive formation";
74 } else if (type == "Formation offense") {
75 type = "Offensive formation";
76 } else if (type == "Set offense") {
78 } else if (type == "Set defense") {
80 } else if (type == "Catch") {
82 } else if (type == "Was d") {
86 return QString::fromUtf8(type);
91 void EventsModel::load_data()
96 // Read the players. (The ordering is used to build the order map.)
98 int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player ORDER BY gender, (number+0), number", -1, &stmt, 0);
99 if (ret != SQLITE_OK) {
100 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
105 ret = sqlite3_step(stmt);
106 if (ret == SQLITE_ROW) {
108 p.player_id = sqlite3_column_int(stmt, 0);
109 p.number = (const char *)sqlite3_column_text(stmt, 1);
110 p.name = (const char *) sqlite3_column_text(stmt, 2);
111 players[p.player_id] = std::move(p);
112 player_ordering[p.player_id] = order++;
113 } else if (ret == SQLITE_DONE) {
116 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
120 ret = sqlite3_finalize(stmt);
121 if (ret != SQLITE_OK) {
122 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
126 // Read the formations.
127 ret = sqlite3_prepare_v2(db, "SELECT formation, name FROM formation", -1, &stmt, 0);
128 if (ret != SQLITE_OK) {
129 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
133 ret = sqlite3_step(stmt);
134 if (ret == SQLITE_ROW) {
136 f.formation_id = sqlite3_column_int(stmt, 0);
137 f.name = (const char *) sqlite3_column_text(stmt, 1);
138 formations[f.formation_id] = std::move(f);
139 } else if (ret == SQLITE_DONE) {
142 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
146 ret = sqlite3_finalize(stmt);
147 if (ret != SQLITE_OK) {
148 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
153 ret = sqlite3_prepare_v2(db, "SELECT event, t, player, formation, type FROM event WHERE match=? ORDER BY t", -1, &stmt, 0);
154 if (ret != SQLITE_OK) {
155 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
158 sqlite3_bind_int64(stmt, 1, match_id);
160 ret = sqlite3_step(stmt);
161 if (ret == SQLITE_ROW) {
163 e.event_id = sqlite3_column_int(stmt, 0);
164 e.t = sqlite3_column_int(stmt, 1);
165 if (sqlite3_column_type(stmt, 2) == SQLITE_INTEGER) { // Non-NULL.
166 e.player_id = sqlite3_column_int(stmt, 2);
168 if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) { // Non-NULL.
169 e.formation_id = sqlite3_column_int(stmt, 3);
171 e.type = (const char *)sqlite3_column_text(stmt, 4);
172 events.push_back(std::move(e));
173 } else if (ret == SQLITE_DONE) {
176 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
180 ret = sqlite3_finalize(stmt);
181 if (ret != SQLITE_OK) {
182 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
187 unsigned EventsModel::insert_event(uint64_t t, optional<int> player_id, optional<int> formation_id, const string &type)
189 auto it = lower_bound(events.begin(), events.end(), t,
190 [](const Event &e, uint64_t t) { return e.t < t; });
191 unsigned pos = distance(events.begin(), it);
192 beginInsertRows(QModelIndex(), pos, pos);
196 e.player_id = player_id;
197 e.formation_id = formation_id;
199 events.insert(events.begin() + pos, e);
203 // Insert the new row into the database.
205 int ret = sqlite3_prepare_v2(db, "INSERT INTO event (match, t, player, formation, type) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0);
206 if (ret != SQLITE_OK) {
207 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
211 sqlite3_bind_int64(stmt, 1, match_id);
212 sqlite3_bind_int64(stmt, 2, t);
214 sqlite3_bind_int64(stmt, 3, *player_id);
216 sqlite3_bind_null(stmt, 3);
219 sqlite3_bind_int64(stmt, 4, *formation_id);
221 sqlite3_bind_null(stmt, 4);
223 sqlite3_bind_text(stmt, 5, type.data(), type.size(), SQLITE_STATIC);
225 ret = sqlite3_step(stmt);
226 if (ret == SQLITE_ROW) {
227 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
231 ret = sqlite3_finalize(stmt);
232 if (ret != SQLITE_OK) {
233 fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
237 events[pos].event_id = sqlite3_last_insert_rowid(db);
241 void EventsModel::delete_event(unsigned pos)
243 int event_id = events[pos].event_id;
245 beginRemoveRows(QModelIndex(), pos, pos);
246 events.erase(events.begin() + pos);
249 // Delete the row from the database.
251 int ret = sqlite3_prepare_v2(db, "DELETE FROM event WHERE event=?", -1, &stmt, 0);
252 if (ret != SQLITE_OK) {
253 fprintf(stderr, "DELETE prepare: %s\n", sqlite3_errmsg(db));
257 sqlite3_bind_int64(stmt, 1, event_id);
259 ret = sqlite3_step(stmt);
260 if (ret == SQLITE_ROW) {
261 fprintf(stderr, "DELETE step: %s\n", sqlite3_errmsg(db));
265 ret = sqlite3_finalize(stmt);
266 if (ret != SQLITE_OK) {
267 fprintf(stderr, "DELETE finalize: %s\n", sqlite3_errmsg(db));
272 void EventsModel::set_event_type(unsigned pos, const string &type)
274 events[pos].type = type;
275 emit dataChanged(createIndex(pos, 0), createIndex(pos, 2));
278 int ret = sqlite3_prepare_v2(db, "UPDATE event SET type=? WHERE event=?", -1, &stmt, 0);
279 if (ret != SQLITE_OK) {
280 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
284 sqlite3_bind_text(stmt, 1, type.data(), type.size(), SQLITE_STATIC);
285 sqlite3_bind_int64(stmt, 2, events[pos].event_id);
287 ret = sqlite3_step(stmt);
288 if (ret == SQLITE_ROW) {
289 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
293 ret = sqlite3_finalize(stmt);
294 if (ret != SQLITE_OK) {
295 fprintf(stderr, "UPDATE finalize: %s\n", sqlite3_errmsg(db));
300 void EventsModel::set_event_formation(unsigned pos, int formation_id)
302 events[pos].formation_id = formation_id;
303 emit dataChanged(createIndex(pos, 0), createIndex(pos, 2));
306 int ret = sqlite3_prepare_v2(db, "UPDATE event SET formation=? WHERE event=?", -1, &stmt, 0);
307 if (ret != SQLITE_OK) {
308 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
312 sqlite3_bind_int64(stmt, 1, formation_id);
313 sqlite3_bind_int64(stmt, 2, events[pos].event_id);
315 ret = sqlite3_step(stmt);
316 if (ret == SQLITE_ROW) {
317 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
321 ret = sqlite3_finalize(stmt);
322 if (ret != SQLITE_OK) {
323 fprintf(stderr, "UPDATE finalize: %s\n", sqlite3_errmsg(db));
328 unsigned EventsModel::get_last_event_pos(uint64_t t) const
330 // upper_bound() gives first where e.t > t,
331 // and the one before that is the one we want.
332 auto it = upper_bound(events.begin(), events.end(), t,
333 [](uint64_t t, const Event &e) { return t < e.t; });
334 if (it == events.begin()) {
337 return distance(events.begin(), it - 1);
341 EventsModel::Status EventsModel::get_status_at(uint64_t t)
346 s.attack_state = Status::NOT_STARTED;
347 s.offensive_formation = 0;
348 s.defensive_formation = 0;
350 s.pull_state = Status::SHOULD_PULL;
351 uint64_t last_gained_possession = 0;
352 uint64_t last_stoppage = 0;
353 uint64_t time_spent_in_stoppage = 0;
354 unsigned num_touches = 0;
356 auto set_offense = [&s] { s.attack_state = Status::OFFENSE; };
357 auto set_defense = [&s] { s.attack_state = Status::DEFENSE; };
359 for (const Event &e : events) {
364 if (e.type == "goal" || e.type == "their_goal") {
365 s.pull_state = Status::SHOULD_PULL;
366 } 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") {
367 // No effect on pull status.
368 } else if (e.type == "pull") {
369 s.pull_state = Status::PULL_IN_AIR;
371 s.pull_state = Status::NOT_PULLING; // Includes pull_landed and pull_oob.
374 if (e.type == "set_offense") {
376 } else if (e.type == "set_defense") {
380 if (e.type == "goal") {
385 if (e.type == "their_goal") {
390 if (e.type == "catch") {
391 if (num_touches == 0) { // Pick up.
392 last_gained_possession = e.t;
393 time_spent_in_stoppage = 0;
397 if (e.type == "interception") {
400 last_gained_possession = e.t;
401 time_spent_in_stoppage = 0;
403 if (e.type == "defense" || e.type == "their_throwaway") {
406 time_spent_in_stoppage = 0;
408 if (e.type == "drop" || e.type == "was_d" || e.type == "throwaway" || e.type == "stallout") {
412 if (e.type == "stoppage") {
416 if (e.type == "restart") {
418 if (last_stoppage != 0) {
419 time_spent_in_stoppage += (e.t - last_stoppage);
423 if (e.type == "formation_offense") {
424 if (e.formation_id) {
425 s.offensive_formation = *e.formation_id;
427 s.offensive_formation = 0;
430 if (e.type == "formation_defense") {
431 if (e.formation_id) {
432 s.defensive_formation = *e.formation_id;
434 s.defensive_formation = 0;
438 if (s.stoppage && last_stoppage != 0) {
439 time_spent_in_stoppage += (t - last_stoppage);
442 s.num_passes = (num_touches == 0) ? 0 : num_touches - 1;
443 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;
444 s.stoppage_sec = (s.attack_state == Status::OFFENSE && last_gained_possession != 0 && num_touches != 0) ? time_spent_in_stoppage / 1000 : 0;
448 set<int> EventsModel::get_team_at(uint64_t t)
451 for (const Event &e : events) {
455 if (e.type == "in") {
456 team.insert(*e.player_id);
458 if (e.type == "out") {
459 team.erase(*e.player_id);
465 void EventsModel::set_team_at(uint64_t t, const set<int> &new_team)
467 // Backdate to the last goal or stoppage, _or_ the last time someone
468 // going out is mentioned. (We don't really track injuries yet;
469 // do we want an explicit injury type? If we had one, it would probably
471 uint64_t backdate_point = 0;
472 for (const Event &e : events) {
476 if (e.type == "goal" || e.type == "their_goal" || e.type == "stoppage" || e.type == "reset" || e.type == "set_offense" || e.type == "set_defense") {
477 backdate_point = e.t + 1;
479 if (e.player_id.has_value() && !new_team.count(*e.player_id)) {
480 backdate_point = e.t + 1;
484 // Delete all in/outs already at the backdate point.
485 for (unsigned i = 0; i < events.size(); ) {
486 if (events[i].t > backdate_point) {
489 if (events[i].t == backdate_point && (events[i].type == "in" || events[i].type == "out")) {
496 // Finally make the subs we need.
497 set<int> old_team = get_team_at(backdate_point);
498 for (int player_id : old_team) {
499 if (!new_team.count(player_id)) {
500 insert_event(backdate_point, player_id, nullopt, "out");
503 for (int player_id : new_team) {
504 if (!old_team.count(player_id)) {
505 insert_event(backdate_point, player_id, nullopt, "in");
510 void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
512 // If there's another goal/stoppage/turnover no more than 20 seconds ago,
513 // we assume that the formation started at that point (it just took
514 // the operator a bit of time to see it). If not, we assume we
515 // changed in the middle of a point.
516 uint64_t backdate_point = 0;
517 for (const Event &e : events) {
521 if (e.type == "goal" || e.type == "their_goal" ||
522 e.type == "in" || e.type == "out" ||
523 e.type == "stoppage" || e.type == "reset" ||
524 e.type == "set_defense" || e.type == "set_offense" ||
525 e.type == "throwaway" || e.type == "their_throwaway" ||
526 e.type == "drop" || e.type == "was_d" || e.type == "defense" || e.type == "interception" || e.type == "stallout" ||
527 e.type == "pull" || e.type == "pull_landed" || e.type == "pull_oob" || e.type == "their_pull" ||
528 e.type == "formation_offense" || e.type == "formation_defense") {
529 backdate_point = e.t + 1;
531 if (e.type == "formation_offense" || e.type == "formation_defense") {
535 if (backdate_point != 0 && t - backdate_point < 20000) {
539 insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_offense");
541 insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_defense");
545 vector<int> EventsModel::sort_team(const set<int> &team) const
547 vector<int> ret(team.begin(), team.end());
548 std::sort(ret.begin(), ret.end(), [this](int a, int b) {
549 return player_ordering.find(a)->second < player_ordering.find(b)->second;