public function actionCreate() { $model = new Words(); $categories = WordsCategory::find()->all(); if ($model->load(\Yii::$app->request->post())) { if (!$model->save()) { var_dump($model->errors); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'categories' => $categories]); } }
public function search($params) { $query = Words::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'category_id' => $this->category_id, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'author', $this->author])->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'slug', $this->slug]); return $dataProvider; }
public static function getImage() { $game = Games::find('first', array('conditions' => array('id' => Session::read('game_id')))); $word = Words::find('first', array('conditions' => array('id' => $game->word_id))); $img_number = self::MAX_IMAGE_COUNT - $game->max_wrong_tries + $game->wrong_tries; if ($game->max_wrong_tries == 5) { switch ($game->wrong_tries) { case 1: $img_number = 3; break; case 2: $img_number = 5; break; case 3: $img_number = 7; break; case 4: $img_number = 9; break; case 5: $img_number = 11; break; case 0: default: $img_number = 0; break; } } return self::HANG_IMAGE_PRE . $img_number . self::HANG_IMAGE_POST; }
public function trychar($char) { // do we have an active game session? if (!Session::check("game_id", array('name' => 'default'))) { return $this->redirect('games::index'); } // state: 0 = in-game, 1 = won, 2 = lost, 3 = already won, 4 = already lost $game = Games::find('first', array('conditions' => array('id' => Session::read('game_id')))); $word = Words::find('first', array('conditions' => array('id' => $game->word_id))); $resp = array("success" => 0, "wrong_tries_left" => $game->max_wrong_tries - $game->wrong_tries, "placeholders" => Games::getPlaceholders(), "image" => Games::getImage(), "message" => "", "state" => 0, "word" => ""); // game already won if ($game->state == 1) { $resp['message'] = "You already won!"; $resp['state'] = 3; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // game already lost } elseif ($game->state == 2) { $resp['message'] = "You already lost!"; $resp['state'] = 4; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); } // ist der gesendete buchstabe erlaubt? if (!Games::isValidChar($char)) { $resp['success'] = 0; $resp['message'] = "Non-valid char used."; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // falls der gesendete buchstabe schonmal gesendet wurde } elseif (stripos($game->input_text, $char) !== false) { $resp['success'] = 0; $resp['message'] = "Char was already used."; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // buchstabe ist valid und wurde nicht schon gesendet } else { $game->input_text .= $char; $game->save(); $resp['success'] = 1; $resp['placeholders'] = Games::getPlaceholders(); // buchstabe kommt in gesuchtem wort vor if (!(stripos($word->value, $char) === false) && stripos($word->value, $char) >= 0) { $resp['image'] = Games::getImage(); // alle zeichen erraten, gewonnen // WIN! if (stripos(Games::getPlaceholders(), self::PLACEHOLDER) === false) { $game->state = 1; // won $game->save(); $resp['state'] = 1; // won $resp['message'] = "You won! Play a new game?"; $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // buchstabe stimmt, aber noch nicht alles erraten } else { $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries; $resp['message'] = "Nice! Guess the next char!"; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); } // buchstabe kommt NICHT vor } else { $game->wrong_tries++; $game->save(); $resp['image'] = Games::getImage(); // hat der benutzer die maximale anzahl versuche überschritten? // LOOSE! if ($game->wrong_tries >= $game->max_wrong_tries) { $resp['wrong_tries_left'] = 0; $resp['message'] = "Out of tries. New Game?"; $resp['state'] = 2; // lost $game->state = 2; // lost $resp['word'] = $word->value; $game->save(); return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // falscher buchstaben eingegeben, benutzer hat weiteren versuch } else { $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries; $resp['message'] = "Nope. Try again!"; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); } } } return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); }
use yii\bootstrap\Html; use yii\helpers\ArrayHelper; use app\models\Words; ?> <div class="words-form"> <?php $form = \yii\bootstrap\ActiveForm::begin(); ?> <?php echo $form->field($model, 'category_id')->dropDownList(ArrayHelper::map($categories, 'id', 'title'), ['prompt' => '']); ?> <?php echo $form->field($model, 'author')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'content')->textarea(); ?> <?php echo $form->field($model, 'status')->dropDownList(Words::getStatus()); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => 'btn btn-success']); ?> </div> <?php \yii\bootstrap\ActiveForm::end(); ?> </div>
/** * @return \yii\db\ActiveQuery */ public function getWords() { return $this->hasMany(Words::className(), ['category_id' => 'id']); }