Added a transactor for getting info from the current tournament.
[ccbs] / bigscreen / ccbs_bigscreen.cpp
1 #include <cstdio>
2 #include <unistd.h>
3 #include <pqxx/pqxx>
4 #include "glwindow.h"
5
6 /* A trigger that sets a flag whenever it's trigged. */
7 class FlagTrigger : pqxx::trigger {
8 private:
9         bool flag;
10         
11 public:
12         FlagTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
13                 : pqxx::trigger(conn, name), flag(false) {}
14         virtual ~FlagTrigger() throw () {}
15         
16         virtual void operator() (int pid)
17         {
18                 flag = true;
19                 std::fprintf(stderr, "Received a flag trigger from pid %u\n", pid);
20         }
21
22         bool get_flag() const
23         {
24                 return flag;
25         }
26
27         void reset_flag()
28         {
29                 flag = false;
30         }
31 };
32
33 /* A transactor that fetches the current tournament and some information about it. */
34 class FetchCurrentTournament : public pqxx::transactor<> {
35 private:
36         int id;
37         std::string name;
38
39 public:
40         FetchCurrentTournament() {}
41         void operator() (pqxx::transaction<> &t)
42         {
43                 pqxx::result res( t.exec("SELECT * FROM bigscreen.active_tournament NATURAL JOIN tournaments") );
44                 try {
45                         pqxx::result::tuple tournament = res.at(0);
46
47                         id = tournament["tournament"].as(id);
48                         name = tournament["tournament"].as(name);
49                 } catch (PGSTD::out_of_range &e) {
50                         std::fprintf(stderr, "foof\n");
51                         
52                         id = -1;
53                         name = "";
54                 }
55         }
56
57         int get_tournament_id() const
58         {
59                 return id;
60         }
61         
62         std::string get_tournament_name() const
63         {
64                 return name;
65         }
66 };
67
68 void init(pqxx::connection &conn)
69 {
70         FetchCurrentTournament fct;
71         conn.perform(fct);
72
73         if (fct.get_tournament_id() == -1) {
74                 std::fprintf(stderr, "No active tournament\n");
75         } else {
76                 std::fprintf(stderr, "Current tournament is %d (name: '%s')\n",
77                         fct.get_tournament_id(), fct.get_tournament_name().c_str());
78         }
79 }
80
81 void main_loop(pqxx::connection &conn)
82 {
83         pqxx::work t(conn, "trx");
84
85         // fetch all songs
86         pqxx::result res( t.exec("SELECT * FROM songs") );
87         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
88                 // std::fprintf(stderr, "%s\n", i["title"].c_str());
89         }
90         t.commit();
91         
92         sleep(1);
93 }
94
95 int main(int argc, char **argv)
96 {
97         GLWindow glw("CCBS bigscreen", 800, 600, 32, false, 16, -1);
98         try {
99                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
100                 FlagTrigger tournament_changed(conn, "active_tournament");
101                 
102                 // when active_tournament is changed, we destroy everything and start from scratch
103                 for ( ;; ) {
104                         tournament_changed.reset_flag();
105                         init(conn);
106                         do {
107                                 main_loop(conn);
108                                 conn.get_notifs();
109                         } while (!tournament_changed.get_flag());
110                         std::fprintf(stderr, "active_tournament changed, resetting...\n");
111                 }
112         } catch (const std::exception &e) {
113                 std::fprintf(stderr, "Exception: %s\n", e.what());
114                 exit(1);
115         }
116         
117         return 0;
118 }