Wt examples  4.0.2
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  = cpp14::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  = cpp14::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(cpp14::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(cpp14::make_unique<WAnchor>("/play", "Gaming Grounds"));
53  backToGameAnchor_->setLink(WLink(LinkType::InternalPath, "/play"));
54 
55  scoresAnchor_ = links_->addWidget(cpp14::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 }
Session session_
Definition: HangmanGame.h:41
Auth::AbstractUserDatabase & users()
Definition: Session.C:208
WStackedWidget * mainStack_
Definition: HangmanGame.h:34
WAnchor * backToGameAnchor_
Definition: HangmanGame.h:38
static const Auth::AuthService & auth()
Definition: Session.C:213
Auth::Login & login()
Definition: Session.h:34
HangmanWidget * game_
Definition: HangmanGame.h:35
static const std::vector< const Auth::OAuthService * > & oAuth()
Definition: Session.C:223
HighScoresWidget * scores_
Definition: HangmanGame.h:36
WContainerWidget * links_
Definition: HangmanGame.h:37
WAnchor * scoresAnchor_
Definition: HangmanGame.h:39
static const Auth::AbstractPasswordService & passwordAuth()
Definition: Session.C:218
void handleInternalPath(const std::string &internalPath)
Definition: HangmanGame.C:77
void onAuthEvent()
Definition: HangmanGame.C:64

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
Session session_
Definition: HangmanGame.h:41
void showGame()
Definition: HangmanGame.C:101
Auth::Login & login()
Definition: Session.h:34

◆ 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 }
Session session_
Definition: HangmanGame.h:41
WStackedWidget * mainStack_
Definition: HangmanGame.h:34
Auth::Login & login()
Definition: Session.h:34
HangmanWidget * game_
Definition: HangmanGame.h:35
HighScoresWidget * scores_
Definition: HangmanGame.h:36
WContainerWidget * links_
Definition: HangmanGame.h:37
void handleInternalPath(const std::string &internalPath)
Definition: HangmanGame.C:77

◆ showGame()

void HangmanGame::showGame ( )
private

Definition at line 101 of file HangmanGame.C.

102 {
103  if (!game_) {
104  game_ = mainStack_->addWidget(cpp14::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 }
std::string userName() const
Definition: Session.C:148
Session session_
Definition: HangmanGame.h:41
WStackedWidget * mainStack_
Definition: HangmanGame.h:34
WAnchor * backToGameAnchor_
Definition: HangmanGame.h:38
HangmanWidget * game_
Definition: HangmanGame.h:35
WAnchor * scoresAnchor_
Definition: HangmanGame.h:39
void addToScore(int s)
Definition: Session.C:156
Wt::Signal< int > & scoreUpdated()
Definition: HangmanWidget.h:25

◆ showHighScores()

void HangmanGame::showHighScores ( )
private

Definition at line 89 of file HangmanGame.C.

90 {
91  if (!scores_)
92  scores_ = mainStack_->addWidget(cpp14::make_unique<HighScoresWidget>(&session_));
93 
94  mainStack_->setCurrentWidget(scores_);
95  scores_->update();
96 
97  backToGameAnchor_->removeStyleClass("selected-link");
98  scoresAnchor_->addStyleClass("selected-link");
99 }
Session session_
Definition: HangmanGame.h:41
WStackedWidget * mainStack_
Definition: HangmanGame.h:34
WAnchor * backToGameAnchor_
Definition: HangmanGame.h:38
HighScoresWidget * scores_
Definition: HangmanGame.h:36
WAnchor * scoresAnchor_
Definition: HangmanGame.h:39

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 Thu Mar 15 2018 for the C++ Web Toolkit (Wt) by doxygen 1.8.14