Wt examples: hangman/HighScoresWidget.C Source File

Wt examples 4.10.4

Loading...
Searching...
No Matches
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
17HighScoresWidget::HighScoresWidget(Session *session)
18 : session_(session)
19{
20 setContentAlignment(AlignmentFlag::Center);
21 setStyleClass("highscores");
22}
23
24 void HighScoresWidget::update()
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}
HighScoresWidget.h
Session.h
HighScoresWidget::HighScoresWidget
HighScoresWidget(Session *session)
Definition HighScoresWidget.C:17
HighScoresWidget::session_
Session * session_
Definition HighScoresWidget.h:22
HighScoresWidget::update
void update()
Definition HighScoresWidget.C:24
Session
Definition Session.h:25
Session::topUsers
std::vector< User > topUsers(int limit)
Definition Session.C:158
Session::userName
std::string userName() const
Definition Session.C:136
Session::findRanking
int findRanking()
Definition Session.C:180
Session::login
Wt::Auth::Login & login()
Definition Session.h:32
Wt
Definition AddresseeEdit.h:16