Wt examples 4.6.1
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(std::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(std::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(std::make_unique<WTable>());
48
49 table->elementAt(0, 0)->addWidget(std::make_unique<WText>("Rank"));
50 table->elementAt(0, 1)->addWidget(std::make_unique<WText>("User"));
51 table->elementAt(0, 2)->addWidget(std::make_unique<WText>("Games"));
52 table->elementAt(0, 3)->addWidget(std::make_unique<WText>("Score"));
53 table->elementAt(0, 4)->addWidget(std::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(std::make_unique<WText>(asString(rank)));
67 table->elementAt(row, 1)->addWidget(std::make_unique<WText>(user.name));
68 table->elementAt(row, 2)->addWidget(std::make_unique<WText>(asString(user.gamesPlayed)));
69 table->elementAt(row, 3)->addWidget(std::make_unique<WText>(asString(user.score)));
70 if (!user.lastGame.isNull())
71 table->elementAt(row, 4)->addWidget(std::make_unique<WText>(user.lastGame.timeTo(WDateTime::currentDateTime())
72 + " ago"));
73 else
74 table->elementAt(row, 4)->addWidget(std::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(std::make_unique<WText>(tr("highscore.info")));
81 fineprint->addStyleClass("fineprint");
82}
Auth::Login & login()
Definition: Session.h:34
std::vector< User > topUsers(int limit)
Definition: Session.C:172
std::string userName() const
Definition: Session.C:150
int findRanking()
Definition: Session.C:194

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 Fri Jan 28 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.3