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