4 #include "groupscreen.h"
5 #include "fetch_group.h"
6 #include "fetch_max_score_for_song.h"
7 #include "fetch_max_score_for_player.h"
8 #include "fetch_needs_update.h"
11 GroupScreen::GroupScreen(pqxx::connection &conn, unsigned tournament, unsigned round, unsigned parallel)
12 : tournament(tournament), round(round), parallel(parallel), scores_changed(conn, "scores"), conn(conn), valid(false)
16 GroupScreen::~GroupScreen()
20 bool GroupScreen::check_invalidated()
24 if (!scores_changed.get_flag())
28 conn.perform(FetchNeedsUpdate(last_updated, tournament, round, parallel, &needs_update));
31 scores_changed.reset_flag();
36 void GroupScreen::draw(unsigned char *buf)
38 scores_changed.reset_flag();
41 conn.perform(FetchGroup(tournament, round, parallel, &group));
42 gettimeofday(&last_updated, NULL);
44 memset(buf, 0, 800 * 600 * 4);
49 sprintf(heading, "Round %u", round);
51 sprintf(heading, "Round %u, Group %u", round, parallel);
55 unsigned width = my_draw_text(heading, NULL, 48.0);
56 my_draw_text(heading, buf, 48.0, 800/2 - width/2, 60);
59 // Find out how wide each column has to be. First try unlimited width (ie.
60 // long titles for everything); if that gets too long, try again with short
61 // titles for chosen songs.
62 unsigned width[16], num_scores;
63 unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
65 for (mode = 0; mode < 2; ++mode) {
66 for (unsigned i = 0; i < 16; ++i)
69 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
71 width[0] = std::max(width[0], my_draw_text(i->nick, NULL, 18.0));
73 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
75 width[col] = std::max(width[col], my_draw_text((mode == 0) ? j->song.title : j->song.short_title, NULL, 12.0) +
78 width[col] = std::max(width[col], my_draw_text(j->song.short_title, NULL, 12.0));
79 width[col] = std::max(width[col], max_num_width);
84 num_scores = group.players[0].scores.size();
86 width[num_scores + 1] = std::max(my_draw_text("Total", NULL, 12.0), max_num_width);
87 width[num_scores + 2] = my_draw_text("Rank", NULL, 12.0);
89 // if we're at long titles and that works, don't try the short ones
91 unsigned sumwidth = 0;
92 for (unsigned i = 0; i <= num_scores + 2; ++i)
93 sumwidth += width[i] + 20;
100 // make column headings from the first player's songs
102 unsigned x = 40 + width[0];
103 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
105 unsigned this_width = my_draw_text(i->song.short_title, NULL, 12.0);
106 my_draw_text(i->song.short_title, buf, 12.0, x + width[col] / 2 - this_width / 2, 100);
108 x += width[col] + 20;
111 my_draw_text("Total", buf, 12.0, x + width[num_scores + 1] / 2 - my_draw_text("Total", NULL, 12.0) / 2, 100);
112 x += width[num_scores + 1] + 20;
113 my_draw_text("Rank", buf, 12.0, x + width[num_scores + 2] / 2 - my_draw_text("Rank", NULL, 12.0) / 2, 100);
115 // show all the players and the scores
117 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
118 my_draw_text(i->nick, buf, 18.0, 20, y);
120 unsigned x = 40 + width[0];
123 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
125 sprintf(text, "%u", j->score);
127 unsigned this_width = my_draw_text(text, NULL, 22.0);
129 if (j->score != -1) {
130 my_draw_text(text, buf, 22.0, x + max_num_width - this_width, y);
132 my_draw_text((mode == 0) ? j->song.title : j->song.short_title, buf, 12.0, x + max_num_width + 10, y);
134 if (j->score != -1) {
135 my_draw_text(text, buf, 22.0, x + width[col] / 2 - this_width / 2, y);
138 x += width[col] + 20;
144 sprintf(text, "%u", i->total);
146 unsigned this_width = my_draw_text(text, NULL, 22.0);
147 my_draw_text(text, buf, 22.0, x + width[num_scores + 1] / 2 - this_width / 2, y);
148 x += width[num_scores + 1] + 20;
155 * Approximate (but probably working quite well in practice) heuristic
156 * for finding the min and max rank of a player works as follows:
158 * First of all, find out, for each player in the group, what the
159 * maximum remaining score possibly can be (the minimum score is of
160 * course identical to the player's current total). For a random song,
161 * this is of course 1000 * (maximum feet rating) (but of course, that
162 * depends on whether we can play single or double! for now, assume
163 * double is okay, but this logic will be deferred to FetchMaxScore
164 * anyhow); for a random song, we simply pick the highest-ranking song
165 * we can find, EXCEPT those the player has chosen earlier AND the
166 * random songs this round, AND all random songs from elimination rounds
167 * (ie. rounds with only one group). (Phew!) This doesn't solve problems
168 * we'd face with more than one chosen song, but it should be good enough.
170 * After we've found the max and min scores for all players, it's a simple
171 * matter of sorting; the best attainable rank for player X is obtained if
172 * X gets max score and all others get min score, the worst attainable rank
173 * is obtained if X gets min score and all others get max score.
175 * This is a bit SQL-heavy, but heck...
177 std::vector<unsigned> max_score, min_score;
178 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
179 unsigned min_score_tp = 0, max_score_tp = 0;
180 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
181 if (j->score != -1) {
183 min_score_tp += j->score;
184 max_score_tp += j->score;
186 unsigned max_score_this_song;
187 if (j->song.id != -1) {
188 // random song, or we know what song the player picked
189 conn.perform(FetchMaxScoreForSong(tournament, j->song.id, &max_score_this_song));
191 conn.perform(FetchMaxScoreForPlayer(tournament, i->id, round, &max_score_this_song));
193 max_score_tp += max_score_this_song;
196 max_score.push_back(max_score_tp);
197 min_score.push_back(min_score_tp);
200 // now finally find min and max rank, and draw it all
202 for (unsigned i = 0; i < group.players.size(); ++i) {
203 unsigned best_rank = 1, worst_rank = 1;
204 for (unsigned j = 0; j < group.players.size(); ++j) {
208 if (max_score[i] < min_score[j])
210 if (min_score[i] <= max_score[j])
215 if (best_rank == worst_rank)
216 std::sprintf(text, "%u", best_rank);
218 std::sprintf(text, "%u-%u", best_rank, worst_rank);
220 unsigned this_width = my_draw_text(text, NULL, 22.0);
221 my_draw_text(text, buf, 22.0, x + width[num_scores + 2] / 2 - this_width / 2, y);