Hardcode single-only songs for now.
[ccbs] / bigscreen / fetch_max_score_for_players.cpp
1 #include "fetch_max_score_for_players.h"
2
3 FetchMaxScoreForPlayers::FetchMaxScoreForPlayers(unsigned tournament, unsigned round, std::map<unsigned, unsigned> *scores)
4         : tournament(tournament), round(round), scores(scores) {}
5         
6 void FetchMaxScoreForPlayers::operator() (pqxx::transaction<> &t)
7 {
8         scores->erase(scores->begin(), scores->end());
9         
10         pqxx::result res( t.exec(
11                 "SELECT player,"
12                 "  ("
13                 "     SELECT feetrating"
14                 "     FROM songratings"
15                 "     WHERE machine=( SELECT machine FROM tournaments WHERE tournament=" + pqxx::to_string(tournament) + " ) " // only find songs on the machine we use
16                 "     AND song NOT IN ("                      // not a song that has been in elimination or seeding
17                 "       SELECT song FROM scores "
18                 "         WHERE tournament=" + pqxx::to_string(tournament) +
19                 "         AND song IS NOT NULL"
20                 "         AND parallel=0"
21                 "         AND chosen='f'"
22                 "     )"
23                 "     AND (player,song) NOT IN ("             // not a song the player has chosen before, or is a random song in this round
24                 "       SELECT player,song FROM scores"
25                 "          WHERE tournament=" + pqxx::to_string(tournament) +
26                 "          AND song IS NOT NULL" +
27                 "          AND ( chosen='t' OR round=" + pqxx::to_string(round) + " )"
28                 "       )"
29                 "     AND playmode='single'"
30                 "     ORDER BY feetrating DESC LIMIT 1"      
31                 "  ) * 1000 AS max_score FROM tournamentparticipation") );
32         
33         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
34                 unsigned player, max_score;
35                 player = i["player"].as(player);
36                 max_score = i["max_score"].as(max_score);
37
38                 scores->insert(std::make_pair(player, max_score));
39         }
40 }