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