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));
}