5 #include "resolution.h"
6 #include "groupscreen.h"
7 #include "fetch_group.h"
8 #include "fetch_max_score_for_songs.h"
9 #include "fetch_max_score_for_players.h"
10 #include "fetch_needs_update.h"
11 #include "fetch_highscore.h"
14 GroupScreen::GroupScreen(pqxx::connection &conn, unsigned tournament, unsigned round, unsigned parallel, unsigned machine, unsigned num_machines, unsigned players_per_machine)
15 : tournament(tournament), round(round), parallel(parallel), machine(machine), num_machines(num_machines), players_per_machine(players_per_machine), scores_changed(conn, "scores"), conn(conn), valid(false)
19 GroupScreen::~GroupScreen()
23 bool GroupScreen::check_invalidated()
27 if (!scores_changed.get_flag())
31 conn.perform(FetchNeedsUpdate(last_updated, tournament, round, parallel, &needs_update));
34 scores_changed.reset_flag();
39 unsigned GroupScreen::get_show_players(const Group &group)
41 unsigned num_players_this_machine = (group.players.size() + num_machines - machine - 1) / num_machines;
42 return std::min(num_players_this_machine, 9U);
45 void GroupScreen::draw_main_heading(std::vector<TextDefer> &td)
48 if (num_machines == 1) {
50 std::sprintf(heading, "Round %u", round);
52 std::sprintf(heading, "Round %u, Group %u", round, parallel);
56 std::sprintf(heading, "Round %u, Machine %u", round, machine + 1);
58 std::sprintf(heading, "Round %u, Group %u, Machine %u", round, parallel, machine + 1);
62 unsigned width = my_draw_text(heading, NULL, 40.0);
63 my_draw_text_deferred(td, heading, 40.0, LOGICAL_SCREEN_WIDTH/2 - width/2, 60);
66 // make column headings from the first player's songs
67 void GroupScreen::draw_column_headings(std::vector<TextDefer> &td, const Group &group, const std::vector<unsigned> &colwidth)
69 unsigned num_scores = group.players[0].scores.size();
72 unsigned x = 40 + colwidth[0];
73 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
75 unsigned this_width = my_draw_text(i->song.short_title, NULL, 12.0);
76 my_draw_text_deferred(td, i->song.short_title, 12.0, x + colwidth[col] / 2 - this_width / 2, 100);
78 x += colwidth[col] + 20;
81 my_draw_text_deferred(td, "Total", 12.0, x + colwidth[num_scores + 1] / 2 - my_draw_text("Total", NULL, 12.0) / 2, 100);
82 x += colwidth[num_scores + 1] + 20;
83 my_draw_text_deferred(td, "Rank", 12.0, x + colwidth[num_scores + 2] / 2 - my_draw_text("Rank", NULL, 12.0) / 2, 100);
86 // show all the players and the scores
87 void GroupScreen::draw_scores(std::vector<TextDefer> &td, const Group &group, const std::vector<unsigned> &colwidth)
89 unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
90 unsigned num_scores = group.players[0].scores.size();
91 unsigned show_players = get_show_players(group);
92 unsigned y = (show_players <= 7) ? 140 : (140 - (show_players - 7) * 5);
94 unsigned row = 0, m = 0, x;
95 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end() && row < 9; ++i) {
96 if (m++ % num_machines != machine)
99 my_draw_text_deferred(td, i->nick, 18.0, 20, y);
101 x = 40 + colwidth[0];
104 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
106 if (j->score != -1) {
107 std::sprintf(text, "%u", j->score);
110 unsigned this_width = my_draw_text(text, NULL, 22.0);
112 my_draw_text_deferred(td, text, 22.0, x + max_num_width - this_width, y);
114 // draw the long name if we can, otherwise use the short one
115 if (my_draw_text(j->song.title, NULL, 12.0) > (colwidth[col] - 10 - max_num_width)) {
116 my_draw_text_deferred(td, j->song.short_title, 12.0, x + max_num_width + 10, y);
118 my_draw_text_deferred(td, j->song.title, 12.0, x + max_num_width + 10, y);
121 my_draw_text_deferred(td, text, 22.0, x + colwidth[col] / 2 - this_width / 2, y);
123 x += colwidth[col] + 20;
129 std::sprintf(text, "%u", i->total);
131 unsigned this_width = my_draw_text(text, NULL, 22.0);
132 my_draw_text_deferred(td, text, 22.0, x + colwidth[num_scores + 1] / 2 - this_width / 2, y);
133 x += colwidth[num_scores + 1] + 20;
136 if (show_players > 7)
137 y += 40 - (show_players - 7) * 4;
145 * Find out how wide each column has to be. First try unlimited width (ie.
146 * long titles for everything); if that gets too long, try again with short
147 * titles for chosen songs.
149 void GroupScreen::find_column_widths(const Group &group, std::vector<unsigned> &colwidth)
152 unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
153 unsigned sumcolwidth;
155 for (unsigned mode = 0; mode < 2; ++mode) {
156 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
159 if (colwidth.size() == 0)
160 colwidth.push_back(0);
162 colwidth[0] = std::max(colwidth[0], my_draw_text(i->nick, NULL, 18.0));
164 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
165 if (colwidth.size() < col+1)
166 colwidth.push_back(0);
169 colwidth[col] = std::max(colwidth[col], my_draw_text((mode == 0) ? j->song.title : j->song.short_title, NULL, 12.0) +
172 colwidth[col] = std::max(colwidth[col], my_draw_text(j->song.short_title, NULL, 12.0));
173 colwidth[col] = std::max(colwidth[col], max_num_width);
178 num_scores = group.players[0].scores.size();
180 if (colwidth.size() < num_scores + 2) {
181 colwidth.push_back(0);
182 colwidth.push_back(0);
185 colwidth[num_scores + 1] = std::max(my_draw_text("Total", NULL, 12.0), max_num_width);
186 colwidth[num_scores + 2] = my_draw_text("Rank", NULL, 12.0);
188 // if we're at long titles and that works, don't try the short ones
191 for (unsigned i = 0; i <= num_scores + 2; ++i)
192 sumcolwidth += colwidth[i] + 20;
194 if (sumcolwidth < 780)
199 * If we have space to go, distribute as much as we can to the chosen song column, so we won't have
200 * total and rank jumping around.
202 if (sumcolwidth < 780) {
203 int first_chosen_col = -1;
206 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
208 first_chosen_col = col;
213 if (first_chosen_col != -1) {
214 colwidth[first_chosen_col] += 780 - sumcolwidth;
219 /* Find the first player with the fewest songs played and part of this machine. */
220 const Player *GroupScreen::get_next_player(const Group &group)
222 unsigned min_played_songs = 9999;
223 const Player *next_player = NULL;
225 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
226 unsigned this_played = 0;
227 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j) {
232 if ((m++ % num_machines == machine) && this_played < min_played_songs) {
233 min_played_songs = this_played;
242 * At the bottom, for a single player, is "who's playing, what will he/she be
243 * playing, and optionally, how much to lead/win and how much to secure
244 * qualification" (the last one only in the final round). We assume playing is
245 * done in a modified zigzag; all the random songs are played first in
246 * zigzag/wrapping order (player 1 song 1, player 2 song 2, player 3 song 3,
247 * player 1 song 2, player 2 song 3, player 3 song 1, etc... assuming three
248 * songs and three players) and then all the chosen songs are played (we assume
249 * only one chosen song).
251 * The lines are as follows:
255 * High score: <hs> by <hsplayer> at <hsevent>
256 * Needs to lead: <leadscore>
257 * Needs to secure qualification: <qualscore>
258 * Needs to win group: <winscore>
260 void GroupScreen::draw_next_up_single(unsigned char *buf, const Group &group, const std::vector<unsigned> &colwidth,
261 std::map<unsigned, unsigned> &song_scores, std::map<unsigned, unsigned> &player_scores,
262 const std::vector<unsigned> &max_score, const std::vector<unsigned> &min_score)
264 unsigned num_scores = group.players[0].scores.size();
266 // Find out how many random songs there are (equal for all players).
267 unsigned num_random_songs = 0;
268 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i) {
274 * Find out which player is next, and what song he she is supposed to play. First
277 const Player *next_player = get_next_player(group);
278 const Score *next_song = NULL;
279 unsigned num_played = 0; // will not always be completely accurate, but always as accurate as we need it :-)
281 for (unsigned i = 0; i < num_random_songs; ++i) {
282 unsigned j = (i + next_player->position - 1) % num_random_songs;
283 if (next_player->scores[j].score == -1) {
284 next_song = &(next_player->scores[j]);
291 // then all songs, if that didn't work out (slightly icky, but hey)
292 if (next_song == NULL) {
293 for (unsigned i = 0; i < num_scores; ++i) {
294 unsigned j = (i + next_player->position) % num_scores;
295 if (next_player->scores[j].score == -1) {
296 next_song = &(next_player->scores[j]);
304 if (next_song != NULL) {
305 widestring text = widestring("Next player: ") + next_player->nick;
306 unsigned this_width = my_draw_text(text, NULL, 24.0);
307 my_draw_text(text, buf, 24.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 420);
309 if (next_song->song.id != -1) {
310 this_width = my_draw_text(next_song->song.title, NULL, 20.0);
311 my_draw_text(next_song->song.title, buf, 20.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 457);
314 conn.perform(FetchHighscore(next_song->song.id, &hs));
316 if (hs.score != -1) {
317 text = widestring("High score: ") + widestring(pqxx::to_string(hs.score)) +
318 widestring(", by ") + hs.nick + widestring(" in ") + hs.tournament_name;
319 this_width = my_draw_text(text, NULL, 16.0);
320 my_draw_text(text, buf, 16.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 487);
324 // only show lead/win/qualify for the last song
325 if (num_played == num_scores - 1) {
327 * Find out how much we need to lead, how much we need to be guaranteed
328 * to win the group, and how much we need to secure qualification.
331 // find the best score we can get
332 unsigned max_score_this_song;
333 if (next_song->song.id != -1) {
334 // random song, or we know what song the player picked
335 max_score_this_song = song_scores[next_song->song.id];
337 max_score_this_song = player_scores[next_player->id];
342 // see what score this player must beat to lead
343 unsigned lead_beat = 0, win_beat = 0;
344 for (unsigned i = 0; i < group.players.size(); ++i) {
345 if (group.players[i].id == next_player->id)
348 lead_beat = std::max(lead_beat, group.players[i].total);
351 // find the best max score among the others
352 for (unsigned i = 0; i < group.players.size(); ++i) {
353 if (group.players[i].id == next_player->id)
356 win_beat = std::max(win_beat, max_score[i]);
360 * There's a somewhat subtle point here. Normally, what a player would be interested in
361 * with regard to qualification would be a set of three values:
363 * 1. How much is the absolute minimum required to qualify, given that all others
365 * 2. How much will give a reasonable chance of qualifying, given the expected performance
367 * 3. How much will be enough to secure qualification, no matter what?
369 * Given perfect guessing, #2 would be "how much is needed to qualify"; however, it is
370 * completely impossible to give an exact value for that, and we're not into the guessing
371 * games. :-) #1 is often so low it's completely unrealistic (ie. far enough from #2 that
372 * it's not interesting), but #3, the most conservative estimate, is often a good measure.
373 * #3 is "how much is needed to _secure_ qualification", and that is usually what we
374 * print out when it's possible.
376 * However, in a few situations, #1 and #3 will be the exact same value, from which it
377 * follows (from the squeeze law, or just common sense :-) ) that #2 will be the same
378 * value as #1 and #3. (This usually happens near or at the end of a group.) In that
379 * case, we know the value we seek (ie. "how much is needed to qualify"), so we drop
380 * the word "secure" and just print it as-is.
382 * To find #1 and #3, we sort and pick out the values we need to beat in the best and
385 int qualify_beat_worst_case = -1, qualify_beat_best_case = -1;
387 if (group.num_qualifying > 0) {
388 std::vector<unsigned> tmp;
390 for (unsigned i = 0; i < group.players.size(); ++i) {
391 if (group.players[i].id == next_player->id)
393 tmp.push_back(max_score[i]);
395 std::sort(tmp.begin(), tmp.end());
396 qualify_beat_worst_case = tmp[tmp.size() - group.num_qualifying];
398 std::vector<unsigned> tmp2;
399 for (unsigned i = 0; i < group.players.size(); ++i) {
400 if (group.players[i].id == next_player->id)
402 tmp2.push_back(min_score[i]);
405 std::sort(tmp2.begin(), tmp2.end());
406 qualify_beat_best_case = tmp2[tmp2.size() - group.num_qualifying];
409 // print out the lines we can attain
410 if (next_player->total + max_score_this_song > lead_beat && (lead_beat != win_beat)) {
411 int lead_need = std::max(lead_beat - next_player->total + 1, 0U);
414 text = widestring("Needs to lead: ") + widestring(pqxx::to_string(lead_need));
415 this_width = my_draw_text(text, NULL, 18.0);
416 my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
422 if (next_player->total + max_score_this_song > win_beat) {
423 int win_need = std::max(win_beat - next_player->total + 1, 0U);
426 text = widestring("Needs to win: ") + widestring(pqxx::to_string(win_need));
428 this_width = my_draw_text(text, NULL, 18.0);
429 my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
435 if (group.num_qualifying > 0 &&
436 next_player->total + max_score_this_song > unsigned(qualify_beat_worst_case) &&
437 (unsigned(qualify_beat_worst_case) != win_beat)) {
438 int qual_need = std::max(qualify_beat_worst_case - next_player->total + 1, 0U);
441 if (qualify_beat_worst_case == qualify_beat_best_case) {
442 text = widestring("Needs to qualify: ") + widestring(pqxx::to_string(qual_need));
444 text = widestring("Needs to secure qualification: ") + widestring(pqxx::to_string(qual_need));
447 this_width = my_draw_text(text, NULL, 18.0);
448 my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
457 // some refactoring done, more should be
458 void GroupScreen::draw(unsigned char *buf, unsigned width, unsigned height)
460 std::vector<TextDefer> td;
462 scores_changed.reset_flag();
463 set_screen_size(width, height);
466 * We'll probably need some values from here later on (although not all), just fetch them
467 * all while we're at it.
469 std::map<unsigned, unsigned> song_scores, player_scores;
470 conn.perform(FetchMaxScoreForSongs(tournament, &song_scores));
471 conn.perform(FetchMaxScoreForPlayers(tournament, round, &player_scores));
474 conn.perform(FetchGroup(tournament, round, parallel, &group));
475 gettimeofday(&last_updated, NULL);
477 memset(buf, 0, width * height * 4);
479 std::vector<unsigned> colwidth;
481 draw_main_heading(td);
482 find_column_widths(group, colwidth);
483 draw_column_headings(td, group, colwidth);
484 draw_scores(td, group, colwidth);
486 unsigned num_scores = group.players[0].scores.size();
489 * Approximate (but probably working quite well in practice) heuristic
490 * for finding the min and max rank of a player works as follows:
492 * First of all, find out, for each player in the group, what the
493 * maximum remaining score possibly can be (the minimum score is of
494 * course identical to the player's current total). For a random song,
495 * this is of course 1000 * (maximum feet rating) (but of course, that
496 * depends on whether we can play single or double! for now, assume
497 * double is okay, but this logic will be deferred to FetchMaxScore
498 * anyhow); for a random song, we simply pick the highest-ranking song
499 * we can find, EXCEPT those the player has chosen earlier AND the
500 * random songs this round, AND all random songs from elimination rounds
501 * (ie. rounds with only one group). (Phew!) This doesn't solve problems
502 * we'd face with more than one chosen song, but it should be good enough.
504 * After we've found the max and min scores for all players, it's a simple
505 * matter of sorting; the best attainable rank for player X is obtained if
506 * X gets max score and all others get min score, the worst attainable rank
507 * is obtained if X gets min score and all others get max score.
509 std::vector<unsigned> max_score, min_score;
510 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
511 unsigned min_score_tp = 0, max_score_tp = 0;
512 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j) {
513 if (j->score != -1) {
515 min_score_tp += j->score;
516 max_score_tp += j->score;
518 unsigned max_score_this_song;
519 if (j->song.id != -1) {
520 // random song, or we know what song the player picked
521 max_score_this_song = song_scores[j->song.id];
523 max_score_this_song = player_scores[i->id];
525 max_score_tp += max_score_this_song;
528 max_score.push_back(max_score_tp);
529 min_score.push_back(min_score_tp);
532 // now finally find min and max rank, and draw it all
533 unsigned show_players = get_show_players(group);
534 unsigned y = (show_players <= 7) ? 140 : (140 - (show_players - 7) * 5);
535 for (unsigned i = 0; i < group.players.size() && (i/num_machines) < show_players; ++i) {
536 unsigned best_rank = 1, worst_rank = 1;
537 for (unsigned j = 0; j < group.players.size(); ++j) {
541 if (max_score[i] < min_score[j])
543 if (min_score[i] <= max_score[j])
548 if (best_rank == worst_rank)
549 std::sprintf(text, "%u", best_rank);
551 std::sprintf(text, "%u-%u", best_rank, worst_rank);
553 if (i % num_machines != machine)
556 // find out where to place this
557 unsigned x = 40 + colwidth[0];
558 for (unsigned j = 1; j <= num_scores + 1; ++j)
559 x += colwidth[j] + 20;
561 unsigned this_width = my_draw_text(text, NULL, 22.0);
562 my_draw_text_deferred(td, text, 22.0, x + colwidth[num_scores + 2] / 2 - this_width / 2, y);
564 if (show_players > 7)
565 y += 40 - (show_players - 7) * 4;
570 draw_next_up_single(buf, group, colwidth, song_scores, player_scores, max_score, min_score);
573 draw_all_deferred_text(buf, td, last_text);
577 int GroupScreen::get_priority()