From: Steinar H. Gunderson Date: Sun, 20 Feb 2005 00:02:09 +0000 (+0000) Subject: Add (a rather sizable chunk of) code for determining the possible best and worst... X-Git-Url: https://git.sesse.net/?p=ccbs;a=commitdiff_plain;h=76518bb5379322ff9015d8cb4eb019724b31c8ec Add (a rather sizable chunk of) code for determining the possible best and worst ranks for all players. --- diff --git a/bigscreen/Makefile b/bigscreen/Makefile index b6697ab..2303ff9 100644 --- a/bigscreen/Makefile +++ b/bigscreen/Makefile @@ -4,7 +4,7 @@ CPPFLAGS=-I/usr/include/postgresql $(shell freetype-config --cflags) -Itinyptc/ CXXFLAGS=-g -Wall LDFLAGS=-L/usr/X11R6/lib LIBS=$(shell freetype-config --libs) $(shell libpq3-config) -lpqxx tinyptc/libtinyptc.a -lX11 -CCBS_BIGSCREEN_OBJS=ccbs_bigscreen.o flagtrigger.o widestring.o fetch_current_tournament.o fetch_list_of_active_groups.o fetch_group.o fonts.o groupscreen.o screen.o +CCBS_BIGSCREEN_OBJS=ccbs_bigscreen.o flagtrigger.o widestring.o fetch_current_tournament.o fetch_list_of_active_groups.o fetch_max_score_for_song.o fetch_max_score_for_player.o fetch_group.o fonts.o groupscreen.o screen.o all: ccbs-bigscreen diff --git a/bigscreen/fetch_max_score_for_player.cpp b/bigscreen/fetch_max_score_for_player.cpp new file mode 100644 index 0000000..70472b0 --- /dev/null +++ b/bigscreen/fetch_max_score_for_player.cpp @@ -0,0 +1,14 @@ +#include "fetch_max_score_for_player.h" + +FetchMaxScoreForPlayer::FetchMaxScoreForPlayer(unsigned tournament, unsigned player, unsigned round, unsigned *score) + : tournament(tournament), player(player), round(round), score(score) {} + +void FetchMaxScoreForPlayer::operator() (pqxx::transaction<> &t) +{ + pqxx::result res( t.exec("SELECT MAX(feetrating)*1000 AS max_score FROM songratings WHERE " + "machine=( SELECT machine FROM tournaments WHERE tournament=" + pqxx::to_string(tournament) + ") AND " + "(song NOT IN ( SELECT song FROM scores WHERE tournament=" + pqxx::to_string(tournament) + " AND song IS NOT NULL AND ( " + "round=" + pqxx::to_string(round) + " OR parallel=0 OR ( player=" + pqxx::to_string(player) + " AND chosen='t' ) ) ) )") ); + + *score = res.at(0)["max_score"].as(*score); +} diff --git a/bigscreen/fetch_max_score_for_player.h b/bigscreen/fetch_max_score_for_player.h new file mode 100644 index 0000000..36122b4 --- /dev/null +++ b/bigscreen/fetch_max_score_for_player.h @@ -0,0 +1,16 @@ +#ifndef _FETCH_MAX_SCORE_FOR_PLAYER_H +#define _FETCH_MAX_SCORE_FOR_PLAYER_H 1 + +#include + +/* A transactor that fetches the maximum score for a song */ +class FetchMaxScoreForPlayer : public pqxx::transactor<> { +private: + unsigned tournament, player, round, *score; + +public: + FetchMaxScoreForPlayer(unsigned tournament, unsigned player, unsigned round, unsigned *score); + void operator() (pqxx::transaction<> &t); +}; + +#endif /* !defined(_FETCH_MAX_SCORE_FOR_PLAYER_H) */ diff --git a/bigscreen/fetch_max_score_for_song.cpp b/bigscreen/fetch_max_score_for_song.cpp new file mode 100644 index 0000000..efac925 --- /dev/null +++ b/bigscreen/fetch_max_score_for_song.cpp @@ -0,0 +1,13 @@ +#include "fetch_max_score_for_song.h" + +FetchMaxScoreForSong::FetchMaxScoreForSong(unsigned tournament, unsigned song, unsigned *score) + : tournament(tournament), song(song), score(score) {} + +void FetchMaxScoreForSong::operator() (pqxx::transaction<> &t) +{ + pqxx::result res( t.exec("SELECT MAX(feetrating)*1000 AS max_score FROM songratings WHERE song=" + + pqxx::to_string(song) + " AND machine=( SELECT machine FROM tournaments WHERE tournament=" + + pqxx::to_string(tournament) + ")") ); + + *score = res.at(0)["max_score"].as(*score); +} diff --git a/bigscreen/fetch_max_score_for_song.h b/bigscreen/fetch_max_score_for_song.h new file mode 100644 index 0000000..f3b9f77 --- /dev/null +++ b/bigscreen/fetch_max_score_for_song.h @@ -0,0 +1,16 @@ +#ifndef _FETCH_MAX_SCORE_FOR_SONG_H +#define _FETCH_MAX_SCORE_FOR_SONG_H 1 + +#include + +/* A transactor that fetches the maximum score for a song */ +class FetchMaxScoreForSong : public pqxx::transactor<> { +private: + unsigned tournament, song, *score; + +public: + FetchMaxScoreForSong(unsigned tournament, unsigned song, unsigned *score); + void operator() (pqxx::transaction<> &t); +}; + +#endif /* !defined(_FETCH_MAX_SCORE_FOR_SONG_H) */ diff --git a/bigscreen/groupscreen.cpp b/bigscreen/groupscreen.cpp index 26d769a..d8450f8 100644 --- a/bigscreen/groupscreen.cpp +++ b/bigscreen/groupscreen.cpp @@ -2,6 +2,8 @@ #include "groupscreen.h" #include "fetch_group.h" +#include "fetch_max_score_for_song.h" +#include "fetch_max_score_for_player.h" #include "fonts.h" GroupScreen::GroupScreen(pqxx::connection &conn, unsigned tournament, unsigned round, unsigned parallel) @@ -136,6 +138,78 @@ void GroupScreen::draw(unsigned char *buf) y += 40; } + /* + * Approximate (but probably working quite well in practice) heuristic + * for finding the min and max rank of a player works as follows: + * + * First of all, find out, for each player in the group, what the + * maximum remaining score possibly can be (the minimum score is of + * course identical to the player's current total). For a random song, + * this is of course 1000 * (maximum feet rating) (but of course, that + * depends on whether we can play single or double! for now, assume + * double is okay, but this logic will be deferred to FetchMaxScore + * anyhow); for a random song, we simply pick the highest-ranking song + * we can find, EXCEPT those the player has chosen earlier AND the + * random songs this round, AND all random songs from elimination rounds + * (ie. rounds with only one group). (Phew!) This doesn't solve problems + * we'd face with more than one chosen song, but it should be good enough. + * + * After we've found the max and min scores for all players, it's a simple + * matter of sorting; the best attainable rank for player X is obtained if + * X gets max score and all others get min score, the worst attainable rank + * is obtained if X gets min score and all others get max score. + * + * This is a bit SQL-heavy, but heck... + */ + std::vector max_score, min_score; + for (std::vector::const_iterator i = group.players.begin(); i != group.players.end(); ++i) { + unsigned min_score_tp = 0, max_score_tp = 0; + for (std::vector::const_iterator j = i->scores.begin(); j != i->scores.end(); ++j, ++col) { + if (j->score != -1) { + // already given + min_score_tp += j->score; + max_score_tp += j->score; + } else { + unsigned max_score_this_song; + if (j->song.id != -1) { + // random song, or we know what song the player picked + conn.perform(FetchMaxScoreForSong(tournament, j->song.id, &max_score_this_song)); + } else { + conn.perform(FetchMaxScoreForPlayer(tournament, i->id, round, &max_score_this_song)); + } + max_score_tp += max_score_this_song; + } + } + max_score.push_back(max_score_tp); + min_score.push_back(min_score_tp); + } + + // now finally find min and max rank, and draw it all + y = 140; + for (unsigned i = 0; i < group.players.size(); ++i) { + unsigned best_rank = 1, worst_rank = 1; + for (unsigned j = 0; j < group.players.size(); ++j) { + if (i == j) + continue; + + if (max_score[i] < min_score[j]) + ++best_rank; + if (min_score[i] <= max_score[j]) + ++worst_rank; + } + + char text[16]; + if (best_rank == worst_rank) + std::sprintf(text, "%u", best_rank); + else + std::sprintf(text, "%u-%u", best_rank, worst_rank); + + unsigned this_width = my_draw_text(text, NULL, 22.0); + my_draw_text(text, buf, 22.0, x + width[num_scores + 2] / 2 - this_width / 2, y); + + y += 40; + } + valid = true; }