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