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
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
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
69void HangmanGame::handleInternalPath(const std::string &internalPath)
70{
71 if (session_.login().loggedIn()) {
72 if (internalPath == "/play")
73 showGame();
74 else if (internalPath == "/highscores")
76 else
77 WApplication::instance()->setInternalPath("/play", true);
78 }
79}
80
82{
83 if (!scores_)
85
86 mainStack_->setCurrentWidget(scores_);
87 scores_->update();
88
89 backToGameAnchor_->removeStyleClass("selected-link");
90 scoresAnchor_->addStyleClass("selected-link");
91}
92
94{
95 if (!game_) {
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 * game_
Definition HangmanGame.h:33
void showHighScores()
Definition HangmanGame.C:81
WContainerWidget * links_
Definition HangmanGame.h:35
Session session_
Definition HangmanGame.h:39
void handleInternalPath(const std::string &internalPath)
Definition HangmanGame.C:69
void onAuthEvent()
Definition HangmanGame.C:56
Wt::WAnchor * scoresAnchor_
Definition HangmanGame.h:37
HighScoresWidget * scores_
Definition HangmanGame.h:34
void showGame()
Definition HangmanGame.C:93
Wt::WStackedWidget * mainStack_
Definition HangmanGame.h:32
Wt::WAnchor * backToGameAnchor_
Definition HangmanGame.h:36
Wt::Signal< int > & scoreUpdated()
static const Wt::Auth::AbstractPasswordService & passwordAuth()
Definition Session.C:206
std::string userName() const
Definition Session.C:136
static const Wt::Auth::AuthService & auth()
Definition Session.C:201
static std::vector< const Wt::Auth::OAuthService * > oAuth()
Definition Session.C:211
Wt::Auth::Login & login()
Definition Session.h:32
Wt::Auth::AbstractUserDatabase & users()
Definition Session.C:196
void addToScore(int s)
Definition Session.C:144