Advertisement
repente

Untitled

May 9th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. <?php
  2. /**
  3. * Create routes using $app programming style.
  4. */
  5. //var_dump(array_keys(get_defined_vars()));
  6.  
  7.  
  8.  
  9. /**
  10. * Init the game and redirect to play the game.
  11. */
  12. $app->router->get("guess/init", function () use ($app) {
  13.  
  14. return $app->response->redirect("guess/play");
  15. });
  16.  
  17.  
  18. /**
  19. * Play the game.
  20. */
  21. $app->router->get("guess/play", function () use ($app) {
  22. // echo "Some debugging information";
  23. if (!isset($_SESSION["game"])) {
  24. $_SESSION["game"] = new Mos\Guess\Guess();
  25. } else {
  26. unset($_SESSION["game"]);
  27. $_SESSION["game"] = new Mos\Guess\Guess();
  28. }
  29. $message = "Beginning of the game. please guess the number 1 - 100";
  30. $tries = 6;
  31. $data = [
  32. "title" => "Gissa numret med PHP",
  33. "message" => $message,
  34. "tries" => $tries,
  35. ];
  36. $app->page->add("guess-template/index", $data);
  37. // $app->page->add("guess-template/debug");
  38. return $app->page->render();
  39. });
  40.  
  41. $app->router->post("guess/play", function () use ($app) {
  42. if (!isset($_SESSION["game"])) {
  43. $_SESSION["game"] = new Mos\Guess\Guess();
  44. }
  45.  
  46. $message = "Beginning of the game. please guess the number 1 - 100";
  47.  
  48. if (isset($_POST["btn"])) {
  49.  
  50. $game = $_SESSION["game"];
  51. switch ($_POST["btn"]) {
  52. #Make a gues
  53. case 'Make a guess':
  54. if ($game->tries()) {
  55. $game->tries -= 1;
  56. $message = $game->makeGuess($_POST['input']);
  57. } else {
  58. $message = "sorry you have no attempts left";
  59. }
  60.  
  61. break;
  62. #start new game
  63. case 'Start from beginning':
  64. unset($_SESSION["game"]);
  65. break;
  66. #view secret number
  67. case 'Cheat':
  68. $message = $game->status_message("Cheat");
  69. break;
  70. }
  71.  
  72.  
  73. }
  74.  
  75.  
  76. if (isset($_SESSION["game"])) {
  77. $tries = $_SESSION["game"]->tries();
  78. } else {
  79. #start message
  80. $tries = "6";
  81. // $message = "Beginning of the game. please guess the number 1 - 100";
  82. }
  83.  
  84. $data = [
  85. "title" => "Gissa numret med PHP",
  86. "message" => $message,
  87. "tries" => $tries,
  88. ];
  89.  
  90.  
  91.  
  92. $app->page->add("guess-template/index" , $data);
  93. // $app->page->add("guess-template/debug");
  94. return $app->page->render();
  95. // return $app->response->redirect("guess/play");
  96. });
  97.  
  98.  
  99. /**
  100. * Showing message Hello World, rendered within the standard page layout.
  101. */
  102. // $app->router->get("lek/hello-world-page", function () use ($app) {
  103. // $title = "Hello World as a page";
  104. // $data = [
  105. // "class" => "hello-world",
  106. // "content" => "Hello World in " . __FILE__,
  107. // ];
  108.  
  109. // $app->page->add("anax/v2/article/default", $data);
  110.  
  111. // return $app->page->render([
  112. // "title" => $title,
  113. // ]);
  114. // });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement