Only allocate space for four digits in the score screen -- if somebody actually gets...
[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,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.nick = i["nick"].as(p.nick);
27                         p.total = 0;
28                         p.rank = 1;
29                         curr_group.players.push_back(p);
30                         
31                         curr_player = i["player"].as(curr_player);
32                 }
33
34                 // note: we _will_ get some duplication here (multiple identical Song
35                 // objects), but it isn't the end of the world
36                 Score sc;
37                 Song so;
38
39                 if (i["song"].is_null()) {
40                         so.id = -1;
41                 } else {
42                         so.id = i["song"].as(so.id);
43                         so.title = i["title"].as(so.title);
44                         so.artist = i["artist"].as(so.artist);
45                         so.short_title = i["shorttitle"].as(so.short_title);
46                 }
47         
48                 sc.song = so;
49                 sc.chosen = i["chosen"].as(sc.chosen);
50
51                 if (i["score"].is_null()) {
52                         sc.score = -1;
53                 } else {
54                         sc.score = i["score"].as(sc.score);
55                 }
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 }