12 Tournament active_tournament;
14 /* A trigger that sets a flag whenever it's trigged. */
15 class FlagTrigger : pqxx::trigger {
20 FlagTrigger(pqxx::connection_base &conn, const PGSTD::string &name)
21 : pqxx::trigger(conn, name), flag(false) {}
22 virtual ~FlagTrigger() throw () {}
24 virtual void operator() (int pid)
27 std::fprintf(stderr, "Received a flag trigger from pid %u\n", pid);
41 /* A transactor that fetches the current tournament and some information about it. */
42 class FetchCurrentTournament : public pqxx::transactor<> {
47 FetchCurrentTournament(Tournament *tourn) : tourn(tourn) {}
48 void operator() (pqxx::transaction<> &t)
50 pqxx::result res( t.exec("SELECT * FROM bigscreen.active_tournament NATURAL JOIN tournaments") );
52 pqxx::result::tuple tournament = res.at(0);
54 tourn->id = tournament["tournament"].as(tourn->id);
55 tourn->name = tournament["tournamentname"].as(tourn->name);
56 } catch (PGSTD::out_of_range &e) {
63 void init(pqxx::connection &conn)
65 conn.perform(FetchCurrentTournament(&active_tournament));
67 if (active_tournament.id == -1) {
68 std::fprintf(stderr, "No active tournament\n");
70 std::fprintf(stderr, "Current tournament is %d (name: '%s')\n",
71 active_tournament.id, active_tournament.name.c_str());
75 void main_loop(pqxx::connection &conn)
77 if (active_tournament.id == -1) {
78 // No active tournament, sleep a second or so and exit
83 pqxx::work t(conn, "trx");
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());
95 int main(int argc, char **argv)
97 GLWindow glw("CCBS bigscreen", 800, 600, 32, false, 16, -1);
99 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
100 FlagTrigger tournament_changed(conn, "active_tournament");
102 // when active_tournament is changed, we destroy everything and start from scratch
104 tournament_changed.reset_flag();
109 } while (!tournament_changed.get_flag());
110 std::fprintf(stderr, "active_tournament changed, resetting...\n");
112 } catch (const std::exception &e) {
113 std::fprintf(stderr, "Exception: %s\n", e.what());