Wt examples 4.7.1
Public Member Functions | Private Member Functions | Private Attributes | List of all members
HangmanGame Class Reference

#include <HangmanGame.h>

Inheritance diagram for HangmanGame:
Inheritance graph
[legend]

Public Member Functions

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

Private Member Functions

void onAuthEvent ()
 
void showGame ()
 
void showHighScores ()
 

Private Attributes

WStackedWidget * mainStack_
 
HangmanWidgetgame_
 
HighScoresWidgetscores_
 
WContainerWidget * links_
 
WAnchor * backToGameAnchor_
 
WAnchor * scoresAnchor_
 
Session session_
 

Detailed Description

Definition at line 26 of file HangmanGame.h.

Constructor & Destructor Documentation

◆ HangmanGame()

HangmanGame::HangmanGame ( )

Definition at line 20 of file HangmanGame.C.

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

Member Function Documentation

◆ handleInternalPath()

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

Definition at line 77 of file HangmanGame.C.

78{
79 if (session_.login().loggedIn()) {
80 if (internalPath == "/play")
81 showGame();
82 else if (internalPath == "/highscores")
84 else
85 WApplication::instance()->setInternalPath("/play", true);
86 }
87}
void showHighScores()
Definition: HangmanGame.C:89
void showGame()
Definition: HangmanGame.C:101

◆ onAuthEvent()

void HangmanGame::onAuthEvent ( )
private

Definition at line 64 of file HangmanGame.C.

65{
66 if (session_.login().loggedIn()) {
67 links_->show();
68 handleInternalPath(WApplication::instance()->internalPath());
69 } else {
70 mainStack_->clear();
71 game_ = 0;
72 scores_ = 0;
73 links_->hide();
74 }
75}

◆ showGame()

void HangmanGame::showGame ( )
private

Definition at line 101 of file HangmanGame.C.

102{
103 if (!game_) {
104 game_ = mainStack_->addWidget(std::make_unique<HangmanWidget>(session_.userName()));
105 game_->scoreUpdated().connect(std::bind(&Session::addToScore,&session_,std::placeholders::_1));
106 }
107
108 mainStack_->setCurrentWidget(game_);
109
110 backToGameAnchor_->addStyleClass("selected-link");
111 scoresAnchor_->removeStyleClass("selected-link");
112}
Wt::Signal< int > & scoreUpdated()
Definition: HangmanWidget.h:25
std::string userName() const
Definition: Session.C:150
void addToScore(int s)
Definition: Session.C:158

◆ showHighScores()

void HangmanGame::showHighScores ( )
private

Definition at line 89 of file HangmanGame.C.

90{
91 if (!scores_)
92 scores_ = mainStack_->addWidget(std::make_unique<HighScoresWidget>(&session_));
93
94 mainStack_->setCurrentWidget(scores_);
95 scores_->update();
96
97 backToGameAnchor_->removeStyleClass("selected-link");
98 scoresAnchor_->addStyleClass("selected-link");
99}

Member Data Documentation

◆ backToGameAnchor_

WAnchor* HangmanGame::backToGameAnchor_
private

Definition at line 38 of file HangmanGame.h.

◆ game_

HangmanWidget* HangmanGame::game_
private

Definition at line 35 of file HangmanGame.h.

◆ links_

WContainerWidget* HangmanGame::links_
private

Definition at line 37 of file HangmanGame.h.

◆ mainStack_

WStackedWidget* HangmanGame::mainStack_
private

Definition at line 34 of file HangmanGame.h.

◆ scores_

HighScoresWidget* HangmanGame::scores_
private

Definition at line 36 of file HangmanGame.h.

◆ scoresAnchor_

WAnchor* HangmanGame::scoresAnchor_
private

Definition at line 39 of file HangmanGame.h.

◆ session_

Session HangmanGame::session_
private

Definition at line 41 of file HangmanGame.h.


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

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