]> git.sesse.net Git - pkanalytics/blob - events.h
Connect insertions/type changes to the database.
[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
11 class EventsModel : public QAbstractTableModel
12 {
13 public:
14         EventsModel(sqlite3 *db) : db(db) {}
15
16         int rowCount(const QModelIndex &parent) const override
17         {
18                 refresh_if_needed();
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         int insert_event(uint64_t t, int player_id);
29         void set_event_type(unsigned row, const std::string &type);
30
31 private:
32         struct Player {
33                 int player_id;
34                 std::string number;
35                 std::string name;
36         };
37         mutable std::map<int, Player> players;
38
39         struct Event {
40                 int event_id;
41                 uint64_t t;
42                 std::optional<int> player_id;
43                 std::string type;
44         };
45         mutable std::vector<Event> events;
46         mutable bool stale = true;
47
48         sqlite3 *db;
49
50         void refresh_if_needed() const;
51 };
52
53 #endif  // !defined(_EVENTS_H)