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