Wt examples  3.2.3
/home/koen/project/wt/public-git/wt/examples/hangman/HighScoresWidget.C
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 
00007 #include <boost/lexical_cast.hpp>
00008 
00009 #include <Wt/WText>
00010 #include <Wt/WTable>
00011 #include <Wt/Dbo/Dbo>
00012 
00013 #include "HighScoresWidget.h"
00014 #include "Session.h"
00015 
00016 using namespace Wt;
00017 
00018 HighScoresWidget::HighScoresWidget(Session *session, WContainerWidget *parent):
00019   WContainerWidget(parent),
00020   session_(session)
00021 {
00022   setContentAlignment(AlignCenter);
00023   setStyleClass("highscores");
00024 }
00025 
00026 void HighScoresWidget::update()
00027 {
00028   clear();
00029   
00030   new WText("<h2>Hall of fame</h2>", this);
00031   
00032   int ranking = session_->findRanking();
00033   
00034   std::string yourScore;
00035   if (ranking == 1)
00036     yourScore = "Congratulations! You are currently leading the pack.";
00037   else {
00038     yourScore = "You are currently ranked number "
00039       + boost::lexical_cast<std::string>(ranking)
00040       + ". Almost there !";
00041   }
00042 
00043   WText *score = new WText("<p>" + yourScore + "</p>", this);
00044   score->addStyleClass("score");
00045   
00046   std::vector<User> top = session_->topUsers(20);
00047 
00048   WTable *table = new WTable(this);
00049 
00050   new WText("Rank", table->elementAt(0, 0));
00051   new WText("User", table->elementAt(0, 1));
00052   new WText("Games", table->elementAt(0, 2));
00053   new WText("Score", table->elementAt(0, 3));
00054   new WText("Last game", table->elementAt(0, 4));
00055   table->setHeaderCount(1);
00056 
00057   int formerScore = -1;
00058   int rank = 0;
00059   for (unsigned i = 0; i < top.size(); i++) {
00060     User u = top[i];
00061 
00062     if (u.score != formerScore) {
00063       formerScore = u.score;
00064       ++rank;
00065     }
00066     
00067     int row = table->rowCount();
00068     new WText(boost::lexical_cast<std::string>(rank),
00069               table->elementAt(row, 0));
00070     new WText(u.name, table->elementAt(row, 1));
00071     new WText(boost::lexical_cast<std::string>(u.gamesPlayed),
00072               table->elementAt(row, 2));
00073     new WText(boost::lexical_cast<std::string>(u.score),
00074               table->elementAt(row, 3));
00075     if (!u.lastGame.isNull())
00076       new WText(u.lastGame.timeTo(WDateTime::currentDateTime())
00077                 + " ago", table->elementAt(row, 4));
00078     else
00079       new WText("---", table->elementAt(row, 4));
00080     
00081     if (session_->login().loggedIn() && session_->userName() == u.name)
00082       table->rowAt(row)->setId("self");
00083   }
00084 
00085   WText *fineprint = new WText(tr("highscore.info"), this);
00086   fineprint->addStyleClass("fineprint");
00087 }

Generated on Thu Nov 1 2012 for the C++ Web Toolkit (Wt) by doxygen 1.7.5.1