]> git.sesse.net Git - pkanalytics/blob - events.cpp
Support filtering on formation.
[pkanalytics] / events.cpp
1 #include <algorithm>
2 #include <string>
3 #include <map>
4 #include <vector>
5 #include <optional>
6 #include <sqlite3.h>
7 #include "events.h"
8
9 using namespace std;
10
11 string format_timestamp(uint64_t pos);
12
13 EventsModel::EventsModel(sqlite3 *db, int match_id) : db(db), match_id(match_id)
14 {
15         load_data();
16 }
17
18 QVariant EventsModel::headerData(int section, Qt::Orientation orientation, int role) const
19 {
20         if (role != Qt::DisplayRole) {
21                 return QVariant();
22         }
23         if (orientation == Qt::Horizontal) {
24                 if (section == 0) {
25                         return "Time";
26                 } else if (section == 1) {
27                         return "Who/what";
28                 } else {
29                         return "Type";
30                 }
31         } else {
32                 return "";
33         }
34 }
35
36 QVariant EventsModel::data(const QModelIndex &index, int role) const
37 {
38         if (role != Qt::DisplayRole) {
39                 return QVariant();
40         }
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;
47                 if (player_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)";
57                 } else {
58                         return QVariant();
59                 }
60         } else if (index.column() == 2) {
61                 string type = e.type;
62                 type[0] = toupper(e.type[0]);
63                 for (char &ch : type) {
64                         if (ch == '_') {
65                                 ch = ' ';
66                         }
67                 }
68
69                 // Various fixups.
70                 if (type == "Pull oob") {
71                         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") {
77                         type = "On offense";
78                 } else if (type == "Set defense") {
79                         type = "On defense";
80                 } else if (type == "Catch") {
81                         type = "Catch/take";
82                 }
83
84                 return QString::fromUtf8(type);
85         }
86         return QVariant();
87 }
88
89 void EventsModel::load_data()
90 {
91         players.clear();
92         events.clear();
93
94         // Read the players. (The ordering is used to build the order map.)
95         sqlite3_stmt *stmt;
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));
99                 abort();
100         }
101         int order = 0;
102         for ( ;; ) {
103                 ret = sqlite3_step(stmt);
104                 if (ret == SQLITE_ROW) {
105                         Player p;
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) {
112                         break;
113                 } else {
114                         fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
115                         abort();
116                 }
117         }
118         ret = sqlite3_finalize(stmt);
119         if (ret != SQLITE_OK) {
120                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
121                 abort();
122         }
123
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));
128                 abort();
129         }
130         for ( ;; ) {
131                 ret = sqlite3_step(stmt);
132                 if (ret == SQLITE_ROW) {
133                         Formation f;
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) {
138                         break;
139                 } else {
140                         fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
141                         abort();
142                 }
143         }
144         ret = sqlite3_finalize(stmt);
145         if (ret != SQLITE_OK) {
146                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
147                 abort();
148         }
149
150         // Read the events.
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));
154                 abort();
155         }
156         sqlite3_bind_int64(stmt, 1, match_id);
157         for ( ;; ) {
158                 ret = sqlite3_step(stmt);
159                 if (ret == SQLITE_ROW) {
160                         Event e;
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);
165                         }
166                         if (sqlite3_column_type(stmt, 3) == SQLITE_INTEGER) {  // Non-NULL.
167                                 e.formation_id = sqlite3_column_int(stmt, 3);
168                         }
169                         e.type = (const char *)sqlite3_column_text(stmt, 4);
170                         events.push_back(std::move(e));
171                 } else if (ret == SQLITE_DONE) {
172                         break;
173                 } else {
174                         fprintf(stderr, "SELECT step: %s\n", sqlite3_errmsg(db));
175                         abort();
176                 }
177         }
178         ret = sqlite3_finalize(stmt);
179         if (ret != SQLITE_OK) {
180                 fprintf(stderr, "SELECT finalize: %s\n", sqlite3_errmsg(db));
181                 abort();
182         }
183 }
184
185 unsigned EventsModel::insert_event(uint64_t t, optional<int> player_id, optional<int> formation_id, const string &type)
186 {
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);
191
192         Event e;
193         e.t = t;
194         e.player_id = player_id;
195         e.formation_id = formation_id;
196         e.type = type;
197         events.insert(events.begin() + pos, e);
198
199         endInsertRows();
200
201         // Insert the new row into the database.
202         sqlite3_stmt *stmt;
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));
206                 abort();
207         }
208
209         sqlite3_bind_int64(stmt, 1, match_id);
210         sqlite3_bind_int64(stmt, 2, t);
211         if (player_id) {
212                 sqlite3_bind_int64(stmt, 3, *player_id);
213         } else {
214                 sqlite3_bind_null(stmt, 3);
215         }
216         if (formation_id) {
217                 sqlite3_bind_int64(stmt, 4, *formation_id);
218         } else {
219                 sqlite3_bind_null(stmt, 4);
220         }
221         sqlite3_bind_text(stmt, 5, type.data(), type.size(), SQLITE_STATIC);
222
223         ret = sqlite3_step(stmt);
224         if (ret == SQLITE_ROW) {
225                 fprintf(stderr, "INSERT step: %s\n", sqlite3_errmsg(db));
226                 abort();
227         }
228
229         ret = sqlite3_finalize(stmt);
230         if (ret != SQLITE_OK) {
231                 fprintf(stderr, "INSERT finalize: %s\n", sqlite3_errmsg(db));
232                 abort();
233         }
234
235         events[pos].event_id = sqlite3_last_insert_rowid(db);
236         return pos;
237 }
238
239 void EventsModel::delete_event(unsigned pos)
240 {
241         int event_id = events[pos].event_id;
242
243         beginRemoveRows(QModelIndex(), pos, pos);
244         events.erase(events.begin() + pos);
245         endRemoveRows();
246
247         // Delete the row from the database.
248         sqlite3_stmt *stmt;
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));
252                 abort();
253         }
254
255         sqlite3_bind_int64(stmt, 1, event_id);
256
257         ret = sqlite3_step(stmt);
258         if (ret == SQLITE_ROW) {
259                 fprintf(stderr, "DELETE step: %s\n", sqlite3_errmsg(db));
260                 abort();
261         }
262
263         ret = sqlite3_finalize(stmt);
264         if (ret != SQLITE_OK) {
265                 fprintf(stderr, "DELETE finalize: %s\n", sqlite3_errmsg(db));
266                 abort();
267         }
268 }
269
270 void EventsModel::set_event_type(unsigned pos, const string &type)
271 {
272         events[pos].type = type;
273         emit dataChanged(createIndex(pos, 0), createIndex(pos, 2));
274
275         sqlite3_stmt *stmt;
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));
279                 abort();
280         }
281
282         sqlite3_bind_text(stmt, 1, type.data(), type.size(), SQLITE_STATIC);
283         sqlite3_bind_int64(stmt, 2, events[pos].event_id);
284
285         ret = sqlite3_step(stmt);
286         if (ret == SQLITE_ROW) {
287                 fprintf(stderr, "UPDATE step: %s\n", sqlite3_errmsg(db));
288                 abort();
289         }
290
291         ret = sqlite3_finalize(stmt);
292         if (ret != SQLITE_OK) {
293                 fprintf(stderr, "UPDATE finalize: %s\n", sqlite3_errmsg(db));
294                 abort();
295         }
296 }
297
298 unsigned EventsModel::get_last_event_pos(uint64_t t) const
299 {
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()) {
305                 return 0;
306         } else {
307                 return distance(events.begin(), it - 1);
308         }
309 }
310
311 EventsModel::Status EventsModel::get_status_at(uint64_t t)
312 {
313         Status s;
314         s.our_score = 0;
315         s.their_score = 0;
316         s.attack_state = Status::NOT_STARTED;
317         s.offensive_formation = 0;
318         s.defensive_formation = 0;
319         s.stoppage = false;
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;
325
326         auto set_offense = [&s] { s.attack_state = Status::OFFENSE; };
327         auto set_defense = [&s] { s.attack_state = Status::DEFENSE; };
328
329         for (const Event &e : events) {
330                 if (e.t > t) {
331                         break;
332                 }
333
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;
340                 } else {
341                         s.pull_state = Status::NOT_PULLING;  // Includes pull_landed and pull_oob.
342                 }
343
344                 if (e.type == "set_offense") {
345                         set_offense();
346                 } else if (e.type == "set_defense") {
347                         set_defense();
348                 }
349
350                 if (e.type == "goal") {
351                         ++s.our_score;
352                         set_defense();
353                         num_touches = 0;
354                 }
355                 if (e.type == "their_goal") {
356                         ++s.their_score;
357                         set_offense();
358                         num_touches = 0;
359                 }
360                 if (e.type == "catch") {
361                         if (num_touches == 0) {  // Pick up.
362                                 last_gained_possession = e.t;
363                                 time_spent_in_stoppage = 0;
364                         }
365                         ++num_touches;
366                 }
367                 if (e.type == "interception") {
368                         num_touches = 1;
369                         set_offense();
370                         last_gained_possession = e.t;
371                         time_spent_in_stoppage = 0;
372                 }
373                 if (e.type == "defense" || e.type == "their_throwaway") {
374                         set_offense();
375                         num_touches = 0;
376                         time_spent_in_stoppage = 0;
377                 }
378                 if (e.type == "drop" || e.type == "throwaway") {
379                         set_defense();
380                         num_touches = 0;
381                 }
382                 if (e.type == "stoppage") {
383                         s.stoppage = true;
384                         last_stoppage = e.t;
385                 }
386                 if (e.type == "restart") {
387                         s.stoppage = false;
388                         if (last_stoppage != 0) {
389                                 time_spent_in_stoppage += (e.t - last_stoppage);
390                                 last_stoppage = 0;
391                         }
392                 }
393                 if (e.type == "formation_offense") {
394                         if (e.formation_id) {
395                                 s.offensive_formation = *e.formation_id;
396                         } else {
397                                 s.offensive_formation = 0;
398                         }
399                 }
400                 if (e.type == "formation_defense") {
401                         if (e.formation_id) {
402                                 s.defensive_formation = *e.formation_id;
403                         } else {
404                                 s.defensive_formation = 0;
405                         }
406                 }
407         }
408         if (s.stoppage && last_stoppage != 0) {
409                 time_spent_in_stoppage += (t - last_stoppage);
410         }
411
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;
415         return s;
416 }
417
418 set<int> EventsModel::get_team_at(uint64_t t)
419 {
420         set<int> team;
421         for (const Event &e : events) {
422                 if (e.t > t) {
423                         break;
424                 }
425                 if (e.type == "in") {
426                         team.insert(*e.player_id);
427                 }
428                 if (e.type == "out") {
429                         team.erase(*e.player_id);
430                 }
431         }
432         return team;
433 }
434
435 void EventsModel::set_team_at(uint64_t t, const set<int> &new_team)
436 {
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
440         // be the simplest.)
441         uint64_t backdate_point = 0;
442         for (const Event &e : events) {
443                 if (e.t > t) {
444                         break;
445                 }
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;
448                 }
449                 if (e.player_id.has_value() && !new_team.count(*e.player_id)) {
450                         backdate_point = e.t + 1;
451                 }
452         }
453
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) {
457                         break;
458                 }
459                 if (events[i].t == backdate_point && (events[i].type == "in" || events[i].type == "out")) {
460                         delete_event(i);
461                 } else {
462                         ++i;
463                 }
464         }
465
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");
471                 }
472         }
473         for (int player_id : new_team) {
474                 if (!old_team.count(player_id)) {
475                         insert_event(backdate_point, player_id, nullopt, "in");
476                 }
477         }
478 }
479
480 void EventsModel::set_formation_at(uint64_t t, bool offense, unsigned formation)
481 {
482         // If there's another goal/stoppage 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) {
488                 if (e.t > t) {
489                         break;
490                 }
491                 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") {
492                         backdate_point = e.t + 1;
493                 }
494                 if (e.type == "formation_offense" || e.type == "formation_defense") {
495                         backdate_point = 0;
496                 }
497         }
498         if (backdate_point != 0 && t - backdate_point < 20000) {
499                 t = backdate_point;
500         }
501         if (offense) {
502                 insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_offense");
503         } else {
504                 insert_event(t, nullopt, formation == 0 ? nullopt : optional{formation}, "formation_defense");
505         }
506 }
507
508 vector<int> EventsModel::sort_team(const set<int> &team) const
509 {
510         vector<int> ret(team.begin(), team.end());
511         std::sort(ret.begin(), ret.end(), [this](int a, int b) {
512                 return player_ordering.find(a)->second < player_ordering.find(b)->second;
513         });
514         return ret;
515 }