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