From: Steinar H. Gunderson Date: Sat, 5 Mar 2005 00:09:06 +0000 (+0000) Subject: Yet more refactoring. X-Git-Url: https://git.sesse.net/?p=ccbs;a=commitdiff_plain;h=11b5968454610745e6cd47fb71edea1218f835dd;ds=sidebyside Yet more refactoring. --- diff --git a/bigscreen/groupscreen.cpp b/bigscreen/groupscreen.cpp index e4f7b4a..03b61db 100644 --- a/bigscreen/groupscreen.cpp +++ b/bigscreen/groupscreen.cpp @@ -216,6 +216,28 @@ void GroupScreen::find_column_widths(const Group &group, std::vector & } } +/* Find the first player with the fewest songs played and part of this machine. */ +const Player *GroupScreen::get_next_player(const Group &group) +{ + unsigned min_played_songs = 9999; + const Player *next_player = NULL; + unsigned m = 0; + for (std::vector::const_iterator i = group.players.begin(); i != group.players.end(); ++i) { + unsigned this_played = 0; + for (std::vector::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j) { + if (j->score != -1) + ++this_played; + } + + if ((m++ % num_machines == machine) && this_played < min_played_songs) { + min_played_songs = this_played; + next_player = &(*i); + } + } + + return next_player; +} + /* * At the bottom, for a single player, is "who's playing, what will he/she be * playing, and optionally, how much to lead/win and how much to secure @@ -241,34 +263,28 @@ void GroupScreen::draw_next_up_single(unsigned char *buf, const Group &group, co { unsigned num_scores = group.players[0].scores.size(); - /* Find the first player with the fewest songs played and part of this machine. */ - unsigned min_played_songs = 9999, num_random_songs = 0; - const Player *next_player = NULL; - unsigned m = 0; - for (std::vector::const_iterator i = group.players.begin(); i != group.players.end(); ++i) { - unsigned this_played = 0, this_random_songs = 0; - for (std::vector::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j) { - if (j->score != -1) - ++this_played; - if (!j->chosen) - ++this_random_songs; - } - - if ((m++ % num_machines == machine) && this_played < min_played_songs) { - min_played_songs = this_played; - next_player = &(*i); - num_random_songs = this_random_songs; // should be equal for all - } + // Find out how many random songs there are (equal for all players). + unsigned num_random_songs = 0; + for (std::vector::const_iterator i = group.players[0].scores.begin(); i != group.players[0].scores.end(); ++i) { + if (!i->chosen) + ++num_random_songs; } - /* Find out what song this player is supposed to play next; try random songs first */ + /* + * Find out which player is next, and what song he she is supposed to play. First + * try random songs. + */ + const Player *next_player = get_next_player(group); const Score *next_song = NULL; + unsigned num_played = 0; // will not always be completely accurate, but always as accurate as we need it :-) for (unsigned i = 0; i < num_random_songs; ++i) { unsigned j = (i + next_player->position - 1) % num_random_songs; if (next_player->scores[j].score == -1) { next_song = &(next_player->scores[j]); break; + } else { + ++num_played; } } @@ -279,6 +295,8 @@ void GroupScreen::draw_next_up_single(unsigned char *buf, const Group &group, co if (next_player->scores[j].score == -1) { next_song = &(next_player->scores[j]); break; + } else { + ++num_played; } } } @@ -304,7 +322,7 @@ void GroupScreen::draw_next_up_single(unsigned char *buf, const Group &group, co } // only show lead/win/qualify for the last song - if (min_played_songs == num_scores - 1) { + if (num_played == num_scores - 1) { /* * Find out how much we need to lead, how much we need to be guaranteed * to win the group, and how much we need to secure qualification. (FIXME: diff --git a/bigscreen/groupscreen.h b/bigscreen/groupscreen.h index 9c32629..09d8d97 100644 --- a/bigscreen/groupscreen.h +++ b/bigscreen/groupscreen.h @@ -25,6 +25,7 @@ private: void draw_main_heading(std::vector &td); void draw_column_headings(std::vector &td, const Group &group, const std::vector &colwidth); void draw_scores(std::vector &td, const Group &group, const std::vector &colwidth); + const Player *get_next_player(const Group &group); void draw_next_up_single(unsigned char *buf, const Group &group, const std::vector &colwidth, std::map &song_scores, std::map &player_scores, const std::vector &max_score, const std::vector &min_score);