]> git.sesse.net Git - ccbs/blob - bigscreen/top5chosenscreen.cpp
702b24fdab551d5e2e8665abfde40c65b8b1f0d4
[ccbs] / bigscreen / top5chosenscreen.cpp
1 #include <cstdio>
2 #include <algorithm>
3
4 #include "resolution.h"
5 #include "top5chosenscreen.h"
6 #include "fonts.h"
7
8 #define RANK_X 30
9 #define SONG_X 70
10 #define FREQUENCY_X (LOGICAL_SCREEN_WIDTH - 55)
11 #define SONG_MAX_WIDTH (LOGICAL_SCREEN_WIDTH - 190)
12
13 Top5ChosenScreen::Top5ChosenScreen(pqxx::connection &conn, unsigned tournament)
14         : conn(conn), tournament(tournament), scores_changed(conn, "scores"), valid(false)
15 {
16 }
17
18 Top5ChosenScreen::~Top5ChosenScreen()
19 {
20 }
21
22 bool Top5ChosenScreen::check_invalidated()
23 {
24         if (!valid)
25                 return true;
26         if (!scores_changed.get_flag())
27                 return false;
28         scores_changed.reset_flag();
29
30         // check that there are indeed changes, otherwise don't bother
31         std::vector<TopChosen> scores;
32         conn.perform(FetchTopChosenSongsForTournament(tournament, 5, &scores));
33
34         for (std::vector<TopChosen>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
35                 if (seen_topchosen.count(*i) == 0) {
36                         valid = false;
37                         return true;
38                 }       
39         }
40         
41         return false;
42 }
43
44 void Top5ChosenScreen::draw(unsigned char *buf, unsigned width, unsigned height)
45 {
46         scores_changed.reset_flag();
47         memset(buf, 0, width * height * 4);
48         set_screen_size(width, height);
49
50         // fetch the top 5 chosen songs
51         std::vector<TopChosen> scores;
52         conn.perform(FetchTopChosenSongsForTournament(tournament, 5, &scores));
53
54         {
55                 unsigned width = my_draw_text("Today's top 5 chosen songs", NULL, 40.0);
56                 my_draw_text("Today's top 5 chosen songs", buf, 40.0, LOGICAL_SCREEN_WIDTH/2 - width/2, 60);
57         }
58
59         // simple headings
60         my_draw_text("Song", buf, 12.0, SONG_X, 100);
61         my_draw_text("Frequency", buf, 12.0, FREQUENCY_X - 35, 100);
62         
63         unsigned row = 1, y = 140;
64         for (std::vector<TopChosen>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
65                 char str[16];
66                 unsigned r = 255, g = 255, b = 255;
67
68                 // print new entries in red
69                 if (seen_topchosen.count(*i) == 0 && seen_topchosen.size() > 0) {
70                         g = b = 0;
71                 }
72
73                 std::sprintf(str, "%u", row++);
74                 unsigned width = my_draw_text(str, NULL, 24.0);
75                 my_draw_text(str, buf, 24.0, RANK_X - width/2, y);
76
77                 if (my_draw_text(i->title, NULL, 24.0) > SONG_MAX_WIDTH) {
78                         my_draw_text(i->shorttitle, buf, 24.0, SONG_X, y, r, g, b);
79                 } else {
80                         my_draw_text(i->title, buf, 24.0, SONG_X, y, r, g, b);
81                 }
82                 
83                 std::sprintf(str, "%u", i->frequency);
84                 width = my_draw_text(str, NULL, 24.0);
85                 my_draw_text(str, buf, 24.0, FREQUENCY_X - width/2, y, r, g, b);
86
87                 y += 40;
88         }
89         
90         valid = true;
91         
92         seen_topchosen.erase(seen_topchosen.begin(), seen_topchosen.end());
93         std::copy(scores.begin(), scores.end(), std::inserter(seen_topchosen, seen_topchosen.end()));
94 }
95