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