Wt examples  4.10.4
Loading...
Searching...
No Matches
HighScoresWidget Class Reference

#include <HighScoresWidget.h>

Inheritance diagram for HighScoresWidget:
[legend]

Public Member Functions

 HighScoresWidget (Session *session)
 
void update ()
 

Private Attributes

Sessionsession_ = nullptr
 

Detailed Description

Definition at line 15 of file HighScoresWidget.h.

Constructor & Destructor Documentation

◆ HighScoresWidget()

HighScoresWidget::HighScoresWidget ( Session * session)
explicit

Definition at line 17 of file HighScoresWidget.C.

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

Member Function Documentation

◆ update()

void HighScoresWidget::update ( )

Definition at line 24 of file HighScoresWidget.C.

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}
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

Member Data Documentation

◆ session_

Session* HighScoresWidget::session_ = nullptr
private

Definition at line 22 of file HighScoresWidget.h.


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