791c785ec61b4fc0fd30bb5db1bc39a2c6cd809f
[ccbs] / bigscreen / top10scorescreen.cpp
1 #include <cstdio>
2 #include <algorithm>
3
4 #include "top10scorescreen.h"
5 #include "fonts.h"
6
7 Top10ScoreScreen::Top10ScoreScreen(pqxx::connection &conn, unsigned tournament)
8         : conn(conn), tournament(tournament), scores_changed(conn, "scores"), valid(false)
9 {
10 }
11
12 Top10ScoreScreen::~Top10ScoreScreen()
13 {
14 }
15
16 bool Top10ScoreScreen::check_invalidated()
17 {
18         if (!valid)
19                 return true;
20         if (!scores_changed.get_flag())
21                 return false;
22
23         return true;
24 }
25
26 void Top10ScoreScreen::draw(unsigned char *buf)
27 {
28         scores_changed.reset_flag();
29         memset(buf, 0, 800 * 600 * 4);
30
31         // fetch the top 10 scores
32         std::vector<TopScore> scores;
33         conn.perform(FetchTopScoresForTournament(tournament, 10, &scores));
34
35         {
36                 unsigned width = my_draw_text("Today's top 10 scores", NULL, 40.0);
37                 my_draw_text("Today's top 10 scores", buf, 40.0, 800/2 - width/2, 60);
38         }
39
40         // simple headings
41         my_draw_text("Player", buf, 12.0, 70, 100);
42         my_draw_text("Song", buf, 12.0, 250, 100);
43         my_draw_text("Score", buf, 12.0, 710, 100);
44         
45         unsigned row = 1, y = 140;
46         for (std::vector<TopScore>::const_iterator i = scores.begin(); i != scores.end(); ++i) {
47                 char str[16];
48                 unsigned r = 255, g = 255, b = 255;
49
50                 // print new entries in red
51                 if (seen_topscore.count(*i) == 0 && seen_topscore.size() > 0) {
52                         g = b = 0;
53                 }
54
55                 std::sprintf(str, "%u", row++);
56                 unsigned width = my_draw_text(str, NULL, 24.0);
57                 my_draw_text(str, buf, 24.0, 30 - width/2, y);
58
59                 my_draw_text(i->nick, buf, 24.0, 70, y, r, g, b);
60
61                 if (my_draw_text(i->title, NULL, 24.0) > 430) {
62                         my_draw_text(i->shorttitle, buf, 24.0, 250, y, r, g, b);
63                 } else {
64                         my_draw_text(i->title, buf, 24.0, 250, y, r, g, b);
65                 }
66                 
67                 std::sprintf(str, "%u", i->score);
68                 width = my_draw_text(str, NULL, 24.0);
69                 my_draw_text(str, buf, 24.0, 728 - width/2, y, r, g, b);
70
71                 y += 40;
72         }
73         
74         valid = true;
75         
76         seen_topscore.erase(seen_topscore.begin(), seen_topscore.end());
77         std::copy(scores.begin(), scores.end(), std::inserter(seen_topscore, seen_topscore.end()));
78 }
79