Wt examples  4.9.1
Loading...
Searching...
No Matches
HangmanGame.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/WAnchor.h>
8#include <Wt/WText.h>
9#include <Wt/WStackedWidget.h>
10#include <Wt/WVBoxLayout.h>
11#include <Wt/WHBoxLayout.h>
12#include <Wt/WApplication.h>
13#include <Wt/Auth/AuthWidget.h>
14#include <Wt/Auth/RegistrationModel.h>
15
16#include "HangmanGame.h"
17#include "HangmanWidget.h"
18#include "HighScoresWidget.h"
19
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}
63
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}
76
77void HangmanGame::handleInternalPath(const std::string &internalPath)
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}
88
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}
100
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}
WAnchor * backToGameAnchor_
Definition: HangmanGame.h:38
HangmanWidget * game_
Definition: HangmanGame.h:35
void showHighScores()
Definition: HangmanGame.C:89
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
void showGame()
Definition: HangmanGame.C:101
Wt::Signal< int > & scoreUpdated()
Definition: HangmanWidget.h:25
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
std::string userName() const
Definition: Session.C:150
static const Auth::AuthService & auth()
Definition: Session.C:215
Auth::AbstractUserDatabase & users()
Definition: Session.C:210
void addToScore(int s)
Definition: Session.C:158