]> git.sesse.net Git - ccbs/blob - bigscreen/fetch_group.cpp
7962897ca328a7eb9a7f2fae5a04ebfe873d03e6
[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                 if (i["song"].is_null()) {
39                         so.id = -1;
40                 } else {
41                         so.id = i["song"].as(so.id);
42                         so.title = i["song"].as(so.title);
43                         so.artist = i["song"].as(so.artist);
44                 }
45         
46                 sc.song = so;
47                 sc.chosen = i["chosen"].as(sc.chosen);
48
49                 if (i["score"].is_null()) {
50                         sc.score = -1;
51                 } else {
52                         sc.score = i["score"].as(sc.score);
53                 }
54
55                 std::printf("score: %u\n", sc.score);
56                 
57                 curr_group.players[curr_group.players.size() - 1].scores.push_back(sc);
58         }
59 }
60
61 void FetchGroup::OnCommit()
62 {
63         *dest_group = curr_group;
64 }