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