]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
Don't show total if we only have one song.
[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_list_of_finished_groups.h"
12 #include "fetch_group.h"
13 #include "fetch_auxilliary_screens.h"
14 #include "fonts.h"
15 #include "groupscreen.h"
16 #include "top10scorescreen.h"
17 #include "top5chosenscreen.h"
18 #include "splitscreen.h"
19 #include "rotatescreen.h"
20
21 Tournament active_tournament;
22 std::vector<SkeletonGroup> active_groups;
23 std::vector<GenericScreen *> screens;
24 GenericScreen *mainscreen = NULL;
25 unsigned char framebuf[SCREEN_WIDTH * SCREEN_HEIGHT * 4], screenbuf[SCREEN_WIDTH * SCREEN_HEIGHT * 4];
26
27 void init(pqxx::connection &conn)
28 {
29         std::vector<widestring> aux_screens;
30                 
31         if (screens.size() == 0 || mainscreen != screens[0])
32                 delete mainscreen;
33         
34         for (std::vector<GenericScreen *>::const_iterator i = screens.begin(); i != screens.end(); ++i) {
35                 delete *i;
36         }
37         screens.erase(screens.begin(), screens.end());
38
39         conn.perform(FetchCurrentTournament(&active_tournament));
40         conn.perform(FetchListOfActiveGroups(&active_groups));
41
42         if (active_tournament.id == -1) {
43                 std::fprintf(stderr, "No active tournament\n");
44         } else {
45                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
46
47                 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
48                         std::fprintf(stderr, "tourn: %u  round: %u  parallel: %u  num_machines: %u\n",
49                                 i->tournament, i->round, i->parallel, i->num_machines);
50
51                         // memory leaks here?
52                         for (unsigned j = 0; j < i->num_machines; ++j) {
53                                 RotateScreen *rs = new RotateScreen();
54                                 screens.push_back(rs);
55                                 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
56                         }
57                 }
58         }
59
60         // show auxilliary screens if we have zero, two or three other screens going
61         if (screens.size() < 4 && screens.size() != 1) {
62                 RotateScreen *aux_screen = new RotateScreen();
63                 screens.push_back(aux_screen);
64                 
65                 conn.perform(FetchAuxilliaryScreens(&aux_screens));
66                 for (std::vector<widestring>::const_iterator i = aux_screens.begin(); i != aux_screens.end(); ++i) {
67                         if (*i == widestring("top10scores")) {
68                                 aux_screen->add_screen(new Top10ScoreScreen(conn, active_tournament.id));
69                                 continue;
70                         }
71                         if (*i == widestring("top5chosen")) {
72                                 aux_screen->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
73                                 continue;
74                         }
75                         std::fprintf(stderr, "Foobarbaz?\n");
76                 }
77
78                 /*
79                  * If we still have room, make yet another rotational screen with
80                  * results from previous groups -- otherwise tack them onto the end
81                  * of the auxilliary screens.
82                  */
83                 RotateScreen *finished_groups_screen = aux_screen;
84                 if (screens.size() < 4 && screens.size() != 1) {
85                         finished_groups_screen = new RotateScreen();
86                         screens.push_back(finished_groups_screen);
87                 }
88                         
89                 std::vector<SkeletonGroup> finished_groups;
90                 conn.perform(FetchListOfFinishedGroups(active_tournament.id, &finished_groups));
91                         
92                 for (std::vector<SkeletonGroup>::const_iterator i = finished_groups.begin(); i != finished_groups.end(); ++i) {
93                         finished_groups_screen->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, 0, 1, 1));
94                 }
95         }
96         
97         // hack
98         screens.push_back(NULL);
99         screens.push_back(NULL);
100         screens.push_back(NULL);
101         screens.push_back(NULL);
102
103         if (screens[1] == NULL) {
104                 mainscreen = screens[0];
105         } else {
106                 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
107         }
108 }
109
110 void main_loop(pqxx::connection &conn)
111 {
112         if (active_tournament.id == -1) {
113                 // No active tournament, sleep a second or so and exit
114                 conn.await_notification(1, 0);
115                 return;
116         }
117
118         if (mainscreen && mainscreen->check_invalidated()) {
119                 mainscreen->draw(framebuf, SCREEN_WIDTH, SCREEN_HEIGHT);
120                 ptc_update(framebuf);
121                 conn.await_notification(0, 10000);
122         } else {
123                 ptc_update(framebuf);
124                 conn.await_notification(0, 200000);
125         }
126 }
127
128 int main(int argc, char **argv)
129 {
130         ptc_open("CCBS bigscreen", SCREEN_WIDTH, SCREEN_HEIGHT);
131         
132         try {
133                 init_freetype();
134                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
135                 FlagTrigger tournament_changed(conn, "active_tournament");
136                 FlagTrigger rounds_changed(conn, "active_groups");
137                 
138                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
139                 // (at least currently)
140                 for ( ;; ) {
141                         tournament_changed.reset_flag();
142                         rounds_changed.reset_flag();
143                         init(conn);
144                         do {
145                                 main_loop(conn);
146                                 conn.get_notifs();
147                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
148                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
149                 }
150         } catch (const std::exception &e) {
151                 std::fprintf(stderr, "Exception: %s\n", e.what());
152                 exit(1);
153         }
154         
155         return 0;
156 }