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