Wt examples  4.0.2
Public Member Functions | Private Attributes | List of all members
HighScoresWidget Class Reference

#include <HighScoresWidget.h>

Inheritance diagram for HighScoresWidget:
Inheritance graph
[legend]

Public Member Functions

 HighScoresWidget (Session *session)
 
void update ()
 

Private Attributes

Sessionsession_
 

Detailed Description

Definition at line 17 of file HighScoresWidget.h.

Constructor & Destructor Documentation

◆ HighScoresWidget()

HighScoresWidget::HighScoresWidget ( Session session)

Definition at line 17 of file HighScoresWidget.C.

17  :
18  WContainerWidget(),
19  session_(session)
20 {
21  setContentAlignment(AlignmentFlag::Center);
22  setStyleClass("highscores");
23 }

Member Function Documentation

◆ update()

void HighScoresWidget::update ( )

Definition at line 25 of file HighScoresWidget.C.

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 }
std::string userName() const
Definition: Session.C:148
int findRanking()
Definition: Session.C:192
Auth::Login & login()
Definition: Session.h:34
std::vector< User > topUsers(int limit)
Definition: Session.C:170

Member Data Documentation

◆ session_

Session* HighScoresWidget::session_
private

Definition at line 24 of file HighScoresWidget.h.


The documentation for this class was generated from the following files:

Generated on Thu Mar 15 2018 for the C++ Web Toolkit (Wt) by doxygen 1.8.14