]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
09aa59cdedc504eeaddc30806e283f31a0f06789
[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 class Tournament {
34 public:
35         int id;
36         std::string name;
37 };
38
39 /* A transactor that fetches the current tournament and some information about it. */
40 class FetchCurrentTournament : public pqxx::transactor<> {
41 private:
42         Tournament *tourn;
43
44 public:
45         FetchCurrentTournament(Tournament *tourn) : tourn(tourn) {}
46         void operator() (pqxx::transaction<> &t)
47         {
48                 pqxx::result res( t.exec("SELECT * FROM bigscreen.active_tournament NATURAL JOIN tournaments") );
49                 try {
50                         pqxx::result::tuple tournament = res.at(0);
51
52                         tourn->id = tournament["tournament"].as(tourn->id);
53                         tourn->name = tournament["tournamentname"].as(tourn->name);
54                 } catch (PGSTD::out_of_range &e) {
55                         tourn->id = -1;
56                         tourn->name = "";
57                 }
58         }
59 };
60
61 void init(pqxx::connection &conn)
62 {
63         Tournament tourn;
64         conn.perform(FetchCurrentTournament(&tourn));
65
66         if (tourn.id == -1) {
67                 std::fprintf(stderr, "No active tournament\n");
68         } else {
69                 std::fprintf(stderr, "Current tournament is %d (name: '%s')\n",
70                         tourn.id, tourn.name.c_str());
71         }
72 }
73
74 void main_loop(pqxx::connection &conn)
75 {
76         pqxx::work t(conn, "trx");
77
78         // fetch all songs
79         pqxx::result res( t.exec("SELECT * FROM songs") );
80         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
81                 // std::fprintf(stderr, "%s\n", i["title"].c_str());
82         }
83         t.commit();
84         
85         sleep(1);
86 }
87
88 int main(int argc, char **argv)
89 {
90         GLWindow glw("CCBS bigscreen", 800, 600, 32, false, 16, -1);
91         try {
92                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
93                 FlagTrigger tournament_changed(conn, "active_tournament");
94                 
95                 // when active_tournament is changed, we destroy everything and start from scratch
96                 for ( ;; ) {
97                         tournament_changed.reset_flag();
98                         init(conn);
99                         do {
100                                 main_loop(conn);
101                                 conn.get_notifs();
102                         } while (!tournament_changed.get_flag());
103                         std::fprintf(stderr, "active_tournament changed, resetting...\n");
104                 }
105         } catch (const std::exception &e) {
106                 std::fprintf(stderr, "Exception: %s\n", e.what());
107                 exit(1);
108         }
109         
110         return 0;
111 }