]> git.sesse.net Git - ccbs/blob - bigscreen/ccbs_bigscreen.cpp
Various changes and fixes for CC1.
[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         RotateScreen *rs = new RotateScreen();
40         mainscreen = rs;
41         
42         conn.perform(FetchCurrentTournament(&active_tournament));
43         conn.perform(FetchListOfActiveGroups(&active_groups));
44
45         if (active_tournament.id == -1) {
46                 std::fprintf(stderr, "No active tournament\n");
47         } else {
48                 std::fprintf(stderr, "Current tournament is %d\n", active_tournament.id);
49
50                 for (std::vector<SkeletonGroup>::const_iterator i = active_groups.begin(); i != active_groups.end(); ++i) {
51                         std::fprintf(stderr, "tourn: %u  round: %u  parallel: %u  num_machines: %u\n",
52                                 i->tournament, i->round, i->parallel, i->num_machines);
53
54                         // memory leaks here?
55                         for (unsigned j = 0; j < i->num_machines; ++j) {
56                                 rs->add_screen(new GroupScreen(conn, i->tournament, i->round, i->parallel, j, i->num_machines, i->players_per_machine));
57                         }
58                 }
59         }
60
61         {
62                 conn.perform(FetchAuxilliaryScreens(&aux_screens));
63                 for (std::vector<widestring>::const_iterator i = aux_screens.begin(); i != aux_screens.end(); ++i) {
64                         if (*i == widestring("top10scores")) {
65                                 rs->add_screen(new Top10ScoreScreen(conn, active_tournament.id));
66                                 continue;
67                         }
68                         if (*i == widestring("top5chosen")) {
69                                 rs->add_screen(new Top5ChosenScreen(conn, active_tournament.id));
70                                 continue;
71                         }
72                 }
73         }
74 }
75
76 void main_loop(pqxx::connection &conn)
77 {
78         if (active_tournament.id == -1) {
79                 // No active tournament, sleep a second or so and exit
80                 conn.await_notification(1, 0);
81                 return;
82         }
83
84         if (mainscreen && mainscreen->check_invalidated()) {
85                 mainscreen->draw(framebuf, SCREEN_WIDTH, SCREEN_HEIGHT);
86                 ptc_update(framebuf);
87                 conn.await_notification(0, 10000);
88         } else {
89                 ptc_update(framebuf);
90                 conn.await_notification(0, 200000);
91         }
92 }
93
94 int main(int argc, char **argv)
95 {
96         ptc_open("CCBS bigscreen", SCREEN_WIDTH, SCREEN_HEIGHT);
97         
98         try {
99                 init_freetype();
100                 pqxx::connection conn("dbname=ccbs host=www.positivegaming.com user=ccbs password=GeT|>>B_");
101                 FlagTrigger tournament_changed(conn, "active_tournament");
102                 FlagTrigger rounds_changed(conn, "active_groups");
103                 
104                 // when active_tournament or active_rounds is changed, we destroy everything and start from scratch
105                 // (at least currently)
106                 for ( ;; ) {
107                         tournament_changed.reset_flag();
108                         rounds_changed.reset_flag();
109                         init(conn);
110                         do {
111                                 main_loop(conn);
112                                 conn.get_notifs();
113                         } while (!tournament_changed.get_flag() && !rounds_changed.get_flag());
114                         std::fprintf(stderr, "active_tournament or active_groups changed, resetting...\n");
115                 }
116         } catch (const std::exception &e) {
117                 std::fprintf(stderr, "Exception: %s\n", e.what());
118                 exit(1);
119         }
120         
121         return 0;
122 }