]> git.sesse.net Git - ccbs/blob - bigscreen/groupscreen.cpp
DDR Europe tournament parser now by default sets all players to Norway. Syntax fixes...
[ccbs] / bigscreen / groupscreen.cpp
1 #include <cstdio>
2 #include <algorithm>
3 #include <map>
4
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"
12 #include "fonts.h"
13
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)
16 {
17 }
18
19 GroupScreen::~GroupScreen()
20 {
21 }
22
23 bool GroupScreen::check_invalidated()
24 {
25         if (!valid)
26                 return true;
27         if (!scores_changed.get_flag())
28                 return false;
29
30         bool needs_update;
31         conn.perform(FetchNeedsUpdate(last_updated, tournament, round, parallel, &needs_update));
32
33         if (!needs_update)
34                 scores_changed.reset_flag();
35         
36         return needs_update;
37 }
38
39 unsigned GroupScreen::get_show_players(const Group &group)
40 {
41         unsigned num_players_this_machine = (group.players.size() + num_machines - machine - 1) / num_machines;
42         return std::min(num_players_this_machine, 9U);
43 }
44
45 void GroupScreen::draw_main_heading(std::vector<TextDefer> &td)
46 {
47         char heading[64];
48         if (num_machines == 1) {
49                 if (parallel == 0) {
50                         std::sprintf(heading, "Round %u", round);
51                 } else {
52                         std::sprintf(heading, "Round %u, Group %u", round, parallel);
53                 }
54         } else {
55                 if (parallel == 0) {
56                         std::sprintf(heading, "Round %u, Machine %u", round, machine + 1);
57                 } else {
58                         std::sprintf(heading, "Round %u, Group %u, Machine %u", round, parallel, machine + 1);
59                 }
60         }
61
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);
64 }
65
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)
68 {
69         unsigned num_scores = group.players[0].scores.size();
70
71         unsigned col = 1;
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) {
74                 if (!i->chosen) {
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);
77                 }
78                 x += colwidth[col] + 20;
79         }
80
81         if (num_scores > 1) {
82                 my_draw_text_deferred(td, "Total", 12.0, x + colwidth[num_scores + 1] / 2 - my_draw_text("Total", NULL, 12.0) / 2, 100);
83                 x += colwidth[num_scores + 1] + 20;
84         }
85         my_draw_text_deferred(td, "Rank", 12.0, x + colwidth[num_scores + 2] / 2 - my_draw_text("Rank", NULL, 12.0) / 2, 100);
86 }       
87         
88 // show all the players and the scores
89 void GroupScreen::draw_scores(std::vector<TextDefer> &td, const Group &group, const std::vector<unsigned> &colwidth)
90 {
91         unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
92         unsigned num_scores = group.players[0].scores.size();
93         unsigned show_players = get_show_players(group);
94         unsigned y = (show_players <= 7) ? 140 : (140 - (show_players - 7) * 5);
95         
96         unsigned row = 0, m = 0, x;
97         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end() && row < 9; ++i) {
98                 if (m++ % num_machines != machine)
99                         continue;
100
101                 my_draw_text_deferred(td, i->nick, 18.0, 20, y);
102
103                 x = 40 + colwidth[0];
104
105                 unsigned col = 1;
106                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
107                         char text[16] = "";
108                         if (j->score != -1) {
109                                 std::sprintf(text, "%u", j->score);
110                         }
111         
112                         unsigned this_width = my_draw_text(text, NULL, 22.0);
113                         if (j->chosen) {
114                                 my_draw_text_deferred(td, text, 22.0, x + max_num_width - this_width, y);
115
116                                 // draw the long name if we can, otherwise use the short one
117                                 if (my_draw_text(j->song.title, NULL, 12.0) > (colwidth[col] - 10 - max_num_width)) {
118                                         my_draw_text_deferred(td, j->song.short_title, 12.0, x + max_num_width + 10, y);
119                                 } else {
120                                         my_draw_text_deferred(td, j->song.title, 12.0, x + max_num_width + 10, y);
121                                 }
122                         } else {
123                                 my_draw_text_deferred(td, text, 22.0, x + colwidth[col] / 2 - this_width / 2, y);
124                         }
125                         x += colwidth[col] + 20;
126                 }
127
128                 // draw total
129                 if (num_scores > 1) {
130                         char text[16];
131                         std::sprintf(text, "%u", i->total);
132                         
133                         unsigned this_width = my_draw_text(text, NULL, 22.0);
134                         my_draw_text_deferred(td, text, 22.0, x + colwidth[num_scores + 1] / 2 - this_width / 2, y);
135                         x += colwidth[num_scores + 1] + 20;
136                 }
137
138                 if (show_players > 7)
139                         y += 40 - (show_players - 7) * 4;
140                 else 
141                         y += 40;
142                 ++row;
143         }
144 }       
145
146 /*
147  * Find out how wide each column has to be. First try unlimited width (ie.
148  * long titles for everything); if that gets too long, try again with short
149  * titles for chosen songs.
150  */
151 void GroupScreen::find_column_widths(const Group &group, std::vector<unsigned> &colwidth)
152 {
153         unsigned num_scores;
154         unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
155         unsigned sumcolwidth;
156         
157         for (unsigned mode = 0; mode < 2; ++mode) {
158                 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
159                         unsigned col = 1;
160                         
161                         if (colwidth.size() == 0)
162                                 colwidth.push_back(0);
163                         
164                         colwidth[0] = std::max(colwidth[0], my_draw_text(i->nick, NULL, 18.0));
165
166                         for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
167                                 if (colwidth.size() < col+1)
168                                         colwidth.push_back(0);
169                                         
170                                 if (j->chosen) {
171                                         colwidth[col] = std::max(colwidth[col], my_draw_text((mode == 0) ? j->song.title : j->song.short_title, NULL, 12.0) + 
172                                                         max_num_width + 10);
173                                 } else {                
174                                         colwidth[col] = std::max(colwidth[col], my_draw_text(j->song.short_title, NULL, 12.0));
175                                         colwidth[col] = std::max(colwidth[col], max_num_width);
176                                 }
177                         }
178                 }
179
180                 num_scores = group.players[0].scores.size();
181
182                 if (colwidth.size() < num_scores + 2) {
183                         colwidth.push_back(0);
184                         colwidth.push_back(0);
185                 }
186         
187                 if (num_scores > 1) {
188                         colwidth[num_scores + 1] = std::max(my_draw_text("Total", NULL, 12.0), max_num_width);
189                 }
190                 colwidth[num_scores + 2] = my_draw_text("Rank", NULL, 12.0);
191
192                 // if we're at long titles and that works, don't try the short ones
193                 sumcolwidth = 0;
194                         
195                 for (unsigned i = 0; i <= num_scores + 2; ++i)
196                         sumcolwidth += colwidth[i] + 20;
197                         
198                 if (sumcolwidth < 780)
199                         break;
200         }
201
202         /* 
203          * If we have space to go, distribute as much as we can to the chosen song column, so we won't have
204          * total and rank jumping around.
205          */
206         if (sumcolwidth < 780) {
207                 int first_chosen_col = -1;
208                 unsigned col = 1;
209
210                 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
211                         if (i->chosen) {
212                                 first_chosen_col = col;
213                                 break;
214                         }
215                 }
216
217                 if (first_chosen_col != -1) {
218                         colwidth[first_chosen_col] += 780 - sumcolwidth;
219                 }
220         }
221 }
222
223 /* Find the first player with the fewest songs played and part of this machine. */
224 const Player *GroupScreen::get_next_player(const Group &group)
225 {
226         unsigned min_played_songs = 9999;
227         const Player *next_player = NULL;
228         unsigned m = 0;
229         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
230                 unsigned this_played = 0;
231                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j) {
232                         if (j->score != -1)
233                                 ++this_played;
234                 }
235
236                 if ((m++ % num_machines == machine) && this_played < min_played_songs) {
237                         min_played_songs = this_played;
238                         next_player = &(*i);
239                 }
240         }
241
242         return next_player;
243 }
244
245 /*
246  * At the bottom, for a single player, is "who's playing, what will he/she be
247  * playing, and optionally, how much to lead/win and how much to secure
248  * qualification" (the last one only in the final round). We assume playing is
249  * done in a modified zigzag; all the random songs are played first in
250  * zigzag/wrapping order (player 1 song 1, player 2 song 2, player 3 song 3,
251  * player 1 song 2, player 2 song 3, player 3 song 1, etc... assuming three
252  * songs and three players) and then all the chosen songs are played (we assume
253  * only one chosen song).
254  *
255  * The lines are as follows:
256  *
257  * <player>
258  * <song>
259  * High score: <hs> by <hsplayer> at <hsevent>
260  * Needs to lead: <leadscore>
261  * Needs to secure qualification: <qualscore>
262  * Needs to win group: <winscore>
263  */
264 void GroupScreen::draw_next_up_single(unsigned char *buf, const Group &group,
265         std::map<unsigned, unsigned> &song_scores, std::map<unsigned, unsigned> &player_scores,
266         const std::vector<unsigned> &max_score, const std::vector<unsigned> &min_score)
267 {
268         unsigned num_scores = group.players[0].scores.size();
269         
270         // Find out how many random songs there are (equal for all players).
271         unsigned num_random_songs = 0;
272         for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i) {
273                 if (!i->chosen)
274                         ++num_random_songs;
275         }
276
277         /* 
278          * Find out which player is next, and what song he she is supposed to play. First
279          * try random songs.
280          */
281         const Player *next_player = get_next_player(group);
282         const Score *next_song = NULL;
283
284         for (unsigned i = 0; i < num_random_songs; ++i) {
285                 unsigned j = (i + next_player->position - 1) % num_random_songs;
286                 if (next_player->scores[j].score == -1) {
287                         next_song = &(next_player->scores[j]);
288                         break;
289                 }
290         }
291
292         // then all songs, if that didn't work out (slightly icky, but hey)
293         if (next_song == NULL) {
294                 for (unsigned i = 0; i < num_scores; ++i) {
295                         unsigned j = (i + next_player->position) % num_scores;
296                         if (next_player->scores[j].score == -1) {
297                                 next_song = &(next_player->scores[j]);
298                                 break;
299                         }
300                 }
301         }
302
303         if (next_song != NULL) {
304                 // find out how many songs we've played in all
305                 unsigned num_played = 0;
306                 for (unsigned i = 0; i < num_scores; ++i) {
307                         if (next_player->scores[i].score != -1) {
308                                 ++num_played;
309                         }
310                 }
311         
312                 bool last_song = (num_played == num_scores - 1);
313                         
314                 draw_next_up_player(buf, group, *next_player, *next_song, last_song, song_scores, player_scores, max_score, min_score);
315         }
316 }
317
318 /*
319  * Some tournaments allow versus play in the initial rounds to save time; this is
320  * of course for random songs only. In this case, the scheme from draw_next_up_single()
321  * is somewhat changed, as we zig-zag across pairs instead of players. (If there's a
322  * stray person left in the group, that player plays the song by him-/herself as in
323  * a usual single tournament.
324  */
325 void GroupScreen::draw_next_up_versus(unsigned char *buf, const Group &group,
326         std::map<unsigned, unsigned> &song_scores, std::map<unsigned, unsigned> &player_scores,
327         const std::vector<unsigned> &max_score, const std::vector<unsigned> &min_score)
328 {
329         // Find out how many random songs there are (equal for all players).
330         unsigned num_random_songs = 0;
331         for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i) {
332                 if (!i->chosen)
333                         ++num_random_songs;
334         }
335
336         // Find the next player and what song he/she is supposed to play, if any.
337         const Player *next_player = get_next_player(group);
338         const Score *next_song = NULL;
339         unsigned song_num;
340
341         for (unsigned i = 0; i < num_random_songs; ++i) {
342                 unsigned j = (i + (next_player->position - 1) / 2) % num_random_songs;
343                 if (next_player->scores[j].score == -1) {
344                         next_song = &(next_player->scores[j]);
345                         song_num = j;
346                         break;
347                 }
348         }
349         
350         /*
351          * If there's no match, we're on the chosen songs (or done),
352          * so just delegate to draw_up_single().
353          */
354         if (next_song == NULL) {
355                 draw_next_up_single(buf, group, song_scores, player_scores, max_score, min_score);
356                 return;
357         }
358         
359         /*
360          * Look for a player with the same amount of random songs played _and_ missing
361          * the same song.
362          */ 
363         unsigned num_songs_played = 0;
364         for (unsigned i = 0; i < num_random_songs; ++i) {
365                 if (next_player->scores[i].score != -1) {
366                         ++num_songs_played;
367                 }
368         }
369         
370         unsigned m = 0;
371         const Player *other_player = NULL;
372         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
373                 if ((m++ % num_machines != machine))
374                         continue;
375                 if (i->id == next_player->id)
376                         continue;
377                 
378                 unsigned this_songs_played = 0;
379                 for (unsigned j = 0; j < num_random_songs; ++j) {
380                         if (i->scores[j].score != -1) {
381                                 ++this_songs_played;
382                         }
383                 }
384
385                 if (this_songs_played != num_songs_played)
386                         continue;
387
388                 if (i->scores[song_num].score == -1) {
389                         other_player = &(*i);
390                         break;
391                 }       
392         }
393
394         // If we didn't find another player, just draw the one we have as usual.
395         if (other_player == NULL) {
396                 draw_next_up_player(buf, group, *next_player, *next_song, false,
397                         song_scores, player_scores, max_score, min_score);
398                 return;
399         }
400         
401         // OK, we have two players. Draw their nicks and the scores
402         widestring text = widestring("Next players: ") + next_player->nick + widestring(" and ") + other_player->nick;
403         unsigned this_width = my_draw_text(text, NULL, 24.0);
404         my_draw_text(text, buf, 24.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 420);
405
406         if (next_song->song.id != -1) {
407                 this_width = my_draw_text(next_song->song.title, NULL, 20.0);
408                 my_draw_text(next_song->song.title, buf, 20.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 457);
409
410                 Highscore hs;
411                 conn.perform(FetchHighscore(next_song->song.id, &hs));
412
413                 if (hs.score != -1) {
414                         text = widestring("High score: ") + widestring(pqxx::to_string(hs.score)) +
415                                 widestring(", by ") + hs.nick + widestring(" in ") + hs.tournament_name;
416                         this_width = my_draw_text(text, NULL, 16.0);
417                         my_draw_text(text, buf, 16.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 487);
418                 }
419         }
420 }
421
422 void GroupScreen::draw_next_up_player(unsigned char *buf, const Group &group, const Player &player, const Score &song, bool last_song,
423         std::map<unsigned, unsigned> &song_scores, std::map<unsigned, unsigned> &player_scores,
424         const std::vector<unsigned> &max_score, const std::vector<unsigned> &min_score)
425 {
426         widestring text = widestring("Next player: ") + player.nick;
427         unsigned this_width = my_draw_text(text, NULL, 24.0);
428         my_draw_text(text, buf, 24.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 420);
429
430         if (song.song.id != -1) {
431                 this_width = my_draw_text(song.song.title, NULL, 20.0);
432                 my_draw_text(song.song.title, buf, 20.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 457);
433
434                 Highscore hs;
435                 conn.perform(FetchHighscore(song.song.id, &hs));
436
437                 if (hs.score != -1) {
438                         text = widestring("High score: ") + widestring(pqxx::to_string(hs.score)) +
439                                 widestring(", by ") + hs.nick + widestring(" in ") + hs.tournament_name;
440                         this_width = my_draw_text(text, NULL, 16.0);
441                         my_draw_text(text, buf, 16.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 487);
442                 }
443         }
444
445         // only show lead/win/qualify for the last song
446         if (last_song) {
447                 /*
448                  * Find out how much we need to lead, how much we need to be guaranteed
449                  * to win the group, and how much we need to secure qualification.
450                  */
451
452                 // find the best score we can get
453                 unsigned max_score_this_song;
454                 if (song.song.id != -1) {
455                         // random song, or we know what song the player picked
456                         max_score_this_song = song_scores[song.song.id];
457                 } else {
458                         max_score_this_song = player_scores[player.id];
459                 }
460
461                 unsigned y = 520;
462
463                 // see what score this player must beat to lead
464                 unsigned lead_beat = 0, win_beat = 0;
465                 for (unsigned i = 0; i < group.players.size(); ++i) {
466                         if (group.players[i].id == player.id)
467                                 continue;
468
469                         lead_beat = std::max(lead_beat, group.players[i].total);
470                 }
471
472                 // find the best max score among the others
473                 for (unsigned i = 0; i < group.players.size(); ++i) {
474                         if (group.players[i].id == player.id)
475                                 continue;
476
477                         win_beat = std::max(win_beat, max_score[i]);
478                 }
479
480                 /*
481                  * There's a somewhat subtle point here. Normally, what a player would be interested in
482                  * with regard to qualification would be a set of three values:
483                  *
484                  * 1. How much is the absolute minimum required to qualify, given that all others
485                  *    fail?
486                  * 2. How much will give a reasonable chance of qualifying, given the expected performance
487                  *    of all the others?
488                  * 3. How much will be enough to secure qualification, no matter what?
489                  *
490                  * Given perfect guessing, #2 would be "how much is needed to qualify"; however, it is
491                  * completely impossible to give an exact value for that, and we're not into the guessing
492                  * games. :-) #1 is often so low it's completely unrealistic (ie. far enough from #2 that
493                  * it's not interesting), but #3, the most conservative estimate, is often a good measure.
494                  * #3 is "how much is needed to _secure_ qualification", and that is usually what we
495                  * print out when it's possible.
496                  *
497                  * However, in a few situations, #1 and #3 will be the exact same value, from which it
498                  * follows (from the squeeze law, or just common sense :-) ) that #2 will be the same
499                  * value as #1 and #3. (This usually happens near or at the end of a group.) In that
500                  * case, we know the value we seek (ie. "how much is needed to qualify"), so we drop
501                  * the word "secure" and just print it as-is.
502                  *
503                  * To find #1 and #3, we sort and pick out the values we need to beat in the best and
504                  * the worst case.
505                  */
506                 int qualify_beat_worst_case = -1, qualify_beat_best_case = -1;
507
508                 if (group.num_qualifying > 0) {
509                         std::vector<unsigned> tmp;
510
511                         for (unsigned i = 0; i < group.players.size(); ++i) {
512                                 if (group.players[i].id == player.id)
513                                         continue;
514                                 tmp.push_back(max_score[i]);
515                         }
516                         std::sort(tmp.begin(), tmp.end());
517                         qualify_beat_worst_case = tmp[tmp.size() - group.num_qualifying];
518
519                         std::vector<unsigned> tmp2;
520                         for (unsigned i = 0; i < group.players.size(); ++i) {
521                                 if (group.players[i].id == player.id)
522                                         continue;
523                                 tmp2.push_back(min_score[i]);
524                         }
525
526                         std::sort(tmp2.begin(), tmp2.end());
527                         qualify_beat_best_case = tmp2[tmp2.size() - group.num_qualifying];
528                 }
529
530                 // print out the lines we can attain
531                 if (player.total + max_score_this_song > lead_beat && (lead_beat != win_beat)) {
532                         int lead_need = std::max(lead_beat - player.total + 1, 0U);
533
534                         if (lead_need > 1) {
535                                 text = widestring("Needs to lead: ") + widestring(pqxx::to_string(lead_need));
536                                 this_width = my_draw_text(text, NULL, 18.0);
537                                 my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
538
539                                 y += 30;
540                         }
541                 }
542
543                 if (player.total + max_score_this_song > win_beat) {
544                         int win_need = std::max(win_beat - player.total + 1, 0U);
545
546                         if (win_need > 0) {
547                                 text = widestring("Needs to win: ") + widestring(pqxx::to_string(win_need));
548
549                                 this_width = my_draw_text(text, NULL, 18.0);
550                                 my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
551
552                                 y += 30;
553                         }
554                 }
555
556                 if (group.num_qualifying > 0 &&
557                     group.num_qualifying != group.players.size() &&
558                                 player.total + max_score_this_song > unsigned(qualify_beat_worst_case) &&
559                                 (unsigned(qualify_beat_worst_case) != win_beat)) {
560                         int qual_need = std::max(qualify_beat_worst_case - player.total + 1, 0U);
561
562                         if (qual_need > 0) {
563                                 if (qualify_beat_worst_case == qualify_beat_best_case) {
564                                         text = widestring("Needs to qualify: ") + widestring(pqxx::to_string(qual_need));
565                                 } else {
566                                         text = widestring("Needs to secure qualification: ") + widestring(pqxx::to_string(qual_need));
567                                 }
568
569                                 this_width = my_draw_text(text, NULL, 18.0);
570                                 my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
571
572                                 y += 30;
573                         }
574                 }
575         }
576 }
577
578 // some refactoring done, more should be
579 void GroupScreen::draw(unsigned char *buf, unsigned width, unsigned height)
580 {
581         std::vector<TextDefer> td;
582         
583         scores_changed.reset_flag();
584         set_screen_size(width, height);
585
586         /*
587          * We'll probably need some values from here later on (although not all), just fetch them
588          * all while we're at it.
589          */
590         std::map<unsigned, unsigned> song_scores, player_scores;
591         conn.perform(FetchMaxScoreForSongs(tournament, &song_scores));
592         conn.perform(FetchMaxScoreForPlayers(tournament, round, &player_scores));
593         
594         Group group;
595         conn.perform(FetchGroup(tournament, round, parallel, &group));
596         gettimeofday(&last_updated, NULL);
597
598         memset(buf, 0, width * height * 4);
599
600         std::vector<unsigned> colwidth;
601         
602         draw_main_heading(td);
603         find_column_widths(group, colwidth);
604         draw_column_headings(td, group, colwidth);
605         draw_scores(td, group, colwidth);
606         
607         unsigned num_scores = group.players[0].scores.size();
608
609         /*
610          * Approximate (but probably working quite well in practice) heuristic
611          * for finding the min and max rank of a player works as follows:
612          *
613          * First of all, find out, for each player in the group, what the
614          * maximum remaining score possibly can be (the minimum score is of
615          * course identical to the player's current total). For a random song,
616          * this is of course 1000 * (maximum feet rating) (but of course, that
617          * depends on whether we can play single or double! for now, assume
618          * double is okay, but this logic will be deferred to FetchMaxScore
619          * anyhow); for a random song, we simply pick the highest-ranking song
620          * we can find, EXCEPT those the player has chosen earlier AND the
621          * random songs this round, AND all random songs from elimination rounds
622          * (ie. rounds with only one group). (Phew!) This doesn't solve problems
623          * we'd face with more than one chosen song, but it should be good enough.
624          *
625          * After we've found the max and min scores for all players, it's a simple
626          * matter of sorting; the best attainable rank for player X is obtained if 
627          * X gets max score and all others get min score, the worst attainable rank
628          * is obtained if X gets min score and all others get max score.
629          */
630         std::vector<unsigned> max_score, min_score;
631         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
632                 unsigned min_score_tp = 0, max_score_tp = 0;
633                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j) {
634                         if (j->score != -1) {
635                                 // already given
636                                 min_score_tp += j->score;
637                                 max_score_tp += j->score;
638                         } else {
639                                 unsigned max_score_this_song;
640                                 if (j->song.id != -1) {
641                                         // random song, or we know what song the player picked
642                                         max_score_this_song = song_scores[j->song.id];
643                                 } else {
644                                         max_score_this_song = player_scores[i->id];
645                                 }
646                                 max_score_tp += max_score_this_song;
647                         }
648                 }
649                 max_score.push_back(max_score_tp);
650                 min_score.push_back(min_score_tp);
651         }
652
653         // now finally find min and max rank, and draw it all
654         unsigned show_players = get_show_players(group);
655         unsigned y = (show_players <= 7) ? 140 : (140 - (show_players - 7) * 5);
656         for (unsigned i = 0; i < group.players.size() && (i/num_machines) < show_players; ++i) {
657                 unsigned best_rank = 1, worst_rank = 1;
658                 for (unsigned j = 0; j < group.players.size(); ++j) {
659                         if (i == j)
660                                 continue;
661
662                         if (max_score[i] < min_score[j])
663                                 ++best_rank;
664                         if (min_score[i] <= max_score[j])
665                                 ++worst_rank;
666                 }
667
668                 char text[16];
669                 if (best_rank == worst_rank)
670                         std::sprintf(text, "%u", best_rank);
671                 else
672                         std::sprintf(text, "%u-%u", best_rank, worst_rank);
673                 
674                 if (i % num_machines != machine)
675                         continue;
676                 
677                 // find out where to place this
678                 unsigned x = 40 + colwidth[0];
679                 for (unsigned j = 1; j <= num_scores + 1; ++j)
680                         x += colwidth[j] + 20;
681
682                 // minor correction :-)
683                 if (num_scores <= 1)
684                         x -= 20;
685                 
686                 unsigned this_width = my_draw_text(text, NULL, 22.0);
687                 my_draw_text_deferred(td, text, 22.0, x + colwidth[num_scores + 2] / 2 - this_width / 2, y);
688
689                 if (show_players > 7)
690                         y += 40 - (show_players - 7) * 4;
691                 else 
692                         y += 40;
693         }
694
695         if (players_per_machine == 2)
696                 draw_next_up_versus(buf, group, song_scores, player_scores, max_score, min_score);
697         else 
698                 draw_next_up_single(buf, group, song_scores, player_scores, max_score, min_score);
699         
700         valid = true;
701         draw_all_deferred_text(buf, td, last_text);
702         last_text = td;
703 }
704
705 int GroupScreen::get_priority()
706 {
707         return 10;
708 }