HangmanGame.C 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (C) 2011 Emweb bvba, Heverlee, Belgium
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <Wt/WAnchor>
  7. #include <Wt/WText>
  8. #include <Wt/WStackedWidget>
  9. #include <Wt/WVBoxLayout>
  10. #include <Wt/WHBoxLayout>
  11. #include <Wt/WApplication>
  12. #include <Wt/Auth/AuthWidget>
  13. #include "HangmanGame.h"
  14. #include "HangmanWidget.h"
  15. #include "HighScoresWidget.h"
  16. using namespace Wt;
  17. HangmanGame::HangmanGame(WContainerWidget *parent):
  18. WContainerWidget(parent),
  19. game_(0),
  20. scores_(0)
  21. {
  22. session_.login().changed().connect(this, &HangmanGame::onAuthEvent);
  23. Auth::AuthModel *authModel = new Auth::AuthModel(Session::auth(),
  24. session_.users(), this);
  25. authModel->addPasswordAuth(&Session::passwordAuth());
  26. authModel->addOAuth(Session::oAuth());
  27. Auth::AuthWidget *authWidget = new Auth::AuthWidget(session_.login());
  28. authWidget->setModel(authModel);
  29. authWidget->setRegistrationEnabled(true);
  30. WText *title = new WText("<h1>A Witty game: Hangman</h1>");
  31. addWidget(title);
  32. addWidget(authWidget);
  33. mainStack_ = new WStackedWidget();
  34. mainStack_->setStyleClass("gamestack");
  35. addWidget(mainStack_);
  36. links_ = new WContainerWidget();
  37. links_->setStyleClass("links");
  38. links_->hide();
  39. addWidget(links_);
  40. backToGameAnchor_ = new WAnchor("/play", "Gaming Grounds", links_);
  41. backToGameAnchor_->setLink(WLink(WLink::InternalPath, "/play"));
  42. scoresAnchor_ = new WAnchor("/highscores", "Highscores", links_);
  43. scoresAnchor_->setLink(WLink(WLink::InternalPath, "/highscores"));
  44. WApplication::instance()->internalPathChanged()
  45. .connect(this, &HangmanGame::handleInternalPath);
  46. authWidget->processEnvironment();
  47. }
  48. void HangmanGame::onAuthEvent()
  49. {
  50. if (session_.login().loggedIn()) {
  51. links_->show();
  52. handleInternalPath(WApplication::instance()->internalPath());
  53. } else {
  54. mainStack_->clear();
  55. game_ = 0;
  56. scores_ = 0;
  57. links_->hide();
  58. }
  59. }
  60. void HangmanGame::handleInternalPath(const std::string &internalPath)
  61. {
  62. if (session_.login().loggedIn()) {
  63. if (internalPath == "/play")
  64. showGame();
  65. else if (internalPath == "/highscores")
  66. showHighScores();
  67. else
  68. WApplication::instance()->setInternalPath("/play", true);
  69. }
  70. }
  71. void HangmanGame::showHighScores()
  72. {
  73. if (!scores_)
  74. scores_ = new HighScoresWidget(&session_, mainStack_);
  75. mainStack_->setCurrentWidget(scores_);
  76. scores_->update();
  77. backToGameAnchor_->removeStyleClass("selected-link");
  78. scoresAnchor_->addStyleClass("selected-link");
  79. }
  80. void HangmanGame::showGame()
  81. {
  82. if (!game_) {
  83. game_ = new HangmanWidget(session_.userName(), mainStack_);
  84. game_->scoreUpdated().connect(&session_, &Session::addToScore);
  85. }
  86. mainStack_->setCurrentWidget(game_);
  87. backToGameAnchor_->addStyleClass("selected-link");
  88. scoresAnchor_->removeStyleClass("selected-link");
  89. }