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