Actually fetch and display the top 10 scores.
[ccbs] / bigscreen / fetch_top_scores_for_tournament.cpp
1 #include "fetch_top_scores_for_tournament.h"
2
3 // small utility function so we can stick TopScores in a std::set
4 bool TopScore::operator< (const TopScore &other) const
5 {
6         if (nick != other.nick)
7                 return (nick < other.nick);
8         if (title != other.title)
9                 return (title < other.title);
10         return (score < other.score);
11 }
12
13 FetchTopScoresForTournament::FetchTopScoresForTournament(unsigned tournament, unsigned num, std::vector<TopScore> *scores)
14         : tournament(tournament), num(num), scores(scores) {}
15         
16 void FetchTopScoresForTournament::operator() (pqxx::transaction<> &t)
17 {
18         scores->erase(scores->begin(), scores->end());
19
20         // Again, this will break if a song has more than one short title
21         pqxx::result res( t.exec(
22                 "SELECT nick, title, shorttitle, score FROM scores NATURAL JOIN tournaments NATURAL JOIN players NATURAL JOIN songs NATURAL LEFT JOIN songshorttitles WHERE tournament=" +
23                         pqxx::to_string(tournament) + " AND score IS NOT NULL ORDER BY score DESC LIMIT " +
24                         pqxx::to_string(num)) );
25         
26         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
27                 TopScore ts;
28
29                 ts.nick = i["nick"].as(ts.nick);
30                 ts.title = i["title"].as(ts.title);
31                 ts.shorttitle = i["shorttitle"].as(ts.shorttitle);
32                 ts.score = i["score"].as(ts.score);
33
34                 scores->push_back(ts);
35         }
36 }