Wt examples  4.10.4
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 <Wt/WText.h>
8 #include <Wt/WTable.h>
9 #include <Wt/Dbo/Dbo.h>
10 #include <Wt/WAny.h>
11 
12 #include "HighScoresWidget.h"
13 #include "Session.h"
14 
15 using namespace Wt;
16 
18  : session_(session)
19 {
20  setContentAlignment(AlignmentFlag::Center);
21  setStyleClass("highscores");
22 }
23 
25 {
26  clear();
27 
28  addNew<Wt::WText>("<h2>Hall of fame</h2>");
29 
30  int ranking = session_->findRanking();
31 
32  std::string yourScore;
33  if (ranking == 1)
34  yourScore = "Congratulations! You are currently leading the pack.";
35  else {
36  yourScore = "You are currently ranked number "
37  + asString(ranking).toUTF8()
38  + ". Almost there !";
39  }
40 
41  WText *score = addNew<WText>("<p>" + yourScore + "</p>");
42  score->addStyleClass("score");
43 
44  std::vector<User> top = session_->topUsers(20);
45 
46  WTable *table = addNew<WTable>();
47 
48  table->elementAt(0, 0)->addNew<WText>("Rank");
49  table->elementAt(0, 1)->addNew<WText>("User");
50  table->elementAt(0, 2)->addNew<WText>("Games");
51  table->elementAt(0, 3)->addNew<WText>("Score");
52  table->elementAt(0, 4)->addNew<WText>("Last game");
53  table->setHeaderCount(1);
54 
55  int formerScore = -1;
56  int rank = 0;
57  for (auto& user : top) {
58 
59  if (user.score != formerScore) {
60  formerScore = user.score;
61  ++rank;
62  }
63 
64  int row = table->rowCount();
65  table->elementAt(row, 0)->addNew<WText>(asString(rank));
66  table->elementAt(row, 1)->addNew<WText>(user.name);
67  table->elementAt(row, 2)->addNew<WText>(asString(user.gamesPlayed));
68  table->elementAt(row, 3)->addNew<WText>(asString(user.score));
69  if (!user.lastGame.isNull())
70  table->elementAt(row, 4)->addNew<WText>(user.lastGame.timeTo(WDateTime::currentDateTime()) + " ago");
71  else
72  table->elementAt(row, 4)->addNew<WText>("---");
73 
74  if (session_->login().loggedIn() && session_->userName() == user.name)
75  table->rowAt(row)->setId("self");
76  }
77 
78  WText *fineprint = addNew<WText>(tr("highscore.info"));
79  fineprint->addStyleClass("fineprint");
80 }
HighScoresWidget(Session *session)
std::vector< User > topUsers(int limit)
Definition: Session.C:158
std::string userName() const
Definition: Session.C:136
int findRanking()
Definition: Session.C:180
Wt::Auth::Login & login()
Definition: Session.h:32
bool loggedIn() const
virtual void clear()
void setContentAlignment(WFlags< AlignmentFlag > contentAlignment)
Widget * addNew(Args &&...args)
std::string toUTF8() const
void setId(const std::string &id)
int rowCount() const
WTableCell * elementAt(int row, int column)
WTableRow * rowAt(int row)
void setHeaderCount(int count, Orientation orientation=Orientation::Horizontal)
virtual void setStyleClass(const WString &styleClass) override
virtual void addStyleClass(const WString &styleClass, bool force=false) override
static WString tr(const char *key)
WString asString(const cpp17::any &v, const WString &formatString=WString())