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") {
84 return QString::fromUtf8(type);
89 void EventsModel::load_data()
94 // Read the players. (The ordering is used to build the order map.)
96 int ret = sqlite3_prepare_v2(db, "SELECT player, number, name FROM player ORDER BY gender, (number+0), number", -1, &stmt, 0);
97 if (ret != SQLITE_OK) {
98 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
103 ret = sqlite3_step(stmt);
104 if (ret == SQLITE_ROW) {
106 p.player_id = sqlite3_column_int(stmt, 0);
107 p.number = (const char *)sqlite3_column_text(stmt, 1);
108 p.name = (const char *) sqlite3_column_text(stmt, 2);
109 players[p.player_id] = std::move(p);
110 player_ordering[p.player_id] = order++;
111 } else if (ret == SQLITE_DONE) {
114 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
118 ret = sqlite3_finalize(stmt);
119 if (ret != SQLITE_OK) {
120 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
124 // Read the formations.
125 ret = sqlite3_prepare_v2(db, "SELECT formation, name FROM formation", -1, &stmt, 0);
126 if (ret != SQLITE_OK) {
127 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
131 ret = sqlite3_step(stmt);
132 if (ret == SQLITE_ROW) {
134 f.formation_id = sqlite3_column_int(stmt, 0);
135 f.name = (const char *) sqlite3_column_text(stmt, 1);
136 formations[f.formation_id] = std::move(f);
137 } else if (ret == SQLITE_DONE) {
140 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
144 ret = sqlite3_finalize(stmt);
145 if (ret != SQLITE_OK) {
146 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
151 ret = sqlite3_prepare_v2(db, "SELECT event, t, player, formation, type FROM event WHERE match=? ORDER BY t", -1, &stmt, 0);
152 if (ret != SQLITE_OK) {
153 fprintf(stderr, "SELECT prepare: %s\n", sqlite3_errmsg(db));
156 sqlite3_bind_int64(stmt, 1, match_id);
158 ret = sqlite3_step(stmt);
159 if (ret == SQLITE_ROW) {
161 e.event_id = sqlite3_column_int(stmt, 0);
162 e.t = sqlite3_column_int(stmt, 1);
163 if (sqlite3_column_type(stmt, 2) == SQLITE_INTEGER) { // Non-NULL.
164 e.player_id = sqlite3_column_int(stmt, 2);
166 if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) { // Non-NULL.
167 e.formation_id = sqlite3_column_int(stmt, 3);
169 e.type = (const char *)sqlite3_column_text(stmt, 4);
170 events.push_back(std::move(e));
171 } else if (ret == SQLITE_DONE) {
174 fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
178 ret = sqlite3_finalize(stmt);
179 if (ret != SQLITE_OK) {
180 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
185 unsigned EventsModel::insert_event(uint64_t t, optional<int> player_id, optional<int> formation_id, const string &type)
187 auto it = lower_bound(events.begin(), events.end(), t,
188 [](const Event &e, uint64_t t) { return e.t < t; });
189 unsigned pos = distance(events.begin(), it);
190 beginInsertRows(QModelIndex(), pos, pos);
194 e.player_id = player_id;
195 e.formation_id = formation_id;
197 events.insert(events.begin() + pos, e);
201 // Insert the new row into the database.
203 int ret = sqlite3_prepare_v2(db, "INSERT INTO event (match, t, player, formation, type) VALUES (?, ?, ?, ?, ?)", -1, &stmt, 0);
204 if (ret != SQLITE_OK) {
205 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
209 sqlite3_bind_int64(stmt, 1, match_id);
210 sqlite3_bind_int64(stmt, 2, t);
212 sqlite3_bind_int64(stmt, 3, *player_id);
214 sqlite3_bind_null(stmt, 3);
217 sqlite3_bind_int64(stmt, 4, *formation_id);
219 sqlite3_bind_null(stmt, 4);
221 sqlite3_bind_text(stmt, 5, type.data(), type.size(), SQLITE_STATIC);
223 ret = sqlite3_step(stmt);
224 if (ret == SQLITE_ROW) {
225 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
229 ret = sqlite3_finalize(stmt);
230 if (ret != SQLITE_OK) {
231 fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
235 events[pos].event_id = sqlite3_last_insert_rowid(db);
239 void EventsModel::delete_event(unsigned pos)
241 int event_id = events[pos].event_id;
243 beginRemoveRows(QModelIndex(), pos, pos);
244 events.erase(events.begin() + pos);
247 // Delete the row from the database.
249 int ret = sqlite3_prepare_v2(db, "DELETE FROM event WHERE event=?", -1, &stmt, 0);
250 if (ret != SQLITE_OK) {
251 fprintf(stderr, "DELETE prepare: %s\n", sqlite3_errmsg(db));
255 sqlite3_bind_int64(stmt, 1, event_id);
257 ret = sqlite3_step(stmt);
258 if (ret == SQLITE_ROW) {
259 fprintf(stderr, "DELETE step: %s\n", sqlite3_errmsg(db));
263 ret = sqlite3_finalize(stmt);
264 if (ret != SQLITE_OK) {
265 fprintf(stderr, "DELETE finalize: %s\n", sqlite3_errmsg(db));
270 void EventsModel::set_event_type(unsigned pos, const string &type)
272 events[pos].type = type;
273 emit dataChanged(createIndex(pos, 0), createIndex(pos, 2));
276 int ret = sqlite3_prepare_v2(db, "UPDATE event SET type=? WHERE event=?", -1, &stmt, 0);
277 if (ret != SQLITE_OK) {
278 fprintf(stderr, "INSERT prepare: %s\n", sqlite3_errmsg(db));
282 sqlite3_bind_text(stmt, 1, type.data(), type.size(), SQLITE_STATIC);
283 sqlite3_bind_int64(stmt, 2, events[pos].event_id);
285 ret = sqlite3_step(stmt);
286 if (ret == SQLITE_ROW) {
287 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
291 ret = sqlite3_finalize(stmt);
292 if (ret != SQLITE_OK) {
293 fprintf(stderr, "UPDATE finalize: %s\n", sqlite3_errmsg(db));
298 unsigned EventsModel::get_last_event_pos(uint64_t t) const
300 // upper_bound() gives first where e.t > t,
301 // and the one before that is the one we want.
302 auto it = upper_bound(events.begin(), events.end(), t,
303 [](uint64_t t, const Event &e) { return t < e.t; });
304 if (it == events.begin()) {
307 return distance(events.begin(), it - 1);
311 EventsModel::Status EventsModel::get_status_at(uint64_t t)
316 s.attack_state = Status::NOT_STARTED;
317 s.offensive_formation = 0;
318 s.defensive_formation = 0;
320 s.pull_state = Status::SHOULD_PULL;
321 uint64_t last_gained_possession = 0;
322 uint64_t last_stoppage = 0;
323 uint64_t time_spent_in_stoppage = 0;
324 unsigned num_touches = 0;
326 auto set_offense = [&s] { s.attack_state = Status::OFFENSE; };
327 auto set_defense = [&s] { s.attack_state = Status::DEFENSE; };
329 for (const Event &e : events) {
334 if (e.type == "goal" || e.type == "their_goal") {
335 s.pull_state = Status::SHOULD_PULL;
336 } 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") {
337 // No effect on pull status.
338 } else if (e.type == "pull") {
339 s.pull_state = Status::PULL_IN_AIR;
341 s.pull_state = Status::NOT_PULLING; // Includes pull_landed and pull_oob.
344 if (e.type == "set_offense") {
346 } else if (e.type == "set_defense") {
350 if (e.type == "goal") {
355 if (e.type == "their_goal") {
360 if (e.type == "catch") {
361 if (num_touches == 0) { // Pick up.
362 last_gained_possession = e.t;
363 time_spent_in_stoppage = 0;
367 if (e.type == "interception") {
370 last_gained_possession = e.t;
371 time_spent_in_stoppage = 0;
373 if (e.type == "defense" || e.type == "their_throwaway") {
376 time_spent_in_stoppage = 0;
378 if (e.type == "drop" || e.type == "throwaway") {
382 if (e.type == "stoppage") {
386 if (e.type == "restart") {
388 if (last_stoppage != 0) {
389 time_spent_in_stoppage += (e.t - last_stoppage);
393 if (e.type == "formation_offense") {
394 if (e.formation_id) {
395 s.offensive_formation = *e.formation_id;
397 s.offensive_formation = 0;
400 if (e.type == "formation_defense") {
401 if (e.formation_id) {
402 s.defensive_formation = *e.formation_id;
404 s.defensive_formation = 0;
408 if (s.stoppage && last_stoppage != 0) {
409 time_spent_in_stoppage += (t - last_stoppage);
412 s.num_passes = (num_touches == 0) ? 0 : num_touches - 1;
413 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;
414 s.stoppage_sec = (s.attack_state == Status::OFFENSE && last_gained_possession != 0 && num_touches != 0) ? time_spent_in_stoppage / 1000 : 0;
418 set<int> EventsModel::get_team_at(uint64_t t)
421 for (const Event &e : events) {
425 if (e.type == "in") {
426 team.insert(*e.player_id);
428 if (e.type == "out") {
429 team.erase(*e.player_id);
435 void EventsModel::set_team_at(uint64_t t, const set<int> &new_team)
437 // Backdate to the last goal or stoppage, _or_ the last time someone
438 // going out is mentioned. (We don't really track injuries yet;
439 // do we want an explicit injury type? If we had one, it would probably
441 uint64_t backdate_point = 0;
442 for (const Event &e : events) {
446 if (e.type == "goal" || e.type == "their_goal" || e.type == "stoppage" || e.type == "reset" || e.type == "set_offense" || e.type == "set_defense") {
447 backdate_point = e.t + 1;
449 if (e.player_id.has_value() && !new_team.count(*e.player_id)) {
450 backdate_point = e.t + 1;
454 // Delete all in/outs already at the backdate point.
455 for (unsigned i = 0; i < events.size(); ) {
456 if (events[i].t > backdate_point) {
459 if (events[i].t == backdate_point && (events[i].type == "in" || events[i].type == "out")) {
466 // Finally make the subs we need.
467 set<int> old_team = get_team_at(backdate_point);
468 for (int player_id : old_team) {
469 if (!new_team.count(player_id)) {
470 insert_event(backdate_point, player_id, nullopt, "out");
473 for (int player_id : new_team) {
474 if (!old_team.count(player_id)) {
475 insert_event(backdate_point, player_id, nullopt, "in");
480 void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
482 // If there's another goal/stoppage/turnover no more than 20 seconds ago,
483 // we assume that the formation started at that point (it just took
484 // the operator a bit of time to see it). If not, we assume we
485 // changed in the middle of a point.
486 uint64_t backdate_point = 0;
487 for (const Event &e : events) {
491 if (e.type == "goal" || e.type == "their_goal" ||
492 e.type == "in" || e.type == "out" ||
493 e.type == "stoppage" || e.type == "reset" ||
494 e.type == "set_defense" || e.type == "set_offense" ||
495 e.type == "throwaway" || e.type == "their_throwaway" ||
496 e.type == "drop" || e.type == "defense" || e.type == "interception" ||
497 e.type == "pull" || e.type == "pull_landed" || e.type == "pull_oob" || e.type == "their_pull" ||
498 e.type == "formation_offense" || e.type == "formation_defense") {
499 backdate_point = e.t + 1;
501 if (e.type == "formation_offense" || e.type == "formation_defense") {
505 if (backdate_point != 0 && t - backdate_point < 20000) {
509 insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_offense");
511 insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_defense");
515 vector<int> EventsModel::sort_team(const set<int> &team) const
517 vector<int> ret(team.begin(), team.end());
518 std::sort(ret.begin(), ret.end(), [this](int a, int b) {
519 return player_ordering.find(a)->second < player_ordering.find(b)->second;