62fbd5000859d0fb43b4ca71c17d283ada6b2169
[ccbs] / bigscreen / fetch_group.cpp
1 #include "fetch_group.h"
2
3 FetchGroup::FetchGroup(unsigned tournament, unsigned round, unsigned parallel, Group *group) :
4         tournament(tournament), round(round), parallel(parallel), dest_group(group) {}
5         
6 void FetchGroup::operator() (pqxx::transaction<> &t)
7 {
8         // note: this _will_ break if any song has more than one short title!
9         pqxx::result res( t.exec("SELECT round,parallel,position,playmode,difficulty,position,songnumber,player,nick,song,title,COALESCE(shorttitle,title) AS shorttitle,artist,chosen,score FROM roundparticipation NATURAL JOIN players NATURAL JOIN scores NATURAL LEFT JOIN songs NATURAL LEFT JOIN songshorttitles WHERE " 
10                 "tournament=" + pqxx::to_string(tournament) + " AND " +
11                 "round=" + pqxx::to_string(round) + " AND " +
12                 "parallel=" + pqxx::to_string(parallel) + " " +
13                 "ORDER BY position,songnumber") );
14
15         curr_group.tournament = tournament;
16         curr_group.round = round;
17         curr_group.parallel = parallel;
18         
19         // massage the data we get back into a Group object and children
20         int curr_player = -1;
21         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
22                 if (i["player"].as(curr_player) != curr_player) {
23                         Player p;
24
25                         p.id = i["player"].as(p.id);
26                         p.position = i["position"].as(p.id);
27                         p.nick = i["nick"].as(p.nick);
28                         p.total = 0;
29                         p.rank = 1;
30                         curr_group.players.push_back(p);
31                         
32                         curr_player = i["player"].as(curr_player);
33                 }
34
35                 // note: we _will_ get some duplication here (multiple identical Song
36                 // objects), but it isn't the end of the world
37                 Score sc;
38                 Song so;
39
40                 if (i["song"].is_null()) {
41                         so.id = -1;
42                 } else {
43                         so.id = i["song"].as(so.id);
44                         so.title = i["title"].as(so.title);
45                         so.artist = i["artist"].as(so.artist);
46                         so.short_title = i["shorttitle"].as(so.short_title);
47                 }
48         
49                 sc.song = so;
50                 sc.chosen = i["chosen"].as(sc.chosen);
51
52                 if (i["score"].is_null()) {
53                         sc.score = -1;
54                 } else {
55                         sc.score = i["score"].as(sc.score);
56                         curr_group.players[curr_group.players.size() - 1].total += sc.score;
57                 }
58
59                 curr_group.players[curr_group.players.size() - 1].scores.push_back(sc);
60         }
61 }
62
63 void FetchGroup::OnCommit()
64 {
65         *dest_group = curr_group;
66 }