Wt examples 3.3.12
HighScoresWidget.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
3 *
4 * See the LICENSE file for terms of use.
5 */
6
7#include <boost/lexical_cast.hpp>
8
9#include <Wt/WText>
10#include <Wt/WTable>
11#include <Wt/Dbo/Dbo>
12
13#include "HighScoresWidget.h"
14#include "Session.h"
15
16using namespace Wt;
17
19 WContainerWidget(parent),
20 session_(session)
21{
23 setStyleClass("highscores");
24}
25
27{
28 clear();
29
30 new WText("<h2>Hall of fame</h2>", this);
31
32 int ranking = session_->findRanking();
33
34 std::string yourScore;
35 if (ranking == 1)
36 yourScore = "Congratulations! You are currently leading the pack.";
37 else {
38 yourScore = "You are currently ranked number "
39 + boost::lexical_cast<std::string>(ranking)
40 + ". Almost there !";
41 }
42
43 WText *score = new WText("<p>" + yourScore + "</p>", this);
44 score->addStyleClass("score");
45
46 std::vector<User> top = session_->topUsers(20);
47
48 WTable *table = new WTable(this);
49
50 new WText("Rank", table->elementAt(0, 0));
51 new WText("User", table->elementAt(0, 1));
52 new WText("Games", table->elementAt(0, 2));
53 new WText("Score", table->elementAt(0, 3));
54 new WText("Last game", table->elementAt(0, 4));
55 table->setHeaderCount(1);
56
57 int formerScore = -1;
58 int rank = 0;
59 for (unsigned i = 0; i < top.size(); i++) {
60 User u = top[i];
61
62 if (u.score != formerScore) {
63 formerScore = u.score;
64 ++rank;
65 }
66
67 int row = table->rowCount();
68 new WText(boost::lexical_cast<std::string>(rank),
69 table->elementAt(row, 0));
70 new WText(u.name, table->elementAt(row, 1));
71 new WText(boost::lexical_cast<std::string>(u.gamesPlayed),
72 table->elementAt(row, 2));
73 new WText(boost::lexical_cast<std::string>(u.score),
74 table->elementAt(row, 3));
75 if (!u.lastGame.isNull())
77 + " ago", table->elementAt(row, 4));
78 else
79 new WText("---", table->elementAt(row, 4));
80
81 if (session_->login().loggedIn() && session_->userName() == u.name)
82 table->rowAt(row)->setId("self");
83 }
84
85 WText *fineprint = new WText(tr("highscore.info"), this);
86 fineprint->addStyleClass("fineprint");
87}
HighScoresWidget(Session *session, Wt::WContainerWidget *parent=0)
std::vector< User > topUsers(int limit)
Definition Session.C:173
std::string userName() const
Definition Session.C:151
int findRanking()
Definition Session.C:195
Wt::Auth::Login & login()
Definition Session.h:32
Definition User.h:23
std::string name
Definition User.h:27
long long score
Definition User.h:29
Wt::WDateTime lastGame
Definition User.h:30
int gamesPlayed
Definition User.h:28
bool loggedIn() const
virtual void clear()
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
bool isNull() const
WString timeTo(const WDateTime &other, int minValue=1) const
static WDateTime currentDateTime()
void setId(const std::string &id)
int rowCount() const
WTableCell * elementAt(int row, int column)
void setHeaderCount(int count, Orientation orientation=Horizontal)
WTableRow * rowAt(int row)
virtual void addStyleClass(const WString &styleClass, bool force=false)
virtual void setStyleClass(const WString &styleClass)
static WString tr(const char *key)
AlignCenter

Generated on Fri May 17 2024 for the C++ Web Toolkit (Wt) by doxygen 1.9.8