]> git.sesse.net Git - pkanalytics/blob - events.h
Make persistent PBOs for faster texture upload.
[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 class EventsModel : public QAbstractTableModel
13 {
14 public:
15         EventsModel(sqlite3 *db, int match_id);
16
17         int rowCount(const QModelIndex &parent) const override
18         {
19                 return events.size();
20         }
21         int columnCount(const QModelIndex &column) const override
22         {
23                 return 3;
24         }
25         QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
26         QVariant data(const QModelIndex &index, int role) const override;
27
28         unsigned insert_event(uint64_t t, std::optional<int> player_id, std::optional<int> formation_id, const std::string &type = "unknown");  // Returns the row.
29         void delete_event(unsigned row);
30         std::string get_event_type(unsigned row) { return events[row].type; }
31         void set_event_type(unsigned row, const std::string &type);
32         void set_event_formation(unsigned row, int formation_id);
33         uint64_t get_time(unsigned row) { return events[row].t; }
34         unsigned get_last_event_pos(uint64_t t) const;  // Last event that happened at or before t.
35         QModelIndex get_last_event_qt(uint64_t t) const {
36                 return createIndex(get_last_event_pos(t), 0);
37         }
38         std::optional<int> get_player_id(unsigned row) { return events[row].player_id; }
39
40         struct Status {
41                 unsigned our_score, their_score;
42                 enum { NOT_STARTED, OFFENSE, DEFENSE } attack_state;
43                 unsigned offensive_formation, defensive_formation;
44                 bool stoppage;
45                 enum { NOT_PULLING, SHOULD_PULL, PULL_IN_AIR } pull_state;
46                 unsigned num_passes;
47                 unsigned possession_sec;
48                 unsigned stoppage_sec;
49         };
50         Status get_status_at(uint64_t t);
51         std::set<int> get_team_at(uint64_t t);
52         void set_team_at(uint64_t, const std::set<int> &new_team);
53         std::vector<int> sort_team(const std::set<int> &team) const;  // Ordered first by gender, then by number.
54         void set_formation_at(uint64_t t, bool offense, unsigned formation);
55
56         // Used to notify when we've inserted a new one into the database.
57         void inserted_new_formation(int formation_id, const std::string &name) {
58                 formations[formation_id] = Formation{ formation_id, name };
59         }
60
61 private:
62         struct Player {
63                 int player_id;
64                 std::string number;
65                 std::string name;
66         };
67         std::map<int, Player> players;
68         std::map<int, int> player_ordering;  // From id to position.
69
70         struct Formation {
71                 int formation_id;
72                 std::string name;
73         };
74         std::map<int, Formation> formations;
75
76         struct Event {
77                 int event_id;
78                 uint64_t t;
79                 std::optional<int> player_id;
80                 std::optional<int> formation_id;
81                 std::string type;
82         };
83         std::vector<Event> events;
84
85         sqlite3 *db;
86         int match_id;
87
88         void load_data();
89 };
90
91 #endif  // !defined(_EVENTS_H)