]> git.sesse.net Git - ccbs/blob - bigscreen/top10scorescreen.cpp
a024e9878d3cf3708d635b2550ba08a26491d996
[ccbs] / bigscreen / top10scorescreen.cpp
1 #include <cstdio>
2 #include <algorithm>
3
4 #include "resolution.h"
5 #include "top10scorescreen.h"
6 #include "fonts.h"
7 #include "theme.h"
8
9 #define RANK_X 30
10 #define PLAYER_X 70
11 #define SONG_X 370
12 #define SCORE_X (LOGICAL_SCREEN_WIDTH - 72)
13 #define SONG_MAX_WIDTH (LOGICAL_SCREEN_WIDTH - 480)
14
15 Top10ScoreScreen::Top10ScoreScreen(pqxx::connection &conn, unsigned tournament)
16         : conn(conn), tournament(tournament), scores_changed(conn, "scores"), valid(false)
17 {
18 }
19
20 Top10ScoreScreen::~Top10ScoreScreen()
21 {
22 }
23
24 bool Top10ScoreScreen::check_invalidated()
25 {
26         if (!valid)
27                 return true;
28         if (!scores_changed.get_flag())
29                 return false;
30         scores_changed.reset_flag();
31
32         // check that there are indeed changes, otherwise don't bother
33         std::vector<TopScore> scores;
34         conn.perform(FetchTopScoresForTournament(tournament, 10, &scores));
35         
36         for (std::vector<TopScore>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
37                 if (seen_topscore.count(*i) == 0) {
38                         valid = false;
39                         return true;
40                 }
41         }
42
43         return false;
44 }
45
46 void Top10ScoreScreen::draw(unsigned char *buf, unsigned width, unsigned height)
47 {
48         scores_changed.reset_flag();
49         fill_background(buf, "top10scorescreen", width, height);
50         set_screen_size(width, height);
51
52         // fetch the top 10 scores
53         std::vector<TopScore> scores;
54         conn.perform(FetchTopScoresForTournament(tournament, 10, &scores));
55
56         {
57                 unsigned width = my_draw_text("Today's top 10 scores", NULL, 40.0, "mainheading");
58                 my_draw_text("Today's top 10 scores", buf, 40.0, "mainheading", LOGICAL_SCREEN_WIDTH/2 - width/2, 60);
59         }
60
61         // simple headings
62         my_draw_text("Player", buf, 12.0, "columnheading", PLAYER_X, 100);
63         my_draw_text("Song", buf, 12.0, "columnheading", SONG_X, 100);
64         width = my_draw_text("Score", NULL, 12.0, "columnheading");
65         my_draw_text("Score", buf, 12.0, "columnheading", SCORE_X - width/2, 100);
66         
67         unsigned row = 1, y = 140;
68         for (std::vector<TopScore>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
69                 char str[16];
70                 std::string theme_element = "data";
71                 std::string heading_theme_element = "rowheading";
72
73                 // print new entries in red
74                 if (seen_topscore.count(*i) == 0 && seen_topscore.size() > 0) {
75                         theme_element = "freshdata";
76                         heading_theme_element = "freshrowheading";
77                 }
78
79                 std::sprintf(str, "%u", row++);
80                 unsigned width = my_draw_text(str, NULL, 24.0, heading_theme_element);
81                 my_draw_text(str, buf, 24.0, heading_theme_element, RANK_X - width/2, y);
82
83                 my_draw_text(i->nick, buf, 24.0, theme_element, PLAYER_X, y);
84
85                 if (my_draw_text(i->title, NULL, 24.0, theme_element) > SONG_MAX_WIDTH &&
86                     !i->shorttitle.empty()) {
87                         my_draw_text(i->shorttitle, buf, 24.0, theme_element, SONG_X, y);
88                 } else {
89                         my_draw_text(i->title, buf, 24.0, theme_element, SONG_X, y);
90                 }
91                 
92                 std::sprintf(str, "%u", i->score);
93                 width = my_draw_text(str, NULL, 24.0, theme_element);
94                 my_draw_text(str, buf, 24.0, theme_element, SCORE_X - width/2, y);
95
96                 y += 40;
97         }
98         
99         valid = true;
100         
101         seen_topscore.erase(seen_topscore.begin(), seen_topscore.end());
102         std::copy(scores.begin(), scores.end(), std::inserter(seen_topscore, seen_topscore.end()));
103 }
104
105 int Top10ScoreScreen::get_priority()
106 {
107         return 5;
108 }