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