]> git.sesse.net Git - pkanalytics/blob - events.h
Support filtering passes by thrower and receiver.
[pkanalytics] / events.h
1 #ifndef _EVENTS_H
2 #define _EVENTS_H 1
3
4 #include <sqlite3.h>
5 #include <stdint.h>
6 #include <QAbstractTableModel>
7 #include <map>
8 #include <vector>
9 #include <optional>
10 #include <set>
11
12 enum class EventType {
13         CATCH,
14         DEFENSE,
15         DEFENSIVE_SOFT_MINUS,
16         DEFENSIVE_SOFT_PLUS,
17         FORMATION_DEFENSE,
18         FORMATION_OFFENSE,
19         DROP,
20         GOAL,
21         SWAP_IN,
22         INTERCEPTION,
23         OFFENSIVE_SOFT_MINUS,
24         OFFENSIVE_SOFT_PLUS,
25         SWAP_OUT,
26         PULL,
27         PULL_LANDED,
28         PULL_OOB,
29         RESTART,
30         SET_DEFENSE,
31         SET_OFFENSE,
32         STALLOUT,
33         STOPPAGE,
34         THEIR_GOAL,
35         THEIR_PULL,
36         THEIR_THROWAWAY,
37         THROWAWAY,
38         UNKNOWN,
39         WAS_D,
40 };
41
42 class EventsModel : public QAbstractTableModel
43 {
44 public:
45         EventsModel(sqlite3 *db, int match_id);
46
47         int rowCount(const QModelIndex &parent) const override
48         {
49                 return events.size();
50         }
51         int columnCount(const QModelIndex &column) const override
52         {
53                 return 3;
54         }
55         QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
56         QVariant data(const QModelIndex &index, int role) const override;
57
58         unsigned insert_event(uint64_t t, std::optional<int> player_id, std::optional<int> formation_id, const std::string &type = "unknown");  // Returns the row.
59         void delete_event(unsigned row);
60         EventType get_event_type(unsigned row) { return events[row].type; }
61         void set_event_type(unsigned row, const std::string &type);
62         void set_event_formation(unsigned row, int formation_id);
63         uint64_t get_time(unsigned row) { return events[row].t; }
64         unsigned get_last_event_pos(uint64_t t) const;  // Last event that happened at or before t.
65         QModelIndex get_last_event_qt(uint64_t t) const {
66                 return createIndex(get_last_event_pos(t), 0);
67         }
68         std::optional<int> get_player_id(unsigned row) { return events[row].player_id; }
69
70         struct Status {
71                 unsigned our_score, their_score;
72                 enum { NOT_STARTED, OFFENSE, DEFENSE } attack_state;
73                 unsigned offensive_formation, defensive_formation;
74                 bool stoppage;
75                 enum { NOT_PULLING, SHOULD_PULL, PULL_IN_AIR } pull_state;
76                 unsigned num_passes;
77                 unsigned possession_sec;
78                 unsigned stoppage_sec;
79                 int last_catching_player;  // -1 if we are not on offense, or last disc-related event wasn't a catch.
80         };
81         Status get_status_at(uint64_t t);
82         std::set<int> get_team_at(uint64_t t);
83         void set_team_at(uint64_t, const std::set<int> &new_team);
84         std::vector<int> sort_team(const std::set<int> &team) const;  // Ordered first by gender, then by number.
85         void set_formation_at(uint64_t t, bool offense, unsigned formation);
86
87         // Used to notify when we've inserted a new one into the database.
88         void inserted_new_formation(int formation_id, const std::string &name) {
89                 formations[formation_id] = Formation{ formation_id, name };
90         }
91
92 private:
93         struct Player {
94                 int player_id;
95                 std::string number;
96                 std::string name;
97         };
98         std::map<int, Player> players;
99         std::map<int, int> player_ordering;  // From id to position.
100
101         struct Formation {
102                 int formation_id;
103                 std::string name;
104         };
105         std::map<int, Formation> formations;
106
107         struct Event {
108                 int event_id;
109                 uint64_t t;
110                 std::optional<int> player_id;
111                 std::optional<int> formation_id;
112                 EventType type;
113         };
114         std::vector<Event> events;
115
116         sqlite3 *db;
117         int match_id;
118
119         void load_data();
120 };
121
122 #endif  // !defined(_EVENTS_H)