X-Git-Url: https://git.sesse.net/?p=ccbs;a=blobdiff_plain;f=bigscreen%2Ffetch_top_scores_for_tournament.cpp;fp=bigscreen%2Ffetch_top_scores_for_tournament.cpp;h=9c38f51e6742708c128b430a4442a4451515c1f6;hp=0000000000000000000000000000000000000000;hb=0e17c7dcfd783acd4792c392c347e5e523757de3;hpb=5a591d5e5db52bb78fd794342f090c7db66211dd diff --git a/bigscreen/fetch_top_scores_for_tournament.cpp b/bigscreen/fetch_top_scores_for_tournament.cpp new file mode 100644 index 0000000..9c38f51 --- /dev/null +++ b/bigscreen/fetch_top_scores_for_tournament.cpp @@ -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 *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); + } +}