Wt examples: hangman/HangmanGame.C Source File

Wt examples 4.10.4

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 "HangmanGame.h"
8
9#include <Wt/WAnchor.h>
10#include <Wt/WText.h>
11#include <Wt/WStackedWidget.h>
12#include <Wt/WApplication.h>
13#include <Wt/Auth/AuthWidget.h>
14
15#include "HangmanWidget.h"
16#include "HighScoresWidget.h"
17
18using namespace Wt;
19
20HangmanGame::HangmanGame()
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}
55
56 void HangmanGame::onAuthEvent()
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}
68
69 void HangmanGame::handleInternalPath(const std::string &internalPath)
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}
80
81 void HangmanGame::showHighScores()
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}
92
93 void HangmanGame::showGame()
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}
HangmanGame.h
HangmanWidget.h
HighScoresWidget.h
HangmanGame::game_
HangmanWidget * game_
Definition HangmanGame.h:33
HangmanGame::showHighScores
void showHighScores()
Definition HangmanGame.C:81
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::scores_
HighScoresWidget * scores_
Definition HangmanGame.h:34
HangmanGame::showGame
void showGame()
Definition HangmanGame.C:93
HangmanGame::mainStack_
Wt::WStackedWidget * mainStack_
Definition HangmanGame.h:32
HangmanGame::backToGameAnchor_
Wt::WAnchor * backToGameAnchor_
Definition HangmanGame.h:36
HangmanGame::HangmanGame
HangmanGame()
Definition HangmanGame.C:20
HangmanWidget
Definition HangmanWidget.h:20
HangmanWidget::scoreUpdated
Wt::Signal< int > & scoreUpdated()
Definition HangmanWidget.h:24
HighScoresWidget
Definition HighScoresWidget.h:16
HighScoresWidget::update
void update()
Definition HighScoresWidget.C:24
Session::passwordAuth
static const Wt::Auth::AbstractPasswordService & passwordAuth()
Definition Session.C:206
Session::userName
std::string userName() const
Definition Session.C:136
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
Session::addToScore
void addToScore(int s)
Definition Session.C:144
Wt
Definition AddresseeEdit.h:16