]> git.sesse.net Git - ccbs/blobdiff - bigscreen/fetch_top_scores_for_tournament.cpp
Actually fetch and display the top 10 scores.
[ccbs] / bigscreen / fetch_top_scores_for_tournament.cpp
diff --git a/bigscreen/fetch_top_scores_for_tournament.cpp b/bigscreen/fetch_top_scores_for_tournament.cpp
new file mode 100644 (file)
index 0000000..9c38f51
--- /dev/null
@@ -0,0 +1,36 @@
+#include "fetch_top_scores_for_tournament.h"
+
+// small utility function so we can stick TopScores in a std::set
+bool TopScore::operator< (const TopScore &other) const
+{
+       if (nick != other.nick)
+               return (nick < other.nick);
+       if (title != other.title)
+               return (title < other.title);
+       return (score < other.score);
+}
+
+FetchTopScoresForTournament::FetchTopScoresForTournament(unsigned tournament, unsigned num, std::vector<TopScore> *scores)
+       : tournament(tournament), num(num), scores(scores) {}
+       
+void FetchTopScoresForTournament::operator() (pqxx::transaction<> &t)
+{
+       scores->erase(scores->begin(), scores->end());
+
+       // Again, this will break if a song has more than one short title
+       pqxx::result res( t.exec(
+               "SELECT nick, title, shorttitle, score FROM scores NATURAL JOIN tournaments NATURAL JOIN players NATURAL JOIN songs NATURAL LEFT JOIN songshorttitles WHERE tournament=" +
+                       pqxx::to_string(tournament) + " AND score IS NOT NULL ORDER BY score DESC LIMIT " +
+                       pqxx::to_string(num)) );
+       
+       for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
+               TopScore ts;
+
+               ts.nick = i["nick"].as(ts.nick);
+               ts.title = i["title"].as(ts.title);
+               ts.shorttitle = i["shorttitle"].as(ts.shorttitle);
+               ts.score = i["score"].as(ts.score);
+
+               scores->push_back(ts);
+       }
+}