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