Fetch the actual high score from the database when showing high score on the group...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 20 Feb 2005 17:35:24 +0000 (17:35 +0000)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 20 Feb 2005 17:35:24 +0000 (17:35 +0000)
bigscreen/Makefile
bigscreen/fetch_highscore.cpp [new file with mode: 0644]
bigscreen/fetch_highscore.h [new file with mode: 0644]
bigscreen/groupscreen.cpp

index 8cdf717..56cd5de 100644 (file)
@@ -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_max_score_for_song.o fetch_max_score_for_player.o fetch_group.o fetch_needs_update.o fonts.o groupscreen.o splitscreen.o rotatescreen.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 fetch_needs_update.o fetch_highscore.o fonts.o groupscreen.o splitscreen.o rotatescreen.o screen.o
 
 all: ccbs-bigscreen
 
diff --git a/bigscreen/fetch_highscore.cpp b/bigscreen/fetch_highscore.cpp
new file mode 100644 (file)
index 0000000..04353cc
--- /dev/null
@@ -0,0 +1,21 @@
+#include "fetch_highscore.h"
+
+FetchHighscore::FetchHighscore(unsigned song, Highscore *hs)
+       : song(song), hs(hs) {}
+       
+void FetchHighscore::operator() (pqxx::transaction<> &t)
+{
+       pqxx::result res( t.exec("SELECT score,nick,tournamentname FROM scores NATURAL JOIN players NATURAL JOIN tournaments WHERE song=" + pqxx::to_string(song) + " AND score IS NOT NULL ORDER BY score DESC LIMIT 1") );
+
+       hs->song = song;
+       
+       try {
+               pqxx::result::tuple highscore = res.at(0);
+               
+               hs->score = highscore["score"].as(hs->score);
+               hs->nick = highscore["nick"].as(hs->nick);
+               hs->tournament_name = highscore["tournamentname"].as(hs->tournament_name);
+        } catch (PGSTD::out_of_range &e) {
+               hs->score = -1;
+       }
+}
diff --git a/bigscreen/fetch_highscore.h b/bigscreen/fetch_highscore.h
new file mode 100644 (file)
index 0000000..a8cf706
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef _FETCH_HIGHSCORE_H
+#define _FETCH_HIGHSCORE_H 1
+
+#include <pqxx/transactor>
+#include "widestring.h"
+
+struct Highscore {
+       unsigned song;
+       int score;
+       widestring nick, tournament_name;
+};
+
+/* A transactor that fetches the all-time high score for a song */
+class FetchHighscore : public pqxx::transactor<> {
+private:
+       unsigned song;
+       Highscore *hs;
+
+public:
+       FetchHighscore(unsigned song, Highscore *hs);
+       void operator() (pqxx::transaction<> &t);
+};
+
+#endif /* !defined(_FETCH_MAX_SCORE_FOR_SONG_H) */
index 2b4aa7d..cd6c86e 100644 (file)
@@ -6,6 +6,7 @@
 #include "fetch_max_score_for_song.h"
 #include "fetch_max_score_for_player.h"
 #include "fetch_needs_update.h"
+#include "fetch_highscore.h"
 #include "fonts.h"
 
 GroupScreen::GroupScreen(pqxx::connection &conn, unsigned tournament, unsigned round, unsigned parallel)
@@ -314,11 +315,15 @@ void GroupScreen::draw(unsigned char *buf)
                        this_width = my_draw_text(next_song->song.title, NULL, 20.0);
                        my_draw_text(next_song->song.title, buf, 20.0, 400 - this_width/2, 457);
 
-                       // fetch the high score later
-                       text = widestring("High score: ") + widestring(pqxx::to_string(1234)) +
-                               widestring(", by dufF in Challenge Cup 1, 2004");
-                       this_width = my_draw_text(text, NULL, 16.0);
-                       my_draw_text(text, buf, 16.0, 400 - this_width/2, 487);
+                       Highscore hs;
+                       conn.perform(FetchHighscore(next_song->song.id, &hs));
+                       
+                       if (hs.score != -1) {
+                               text = widestring("High score: ") + widestring(pqxx::to_string(hs.score)) +
+                                       widestring(", by ") + hs.nick + widestring(" in ") + hs.tournament_name;
+                               this_width = my_draw_text(text, NULL, 16.0);
+                               my_draw_text(text, buf, 16.0, 400 - this_width/2, 487);
+                       }
                }
 
                // only show lead/win/qualify for the last song