]> git.sesse.net Git - ccbs/blob - bigscreen/groupscreen.cpp
4b108f56784187a4b9e72409bbb43afecf3c6e0a
[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 void GroupScreen::draw_main_heading(std::vector<TextDefer> &td)
40 {
41         char heading[64];
42         if (num_machines == 1) {
43                 if (parallel == 0) {
44                         std::sprintf(heading, "Round %u", round);
45                 } else {
46                         std::sprintf(heading, "Round %u, Group %u", round, parallel);
47                 }
48         } else {
49                 if (parallel == 0) {
50                         std::sprintf(heading, "Round %u, Machine %u", round, machine + 1);
51                 } else {
52                         std::sprintf(heading, "Round %u, Group %u, Machine %u", round, parallel, machine + 1);
53                 }
54         }
55
56         unsigned width = my_draw_text(heading, NULL, 40.0);
57         my_draw_text_deferred(td, heading, 40.0, LOGICAL_SCREEN_WIDTH/2 - width/2, 60);
58 }
59
60 // make column headings from the first player's songs
61 void GroupScreen::draw_column_headings(std::vector<TextDefer> &td, const Group &group, const std::vector<unsigned> &colwidth)
62 {
63         unsigned num_scores = group.players[0].scores.size();
64
65         unsigned col = 1;
66         unsigned x = 40 + colwidth[0];
67         for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
68                 if (!i->chosen) {
69                         unsigned this_width = my_draw_text(i->song.short_title, NULL, 12.0);
70                         my_draw_text_deferred(td, i->song.short_title, 12.0, x + colwidth[col] / 2 - this_width / 2, 100);
71                 }
72                 x += colwidth[col] + 20;
73         }
74
75         my_draw_text_deferred(td, "Total", 12.0, x + colwidth[num_scores + 1] / 2 - my_draw_text("Total", NULL, 12.0) / 2, 100);
76         x += colwidth[num_scores + 1] + 20;
77         my_draw_text_deferred(td, "Rank", 12.0, x + colwidth[num_scores + 2] / 2 - my_draw_text("Rank", NULL, 12.0) / 2, 100);
78 }       
79
80 /*
81  * Find out how wide each column has to be. First try unlimited width (ie.
82  * long titles for everything); if that gets too long, try again with short
83  * titles for chosen songs.
84  */
85 void GroupScreen::find_column_widths(const Group &group, std::vector<unsigned> &colwidth)
86 {
87         unsigned num_scores;
88         unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
89         unsigned sumcolwidth;
90         
91         for (unsigned mode = 0; mode < 2; ++mode) {
92                 for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
93                         unsigned col = 1;
94                         
95                         if (colwidth.size() == 0)
96                                 colwidth.push_back(0);
97                         
98                         colwidth[0] = std::max(colwidth[0], my_draw_text(i->nick, NULL, 18.0));
99
100                         for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
101                                 if (colwidth.size() < col+1)
102                                         colwidth.push_back(0);
103                                         
104                                 if (j->chosen) {
105                                         colwidth[col] = std::max(colwidth[col], my_draw_text((mode == 0) ? j->song.title : j->song.short_title, NULL, 12.0) + 
106                                                         max_num_width + 10);
107                                 } else {                
108                                         colwidth[col] = std::max(colwidth[col], my_draw_text(j->song.short_title, NULL, 12.0));
109                                         colwidth[col] = std::max(colwidth[col], max_num_width);
110                                 }
111                         }
112                 }
113
114                 num_scores = group.players[0].scores.size();
115
116                 if (colwidth.size() < num_scores + 2) {
117                         colwidth.push_back(0);
118                         colwidth.push_back(0);
119                 }
120                 
121                 colwidth[num_scores + 1] = std::max(my_draw_text("Total", NULL, 12.0), max_num_width);
122                 colwidth[num_scores + 2] = my_draw_text("Rank", NULL, 12.0);
123
124                 // if we're at long titles and that works, don't try the short ones
125                 sumcolwidth = 0;
126                         
127                 for (unsigned i = 0; i <= num_scores + 2; ++i)
128                         sumcolwidth += colwidth[i] + 20;
129                         
130                 if (sumcolwidth < 780)
131                         break;
132         }
133
134         /* 
135          * If we have space to go, distribute as much as we can to the chosen song column, so we won't have
136          * total and rank jumping around.
137          */
138         if (sumcolwidth < 780) {
139                 int first_chosen_col = -1;
140                 unsigned col = 1;
141
142                 for (std::vector<Score>::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i, ++col) {
143                         if (i->chosen) {
144                                 first_chosen_col = col;
145                                 break;
146                         }
147                 }
148
149                 if (first_chosen_col != -1) {
150                         colwidth[first_chosen_col] += 780 - sumcolwidth;
151                 }
152         }
153 }
154
155 void GroupScreen::draw(unsigned char *buf, unsigned width, unsigned height)
156 {
157         std::vector<TextDefer> td;
158         
159         scores_changed.reset_flag();
160         set_screen_size(width, height);
161
162         /*
163          * We'll probably need some values from here later on (although not all), just fetch them
164          * all while we're at it.
165          */
166         std::map<unsigned, unsigned> song_scores, player_scores;
167         conn.perform(FetchMaxScoreForSongs(tournament, &song_scores));
168         conn.perform(FetchMaxScoreForPlayers(tournament, round, &player_scores));
169         
170         Group group;
171         conn.perform(FetchGroup(tournament, round, parallel, &group));
172         gettimeofday(&last_updated, NULL);
173
174         memset(buf, 0, width * height * 4);
175
176         std::vector<unsigned> colwidth;
177         
178         draw_main_heading(td);
179         find_column_widths(group, colwidth);
180         draw_column_headings(td, group, colwidth);
181         
182         unsigned max_num_width = my_draw_text("8888", NULL, 22.0);
183         unsigned num_scores = group.players[0].scores.size();
184
185         // show all the players and the scores
186         unsigned num_players_this_machine = (group.players.size() + num_machines - machine - 1) / num_machines;
187         unsigned show_players = std::min(num_players_this_machine, 9U);
188         unsigned y = (show_players <= 7) ? 140 : (140 - (show_players - 7) * 5);
189         
190         unsigned row = 0, m = 0, x;
191         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end() && row < 9; ++i) {
192                 if (m++ % num_machines != machine)
193                         continue;
194
195                 my_draw_text_deferred(td, i->nick, 18.0, 20, y);
196
197                 x = 40 + colwidth[0];
198
199                 unsigned col = 1;
200                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) {
201                         char text[16] = "";
202                         if (j->score != -1) {
203                                 std::sprintf(text, "%u", j->score);
204                         }
205         
206                         unsigned this_width = my_draw_text(text, NULL, 22.0);
207                         if (j->chosen) {
208                                 my_draw_text_deferred(td, text, 22.0, x + max_num_width - this_width, y);
209
210                                 // draw the long name if we can, otherwise use the short one
211                                 if (my_draw_text(j->song.title, NULL, 12.0) > (colwidth[col] - 10 - max_num_width)) {
212                                         my_draw_text_deferred(td, j->song.short_title, 12.0, x + max_num_width + 10, y);
213                                 } else {
214                                         my_draw_text_deferred(td, j->song.title, 12.0, x + max_num_width + 10, y);
215                                 }
216                         } else {
217                                 my_draw_text_deferred(td, text, 22.0, x + colwidth[col] / 2 - this_width / 2, y);
218                         }
219                         x += colwidth[col] + 20;
220                 }
221
222                 // draw total
223                 {
224                         char text[16];
225                         std::sprintf(text, "%u", i->total);
226                         
227                         unsigned this_width = my_draw_text(text, NULL, 22.0);
228                         my_draw_text_deferred(td, text, 22.0, x + colwidth[num_scores + 1] / 2 - this_width / 2, y);
229                         x += colwidth[num_scores + 1] + 20;
230                 }
231
232                 if (show_players > 7)
233                         y += 40 - (show_players - 7) * 4;
234                 else 
235                         y += 40;
236                 ++row;
237                 
238         }
239         
240         /*
241          * Approximate (but probably working quite well in practice) heuristic
242          * for finding the min and max rank of a player works as follows:
243          *
244          * First of all, find out, for each player in the group, what the
245          * maximum remaining score possibly can be (the minimum score is of
246          * course identical to the player's current total). For a random song,
247          * this is of course 1000 * (maximum feet rating) (but of course, that
248          * depends on whether we can play single or double! for now, assume
249          * double is okay, but this logic will be deferred to FetchMaxScore
250          * anyhow); for a random song, we simply pick the highest-ranking song
251          * we can find, EXCEPT those the player has chosen earlier AND the
252          * random songs this round, AND all random songs from elimination rounds
253          * (ie. rounds with only one group). (Phew!) This doesn't solve problems
254          * we'd face with more than one chosen song, but it should be good enough.
255          *
256          * After we've found the max and min scores for all players, it's a simple
257          * matter of sorting; the best attainable rank for player X is obtained if 
258          * X gets max score and all others get min score, the worst attainable rank
259          * is obtained if X gets min score and all others get max score.
260          */
261         std::vector<unsigned> max_score, min_score;
262         for (std::vector<Player>::const_iterator i = group.players.begin(); i != group.players.end(); ++i) {
263                 unsigned min_score_tp = 0, max_score_tp = 0;
264                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j) {
265                         if (j->score != -1) {
266                                 // already given
267                                 min_score_tp += j->score;
268                                 max_score_tp += j->score;
269                         } else {
270                                 unsigned max_score_this_song;
271                                 if (j->song.id != -1) {
272                                         // random song, or we know what song the player picked
273                                         max_score_this_song = song_scores[j->song.id];
274                                 } else {
275                                         max_score_this_song = player_scores[i->id];
276                                 }
277                                 max_score_tp += max_score_this_song;
278                         }
279                 }
280                 max_score.push_back(max_score_tp);
281                 min_score.push_back(min_score_tp);
282         }
283
284         // now finally find min and max rank, and draw it all
285         y = (show_players <= 7) ? 140 : (140 - (show_players - 7) * 5);
286         for (unsigned i = 0; i < group.players.size() && (i/num_machines) < show_players; ++i) {
287                 unsigned best_rank = 1, worst_rank = 1;
288                 for (unsigned j = 0; j < group.players.size(); ++j) {
289                         if (i == j)
290                                 continue;
291
292                         if (max_score[i] < min_score[j])
293                                 ++best_rank;
294                         if (min_score[i] <= max_score[j])
295                                 ++worst_rank;
296                 }
297
298                 char text[16];
299                 if (best_rank == worst_rank)
300                         std::sprintf(text, "%u", best_rank);
301                 else
302                         std::sprintf(text, "%u-%u", best_rank, worst_rank);
303                 
304                 if (i % num_machines != machine)
305                         continue;
306                 
307                 unsigned this_width = my_draw_text(text, NULL, 22.0);
308                 my_draw_text_deferred(td, text, 22.0, x + colwidth[num_scores + 2] / 2 - this_width / 2, y);
309
310                 if (show_players > 7)
311                         y += 40 - (show_players - 7) * 4;
312                 else 
313                         y += 40;
314         }
315                 
316         /*
317          * Next up (at the bottom) is "who's playing, what will he/she be playing, and
318          * optionally, how much to lead/win and how much to secure qualification" (the
319          * last one only in the final round). We assume playing is done in a modified
320          * zigzag; all the random songs are played first in zigzag/wrapping order (player
321          * 1 song 1, player 2 song 2, player 3 song 3, player 1 song 2, player 2 song 3,
322          * player 3 song 1, etc... assuming three songs and three players) and then all
323          * the chosen songs are played (we assume only one chosen song).
324          *
325          * The lines are as follows:
326          *
327          * <player>
328          * <song>
329          * High score: <hs> by <hsplayer> at <hsevent>
330          * Needs to lead: <leadscore>
331          * Needs to secure qualification: <qualscore>
332          * Needs to win group: <winscore>
333          */
334         
335         /* Find the first player with the fewest songs played and part of this machine. */
336         unsigned min_played_songs = 9999, num_random_songs = 0;
337         Player *next_player = NULL;
338         m = 0;
339         for (std::vector<Player>::iterator i = group.players.begin(); i != group.players.end(); ++i) {
340                 unsigned this_played = 0, this_random_songs = 0;
341                 for (std::vector<Score>::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j) {
342                         if (j->score != -1)
343                                 ++this_played;
344                         if (!j->chosen)
345                                 ++this_random_songs;
346                 }
347
348                 if ((m++ % num_machines == machine) && this_played < min_played_songs) {
349                         min_played_songs = this_played;
350                         next_player = &(*i);
351                         num_random_songs = this_random_songs;  // should be equal for all
352                 }
353         }
354
355         /* Find out what song this player is supposed to play next; try random songs first */ 
356         Score *next_song = NULL;
357
358         for (unsigned i = 0; i < num_random_songs; ++i) {
359                 unsigned j = (i + next_player->position - 1) % num_random_songs;
360                 if (next_player->scores[j].score == -1) {
361                         next_song = &(next_player->scores[j]);
362                         break;
363                 }
364         }
365
366         // then all songs, if that didn't work out (slightly icky, but hey)
367         if (next_song == NULL) {
368                 for (unsigned i = 0; i < num_scores; ++i) {
369                         unsigned j = (i + next_player->position) % num_scores;
370                         if (next_player->scores[j].score == -1) {
371                                 next_song = &(next_player->scores[j]);
372                                 break;
373                         }
374                 }
375         }
376
377         if (next_song != NULL) {
378                 widestring text = widestring("Next player: ") + next_player->nick;
379                 unsigned this_width = my_draw_text(text, NULL, 24.0);
380                 my_draw_text(text, buf, 24.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 420);
381
382                 if (next_song->song.id != -1) {
383                         this_width = my_draw_text(next_song->song.title, NULL, 20.0);
384                         my_draw_text(next_song->song.title, buf, 20.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 457);
385
386                         Highscore hs;
387                         conn.perform(FetchHighscore(next_song->song.id, &hs));
388                         
389                         if (hs.score != -1) {
390                                 text = widestring("High score: ") + widestring(pqxx::to_string(hs.score)) +
391                                         widestring(", by ") + hs.nick + widestring(" in ") + hs.tournament_name;
392                                 this_width = my_draw_text(text, NULL, 16.0);
393                                 my_draw_text(text, buf, 16.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, 487);
394                         }
395                 }
396
397                 // only show lead/win/qualify for the last song
398                 if (min_played_songs == num_scores - 1) {
399                         /*
400                          * Find out how much we need to lead, how much we need to be guaranteed
401                          * to win the group, and how much we need to secure qualification. (FIXME:
402                          * do the last one :-) )
403                          */
404                         
405                         // find the best score we can get
406                         unsigned max_score_this_song;
407                         if (next_song->song.id != -1) {
408                                 // random song, or we know what song the player picked
409                                 max_score_this_song = song_scores[next_song->song.id];
410                         } else {
411                                 max_score_this_song = player_scores[next_player->id];
412                         }
413
414                         unsigned y = 520;
415                         
416                         // see what score this player must beat to lead
417                         unsigned lead_beat = 0, win_beat = 0;
418                         for (unsigned i = 0; i < group.players.size(); ++i) {
419                                 if (group.players[i].id == next_player->id)
420                                         continue;
421                                 
422                                 lead_beat = std::max(lead_beat, group.players[i].total);
423                         }
424
425                         // find the best max score among the others
426                         for (unsigned i = 0; i < group.players.size(); ++i) {
427                                 if (group.players[i].id == next_player->id)
428                                         continue;
429
430                                 win_beat = std::max(win_beat, max_score[i]);
431                         }
432
433                         /*
434                          * There's a somewhat subtle point here. Normally, what a player would be interested in
435                          * with regard to qualification would be a set of three values:
436                          *
437                          * 1. How much is the absolute minimum required to qualify, given that all others
438                          *    fail?
439                          * 2. How much will give a reasonable chance of qualifying, given the expected performance
440                          *    of all the others?
441                          * 3. How much will be enough to secure qualification, no matter what?
442                          *
443                          * Given perfect guessing, #2 would be "how much is needed to qualify"; however, it is
444                          * completely impossible to give an exact value for that, and we're not into the guessing
445                          * games. :-) #1 is often so low it's completely unrealistic (ie. far enough from #2 that
446                          * it's not interesting), but #3, the most conservative estimate, is often a good measure.
447                          * #3 is "how much is needed to _secure_ qualification", and that is usually what we
448                          * print out when it's possible.
449                          *
450                          * However, in a few situations, #1 and #3 will be the exact same value, from which it
451                          * follows (from the squeeze law, or just common sense :-) ) that #2 will be the same
452                          * value as #1 and #3. (This usually happens near or at the end of a group.) In that
453                          * case, we know the value we seek (ie. "how much is needed to qualify"), so we drop
454                          * the word "secure" and just print it as-is.
455                          *
456                          * To find #1 and #3, we sort and pick out the values we need to beat in the best and
457                          * the worst case.
458                          */
459                         int qualify_beat_worst_case = -1, qualify_beat_best_case = -1;
460
461                         if (group.num_qualifying > 0) {
462                                 std::vector<unsigned> tmp;
463                                 
464                                 for (unsigned i = 0; i < group.players.size(); ++i) {
465                                         if (group.players[i].id == next_player->id)
466                                                 continue;
467                                         tmp.push_back(max_score[i]);
468                                 }
469                                 std::sort(tmp.begin(), tmp.end());
470                                 qualify_beat_worst_case = tmp[tmp.size() - group.num_qualifying];
471                                 
472                                 std::vector<unsigned> tmp2;
473                                 for (unsigned i = 0; i < group.players.size(); ++i) {
474                                         if (group.players[i].id == next_player->id)
475                                                 continue;
476                                         tmp2.push_back(min_score[i]);
477                                 }
478
479                                 std::sort(tmp2.begin(), tmp2.end());
480                                 qualify_beat_best_case = tmp2[tmp2.size() - group.num_qualifying];
481                         }
482                         
483                         // print out the lines we can attain
484                         if (next_player->total + max_score_this_song > lead_beat && (lead_beat != win_beat)) {
485                                 int lead_need = std::max(lead_beat - next_player->total + 1, 0U);
486                         
487                                 if (lead_need > 0) {
488                                         text = widestring("Needs to lead: ") + widestring(pqxx::to_string(lead_need));
489                                         this_width = my_draw_text(text, NULL, 18.0);
490                                         my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
491
492                                         y += 30;
493                                 }
494                         }
495                         
496                         if (next_player->total + max_score_this_song > win_beat) {
497                                 int win_need = std::max(win_beat - next_player->total + 1, 0U);
498                                 
499                                 if (win_need > 0) {
500                                         text = widestring("Needs to win: ") + widestring(pqxx::to_string(win_need));
501
502                                         this_width = my_draw_text(text, NULL, 18.0);
503                                         my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
504
505                                         y += 30;
506                                 }
507                         }
508
509                         if (group.num_qualifying > 0 &&
510                             next_player->total + max_score_this_song > unsigned(qualify_beat_worst_case) &&
511                             (unsigned(qualify_beat_worst_case) != win_beat)) {
512                                 int qual_need = std::max(qualify_beat_worst_case - next_player->total + 1, 0U);
513                                 
514                                 if (qual_need > 0) {
515                                         if (qualify_beat_worst_case == qualify_beat_best_case) {
516                                                 text = widestring("Needs to qualify: ") + widestring(pqxx::to_string(qual_need));
517                                         } else {
518                                                 text = widestring("Needs to secure qualification: ") + widestring(pqxx::to_string(qual_need));
519                                         }
520                                         
521                                         this_width = my_draw_text(text, NULL, 18.0);
522                                         my_draw_text(text, buf, 18.0, (LOGICAL_SCREEN_WIDTH/2) - this_width/2, y);
523                                 
524                                         y += 30;
525                                 }
526                         }
527                 }
528         }
529         
530         valid = true;
531         draw_all_deferred_text(buf, td, last_text);
532         last_text = td;
533 }
534
535 int GroupScreen::get_priority()
536 {
537         return 10;
538 }