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