Wt examples 4.7.2
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
15using namespace Wt;
16
18 WContainerWidget(),
19 session_(session)
20{
21 setContentAlignment(AlignmentFlag::Center);
22 setStyleClass("highscores");
23}
24
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}
HighScoresWidget(Session *session)
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

Generated on Fri May 13 2022 for the C++ Web Toolkit (Wt) by doxygen 1.9.4