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"
9 #include "fetch_highscore.h"
12 GroupScreen::GroupScreen(pqxx::connection &conn, unsigned tournament, unsigned round, unsigned parallel)
13 : tournament(tournament), round(round), parallel(parallel), scores_changed(conn, "scores"), conn(conn), valid(false)
17 GroupScreen::~GroupScreen()
21 bool GroupScreen::check_invalidated()
25 if (!scores_changed.get_flag())
29 conn.perform(FetchNeedsUpdate(last_updated, tournament, round, parallel, &needs_update));
32 scores_changed.reset_flag();
37 void GroupScreen::draw(unsigned char *buf)
39 std::vector<TextDefer> td;
41 scores_changed.reset_flag();
44 conn.perform(FetchGroup(tournament, round, parallel, &group));
45 gettimeofday(&last_updated, NULL);
47 memset(buf, 0, 800 * 600 * 4);
52 sprintf(heading, "Round %u", round);
54 sprintf(heading, "Round %u, Group %u", round, parallel);
58 unsigned width = my_draw_text(heading, NULL, 48.0);
59 my_draw_text_deferred(td, heading, 48.0, 800/2 - width/2, 60);
62 // Find out how wide each column has to be. First try unlimited width (ie.
63 // long titles for everything); if that gets too long, try again with short
64 // titles for chosen songs.
65 unsigned width[16], num_scores;
66 unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
67 unsigned mode, sumwidth;
68 for (mode = 0; mode < 2; ++mode) {
69 for (unsigned i = 0; i < 16; ++i)
72 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
74 width[0] = std::max(width[0], my_draw_text(i->nick, NULL, 18.0));
76 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
78 width[col] = std::max(width[col], my_draw_text((mode == 0) ? j->song.title : j->song.short_title, NULL, 12.0) +
81 width[col] = std::max(width[col], my_draw_text(j->song.short_title, NULL, 12.0));
82 width[col] = std::max(width[col], max_num_width);
87 num_scores = group.players[0].scores.size();
89 width[num_scores + 1] = std::max(my_draw_text("Total", NULL, 12.0), max_num_width);
90 width[num_scores + 2] = my_draw_text("Rank", NULL, 12.0);
92 // if we're at long titles and that works, don't try the short ones
95 for (unsigned i = 0; i <= num_scores + 2; ++i)
96 sumwidth += width[i] + 20;
103 * If we have space to go, distribute as much as we can to the chosen song column, so we won't have
104 * total and rank jumping around.
106 if (sumwidth < 780) {
107 int first_chosen_col = -1;
110 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
112 first_chosen_col = col;
117 if (first_chosen_col != -1) {
118 printf("first_chosen_col=%u\n", first_chosen_col);
119 width[first_chosen_col] += 780 - sumwidth;
123 // make column headings from the first player's songs
125 unsigned x = 40 + width[0];
126 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
128 unsigned this_width = my_draw_text(i->song.short_title, NULL, 12.0);
129 my_draw_text_deferred(td, i->song.short_title, 12.0, x + width[col] / 2 - this_width / 2, 100);
131 x += width[col] + 20;
134 my_draw_text_deferred(td, "Total", 12.0, x + width[num_scores + 1] / 2 - my_draw_text("Total", NULL, 12.0) / 2, 100);
135 x += width[num_scores + 1] + 20;
136 my_draw_text_deferred(td, "Rank", 12.0, x + width[num_scores + 2] / 2 - my_draw_text("Rank", NULL, 12.0) / 2, 100);
138 // show all the players and the scores
140 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
141 my_draw_text_deferred(td, i->nick, 18.0, 20, y);
143 unsigned x = 40 + width[0];
146 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
148 if (j->score != -1) {
149 sprintf(text, "%u", j->score);
152 unsigned this_width = my_draw_text(text, NULL, 22.0);
154 my_draw_text_deferred(td, text, 22.0, x + max_num_width - this_width, y);
156 // draw the long name if we can, otherwise use the short one
157 if (my_draw_text(j->song.title, NULL, 12.0) > width[col]) {
158 my_draw_text_deferred(td, j->song.short_title, 12.0, x + max_num_width + 10, y);
160 my_draw_text_deferred(td, j->song.title, 12.0, x + max_num_width + 10, y);
163 my_draw_text_deferred(td, text, 22.0, x + width[col] / 2 - this_width / 2, y);
165 x += width[col] + 20;
171 sprintf(text, "%u", i->total);
173 unsigned this_width = my_draw_text(text, NULL, 22.0);
174 my_draw_text_deferred(td, text, 22.0, x + width[num_scores + 1] / 2 - this_width / 2, y);
175 x += width[num_scores + 1] + 20;
182 * Approximate (but probably working quite well in practice) heuristic
183 * for finding the min and max rank of a player works as follows:
185 * First of all, find out, for each player in the group, what the
186 * maximum remaining score possibly can be (the minimum score is of
187 * course identical to the player's current total). For a random song,
188 * this is of course 1000 * (maximum feet rating) (but of course, that
189 * depends on whether we can play single or double! for now, assume
190 * double is okay, but this logic will be deferred to FetchMaxScore
191 * anyhow); for a random song, we simply pick the highest-ranking song
192 * we can find, EXCEPT those the player has chosen earlier AND the
193 * random songs this round, AND all random songs from elimination rounds
194 * (ie. rounds with only one group). (Phew!) This doesn't solve problems
195 * we'd face with more than one chosen song, but it should be good enough.
197 * After we've found the max and min scores for all players, it's a simple
198 * matter of sorting; the best attainable rank for player X is obtained if
199 * X gets max score and all others get min score, the worst attainable rank
200 * is obtained if X gets min score and all others get max score.
202 * This is a bit SQL-heavy, but heck...
204 std::vector<unsigned> max_score, min_score;
205 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
206 unsigned min_score_tp = 0, max_score_tp = 0;
207 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
208 if (j->score != -1) {
210 min_score_tp += j->score;
211 max_score_tp += j->score;
213 unsigned max_score_this_song;
214 if (j->song.id != -1) {
215 // random song, or we know what song the player picked
216 conn.perform(FetchMaxScoreForSong(tournament, j->song.id, &max_score_this_song));
218 conn.perform(FetchMaxScoreForPlayer(tournament, i->id, round, &max_score_this_song));
220 max_score_tp += max_score_this_song;
223 max_score.push_back(max_score_tp);
224 min_score.push_back(min_score_tp);
227 // now finally find min and max rank, and draw it all
229 for (unsigned i = 0; i < group.players.size(); ++i) {
230 unsigned best_rank = 1, worst_rank = 1;
231 for (unsigned j = 0; j < group.players.size(); ++j) {
235 if (max_score[i] < min_score[j])
237 if (min_score[i] <= max_score[j])
242 if (best_rank == worst_rank)
243 std::sprintf(text, "%u", best_rank);
245 std::sprintf(text, "%u-%u", best_rank, worst_rank);
247 unsigned this_width = my_draw_text(text, NULL, 22.0);
248 my_draw_text_deferred(td, text, 22.0, x + width[num_scores + 2] / 2 - this_width / 2, y);
254 * Next up (at the bottom) is "who's playing, what will he/she be playing, and
255 * optionally, how much to lead/win and how much to secure qualification" (the
256 * last one only in the final round). We assume playing is done in a modified
257 * zigzag; all the random songs are played first in zigzag/wrapping order (player
258 * 1 song 1, player 2 song 2, player 3 song 3, player 1 song 2, player 2 song 3,
259 * player 3 song 1, etc... assuming three songs and three players) and then all
260 * the chosen songs are played (we assume only one chosen song).
262 * The lines are as follows:
266 * High score: <hs> by <hsplayer> at <hsevent>
267 * Needs to lead: <leadscore>
268 * Needs to secure qualification: <qualscore>
269 * Needs to win group: <winscore>
272 /* Find the first player with the fewest songs played. */
273 unsigned min_played_songs = 9999, num_random_songs = 0;
274 Player *next_player = NULL;
275 for (std::vector<Player>::iterator i = group.players.begin(); i != group.players.end(); ++i) {
276 unsigned this_played = 0, this_random_songs = 0;
277 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
284 if (this_played < min_played_songs) {
285 min_played_songs = this_played;
287 num_random_songs = this_random_songs; // should be equal for all
291 /* Find out what song this player is supposed to play next; try random songs first */
292 Score *next_song = NULL;
294 for (unsigned i = 0; i < num_random_songs; ++i) {
295 unsigned j = (i + next_player->position - 1) % num_random_songs;
296 if (next_player->scores[j].score == -1) {
297 next_song = &(next_player->scores[j]);
298 printf("Selecting score %u\n", j);
303 // then all songs, if that didn't work out (slightly icky, but hey)
304 if (next_song == NULL) {
305 for (unsigned i = 0; i < num_scores; ++i) {
306 unsigned j = (i + next_player->position) % num_scores;
307 if (next_player->scores[j].score == -1) {
308 next_song = &(next_player->scores[j]);
309 printf("Selecting score %u\n", j);
315 if (next_song != NULL) {
316 widestring text = widestring("Next player: ") + next_player->nick;
317 unsigned this_width = my_draw_text(text, NULL, 24.0);
318 my_draw_text(text, buf, 24.0, 400 - this_width/2, 420);
320 if (next_song->song.id != -1) {
321 this_width = my_draw_text(next_song->song.title, NULL, 20.0);
322 my_draw_text(next_song->song.title, buf, 20.0, 400 - this_width/2, 457);
325 conn.perform(FetchHighscore(next_song->song.id, &hs));
327 if (hs.score != -1) {
328 text = widestring("High score: ") + widestring(pqxx::to_string(hs.score)) +
329 widestring(", by ") + hs.nick + widestring(" in ") + hs.tournament_name;
330 this_width = my_draw_text(text, NULL, 16.0);
331 my_draw_text(text, buf, 16.0, 400 - this_width/2, 487);
335 // only show lead/win/qualify for the last song
336 if (min_played_songs == num_scores - 1) {
338 * Find out how much we need to lead, how much we need to be guaranteed
339 * to win the group, and how much we need to secure qualification. (FIXME:
340 * do the last one :-) )
343 // find the best score we can get
344 unsigned max_score_this_song;
345 if (next_song->song.id != -1) {
346 // random song, or we know what song the player picked
347 conn.perform(FetchMaxScoreForSong(tournament, next_song->song.id, &max_score_this_song));
349 conn.perform(FetchMaxScoreForPlayer(tournament, next_player->id, round, &max_score_this_song));
354 // see what score this player must beat to lead
355 unsigned lead_beat = 0, win_beat = 0;
356 for (unsigned i = 0; i < group.players.size(); ++i) {
357 if (group.players[i].id == next_player->id)
360 lead_beat = std::max(lead_beat, group.players[i].total);
363 // find the best max score among the others
364 for (unsigned i = 0; i < group.players.size(); ++i) {
365 if (group.players[i].id == next_player->id)
368 win_beat = std::max(win_beat, max_score[i]);
372 * There's a somewhat subtle point here. Normally, what a player would be interested in
373 * with regard to qualification would be a set of three values:
375 * 1. How much is the absolute minimum required to qualify, given that all others
377 * 2. How much will give a reasonable chance of qualifying, given the expected performance
379 * 3. How much will be enough to secure qualification, no matter what?
381 * Given perfect guessing, #2 would be "how much is needed to qualify"; however, it is
382 * completely impossible to give an exact value for that, and we're not into the guessing
383 * games. :-) #1 is often so low it's completely unrealistic (ie. far enough from #2 that
384 * it's not interesting), but #3, the most conservative estimate, is often a good measure.
385 * #3 is "how much is needed to _secure_ qualification", and that is usually what we
386 * print out when it's possible.
388 * However, in a few situations, #1 and #3 will be the exact same value, from which it
389 * follows (from the squeeze law, or just common sense :-) ) that #2 will be the same
390 * value as #1 and #3. (This usually happens near or at the end of a group.) In that
391 * case, we know the value we seek (ie. "how much is needed to qualify"), so we drop
392 * the word "secure" and just print it as-is.
394 * To find #1 and #3, we sort and pick out the values we need to beat in the best and
397 int qualify_beat_worst_case = -1, qualify_beat_best_case = -1;
399 if (group.num_qualifying > 0) {
400 std::vector<unsigned> tmp;
402 for (unsigned i = 0; i < group.players.size(); ++i) {
403 if (group.players[i].id == next_player->id)
405 tmp.push_back(max_score[i]);
407 std::sort(tmp.begin(), tmp.end());
408 qualify_beat_worst_case = tmp[tmp.size() - group.num_qualifying];
410 std::vector<unsigned> tmp2;
411 for (unsigned i = 0; i < group.players.size(); ++i) {
412 if (group.players[i].id == next_player->id)
414 tmp2.push_back(min_score[i]);
417 std::sort(tmp2.begin(), tmp2.end());
418 qualify_beat_best_case = tmp2[tmp2.size() - group.num_qualifying];
421 // print out the lines we can attain
422 if (next_player->total + max_score_this_song > lead_beat && (lead_beat != win_beat)) {
423 int lead_need = std::max(lead_beat - next_player->total + 1, 0U);
425 text = widestring("Needs to lead: ") + widestring(pqxx::to_string(lead_need));
426 this_width = my_draw_text(text, NULL, 18.0);
427 my_draw_text(text, buf, 18.0, 400 - this_width/2, y);
432 if (next_player->total + max_score_this_song > win_beat) {
433 int win_need = std::max(win_beat - next_player->total + 1, 0U);
435 text = widestring("Needs to win: ") + widestring(pqxx::to_string(win_need));
437 this_width = my_draw_text(text, NULL, 18.0);
438 my_draw_text(text, buf, 18.0, 400 - this_width/2, y);
443 if (next_player->total + max_score_this_song > qualify_beat_worst_case && (qualify_beat_worst_case != win_beat)) {
444 int qual_need = std::max(qualify_beat_worst_case - next_player->total + 1, 0U);
446 if (qualify_beat_worst_case == qualify_beat_best_case) {
447 text = widestring("Needs to qualify: ") + widestring(pqxx::to_string(qual_need));
449 text = widestring("Needs to secure qualification: ") + widestring(pqxx::to_string(qual_need));
452 this_width = my_draw_text(text, NULL, 18.0);
453 my_draw_text(text, buf, 18.0, 400 - this_width/2, y);
461 draw_all_deferred_text(buf, td, last_text);