Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace My\Dice;
- use Anax\Commons\AppInjectableInterface;
- use Anax\Commons\AppInjectableTrait;
- // use Anax\Route\Exception\ForbiddenException;
- // use Anax\Route\Exception\NotFoundException;
- // use Anax\Route\Exception\InternalErrorException;
- /**
- * A sample controller to show how a controller class can be implemented.
- * The controller will be injected with $app if implementing the interface
- * AppInjectableInterface, like this sample class does.
- * The controller is mounted on a particular route and can then handle all
- * requests for that mount point.
- *
- * @SuppressWarnings(PHPMD.TooManyPublicMethods)
- */
- class DiceController implements AppInjectableInterface
- {
- use AppInjectableTrait;
- /**
- * @var string $db a sample member variable that gets initialised
- */
- private $db = "not active";
- /**
- * The method `form` controls the output of the desired form for the game
- *
- * @return string
- */
- public function form() : string
- {
- function sub01($session, $request)
- {
- if ($request->getGet("form", 0))
- {
- switch ($request->getGet("form"))
- {
- case '1':
- $session->set("players_count",
- [
- "count" => (int) $request->getGet("count"),
- ]);
- $session->set("form", 2);
- break;
- case '2':
- $players = [];
- $i = 1;
- while (true) {
- if ($request->getGet("name_player_$i"))
- {
- $players[] = [$request->getGet("name_player_$i"), $request->getGet("type_player_$i")];
- }
- else
- {
- break;
- }
- $i++;
- }
- $session->set("players", $players);
- $session->set("form", 3);
- break;
- }
- }
- }
- function sub02($session, $request)
- {
- $index = $session->get("form");
- $form = '
- <form method="GET">
- <fieldset>
- <legend>Enter data players</legend>
- <label>%s</label>
- %s
- <input type="hidden" name="form" value="%s">
- </fieldset>
- </form>
- ';
- switch ($index)
- {
- case 1:
- $name = "Number of players? => ";
- $field = '<input required type="number" name="count">';
- $field .= '<input type="submit" name="btn" value="Send">';
- $codeForm = "1";
- return sprintf($form, $name, $field, $codeForm);
- case 2:
- $countPlayers = $session->get("players_count")["count"];
- $name = "Enter player names" . "<br>";
- $field = '';
- for ($i = 0; $i < $countPlayers; $i++)
- {
- $field .= $i + 1 . '. <input placeholder="name player" required type="text" name="name_player_'. ($i + 1) . '">';
- $field .=
- '
- <select name="type_player_' . ($i + 1) .'">
- <option value="Human">Human</option>
- <option value="Machine">Machine</option>
- </select> <br>
- ';
- };
- $field .= '<input type="submit" name="btn" value="Send">';
- $codeForm = "2";
- return sprintf($form, $name, $field, $codeForm);
- case 3:
- $field = "";
- if (1)
- {
- $i = 1;
- foreach ($session->get("players") as $value)
- {
- $field .= "π $i. " . "Player [" . $value[0] . "] : ready => OK!" . '<br>';
- $i++;
- }
- }
- $field .= '<a href="game"><button>Start Game</button></a>';
- return $field;
- }
- }
- sub01($this->app->session, $this->app->request);
- return sub02($this->app->session, $this->app->request);
- }
- /**
- * This is the debug method action, it handles:
- *
- * @return string
- */
- public function debugAction() : string
- {
- return "Debu!! my Game!!";
- }
- /**
- * This is the index method action, it handles:
- *
- * @return string
- */
- public function indexAction() : string
- {
- return "INDEX!!!";
- }
- /**
- * This is the start method action, it handles:
- *
- * @return object
- */
- public function startActionGet() : object
- {
- $session = $this->app->session;
- $session->set("form", 1);
- $session->set("players_count", null);
- $session->set("players", null);
- $session->set("dice_game", null);
- $title = "Welcome Dice Game";
- $this->app->page->add("dice/short-game-terms");
- return $this->app->page->render([
- "title" => $title,
- ]);
- }
- /**
- * This is the form method action, it handles:
- *
- * @return object
- */
- public function formActionGet() : object
- {
- $title = "Please insert data players";
- $data = [
- "name" => "hello-world",
- "content" => "Hello World in " . __FILE__,
- "form" => $this->form(),
- ];
- $this->app->page->add("dice/starting", $data);
- return $this->app->page->render([
- "title" => $title,
- ]);
- }
- /**
- * This is the game method action, it handles:
- *
- * @return object
- */
- public function gameActionGet() : object
- {
- $winner = null;
- $type_player = null;
- $name_player_move = null;
- $result = null;
- $trows = [6, 6];
- $request = $this->app->request;
- $session = $this->app->session;
- if (!($this->app->session->get("dice_game")))
- {
- $players = [];
- foreach ($this->app->session->get("players") as $key => $value)
- {
- $players[] = [$value[0], $value[1]];
- }
- $session->set("dice_game", new Dice($players));
- $session->get("dice_game")->contestWhoIsFirst();
- $type_player = $session->get("dice_game")->players[0]->type;
- $name_player_move = $session->get("dice_game")->players[0]->name;
- }
- if ($request->getGet("btn", 0))
- {
- $result = $session->get("dice_game")->moves();
- switch ($result[0]) {
- case 'next':
- $name_player_move = $result[1];
- $type_player = $result[2];
- $trows = $result[3];
- break;
- case 'winner':
- $winner = $result[1];
- break;
- }
- }
- if ($request->getGet("continue", 0))
- {
- $result = $session->get("dice_game")->moves(1);
- switch ($result[0]) {
- case 'next':
- $name_player_move = $result[1];
- $type_player = $result[2];
- $trows = $result[3];
- break;
- case 'winner':
- $winner = $result[1];
- break;
- }
- }
- $histogram = new DiceHistogram();
- $dice = $session->get("dice_game");
- $histogram->injectData($dice);
- $hist = $histogram->getAsText();
- $players_statistics = $session->get("dice_game")->printedPlayers();
- $currentTotal = $session->get("dice_game")->currentTotal ? $session->get("dice_game")->currentTotal : $session->get("dice_game")->tempValue;
- $title = "Live game Dice 100";
- $data = [
- "h1" => $title,
- "statistics" => $players_statistics,
- "type_player" => $type_player,
- "name_player_move" => $name_player_move,
- "winner" => $winner,
- "trows" => $trows,
- "current" => $currentTotal,
- "histogram" => $hist,
- ];
- $this->app->page->add("dice/game", $data);
- return $this->app->page->render([
- "title" => $title,
- ]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement