Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\Alexa\EinstellungstestTrainer\Allgemeinwissenstest;
- use App\Assessment;
- use App\TestQuestion;
- use App\QuestionAnswer;
- use App\Http\Controllers\Alexa\Statics;
- use App\Http\Controllers\Controller;
- class VersionController extends Controller
- {
- public $post;
- /**
- * VersionController constructor.
- * @param $version
- */
- public function __construct($version)
- {
- switch ($version) {
- case "v1":
- return Statics::notImplemented();
- break;
- case "v2":
- $this->post = new v2_0($this);
- break;
- case "v3":
- $this->post = new v3_0($this);
- break;
- }
- }
- /**
- * @param $count
- * @return array
- */
- public function questionIDs($count)
- {
- foreach (Assessment::find(58)->all_tests() as $test)
- foreach ($test->questions as $question) {
- $answs = array();
- foreach ($question->answers as $answer)
- $answs[] = collect($answer)
- ->only(["id", "expected_value"])
- ->all();
- $question->answs = $answs;
- $quests[] = collect($question)
- ->only(["id", "answs"])
- ->all();
- }
- if ($count > count($quests))
- $count = count($quests);
- $return = array();
- $rands = array_rand($quests, $count);
- if ($count == 1)
- $return[] = $quests[$rands];
- else
- foreach ($rands as $int)
- $return[] = $quests[$int];
- return json_decode(json_encode($return));
- }
- /**
- * @param $request
- * @param null $questions
- * @return array
- */
- public function nextQuestionText($request, $questions = null)
- {
- $data = Statics::getRequest($request);
- $nextIndex = $data->session->attributes->currentIndex + 1;
- if ($questions == null)
- $questions = $data->session->attributes->questions;
- $numberOfAnsws = count($questions{$nextIndex}->answs);
- $nextQuestion = $nextIndex + 1;
- $questionText = $this->question($questions{$nextIndex}->id);
- $outputText = "Frage $nextQuestion: $questionText ";
- if ($numberOfAnsws > 0) {
- $answerText = $this->answer($questions{$nextIndex}->answs{0}->id);
- if ($questions{$nextIndex}->answs{0}->expected_value == 1) {
- $correctIndex = 1;
- $correctText = $answerText;
- }
- $outputText .= "1. $answerText ";
- }
- if ($numberOfAnsws > 1) {
- $answerText = $this->answer($questions{$nextIndex}->answs{1}->id);
- if ($questions{$nextIndex}->answs{1}->expected_value == 1) {
- $correctIndex = 2;
- $correctText = $answerText;
- }
- $outputText .= "2. $answerText ";
- }
- if ($numberOfAnsws > 2) {
- $answerText = $this->answer($questions{$nextIndex}->answs{2}->id);
- if ($questions{$nextIndex}->answs{2}->expected_value == 1) {
- $correctIndex = 3;
- $correctText = $answerText;
- }
- $outputText .= "3. $answerText ";
- }
- if ($numberOfAnsws > 3) {
- $answerText = $this->answer($questions{$nextIndex}->answs{3}->id);
- if ($questions{$nextIndex}->answs{3}->expected_value == 1) {
- $correctIndex = 4;
- $correctText = $answerText;
- }
- $outputText .= "4. $answerText ";
- }
- if ($numberOfAnsws > 4) {
- $answerText = $this->answer($questions{$nextIndex}->answs{4}->id);
- if ($questions{$nextIndex}->answs{4}->expected_value == 1) {
- $correctIndex = 5;
- $correctText = $answerText;
- }
- $outputText .= "5. $answerText ";
- }
- if ($numberOfAnsws > 5) {
- $answerText = $this->answer($questions{$nextIndex}->answs{5}->id);
- if ($questions{$nextIndex}->answs{5}->expected_value == 1) {
- $correctIndex = 6;
- $correctText = $answerText;
- }
- $outputText .= "6. $answerText ";
- }
- return json_decode(json_encode([
- "newIndex" => $nextIndex,
- "question" => $outputText,
- "correctIndex" => $correctIndex,
- "correctText" => $correctText,
- ]));
- }
- /**
- * @param $id
- * @return string
- */
- public function question($id)
- {
- $question = TestQuestion::find($id);
- return $question->question;
- }
- /**
- * @param $id
- * @return string
- */
- public function answer($id)
- {
- $answer = QuestionAnswer::find($id);
- return $answer->text;
- }
- }
- class v0_0
- {
- protected $versionController;
- /**
- * v3_0 constructor.
- * @param VersionController $versionController
- */
- public function __construct(VersionController $versionController)
- {
- $this->versionController = $versionController;
- }
- protected $attributes = [
- "selectedMode" => "Allgemeinwissenstest",
- "StartOver" => false,
- "Stop" => false
- ];
- public function index($request)
- {
- return Statics::notImplemented($request);
- }
- }
- class v2_0
- {
- protected $versionController;
- /**
- * v2_0 constructor.
- * @param VersionController $versionController
- */
- public function __construct(VersionController $versionController)
- {
- $this->versionController = $versionController;
- }
- protected $attributes = [
- "selectedMode" => "Allgemeinwissenstest",
- "numberOfGames" => 0,
- "gameFinished" => false,
- "StartOver" => false,
- "Stop" => false
- ];
- /**
- * @param $request
- * @return array
- */
- public function index($request)
- {
- Statics::logError($request, "WholeRequest");
- // Get Alexa Request
- $data = Statics::getRequest($request);
- switch ($data->request->type) {
- //temp
- case "LaunchRequest":
- if (!isset($data->session->attributes->numberOfGames))
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.welcome", ["gameName" => "Einstllungstest Trainer"]) . __("alexa.questionsLimit")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
- "shouldEndSession" => false
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . __("alexa.questionsLimit") . "</div>"
- ]
- ]
- ]
- ],
- "sessionAttributes" => [
- "speechOutput" => __("alexa.modeChoice"),
- "selectedMode" => $this->attributes["selectedMode"],
- "numberOfGames" => $this->attributes["numberOfGames"],
- "score" => 0,
- "currentIndex" => -1,
- "StartOver" => $this->attributes["StartOver"],
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => false
- ],
- "userAgent" => Statics::userAgent()
- ];
- else
- return $this->intentSwitch($request);
- break;
- case "IntentRequest":
- if (!isset($data->session->attributes->numberOfGames))
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
- "shouldEndSession" => false
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . __("alexa.questionsLimit") . "</div>"
- ]
- ]
- ]
- ],
- "sessionAttributes" => [
- "speechOutput" => __("alexa.modeChoice"),
- "selectedMode" => $this->attributes["selectedMode"],
- "numberOfGames" => $this->attributes["numberOfGames"],
- "score" => 0,
- "currentIndex" => -1,
- "StartOver" => $this->attributes["StartOver"],
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => false
- ],
- "userAgent" => Statics::userAgent()
- ];
- else
- return $this->intentSwitch($request);
- break;
- case "SessionEndedRequest":
- return $this->intentSwitch($request, "AMAZON.StopIntent");
- break;
- default:
- Statics::logError($request, "malious IntentType");
- return Statics::notUnderstood($request);
- break;
- }
- }
- /**
- * @param $request
- * @param null $intent
- * @return array
- */
- private function intentSwitch($request, $intent = null)
- {
- $data = Statics::getRequest($request);
- if ($intent == null)
- $intent = $data->request->intent->name;
- switch ($intent) {
- //Custom Intents
- case "AnswerIntent":
- if ($data->session->attributes->numberOfGames == null) {
- $numberOfGames = $data->request->intent->slots->answer->value;
- $questions = $this->versionController->questionIDs($numberOfGames);
- $questAttr = $this->versionController->nextQuestionText($request, $questions);
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.ok") . $questAttr->question],
- "reprompt" => ["type" => "PlainText", "text" => $questAttr->question],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => [
- "speechOutput" => $questAttr->question,
- "score" => 0,
- "currentIndex" => $questAttr->newIndex,
- "correctIndex" => $questAttr->correctIndex,
- "correctText" => $questAttr->correctText,
- "numberOfGames" => $numberOfGames,
- "selectedMode" => $this->attributes["selectedMode"],
- "StartOver" => $this->attributes["StartOver"],
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => false,
- "questions" => $questions
- ],
- "userAgent" => Statics::userAgent()
- ];
- } else
- return $this->buildAnswer($request, "");
- break;
- case "CodeIntent":
- return Statics::notUnderstood($request);
- break;
- //Standard Intents
- case "AMAZON.YesIntent":
- if ($data->session->attributes->Stop)
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.bye")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.bye")],
- "shouldEndSession" => true
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . __("alexa.bye") . "</div>"
- ]
- ]
- ]
- ],
- "userAgent" => Statics::userAgent()
- ];
- elseif ($data->session->attributes->StartOver)
- $data->request->type = "LaunchRequest";
- break;
- case "AMAZON.NoIntent":
- if ($data->session->attributes->Stop) {
- $data->session->attributes->Stop = false;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- } elseif ($data->session->attributes->StartOver) {
- if ($data->session->attributes->gameFinished)
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.bye")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.bye")],
- "shouldEndSession" => true
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . __("alexa.bye") . "</div>"
- ]
- ]
- ]
- ],
- "userAgent" => Statics::userAgent()
- ];
- $data->session->attributes->StartOver = false;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
- "shouldEndSession" => true
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- }
- break;
- case "AMAZON.RepeatIntent":
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => $data->session->attributes->speechOutput],
- "reprompt" => ["type" => "PlainText", "text" => $data->session->attributes->speechOutput],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- case "AMAZON.CancelIntent":
- $data->session->attributes->Stop = true;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.abort")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.abort")],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- case "AMAZON.StopIntent":
- $data->session->attributes->Stop = true;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.abort")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.abort")],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- case "AMAZON.StartOverIntent":
- $data->session->attributes->StartOver = true;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.startOver")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.startOver")],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- case "AMAZON.HelpIntent":
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.help") . $data->session->attributes->speechOutput],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.help") . $data->session->attributes->speechOutput],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- //Pause / Resume Intents
- case "AMAZON.ResumeIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.PauseIntent":
- return Statics::notImplemented($request);
- break;
- //Navigation
- case "AMAZON.NextIntent":
- return $this->buildAnswer($request, __("alexa.rightAnswer", ["rightIndex" => $data->session->attributes->correctIndex, "rightText" => $data->session->attributes->correctText]));
- break;
- case "AMAZON.PreviousIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.MoreIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.NavigateHomeIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.NavigateSettingsIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.PageUpIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.PageDownIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ScrollUpIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ScrollDownIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ScrollRightIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ScrollLeftIntent":
- return Statics::notImplemented($request);
- break;
- //On / Off Intents
- case "AMAZON.LoopOnIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.LoopOffIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ShuffleOnIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ShuffleOffIntent":
- return Statics::notImplemented($request);
- break;
- //Other
- case "AMAZON.FallbackIntent":
- return Statics::notUnderstood($request);
- break;
- //Default
- default:
- Statics::logError($request, "UnknownIntent");
- return Statics::notUnderstood($request);
- break;
- }
- }
- /**
- * @param $request
- * @param $outputSpeech
- * @return array
- */
- private function buildAnswer($request, $outputSpeech)
- {
- $data = Statics::getRequest($request);
- if ($data->request->intent->name == "AnswerIntent") {
- if ($data->request->intent->slots->answer->value == $data->session->attributes->correctIndex) {
- $outputSpeech .= __("alexa.right");
- $score = $data->session->attributes->score + 1;
- } else {
- $outputSpeech .= __("alexa.wrong") . __("alexa.rightAnswer", ["rightIndex" => $data->session->attributes->correctIndex, "rightText" => $data->session->attributes->correctText]);
- $score = $data->session->attributes->score;
- }
- } elseif ($data->request->intent->name == "AMAZON.NextIntent")
- $score = $data->session->attributes->score;
- if ($data->session->attributes->currentIndex == ($data->session->attributes->numberOfGames - 1)) {
- $outputSpeech .= __("alexa.done");
- $numberOfGames = $data->session->attributes->numberOfGames;
- if ($score == $numberOfGames)
- $outputSpeech .= __("alexa.result_perfect");
- else
- $outputSpeech .= __("alexa.result", ["score" => $score, "totalQuestions" => $numberOfGames]);
- $playAgain = false;
- if ($score == $numberOfGames) {
- $outputSpeech .= trans_choice("alexa.result_answer", 1);
- } elseif ($score <= $numberOfGames / 5) {
- $outputSpeech .= trans_choice("alexa.result_answer", 4);
- $playAgain = true;
- } elseif ($score <= $numberOfGames / 2) {
- $outputSpeech .= trans_choice("alexa.result_answer", 3);
- $playAgain = true;
- } elseif ($score > $numberOfGames / 2) {
- $outputSpeech .= trans_choice("alexa.result_answer", 2);
- }
- if ($playAgain)
- $outputSpeech .= __("alexa.playAgain");
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => $outputSpeech],
- "reprompt" => ["type" => "PlainText", "text" => $outputSpeech],
- "shouldEndSession" => !$playAgain
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . $score . " / " . $numberOfGames . "</div>"
- ]
- ]
- ]
- ],
- "sessionAttributes" => [
- "speechOutput" => $outputSpeech,
- "selectedMode" => $this->attributes["selectedMode"],
- "numberOfGames" => 0,
- "StartOver" => $playAgain,
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => true
- ],
- "userAgent" => Statics::userAgent()
- ];
- } else {
- $questions = $data->session->attributes->questions;
- $questAttr = $this->versionController->nextQuestionText($request);
- $outputSpeech .= __("alexa.nextQuestion") . $questAttr->question;
- }
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => $outputSpeech],
- "reprompt" => ["type" => "PlainText", "text" => $outputSpeech],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => [
- "speechOutput" => $outputSpeech,
- "score" => $score,
- "currentIndex" => $questAttr->newIndex,
- "correctIndex" => $questAttr->correctIndex,
- "correctText" => $questAttr->correctText,
- "numberOfGames" => $data->session->attributes->numberOfGames,
- "selectedMode" => $this->attributes["selectedMode"],
- "StartOver" => $this->attributes["StartOver"],
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => true,
- "questions" => $questions
- ],
- "userAgent" => Statics::userAgent()
- ];
- }
- }
- class v3_0
- {
- protected $versionController;
- /**
- * v2_0 constructor.
- * @param VersionController $versionController
- */
- public function __construct(VersionController $versionController)
- {
- $this->versionController = $versionController;
- }
- protected $attributes = [
- "selectedMode" => "Allgemeinwissenstest",
- "gameFinished" => false,
- "StartOver" => false,
- "Stop" => false
- ];
- /**
- * @param $request
- * @return array
- */
- public function index($request)
- {
- // Get Alexa Request
- $data = Statics::getRequest($request);
- switch ($data->request->type) {
- case "IntentRequest":
- if (!isset($data->session->attributes->numberOfGames))
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
- "shouldEndSession" => false
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . __("alexa.questionsLimit") . "</div>"
- ]
- ]
- ]
- ],
- "sessionAttributes" => [
- "speechOutput" => __("alexa.modeChoice"),
- "selectedMode" => $this->attributes["selectedMode"],
- "numberOfGames" => 0,
- "score" => 0,
- "currentIndex" => -1,
- "StartOver" => $this->attributes["StartOver"],
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => false
- ],
- "userAgent" => Statics::userAgent()
- ];
- else
- return $this->intentSwitch($request);
- break;
- case "SessionEndedRequest":
- return $this->intentSwitch($request, "AMAZON.StopIntent");
- break;
- default:
- Statics::logError($request, "malious IntentType");
- return Statics::notUnderstood($request);
- break;
- }
- }
- /**
- * @param $request
- * @param null $intent
- * @return array
- */
- private function intentSwitch($request, $intent = null)
- {
- $data = Statics::getRequest($request);
- if ($intent != null)
- $data->request->intent->name = $intent;
- switch ($data->request->intent->name) {
- //Custom Intents
- case "AnswerIntent":
- if ($data->session->attributes->numberOfGames == null) {
- $numberOfGames = $data->request->intent->slots->answer->value;
- $questions = json_decode(json_encode($this->versionController->questionIDs($numberOfGames)));
- $questAttr = $this->versionController->nextQuestionText($request, $questions);
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.ok") . $questAttr->question],
- "reprompt" => ["type" => "PlainText", "text" => $questAttr->question],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => [
- "speechOutput" => $questAttr->question,
- "score" => 0,
- "currentIndex" => $questAttr->newIndex,
- "correctIndex" => $questAttr->correctIndex,
- "correctText" => $questAttr->correctText,
- "numberOfGames" => $numberOfGames,
- "selectedMode" => $this->attributes["selectedMode"],
- "StartOver" => $this->attributes["StartOver"],
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => false,
- "questions" => $questions
- ],
- "userAgent" => Statics::userAgent()
- ];
- } else
- return $this->buildAnswer($request, "");
- break;
- case "CodeIntent":
- return Statics::notUnderstood($request);
- break;
- //Standard Intents
- case "AMAZON.YesIntent":
- if ($data->session->attributes->Stop)
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.bye")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.bye")],
- "shouldEndSession" => true
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . __("alexa.bye") . "</div>"
- ]
- ]
- ]
- ],
- "userAgent" => Statics::userAgent()
- ];
- elseif ($data->session->attributes->StartOver)
- $data->request->type = "LaunchRequest";
- break;
- case "AMAZON.NoIntent":
- if ($data->session->attributes->Stop) {
- $data->session->attributes->Stop = false;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- } elseif ($data->session->attributes->StartOver) {
- if ($data->session->attributes->gameFinished)
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.bye")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.bye")],
- "shouldEndSession" => true
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . __("alexa.bye") . "</div>"
- ]
- ]
- ]
- ],
- "userAgent" => Statics::userAgent()
- ];
- $data->session->attributes->StartOver = false;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
- "shouldEndSession" => true
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- }
- break;
- case "AMAZON.RepeatIntent":
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => $data->session->attributes->speechOutput],
- "reprompt" => ["type" => "PlainText", "text" => $data->session->attributes->speechOutput],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- case "AMAZON.CancelIntent":
- $data->session->attributes->Stop = true;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.abort")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.abort")],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- case "AMAZON.StopIntent":
- $data->session->attributes->Stop = true;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.abort")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.abort")],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- case "AMAZON.StartOverIntent":
- $data->session->attributes->StartOver = true;
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.startOver")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.startOver")],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- case "AMAZON.HelpIntent":
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.help")],
- "reprompt" => ["type" => "PlainText", "text" => __("alexa.help")],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => $data->session->attributes,
- "userAgent" => Statics::userAgent()
- ];
- break;
- //Pause / Resume Intents
- case "AMAZON.ResumeIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.PauseIntent":
- return Statics::notImplemented($request);
- break;
- //Navigation
- case "AMAZON.NextIntent":
- return $this->buildAnswer($request, __("alexa.rightAnswer", ["rightIndex" => $data->session->attributes->correctIndex, "rightText" => $data->session->attributes->correctText]));
- break;
- case "AMAZON.PreviousIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.MoreIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.NavigateHomeIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.NavigateSettingsIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.PageUpIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.PageDownIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ScrollUpIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ScrollDownIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ScrollRightIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ScrollLeftIntent":
- return Statics::notImplemented($request);
- break;
- //On / Off Intents
- case "AMAZON.LoopOnIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.LoopOffIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ShuffleOnIntent":
- return Statics::notImplemented($request);
- break;
- case "AMAZON.ShuffleOffIntent":
- return Statics::notImplemented($request);
- break;
- //Other
- case "AMAZON.FallbackIntent":
- return Statics::notUnderstood($request);
- break;
- //Default
- default:
- Statics::logError($request, "UnknownIntent");
- return Statics::notUnderstood($request);
- break;
- }
- }
- /**
- * @param $request
- * @param $outputSpeech
- * @return array
- */
- private function buildAnswer($request, $outputSpeech)
- {
- $data = Statics::getRequest($request);
- if ($data->request->intent->name == "AnswerIntent") {
- if ($data->request->intent->slots->answer->value == $data->session->attributes->correctIndex) {
- $outputSpeech .= __("alexa.right");
- $score = $data->session->attributes->score + 1;
- } else {
- $outputSpeech .= __("alexa.wrong") . __("alexa.rightAnswer", ["rightIndex" => $data->session->attributes->correctIndex, "rightText" => $data->session->attributes->correctText]);
- $score = $data->session->attributes->score;
- }
- } elseif ($data->request->intent->name == "AMAZON.NextIntent")
- $score = $data->session->attributes->score;
- if ($data->session->attributes->currentIndex == ($data->session->attributes->numberOfGames - 1)) {
- $outputSpeech .= __("alexa.done");
- $numberOfGames = $data->session->attributes->numberOfGames;
- if ($score == $numberOfGames)
- $outputSpeech .= __("alexa.result_perfect");
- else
- $outputSpeech .= __("alexa.result", ["score" => $score, "totalQuestions" => $numberOfGames]);
- $playAgain = false;
- if ($score == $numberOfGames) {
- $outputSpeech .= trans_choice("alexa.result_answer", 1);
- } elseif ($score <= $numberOfGames / 5) {
- $outputSpeech .= trans_choice("alexa.result_answer", 4);
- $playAgain = true;
- } elseif ($score <= $numberOfGames / 2) {
- $outputSpeech .= trans_choice("alexa.result_answer", 3);
- $playAgain = true;
- } elseif ($score > $numberOfGames / 2) {
- $outputSpeech .= trans_choice("alexa.result_answer", 2);
- }
- if ($playAgain)
- $outputSpeech .= __("alexa.playAgain");
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => $outputSpeech],
- "reprompt" => ["type" => "PlainText", "text" => $outputSpeech],
- "shouldEndSession" => !$playAgain
- ],
- "directives" => [
- "type" => "Display.RenderTemplate",
- "template" => [
- "type" => "BodyTemplate3",
- "textContent" => [
- "primaryText" => [
- "type" => "RichText",
- "text" => "<div align='center'>" . $score . " / " . $numberOfGames . "</div>"
- ]
- ]
- ]
- ],
- "sessionAttributes" => [
- "speechOutput" => $outputSpeech,
- "selectedMode" => $this->attributes["selectedMode"],
- "numberOfGames" => 0,
- "StartOver" => $playAgain,
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => true
- ],
- "userAgent" => Statics::userAgent()
- ];
- } else {
- $questions = $data->session->attributes->questions;
- $questAttr = $this->versionController->nextQuestionText($request);
- $outputSpeech .= __("alexa.nextQuestion") . $questAttr->question;
- }
- return [
- "response" => [
- "outputSpeech" => ["type" => "PlainText", "text" => $outputSpeech],
- "reprompt" => ["type" => "PlainText", "text" => $outputSpeech],
- "shouldEndSession" => false
- ],
- "sessionAttributes" => [
- "speechOutput" => $outputSpeech,
- "score" => $score,
- "currentIndex" => $questAttr->newIndex,
- "correctIndex" => $questAttr->correctIndex,
- "correctText" => $questAttr->correctText,
- "numberOfGames" => $data->session->attributes->numberOfGames,
- "selectedMode" => $this->attributes["selectedMode"],
- "StartOver" => $this->attributes["StartOver"],
- "Stop" => $this->attributes["Stop"],
- "gameFinished" => true,
- "questions" => $questions
- ],
- "userAgent" => Statics::userAgent()
- ];
- }
- }
Add Comment
Please, Sign In to add comment