Wt examples  3.7.1
HighScoresWidget.C
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Emweb bv, Herent, 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 
16 using namespace Wt;
17 
19  WContainerWidget(parent),
20  session_(session)
21 {
22  setContentAlignment(AlignCenter);
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())
76  new WText(u.lastGame.timeTo(WDateTime::currentDateTime())
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 }
bool isNull() const
HighScoresWidget(Session *session, Wt::WContainerWidget *parent=0)
virtual void setStyleClass(const WString &styleClass) override
bool loggedIn() const
void setHeaderCount(int count, Orientation orientation=Orientation::Horizontal)
static WString tr(const char *key)
Wt::WDateTime lastGame
Definition: User.h:30
std::string userName() const
Definition: Session.C:151
int findRanking()
Definition: Session.C:195
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
Definition: User.h:22
virtual void addStyleClass(const WString &styleClass, bool force=false) override
int gamesPlayed
Definition: User.h:28
WTableCell * elementAt(int row, int column)
virtual void clear()
long long score
Definition: User.h:29
void setId(const std::string &id)
Wt::Auth::Login & login()
Definition: Session.h:32
int rowCount() const
std::vector< User > topUsers(int limit)
Definition: Session.C:173
WString timeTo(const WDateTime &other, std::chrono::seconds minValue=std::chrono::seconds(1)) const
std::string name
Definition: User.h:27
WTableRow * rowAt(int row)

Generated on Tue Dec 15 2020 for the C++ Web Toolkit (Wt) by doxygen 1.8.13