Wt examples: HangmanGame Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
HangmanGame Class Reference

`#include <HangmanGame.h>`

Inheritance diagram for HangmanGame:
[legend]

## Public Member Functions
| HangmanGame ()
void | handleInternalPath (const std::string &internalPath)

## Private Member Functions
void | onAuthEvent ()
void | showGame ()
void | showHighScores ()

## Private Attributes
Wt::WStackedWidget * | mainStack_ = nullptr
HangmanWidget * | game_ = nullptr
HighScoresWidget * | scores_ = nullptr
WContainerWidget * | links_ = nullptr
Wt::WAnchor * | backToGameAnchor_ = nullptr
Wt::WAnchor * | scoresAnchor_ = nullptr
Session | session_

## Detailed Description

Definition at line 24 of file HangmanGame.h.

## Constructor & Destructor Documentation

## ◆ HangmanGame()

HangmanGame::HangmanGame | ( |  | ) |

Definition at line 20 of file HangmanGame.C.

21{
22 session_.login().changed().connect(this, &HangmanGame::onAuthEvent);
23
24 auto authModel = std::make_unique<Auth::AuthModel>(Session::auth(), session_.users());
25 authModel->addPasswordAuth(&Session::passwordAuth());
26 authModel->addOAuth(Session::oAuth());
27
28 auto authWidget = std::make_unique<Auth::AuthWidget>(session_.login());
29 auto authWidgetPtr = authWidget.get();
30 authWidget->setModel(std::move(authModel));
31 authWidget->setRegistrationEnabled(true);
32
33 addNew<WText>("<h1>A Witty game: Hangman</h1>");
34
35 addWidget(std::move(authWidget));
36
37 mainStack_ = addNew<WStackedWidget>();
38 mainStack_->setStyleClass("gamestack");
39
40 links_ = addNew<WContainerWidget>();
41 links_->setStyleClass("links");
42 links_->hide();
43
44 backToGameAnchor_ = links_->addNew<WAnchor>("/play", "Gaming Grounds");
45 backToGameAnchor_->setLink(WLink(LinkType::InternalPath, "/play"));
46
47 scoresAnchor_ = links_->addNew<WAnchor>("/highscores", "Highscores");
48 scoresAnchor_->setLink(WLink(LinkType::InternalPath, "/highscores"));
49
50 WApplication::instance()->internalPathChanged()
51 .connect(this, &HangmanGame::handleInternalPath);
52
53 authWidgetPtr->processEnvironment();
54}
HangmanGame::links_
WContainerWidget * links_
Definition HangmanGame.h:35
HangmanGame::session_
Session session_
Definition HangmanGame.h:39
HangmanGame::handleInternalPath
void handleInternalPath(const std::string &internalPath)
Definition HangmanGame.C:69
HangmanGame::onAuthEvent
void onAuthEvent()
Definition HangmanGame.C:56
HangmanGame::scoresAnchor_
Wt::WAnchor * scoresAnchor_
Definition HangmanGame.h:37
HangmanGame::mainStack_
Wt::WStackedWidget * mainStack_
Definition HangmanGame.h:32
HangmanGame::backToGameAnchor_
Wt::WAnchor * backToGameAnchor_
Definition HangmanGame.h:36
Session::passwordAuth
static const Wt::Auth::AbstractPasswordService & passwordAuth()
Definition Session.C:206
Session::auth
static const Wt::Auth::AuthService & auth()
Definition Session.C:201
Session::oAuth
static std::vector< const Wt::Auth::OAuthService * > oAuth()
Definition Session.C:211
Session::login
Wt::Auth::Login & login()
Definition Session.h:32
Session::users
Wt::Auth::AbstractUserDatabase & users()
Definition Session.C:196

## Member Function Documentation

## ◆ handleInternalPath()

void HangmanGame::handleInternalPath | ( | const std::string & | internalPath | ) |

Definition at line 69 of file HangmanGame.C.

70{
71 if (session_.login().loggedIn()) {
72 if (internalPath == "/play")
73 showGame();
74 else if (internalPath == "/highscores")
75 showHighScores();
76 else
77 WApplication::instance()->setInternalPath("/play", true);
78 }
79}
HangmanGame::showHighScores
void showHighScores()
Definition HangmanGame.C:81
HangmanGame::showGame
void showGame()
Definition HangmanGame.C:93

## ◆ onAuthEvent()

void HangmanGame::onAuthEvent | ( |  | ) |
| private

Definition at line 56 of file HangmanGame.C.

57{
58 if (session_.login().loggedIn()) {
59 links_->show();
60 handleInternalPath(WApplication::instance()->internalPath());
61 } else {
62 mainStack_->clear();
63 game_ = nullptr;
64 scores_ = nullptr;
65 links_->hide();
66 }
67}
HangmanGame::game_
HangmanWidget * game_
Definition HangmanGame.h:33
HangmanGame::scores_
HighScoresWidget * scores_
Definition HangmanGame.h:34

## ◆ showGame()

void HangmanGame::showGame | ( |  | ) |
| private

Definition at line 93 of file HangmanGame.C.

94{
95 if (!game_) {
96 game_ = mainStack_->addNew<HangmanWidget>(session_.userName());
97 game_->scoreUpdated().connect(std::bind(&Session::addToScore, &session_, std::placeholders::_1));
98 }
99
100 mainStack_->setCurrentWidget(game_);
101
102 backToGameAnchor_->addStyleClass("selected-link");
103 scoresAnchor_->removeStyleClass("selected-link");
104}
HangmanWidget
Definition HangmanWidget.h:20
HangmanWidget::scoreUpdated
Wt::Signal< int > & scoreUpdated()
Definition HangmanWidget.h:24
Session::userName
std::string userName() const
Definition Session.C:136
Session::addToScore
void addToScore(int s)
Definition Session.C:144

## ◆ showHighScores()

void HangmanGame::showHighScores | ( |  | ) |
| private

Definition at line 81 of file HangmanGame.C.

82{
83 if (!scores_)
84 scores_ = mainStack_->addNew<HighScoresWidget>(&session_);
85
86 mainStack_->setCurrentWidget(scores_);
87 scores_->update();
88
89 backToGameAnchor_->removeStyleClass("selected-link");
90 scoresAnchor_->addStyleClass("selected-link");
91}
HighScoresWidget
Definition HighScoresWidget.h:16
HighScoresWidget::update
void update()
Definition HighScoresWidget.C:24

## Member Data Documentation

## ◆ backToGameAnchor_

Wt::WAnchor* HangmanGame::backToGameAnchor_ = nullptr
| private

Definition at line 36 of file HangmanGame.h.

## ◆ game_

HangmanWidget* HangmanGame::game_ = nullptr
| private

Definition at line 33 of file HangmanGame.h.

## ◆ links_

WContainerWidget* HangmanGame::links_ = nullptr
| private

Definition at line 35 of file HangmanGame.h.

## ◆ mainStack_

Wt::WStackedWidget* HangmanGame::mainStack_ = nullptr
| private

Definition at line 32 of file HangmanGame.h.

## ◆ scores_

HighScoresWidget* HangmanGame::scores_ = nullptr
| private

Definition at line 34 of file HangmanGame.h.

## ◆ scoresAnchor_

Wt::WAnchor* HangmanGame::scoresAnchor_ = nullptr
| private

Definition at line 37 of file HangmanGame.h.

## ◆ session_

Session HangmanGame::session_
| private

Definition at line 39 of file HangmanGame.h.

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

- hangman/HangmanGame.h
- hangman/HangmanGame.C