]> git.sesse.net Git - ccbs/blob - bigscreen/fetch_group.cpp
e1d0112160f2fa6f9e85d692e731f5f0e32fd9d1
[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         pqxx::result res( t.exec("SELECT round,parallel,position,playmode,difficulty,songnumber,player,nick,song,title,artist,chosen,score FROM roundparticipation NATURAL JOIN players NATURAL JOIN scores NATURAL LEFT JOIN songs WHERE " 
9                 "tournament=" + pqxx::to_string(tournament) + " AND " +
10                 "round=" + pqxx::to_string(round) + " AND " +
11                 "parallel=" + pqxx::to_string(parallel) + " " +
12                 "ORDER BY position,songnumber") );
13
14         curr_group.tournament = tournament;
15         curr_group.round = round;
16         curr_group.parallel = parallel;
17         
18         // massage the data we get back into a Group object and children
19         int curr_player = -1;
20         for (pqxx::result::const_iterator i = res.begin(); i != res.end(); ++i) {
21                 if (i["player"].as(curr_player) != curr_player) {
22                         Player p;
23
24                         p.id = i["player"].as(p.id);
25                         p.nick = i["nick"].as(p.nick);
26                         p.total = 0;
27                         p.rank = 1;
28                         curr_group.players.push_back(p);
29                         
30                         curr_player = i["player"].as(curr_player);
31                 }
32
33                 // note: we _will_ get some duplication here (multiple identical Song
34                 // objects), but it isn't the end of the world
35                 Score sc;
36                 Song so;
37
38                 so.id = i["song"].as(so.id);
39                 so.title = i["song"].as(so.title);
40                 so.artist = i["song"].as(so.artist);
41         
42                 sc.song = so;
43                 sc.chosen = i["chosen"].as(sc.chosen);
44                 sc.score = i["score"].as(sc.score);
45                 
46                 curr_group.players[curr_group.players.size() - 1].scores.push_back(sc);
47         }
48 }
49
50 void FetchGroup::OnCommit()
51 {
52         *dest_group = curr_group;
53 }