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

Generated on Mon Sep 4 2017 for the C++ Web Toolkit (Wt) by doxygen 1.8.11