Advertisement
repente

Untitled

Jun 19th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. <?php
  2.  
  3. namespace My\Dice;
  4.  
  5. use Anax\Commons\AppInjectableInterface;
  6. use Anax\Commons\AppInjectableTrait;
  7.  
  8. // use Anax\Route\Exception\ForbiddenException;
  9. // use Anax\Route\Exception\NotFoundException;
  10. // use Anax\Route\Exception\InternalErrorException;
  11.  
  12. /**
  13. * A sample controller to show how a controller class can be implemented.
  14. * The controller will be injected with $app if implementing the interface
  15. * AppInjectableInterface, like this sample class does.
  16. * The controller is mounted on a particular route and can then handle all
  17. * requests for that mount point.
  18. *
  19. * @SuppressWarnings(PHPMD.TooManyPublicMethods)
  20. */
  21. class DiceController implements AppInjectableInterface
  22. {
  23. use AppInjectableTrait;
  24.  
  25.  
  26.  
  27. /**
  28. * @var string $db a sample member variable that gets initialised
  29. */
  30. private $db = "not active";
  31.  
  32.  
  33.  
  34. /**
  35. * The method `form` controls the output of the desired form for the game
  36. *
  37. * @return string
  38. */
  39. public function form() : string
  40. {
  41. function sub01($session, $request)
  42. {
  43. if ($request->getGet("form", 0))
  44. {
  45. switch ($request->getGet("form"))
  46. {
  47. case '1':
  48. $session->set("players_count",
  49. [
  50. "count" => (int) $request->getGet("count"),
  51. ]);
  52. $session->set("form", 2);
  53. break;
  54. case '2':
  55. $players = [];
  56. $i = 1;
  57. while (true) {
  58. if ($request->getGet("name_player_$i"))
  59. {
  60. $players[] = [$request->getGet("name_player_$i"), $request->getGet("type_player_$i")];
  61. }
  62. else
  63. {
  64. break;
  65. }
  66. $i++;
  67. }
  68. $session->set("players", $players);
  69. $session->set("form", 3);
  70. break;
  71. }
  72. }
  73. }
  74.  
  75. function sub02($session, $request)
  76. {
  77. $index = $session->get("form");
  78. $form = '
  79. <form method="GET">
  80. <fieldset>
  81. <legend>Enter data players</legend>
  82. <label>%s</label>
  83. %s
  84. <input type="hidden" name="form" value="%s">
  85. </fieldset>
  86. </form>
  87. ';
  88. switch ($index)
  89. {
  90. case 1:
  91. $name = "Number of players? => ";
  92. $field = '<input required type="number" name="count">';
  93. $field .= '<input type="submit" name="btn" value="Send">';
  94. $codeForm = "1";
  95. return sprintf($form, $name, $field, $codeForm);
  96. case 2:
  97. $countPlayers = $session->get("players_count")["count"];
  98. $name = "Enter player names" . "<br>";
  99. $field = '';
  100. for ($i = 0; $i < $countPlayers; $i++)
  101. {
  102. $field .= $i + 1 . '. <input placeholder="name player" required type="text" name="name_player_'. ($i + 1) . '">';
  103. $field .=
  104. '
  105. <select name="type_player_' . ($i + 1) .'">
  106. <option value="Human">Human</option>
  107. <option value="Machine">Machine</option>
  108. </select> <br>
  109. ';
  110. };
  111. $field .= '<input type="submit" name="btn" value="Send">';
  112. $codeForm = "2";
  113. return sprintf($form, $name, $field, $codeForm);
  114. case 3:
  115. $field = "";
  116. if (1)
  117. {
  118. $i = 1;
  119. foreach ($session->get("players") as $value)
  120. {
  121. $field .= "πŸ˜† $i. " . "Player [" . $value[0] . "] : ready => OK!" . '<br>';
  122. $i++;
  123. }
  124. }
  125. $field .= '<a href="game"><button>Start Game</button></a>';
  126. return $field;
  127. }
  128. }
  129. sub01($this->app->session, $this->app->request);
  130. return sub02($this->app->session, $this->app->request);
  131. }
  132.  
  133. /**
  134. * This is the debug method action, it handles:
  135. *
  136. * @return string
  137. */
  138. public function debugAction() : string
  139. {
  140. return "Debu!! my Game!!";
  141. }
  142.  
  143. /**
  144. * This is the index method action, it handles:
  145. *
  146. * @return string
  147. */
  148. public function indexAction() : string
  149. {
  150. return "INDEX!!!";
  151. }
  152.  
  153. /**
  154. * This is the start method action, it handles:
  155. *
  156. * @return object
  157. */
  158. public function startActionGet() : object
  159. {
  160. $session = $this->app->session;
  161. $session->set("form", 1);
  162. $session->set("players_count", null);
  163. $session->set("players", null);
  164. $session->set("dice_game", null);
  165.  
  166. $title = "Welcome Dice Game";
  167.  
  168. $this->app->page->add("dice/short-game-terms");
  169.  
  170. return $this->app->page->render([
  171. "title" => $title,
  172. ]);
  173. }
  174.  
  175. /**
  176. * This is the form method action, it handles:
  177. *
  178. * @return object
  179. */
  180. public function formActionGet() : object
  181. {
  182. $title = "Please insert data players";
  183. $data = [
  184. "name" => "hello-world",
  185. "content" => "Hello World in " . __FILE__,
  186. "form" => $this->form(),
  187. ];
  188.  
  189. $this->app->page->add("dice/starting", $data);
  190.  
  191. return $this->app->page->render([
  192. "title" => $title,
  193. ]);
  194. }
  195.  
  196. /**
  197. * This is the game method action, it handles:
  198. *
  199. * @return object
  200. */
  201. public function gameActionGet() : object
  202. {
  203.  
  204. $winner = null;
  205. $type_player = null;
  206. $name_player_move = null;
  207. $result = null;
  208. $trows = [6, 6];
  209. $request = $this->app->request;
  210. $session = $this->app->session;
  211. if (!($this->app->session->get("dice_game")))
  212. {
  213. $players = [];
  214. foreach ($this->app->session->get("players") as $key => $value)
  215. {
  216. $players[] = [$value[0], $value[1]];
  217. }
  218. $session->set("dice_game", new Dice($players));
  219. $session->get("dice_game")->contestWhoIsFirst();
  220. $type_player = $session->get("dice_game")->players[0]->type;
  221. $name_player_move = $session->get("dice_game")->players[0]->name;
  222. }
  223. if ($request->getGet("btn", 0))
  224. {
  225. $result = $session->get("dice_game")->moves();
  226. switch ($result[0]) {
  227. case 'next':
  228. $name_player_move = $result[1];
  229. $type_player = $result[2];
  230. $trows = $result[3];
  231. break;
  232.  
  233. case 'winner':
  234. $winner = $result[1];
  235. break;
  236. }
  237. }
  238.  
  239. if ($request->getGet("continue", 0))
  240. {
  241. $result = $session->get("dice_game")->moves(1);
  242. switch ($result[0]) {
  243. case 'next':
  244. $name_player_move = $result[1];
  245. $type_player = $result[2];
  246. $trows = $result[3];
  247. break;
  248.  
  249. case 'winner':
  250. $winner = $result[1];
  251. break;
  252. }
  253. }
  254. $histogram = new DiceHistogram();
  255. $dice = $session->get("dice_game");
  256. $histogram->injectData($dice);
  257. $hist = $histogram->getAsText();
  258. $players_statistics = $session->get("dice_game")->printedPlayers();
  259. $currentTotal = $session->get("dice_game")->currentTotal ? $session->get("dice_game")->currentTotal : $session->get("dice_game")->tempValue;
  260.  
  261. $title = "Live game Dice 100";
  262. $data = [
  263. "h1" => $title,
  264. "statistics" => $players_statistics,
  265. "type_player" => $type_player,
  266. "name_player_move" => $name_player_move,
  267. "winner" => $winner,
  268. "trows" => $trows,
  269. "current" => $currentTotal,
  270. "histogram" => $hist,
  271. ];
  272.  
  273. $this->app->page->add("dice/game", $data);
  274.  
  275. return $this->app->page->render([
  276. "title" => $title,
  277. ]);
  278. }
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement