hangman/HighScoresWidget.C

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005 Wim Dumon
00003  *
00004  * See the LICENSE file for terms of use.
00005  */
00006 
00007 #include <boost/lexical_cast.hpp>
00008 #include <sstream>
00009 
00010 #include <WBreak>
00011 #include <WText>
00012 #include <WTable>
00013 #include <WTableCell>
00014 #include <WCssDecorationStyle>
00015 
00016 #include "HighScoresWidget.h"
00017 #include "HangmanDb.h"
00018 
00019 using namespace std;
00020 
00021 HighScoresWidget::HighScoresWidget(const std::wstring &user,
00022                                    WContainerWidget *parent):
00023    WContainerWidget(parent),
00024    User(user)
00025 {
00026    setContentAlignment(AlignCenter);
00027    setStyleClass(L"highscores");
00028 }
00029 
00030 void HighScoresWidget::update()
00031 {
00032    clear();
00033 
00034    WText *title = new WText(L"Hall of fame", this);
00035    title->decorationStyle().font().setSize(WFont::XLarge);
00036    title->setMargin(WLength(10), Top | Bottom);
00037 
00038    new WBreak(this);
00039 
00040    HangmanDb::Score s = HangmanDb::getUserPosition(User);
00041 
00042    std::wstring yourScore;
00043    if (s.number == 1)
00044      yourScore = L"Congratulations! You are currently leading the pack.";
00045    else {
00046      yourScore = L"You are currently ranked number "
00047        + boost::lexical_cast<std::wstring>(s.number)
00048        + L". Almost there !";
00049    }
00050 
00051    WText *score = new WText(L"<p>" + yourScore + L"</p>", this);
00052    score->decorationStyle().font().setSize(WFont::Large);
00053 
00054    std::vector<HangmanDb::Score> top = HangmanDb::getHighScores(20);
00055 
00056    WTable *table = new WTable(this);
00057    new WText(L"Rank", table->elementAt(0, 0));
00058    new WText(L"User", table->elementAt(0, 1));
00059    new WText(L"Games", table->elementAt(0, 2));
00060    new WText(L"Score", table->elementAt(0, 3));
00061    new WText(L"Last game", table->elementAt(0, 4));
00062    for(unsigned int i = 0; i < top.size(); ++i) {
00063       new WText(boost::lexical_cast<wstring>(top[i].number),
00064                 table->elementAt(i + 1, 0));
00065       new WText(top[i].user, table->elementAt(i + 1, 1));
00066       new WText(boost::lexical_cast<std::wstring>(top[i].numgames),
00067                 table->elementAt(i+ 1, 2));
00068       new WText(boost::lexical_cast<std::wstring>(top[i].score),
00069                 table->elementAt(i + 1, 3));
00070       new WText(top[i].lastseen, table->elementAt(i + 1, 4));
00071    }
00072 
00073    table->resize(WLength(60, WLength::FontEx), WLength());
00074    table->setMargin(WLength(20), Top | Bottom);
00075    table->decorationStyle().setBorder(WBorder(WBorder::Solid));
00076 
00077    /*
00078     * Apply cell styles
00079     */
00080    for (int row = 0; row < table->numRows(); ++row) {
00081      for (int col = 0; col < table->numColumns(); ++col) {
00082        WTableCell *cell = table->elementAt(row, col);
00083        cell->setContentAlignment(AlignMiddle | AlignCenter);
00084 
00085        if (row == 0)
00086          cell->setStyleClass(L"highscoresheader");
00087 
00088        if (row == s.number)
00089          cell->setStyleClass(L"highscoresself");
00090      }
00091    }
00092 
00093    WText *fineprint
00094      = new WText(L"<p>For each game won, you gain 20 points, "
00095                  L"minus one point for each wrong letter guess.<br />"
00096                  L"For each game lost, you loose 10 points, so you "
00097                  L"better try hard to guess the word!</p>", this);
00098    fineprint->decorationStyle().font().setSize(WFont::Smaller);
00099 }

Generated on Sun Jul 1 19:37:23 2007 for Wt by doxygen 1.4.7