diff --git a/controllers/SiteController.php b/controllers/SiteController.php index 67c3f50..0bdb4e2 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -2,6 +2,7 @@ namespace app\controllers; +use app\models\ComputationResult; use Yii; use yii\filters\AccessControl; use yii\web\Controller; @@ -9,120 +10,50 @@ use yii\web\Response; use yii\filters\VerbFilter; use app\models\LoginForm; use app\models\ContactForm; +use app\models\DeckForm; class SiteController extends Controller { - /** - * {@inheritdoc} - */ - public function behaviors() - { - return [ - 'access' => [ - 'class' => AccessControl::class, - 'only' => ['logout'], - 'rules' => [ - [ - 'actions' => ['logout'], - 'allow' => true, - 'roles' => ['@'], - ], - ], - ], - 'verbs' => [ - 'class' => VerbFilter::class, - 'actions' => [ - 'logout' => ['post'], - ], - ], - ]; - } + /** + * {@inheritdoc} + */ + public function behaviors() + { + return [ + ]; + } - /** - * {@inheritdoc} - */ - public function actions() - { - return [ - 'error' => [ - 'class' => 'yii\web\ErrorAction', - ], - 'captcha' => [ - 'class' => 'yii\captcha\CaptchaAction', - 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, - ], - ]; - } + /** + * {@inheritdoc} + */ + public function actions() + { + return [ + 'error' => [ + 'class' => 'yii\web\ErrorAction', + ], + ]; + } - /** - * Displays homepage. - * - * @return string - */ - public function actionIndex() - { - return $this->render('index'); - } - - /** - * Login action. - * - * @return Response|string - */ - public function actionLogin() - { - if (!Yii::$app->user->isGuest) { - return $this->goHome(); - } - - $model = new LoginForm(); - if ($model->load(Yii::$app->request->post()) && $model->login()) { - return $this->goBack(); - } - - $model->password = ''; - return $this->render('login', [ - 'model' => $model, - ]); - } - - /** - * Logout action. - * - * @return Response - */ - public function actionLogout() - { - Yii::$app->user->logout(); - - return $this->goHome(); - } - - /** - * Displays contact page. - * - * @return Response|string - */ - public function actionContact() - { - $model = new ContactForm(); - if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) { - Yii::$app->session->setFlash('contactFormSubmitted'); - - return $this->refresh(); - } - return $this->render('contact', [ - 'model' => $model, - ]); - } - - /** - * Displays about page. - * - * @return string - */ - public function actionAbout() - { - return $this->render('about'); - } + /** + * Displays homepage. + * + * @return string + */ + public function actionIndex() + { + $computation_result = new ComputationResult(); + $deck_form = new DeckForm(); + if ($deck_form->load(Yii::$app->request->post())) + { + if ($deck_form->validate()) + { + $computation_result = $deck_form->compute(); + } + } + return $this->render('index', [ + 'deck_form' => $deck_form, + 'result' => $computation_result, + ]); + } } diff --git a/models/ComputationResult.php b/models/ComputationResult.php new file mode 100644 index 0000000..433f969 --- /dev/null +++ b/models/ComputationResult.php @@ -0,0 +1,7 @@ + 'Размер колоды', + 'weapons_items' => 'Оружие (вещь)', + 'weapons_spells' => 'Оружие (заклинание)', + 'card_prepared_to_worst' => 'Готовность к худшему', + 'card_backpack_0' => 'Рюкзак (0)', + 'card_backpack_2' => 'Рюкзак (2)', + 'card_initiate' => 'Посвящённая', + ]; + } + + private $result; + + public function compute() : ComputationResult + { + $this->result = new ComputationResult(); + $this->result->probabilities = []; + for ($i = 0; $i < self::COMPUTE_ROUNDS; $i++) $this->result->probabilities[] = 0; + for ($i = 0; $i < self::COMPUTE_TRIES; $i++) + { + $this->generateTry(); + } + for ($i = 0; $i < self::COMPUTE_ROUNDS; $i++) $this->result->probabilities[$i] /= (self::COMPUTE_TRIES + 0.0); + return $this->result; + } + + private $deck; + private $hand; + + private function fillDeck() + { + $this->deck = []; + for ($i = 0; $i < $this->size; $i++) $this->deck[] = self::CARD_ANY; + $pos = 0; + for ($i = 0; $i < $this->weapons_items && $pos < count($this->deck); $i++) + { + $this->deck[$pos++] = self::CARD_WEAPON_ITEM; + } + for ($i = 0; $i < $this->weapons_spells && $pos < count($this->deck); $i++) + { + $this->deck[$pos++] = self::CARD_WEAPON_SPELL; + } + for ($i = 0; $i < $this->card_backpack_0 && $pos < count($this->deck); $i++) + { + $this->deck[$pos++] = self::CARD_BACKPACK_0; + } + for ($i = 0; $i < $this->card_backpack_2 && $pos < count($this->deck); $i++) + { + $this->deck[$pos++] = self::CARD_BACKPACK_2; + } + for ($i = 0; $i < $this->card_prepared_to_worst && $pos < count($this->deck); $i++) + { + $this->deck[$pos++] = self::CARD_PREPARED; + } + for ($i = 0; $i < $this->card_initiate && $pos < count($this->deck); $i++) + { + $this->deck[$pos++] = self::CARD_INITIATE; + } + } + + private function shuffleDeck() + { + for ($i = 0; $i < count($this->deck); $i++) + { + $j = rand(0, $i); + $t = $this->deck[$j]; + $this->deck[$j] = $this->deck[$i]; + $this->deck[$i] = $t; + } + } + + private function addProb($round) + { + for ($i = $round; $i < self::COMPUTE_ROUNDS; $i++) $this->result->probabilities[$i]++; + } + + private function haveWeapon() + { + foreach ($this->hand as $card) + { + if ($card == self::CARD_WEAPON_ITEM) return true; + if ($card == self::CARD_WEAPON_SPELL) return true; + } + return false; + } + + private function generateTry() + { + $this->fillDeck(); + $this->shuffleDeck(); + // mulligan + $this->hand = []; + $replace = []; + for ($i = 0; $i < 5; $i++) + { + $current = array_pop($this->deck); + if ($current != self::CARD_ANY) $this->hand[] = $current; + else $replace[] = $current; + } + while (count($this->hand) < 5) $this->hand[] = array_pop($this->deck); + foreach ($replace as $r) $this->deck[] = $r; + $this->shuffleDeck(); + for ($round = 0; $round < self::COMPUTE_ROUNDS; $round++) + { + if ($this->haveWeapon()) + { + $this->addProb($round); + return; + } + } + } +} diff --git a/views/site/index.php b/views/site/index.php index df5df7c..4570521 100644 --- a/views/site/index.php +++ b/views/site/index.php @@ -1,53 +1,40 @@ title = 'My Yii Application'; +$this->title = 'Карточный ужас Аркхэма. Калькулятор вероятностей.'; ?>
You have successfully created your Yii-powered application.
- - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et - dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip - ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu - fugiat nulla pariatur.
- - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et - dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip - ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu - fugiat nulla pariatur.
- - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et - dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip - ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu - fugiat nulla pariatur.
- - -