]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
Make the "top 10 scores" and "top 5 chosen songs" screens only say they're updated...
[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[800 * 600 * 4], screenbuf[800 * 600 * 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));
56                         }
57                 }
58         }
59
60         RotateScreen *aux_screen = new RotateScreen();
61         screens.push_back(aux_screen);
62         
63         conn.perform(FetchAuxilliaryScreens(&aux_screens));
64         for (std::vector<widestring>::const_iterator i = aux_screens.begin(); i != aux_screens.end(); ++i) {
65                 if (*i == widestring("top10scores")) {
66                         aux_screen->add_screen(new Top10ScoreScreen(conn, active_tournament.id));
67                         continue;
68                 }
69                 if (*i == widestring("top5chosen")) {
70                         aux_screen->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
71                         continue;
72                 }
73                 std::fprintf(stderr, "Foobarbaz?\n");
74         }
75
76         // add all finished screens to the auxilliary screens
77         std::vector<SkeletonGroup> finished_groups;
78         conn.perform(FetchListOfFinishedGroups(active_tournament.id, &finished_groups));
79                 
80         for (std::vector<SkeletonGroup>::const_iterator i = finished_groups.begin(); i != finished_groups.end(); ++i) {
81                 aux_screen->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, 0, 1));
82         }
83         
84         // hack
85         screens.push_back(NULL);
86         screens.push_back(NULL);
87         screens.push_back(NULL);
88         screens.push_back(NULL);
89
90         if (screens[1] == NULL) {
91                 mainscreen = screens[0];
92         } else {
93                 mainscreen = new SplitScreen(screens[0], screens[1], screens[2], screens[3]);
94         }
95 }
96
97 void main_loop(pqxx::connection &conn)
98 {
99         if (active_tournament.id == -1) {
100                 // No active tournament, sleep a second or so and exit
101                 conn.await_notification(1, 0);
102                 return;
103         }
104
105         if (mainscreen && mainscreen->check_invalidated()) {
106                 mainscreen->draw(framebuf);
107                 ptc_update(framebuf);
108                 conn.await_notification(0, 10000);
109         } else {
110                 ptc_update(framebuf);
111                 conn.await_notification(0, 200000);
112         }
113 }
114
115 int main(int argc, char **argv)
116 {
117         ptc_open("CCBS bigscreen", 800, 600);
118         
119         try {
120                 init_freetype();
121                 pqxx::connection conn("dbname=ccbs host=altersex.samfundet.no user=ccbs password=GeT|>>B_");
122                 FlagTrigger tournament_changed(conn, "active_tournament");
123                 FlagTrigger rounds_changed(conn, "active_groups");
124                 
125                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
126                 // (at least currently)
127                 for ( ;; ) {
128                         tournament_changed.reset_flag();
129                         rounds_changed.reset_flag();
130                         init(conn);
131                         do {
132                                 main_loop(conn);
133                                 conn.get_notifs();
134                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
135                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
136                 }
137         } catch (const std::exception &e) {
138                 std::fprintf(stderr, "Exception: %s\n", e.what());
139                 exit(1);
140         }
141         
142         return 0;
143 }