]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
Hit the group-fetching code so it compiles.
[ccbs] / bigscreen / ccbs_bigscreen.cpp
1 #include <cstdio>
2 #include <cstring>
3 #include <iconv.h>
4 #include <unistd.h>
5 #include <pqxx/pqxx>
6 #include <tinyptc.h>
7 #include "flagtrigger.h"
8 #include "widestring.h"
9 #include "fetch_current_tournament.h"
10 #include "fetch_list_of_active_groups.h"
11 #include "fetch_group.h"
12 #include "fonts.h"
13
14 Tournament active_tournament;
15 std::vector<SkeletonGroup> active_groups;
16 unsigned char framebuf[800 * 600 * 4];
17
18 void init(pqxx::connection &conn)
19 {
20         conn.perform(FetchCurrentTournament(&active_tournament));
21         conn.perform(FetchListOfActiveGroups(&active_groups));
22
23         if (active_tournament.id == -1) {
24                 std::fprintf(stderr, "No active tournament\n");
25         } else {
26                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
27
28                 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
29                         std::fprintf(stderr, "tourn: %u  round: %u   parallel: %u\n",
30                                 i->tournament, i->round, i->parallel);
31
32                         Group gr;
33                         conn.perform(FetchGroup(i->tournament, i->round, i->parallel, &gr));
34                         std::fprintf(stderr, "%u players in group\n", gr.players.size());
35                 }
36         }
37 }
38
39 void main_loop(pqxx::connection &conn)
40 {
41         if (active_tournament.id == -1) {
42                 // No active tournament, sleep a second or so and exit
43                 sleep(1);
44                 return;
45         }
46
47         memset(framebuf, 0, 800*600*4);
48         
49         pqxx::work t(conn, "trx");
50
51         // fetch all songs
52         pqxx::result res( t.exec("SELECT * FROM songs WHERE title LIKE 'M%'") );
53         unsigned y = 0;
54         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
55                 my_draw_text(i["title"].as(widestring()), framebuf, 0, y, 1, 255, 255, 255);
56                 y += 20;
57 //              std::fprintf(stderr, "%s\n", i["title"].c_str());
58         }
59         t.commit();
60
61         ptc_update(framebuf);
62         sleep(1);
63 }
64
65 int main(int argc, char **argv)
66 {
67         ptc_open("CCBS bigscreen", 800, 600);
68         
69         try {
70                 init_freetype();
71                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
72                 FlagTrigger tournament_changed(conn, "active_tournament");
73                 FlagTrigger rounds_changed(conn, "active_groups");
74                 
75                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
76                 // (at least currently)
77                 for ( ;; ) {
78                         tournament_changed.reset_flag();
79                         rounds_changed.reset_flag();
80                         init(conn);
81                         do {
82                                 main_loop(conn);
83                                 conn.get_notifs();
84                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
85                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
86                 }
87         } catch (const std::exception &e) {
88                 std::fprintf(stderr, "Exception: %s\n", e.what());
89                 exit(1);
90         }
91         
92         return 0;
93 }