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