5 #include "groupscreen.h"
6 #include "fetch_group.h"
7 #include "fetch_max_score_for_songs.h"
8 #include "fetch_max_score_for_players.h"
9 #include "fetch_needs_update.h"
10 #include "fetch_highscore.h"
13 GroupScreen::GroupScreen(pqxx::connection &conn, unsigned tournament, unsigned round, unsigned parallel)
14 : tournament(tournament), round(round), parallel(parallel), scores_changed(conn, "scores"), conn(conn), valid(false)
18 GroupScreen::~GroupScreen()
22 bool GroupScreen::check_invalidated()
26 if (!scores_changed.get_flag())
30 conn.perform(FetchNeedsUpdate(last_updated, tournament, round, parallel, &needs_update));
33 scores_changed.reset_flag();
38 void GroupScreen::draw(unsigned char *buf)
40 std::vector<TextDefer> td;
42 scores_changed.reset_flag();
45 * We'll probably need some values from here later on (although not all), just fetch them
46 * all while we're at it.
48 std::map<unsigned, unsigned> song_scores, player_scores;
49 conn.perform(FetchMaxScoreForSongs(tournament, &song_scores));
50 conn.perform(FetchMaxScoreForPlayers(tournament, round, &player_scores));
53 conn.perform(FetchGroup(tournament, round, parallel, &group));
54 gettimeofday(&last_updated, NULL);
56 memset(buf, 0, 800 * 600 * 4);
61 std::sprintf(heading, "Round %u", round);
63 std::sprintf(heading, "Round %u, Group %u", round, parallel);
67 unsigned width = my_draw_text(heading, NULL, 48.0);
68 my_draw_text_deferred(td, heading, 48.0, 800/2 - width/2, 60);
71 // Find out how wide each column has to be. First try unlimited width (ie.
72 // long titles for everything); if that gets too long, try again with short
73 // titles for chosen songs.
74 unsigned width[16], num_scores;
75 unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
77 for (unsigned mode = 0; mode < 2; ++mode) {
78 for (unsigned i = 0; i < 16; ++i)
81 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
83 width[0] = std::max(width[0], my_draw_text(i->nick, NULL, 18.0));
85 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
87 width[col] = std::max(width[col], my_draw_text((mode == 0) ? j->song.title : j->song.short_title, NULL, 12.0) +
90 width[col] = std::max(width[col], my_draw_text(j->song.short_title, NULL, 12.0));
91 width[col] = std::max(width[col], max_num_width);
96 num_scores = group.players[0].scores.size();
98 width[num_scores + 1] = std::max(my_draw_text("Total", NULL, 12.0), max_num_width);
99 width[num_scores + 2] = my_draw_text("Rank", NULL, 12.0);
101 // if we're at long titles and that works, don't try the short ones
104 for (unsigned i = 0; i <= num_scores + 2; ++i)
105 sumwidth += width[i] + 20;
112 * If we have space to go, distribute as much as we can to the chosen song column, so we won't have
113 * total and rank jumping around.
115 if (sumwidth < 780) {
116 int first_chosen_col = -1;
119 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
121 first_chosen_col = col;
126 if (first_chosen_col != -1) {
127 width[first_chosen_col] += 780 - sumwidth;
131 // make column headings from the first player's songs
133 unsigned x = 40 + width[0];
134 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
136 unsigned this_width = my_draw_text(i->song.short_title, NULL, 12.0);
137 my_draw_text_deferred(td, i->song.short_title, 12.0, x + width[col] / 2 - this_width / 2, 100);
139 x += width[col] + 20;
142 my_draw_text_deferred(td, "Total", 12.0, x + width[num_scores + 1] / 2 - my_draw_text("Total", NULL, 12.0) / 2, 100);
143 x += width[num_scores + 1] + 20;
144 my_draw_text_deferred(td, "Rank", 12.0, x + width[num_scores + 2] / 2 - my_draw_text("Rank", NULL, 12.0) / 2, 100);
146 // show all the players and the scores
147 unsigned show_players = std::min(group.players.size(), 9U);
148 unsigned y = (show_players <= 7) ? 140 : (140 - (show_players - 7) * 5);
151 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end() && row < 9; ++i, ++row) {
152 my_draw_text_deferred(td, i->nick, 18.0, 20, y);
154 unsigned x = 40 + width[0];
157 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
159 if (j->score != -1) {
160 std::sprintf(text, "%u", j->score);
163 unsigned this_width = my_draw_text(text, NULL, 22.0);
165 my_draw_text_deferred(td, text, 22.0, x + max_num_width - this_width, y);
167 // draw the long name if we can, otherwise use the short one
168 if (my_draw_text(j->song.title, NULL, 12.0) > (width[col] - 10 - max_num_width)) {
169 my_draw_text_deferred(td, j->song.short_title, 12.0, x + max_num_width + 10, y);
171 my_draw_text_deferred(td, j->song.title, 12.0, x + max_num_width + 10, y);
174 my_draw_text_deferred(td, text, 22.0, x + width[col] / 2 - this_width / 2, y);
176 x += width[col] + 20;
182 std::sprintf(text, "%u", i->total);
184 unsigned this_width = my_draw_text(text, NULL, 22.0);
185 my_draw_text_deferred(td, text, 22.0, x + width[num_scores + 1] / 2 - this_width / 2, y);
186 x += width[num_scores + 1] + 20;
189 if (show_players > 7)
190 y += 40 - (show_players - 7) * 4;
196 * Approximate (but probably working quite well in practice) heuristic
197 * for finding the min and max rank of a player works as follows:
199 * First of all, find out, for each player in the group, what the
200 * maximum remaining score possibly can be (the minimum score is of
201 * course identical to the player's current total). For a random song,
202 * this is of course 1000 * (maximum feet rating) (but of course, that
203 * depends on whether we can play single or double! for now, assume
204 * double is okay, but this logic will be deferred to FetchMaxScore
205 * anyhow); for a random song, we simply pick the highest-ranking song
206 * we can find, EXCEPT those the player has chosen earlier AND the
207 * random songs this round, AND all random songs from elimination rounds
208 * (ie. rounds with only one group). (Phew!) This doesn't solve problems
209 * we'd face with more than one chosen song, but it should be good enough.
211 * After we've found the max and min scores for all players, it's a simple
212 * matter of sorting; the best attainable rank for player X is obtained if
213 * X gets max score and all others get min score, the worst attainable rank
214 * is obtained if X gets min score and all others get max score.
216 std::vector<unsigned> max_score, min_score;
217 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
218 unsigned min_score_tp = 0, max_score_tp = 0;
219 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
220 if (j->score != -1) {
222 min_score_tp += j->score;
223 max_score_tp += j->score;
225 unsigned max_score_this_song;
226 if (j->song.id != -1) {
227 // random song, or we know what song the player picked
228 max_score_this_song = song_scores[j->song.id];
230 max_score_this_song = player_scores[i->id];
232 max_score_tp += max_score_this_song;
235 max_score.push_back(max_score_tp);
236 min_score.push_back(min_score_tp);
239 // now finally find min and max rank, and draw it all
240 y = (show_players <= 7) ? 140 : (140 - (show_players - 7) * 5);
241 for (unsigned i = 0; i < show_players; ++i) {
242 unsigned best_rank = 1, worst_rank = 1;
243 for (unsigned j = 0; j < group.players.size(); ++j) {
247 if (max_score[i] < min_score[j])
249 if (min_score[i] <= max_score[j])
254 if (best_rank == worst_rank)
255 std::sprintf(text, "%u", best_rank);
257 std::sprintf(text, "%u-%u", best_rank, worst_rank);
259 unsigned this_width = my_draw_text(text, NULL, 22.0);
260 my_draw_text_deferred(td, text, 22.0, x + width[num_scores + 2] / 2 - this_width / 2, y);
262 if (show_players > 7)
263 y += 40 - (show_players - 7) * 4;
269 * Next up (at the bottom) is "who's playing, what will he/she be playing, and
270 * optionally, how much to lead/win and how much to secure qualification" (the
271 * last one only in the final round). We assume playing is done in a modified
272 * zigzag; all the random songs are played first in zigzag/wrapping order (player
273 * 1 song 1, player 2 song 2, player 3 song 3, player 1 song 2, player 2 song 3,
274 * player 3 song 1, etc... assuming three songs and three players) and then all
275 * the chosen songs are played (we assume only one chosen song).
277 * The lines are as follows:
281 * High score: <hs> by <hsplayer> at <hsevent>
282 * Needs to lead: <leadscore>
283 * Needs to secure qualification: <qualscore>
284 * Needs to win group: <winscore>
287 /* Find the first player with the fewest songs played. */
288 unsigned min_played_songs = 9999, num_random_songs = 0;
289 Player *next_player = NULL;
290 for (std::vector<Player>::iterator i = group.players.begin(); i != group.players.end(); ++i) {
291 unsigned this_played = 0, this_random_songs = 0;
292 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
299 if (this_played < min_played_songs) {
300 min_played_songs = this_played;
302 num_random_songs = this_random_songs; // should be equal for all
306 /* Find out what song this player is supposed to play next; try random songs first */
307 Score *next_song = NULL;
309 for (unsigned i = 0; i < num_random_songs; ++i) {
310 unsigned j = (i + next_player->position - 1) % num_random_songs;
311 if (next_player->scores[j].score == -1) {
312 next_song = &(next_player->scores[j]);
317 // then all songs, if that didn't work out (slightly icky, but hey)
318 if (next_song == NULL) {
319 for (unsigned i = 0; i < num_scores; ++i) {
320 unsigned j = (i + next_player->position) % num_scores;
321 if (next_player->scores[j].score == -1) {
322 next_song = &(next_player->scores[j]);
328 if (next_song != NULL) {
329 widestring text = widestring("Next player: ") + next_player->nick;
330 unsigned this_width = my_draw_text(text, NULL, 24.0);
331 my_draw_text(text, buf, 24.0, 400 - this_width/2, 420);
333 if (next_song->song.id != -1) {
334 this_width = my_draw_text(next_song->song.title, NULL, 20.0);
335 my_draw_text(next_song->song.title, buf, 20.0, 400 - this_width/2, 457);
338 conn.perform(FetchHighscore(next_song->song.id, &hs));
340 if (hs.score != -1) {
341 text = widestring("High score: ") + widestring(pqxx::to_string(hs.score)) +
342 widestring(", by ") + hs.nick + widestring(" in ") + hs.tournament_name;
343 this_width = my_draw_text(text, NULL, 16.0);
344 my_draw_text(text, buf, 16.0, 400 - this_width/2, 487);
348 // only show lead/win/qualify for the last song
349 if (min_played_songs == num_scores - 1) {
351 * Find out how much we need to lead, how much we need to be guaranteed
352 * to win the group, and how much we need to secure qualification. (FIXME:
353 * do the last one :-) )
356 // find the best score we can get
357 unsigned max_score_this_song;
358 if (next_song->song.id != -1) {
359 // random song, or we know what song the player picked
360 max_score_this_song = song_scores[next_song->song.id];
362 max_score_this_song = player_scores[next_player->id];
367 // see what score this player must beat to lead
368 unsigned lead_beat = 0, win_beat = 0;
369 for (unsigned i = 0; i < group.players.size(); ++i) {
370 if (group.players[i].id == next_player->id)
373 lead_beat = std::max(lead_beat, group.players[i].total);
376 // find the best max score among the others
377 for (unsigned i = 0; i < group.players.size(); ++i) {
378 if (group.players[i].id == next_player->id)
381 win_beat = std::max(win_beat, max_score[i]);
385 * There's a somewhat subtle point here. Normally, what a player would be interested in
386 * with regard to qualification would be a set of three values:
388 * 1. How much is the absolute minimum required to qualify, given that all others
390 * 2. How much will give a reasonable chance of qualifying, given the expected performance
392 * 3. How much will be enough to secure qualification, no matter what?
394 * Given perfect guessing, #2 would be "how much is needed to qualify"; however, it is
395 * completely impossible to give an exact value for that, and we're not into the guessing
396 * games. :-) #1 is often so low it's completely unrealistic (ie. far enough from #2 that
397 * it's not interesting), but #3, the most conservative estimate, is often a good measure.
398 * #3 is "how much is needed to _secure_ qualification", and that is usually what we
399 * print out when it's possible.
401 * However, in a few situations, #1 and #3 will be the exact same value, from which it
402 * follows (from the squeeze law, or just common sense :-) ) that #2 will be the same
403 * value as #1 and #3. (This usually happens near or at the end of a group.) In that
404 * case, we know the value we seek (ie. "how much is needed to qualify"), so we drop
405 * the word "secure" and just print it as-is.
407 * To find #1 and #3, we sort and pick out the values we need to beat in the best and
410 int qualify_beat_worst_case = -1, qualify_beat_best_case = -1;
412 if (group.num_qualifying > 0) {
413 std::vector<unsigned> tmp;
415 for (unsigned i = 0; i < group.players.size(); ++i) {
416 if (group.players[i].id == next_player->id)
418 tmp.push_back(max_score[i]);
420 std::sort(tmp.begin(), tmp.end());
421 qualify_beat_worst_case = tmp[tmp.size() - group.num_qualifying];
423 std::vector<unsigned> tmp2;
424 for (unsigned i = 0; i < group.players.size(); ++i) {
425 if (group.players[i].id == next_player->id)
427 tmp2.push_back(min_score[i]);
430 std::sort(tmp2.begin(), tmp2.end());
431 qualify_beat_best_case = tmp2[tmp2.size() - group.num_qualifying];
434 // print out the lines we can attain
435 if (next_player->total + max_score_this_song > lead_beat && (lead_beat != win_beat)) {
436 int lead_need = std::max(lead_beat - next_player->total + 1, 0U);
439 text = widestring("Needs to lead: ") + widestring(pqxx::to_string(lead_need));
440 this_width = my_draw_text(text, NULL, 18.0);
441 my_draw_text(text, buf, 18.0, 400 - this_width/2, y);
447 if (next_player->total + max_score_this_song > win_beat) {
448 int win_need = std::max(win_beat - next_player->total + 1, 0U);
451 text = widestring("Needs to win: ") + widestring(pqxx::to_string(win_need));
453 this_width = my_draw_text(text, NULL, 18.0);
454 my_draw_text(text, buf, 18.0, 400 - this_width/2, y);
460 if (group.num_qualifying > 0 &&
461 next_player->total + max_score_this_song > unsigned(qualify_beat_worst_case) &&
462 (unsigned(qualify_beat_worst_case) != win_beat)) {
463 int qual_need = std::max(qualify_beat_worst_case - next_player->total + 1, 0U);
466 if (qualify_beat_worst_case == qualify_beat_best_case) {
467 text = widestring("Needs to qualify: ") + widestring(pqxx::to_string(qual_need));
469 text = widestring("Needs to secure qualification: ") + widestring(pqxx::to_string(qual_need));
472 this_width = my_draw_text(text, NULL, 18.0);
473 my_draw_text(text, buf, 18.0, 400 - this_width/2, y);
482 draw_all_deferred_text(buf, td, last_text);