Guest User

Untitled

a guest
Sep 19th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 46.02 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Alexa\EinstellungstestTrainer\Allgemeinwissenstest;
  4.  
  5. use App\Assessment;
  6. use App\TestQuestion;
  7. use App\QuestionAnswer;
  8. use App\Http\Controllers\Alexa\Statics;
  9. use App\Http\Controllers\Controller;
  10.  
  11. class VersionController extends Controller
  12. {
  13. public $post;
  14.  
  15. /**
  16. * VersionController constructor.
  17. * @param $version
  18. */
  19. public function __construct($version)
  20. {
  21. switch ($version) {
  22. case "v1":
  23. return Statics::notImplemented();
  24. break;
  25.  
  26. case "v2":
  27. $this->post = new v2_0($this);
  28. break;
  29.  
  30. case "v3":
  31. $this->post = new v3_0($this);
  32. break;
  33. }
  34. }
  35.  
  36. /**
  37. * @param $count
  38. * @return array
  39. */
  40. public function questionIDs($count)
  41. {
  42. foreach (Assessment::find(58)->all_tests() as $test)
  43. foreach ($test->questions as $question) {
  44. $answs = array();
  45. foreach ($question->answers as $answer)
  46. $answs[] = collect($answer)
  47. ->only(["id", "expected_value"])
  48. ->all();
  49. $question->answs = $answs;
  50. $quests[] = collect($question)
  51. ->only(["id", "answs"])
  52. ->all();
  53. }
  54.  
  55. if ($count > count($quests))
  56. $count = count($quests);
  57.  
  58. $return = array();
  59. $rands = array_rand($quests, $count);
  60. if ($count == 1)
  61. $return[] = $quests[$rands];
  62. else
  63. foreach ($rands as $int)
  64. $return[] = $quests[$int];
  65.  
  66. return json_decode(json_encode($return));
  67. }
  68.  
  69. /**
  70. * @param $request
  71. * @param null $questions
  72. * @return array
  73. */
  74. public function nextQuestionText($request, $questions = null)
  75. {
  76. $data = Statics::getRequest($request);
  77. $nextIndex = $data->session->attributes->currentIndex + 1;
  78. if ($questions == null)
  79. $questions = $data->session->attributes->questions;
  80.  
  81. $numberOfAnsws = count($questions{$nextIndex}->answs);
  82. $nextQuestion = $nextIndex + 1;
  83. $questionText = $this->question($questions{$nextIndex}->id);
  84.  
  85. $outputText = "Frage $nextQuestion: $questionText ";
  86.  
  87.  
  88. if ($numberOfAnsws > 0) {
  89. $answerText = $this->answer($questions{$nextIndex}->answs{0}->id);
  90. if ($questions{$nextIndex}->answs{0}->expected_value == 1) {
  91. $correctIndex = 1;
  92. $correctText = $answerText;
  93. }
  94. $outputText .= "1. $answerText ";
  95. }
  96. if ($numberOfAnsws > 1) {
  97. $answerText = $this->answer($questions{$nextIndex}->answs{1}->id);
  98. if ($questions{$nextIndex}->answs{1}->expected_value == 1) {
  99. $correctIndex = 2;
  100. $correctText = $answerText;
  101. }
  102. $outputText .= "2. $answerText ";
  103. }
  104. if ($numberOfAnsws > 2) {
  105. $answerText = $this->answer($questions{$nextIndex}->answs{2}->id);
  106. if ($questions{$nextIndex}->answs{2}->expected_value == 1) {
  107. $correctIndex = 3;
  108. $correctText = $answerText;
  109. }
  110. $outputText .= "3. $answerText ";
  111. }
  112. if ($numberOfAnsws > 3) {
  113. $answerText = $this->answer($questions{$nextIndex}->answs{3}->id);
  114. if ($questions{$nextIndex}->answs{3}->expected_value == 1) {
  115. $correctIndex = 4;
  116. $correctText = $answerText;
  117. }
  118. $outputText .= "4. $answerText ";
  119. }
  120. if ($numberOfAnsws > 4) {
  121. $answerText = $this->answer($questions{$nextIndex}->answs{4}->id);
  122. if ($questions{$nextIndex}->answs{4}->expected_value == 1) {
  123. $correctIndex = 5;
  124. $correctText = $answerText;
  125. }
  126. $outputText .= "5. $answerText ";
  127. }
  128. if ($numberOfAnsws > 5) {
  129. $answerText = $this->answer($questions{$nextIndex}->answs{5}->id);
  130. if ($questions{$nextIndex}->answs{5}->expected_value == 1) {
  131. $correctIndex = 6;
  132. $correctText = $answerText;
  133. }
  134. $outputText .= "6. $answerText ";
  135. }
  136.  
  137. return json_decode(json_encode([
  138. "newIndex" => $nextIndex,
  139. "question" => $outputText,
  140. "correctIndex" => $correctIndex,
  141. "correctText" => $correctText,
  142. ]));
  143. }
  144.  
  145. /**
  146. * @param $id
  147. * @return string
  148. */
  149. public function question($id)
  150. {
  151. $question = TestQuestion::find($id);
  152. return $question->question;
  153. }
  154.  
  155. /**
  156. * @param $id
  157. * @return string
  158. */
  159. public function answer($id)
  160. {
  161. $answer = QuestionAnswer::find($id);
  162. return $answer->text;
  163. }
  164. }
  165.  
  166. class v0_0
  167. {
  168. protected $versionController;
  169.  
  170. /**
  171. * v3_0 constructor.
  172. * @param VersionController $versionController
  173. */
  174. public function __construct(VersionController $versionController)
  175. {
  176. $this->versionController = $versionController;
  177. }
  178.  
  179. protected $attributes = [
  180. "selectedMode" => "Allgemeinwissenstest",
  181. "StartOver" => false,
  182. "Stop" => false
  183. ];
  184.  
  185. public function index($request)
  186. {
  187. return Statics::notImplemented($request);
  188. }
  189. }
  190.  
  191. class v2_0
  192. {
  193. protected $versionController;
  194.  
  195. /**
  196. * v2_0 constructor.
  197. * @param VersionController $versionController
  198. */
  199. public function __construct(VersionController $versionController)
  200. {
  201. $this->versionController = $versionController;
  202. }
  203.  
  204. protected $attributes = [
  205. "selectedMode" => "Allgemeinwissenstest",
  206. "numberOfGames" => 0,
  207. "gameFinished" => false,
  208. "StartOver" => false,
  209. "Stop" => false
  210. ];
  211.  
  212. /**
  213. * @param $request
  214. * @return array
  215. */
  216. public function index($request)
  217. {
  218. Statics::logError($request, "WholeRequest");
  219. // Get Alexa Request
  220. $data = Statics::getRequest($request);
  221.  
  222. switch ($data->request->type) {
  223. //temp
  224. case "LaunchRequest":
  225. if (!isset($data->session->attributes->numberOfGames))
  226. return [
  227. "response" => [
  228. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.welcome", ["gameName" => "Einstllungstest Trainer"]) . __("alexa.questionsLimit")],
  229. "reprompt" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
  230. "shouldEndSession" => false
  231. ],
  232. "directives" => [
  233. "type" => "Display.RenderTemplate",
  234. "template" => [
  235. "type" => "BodyTemplate3",
  236. "textContent" => [
  237. "primaryText" => [
  238. "type" => "RichText",
  239. "text" => "<div align='center'>" . __("alexa.questionsLimit") . "</div>"
  240. ]
  241. ]
  242. ]
  243. ],
  244. "sessionAttributes" => [
  245. "speechOutput" => __("alexa.modeChoice"),
  246. "selectedMode" => $this->attributes["selectedMode"],
  247. "numberOfGames" => $this->attributes["numberOfGames"],
  248. "score" => 0,
  249. "currentIndex" => -1,
  250. "StartOver" => $this->attributes["StartOver"],
  251. "Stop" => $this->attributes["Stop"],
  252. "gameFinished" => false
  253. ],
  254. "userAgent" => Statics::userAgent()
  255. ];
  256. else
  257. return $this->intentSwitch($request);
  258. break;
  259.  
  260. case "IntentRequest":
  261. if (!isset($data->session->attributes->numberOfGames))
  262. return [
  263. "response" => [
  264. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
  265. "reprompt" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
  266. "shouldEndSession" => false
  267. ],
  268. "directives" => [
  269. "type" => "Display.RenderTemplate",
  270. "template" => [
  271. "type" => "BodyTemplate3",
  272. "textContent" => [
  273. "primaryText" => [
  274. "type" => "RichText",
  275. "text" => "<div align='center'>" . __("alexa.questionsLimit") . "</div>"
  276. ]
  277. ]
  278. ]
  279. ],
  280. "sessionAttributes" => [
  281. "speechOutput" => __("alexa.modeChoice"),
  282. "selectedMode" => $this->attributes["selectedMode"],
  283. "numberOfGames" => $this->attributes["numberOfGames"],
  284. "score" => 0,
  285. "currentIndex" => -1,
  286. "StartOver" => $this->attributes["StartOver"],
  287. "Stop" => $this->attributes["Stop"],
  288. "gameFinished" => false
  289. ],
  290. "userAgent" => Statics::userAgent()
  291. ];
  292. else
  293. return $this->intentSwitch($request);
  294. break;
  295.  
  296. case "SessionEndedRequest":
  297. return $this->intentSwitch($request, "AMAZON.StopIntent");
  298. break;
  299.  
  300. default:
  301. Statics::logError($request, "malious IntentType");
  302. return Statics::notUnderstood($request);
  303. break;
  304. }
  305. }
  306.  
  307. /**
  308. * @param $request
  309. * @param null $intent
  310. * @return array
  311. */
  312. private function intentSwitch($request, $intent = null)
  313. {
  314. $data = Statics::getRequest($request);
  315.  
  316. if ($intent == null)
  317. $intent = $data->request->intent->name;
  318.  
  319. switch ($intent) {
  320. //Custom Intents
  321. case "AnswerIntent":
  322. if ($data->session->attributes->numberOfGames == null) {
  323. $numberOfGames = $data->request->intent->slots->answer->value;
  324. $questions = $this->versionController->questionIDs($numberOfGames);
  325. $questAttr = $this->versionController->nextQuestionText($request, $questions);
  326.  
  327. return [
  328. "response" => [
  329. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.ok") . $questAttr->question],
  330. "reprompt" => ["type" => "PlainText", "text" => $questAttr->question],
  331. "shouldEndSession" => false
  332. ],
  333. "sessionAttributes" => [
  334. "speechOutput" => $questAttr->question,
  335. "score" => 0,
  336. "currentIndex" => $questAttr->newIndex,
  337. "correctIndex" => $questAttr->correctIndex,
  338. "correctText" => $questAttr->correctText,
  339. "numberOfGames" => $numberOfGames,
  340. "selectedMode" => $this->attributes["selectedMode"],
  341. "StartOver" => $this->attributes["StartOver"],
  342. "Stop" => $this->attributes["Stop"],
  343. "gameFinished" => false,
  344. "questions" => $questions
  345. ],
  346. "userAgent" => Statics::userAgent()
  347. ];
  348. } else
  349. return $this->buildAnswer($request, "");
  350. break;
  351.  
  352. case "CodeIntent":
  353. return Statics::notUnderstood($request);
  354. break;
  355.  
  356. //Standard Intents
  357. case "AMAZON.YesIntent":
  358. if ($data->session->attributes->Stop)
  359. return [
  360. "response" => [
  361. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.bye")],
  362. "reprompt" => ["type" => "PlainText", "text" => __("alexa.bye")],
  363. "shouldEndSession" => true
  364. ],
  365. "directives" => [
  366. "type" => "Display.RenderTemplate",
  367. "template" => [
  368. "type" => "BodyTemplate3",
  369. "textContent" => [
  370. "primaryText" => [
  371. "type" => "RichText",
  372. "text" => "<div align='center'>" . __("alexa.bye") . "</div>"
  373. ]
  374. ]
  375. ]
  376. ],
  377. "userAgent" => Statics::userAgent()
  378. ];
  379. elseif ($data->session->attributes->StartOver)
  380. $data->request->type = "LaunchRequest";
  381. break;
  382.  
  383. case "AMAZON.NoIntent":
  384. if ($data->session->attributes->Stop) {
  385. $data->session->attributes->Stop = false;
  386. return [
  387. "response" => [
  388. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
  389. "reprompt" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
  390. "shouldEndSession" => false
  391. ],
  392. "sessionAttributes" => $data->session->attributes,
  393. "userAgent" => Statics::userAgent()
  394. ];
  395. } elseif ($data->session->attributes->StartOver) {
  396. if ($data->session->attributes->gameFinished)
  397. return [
  398. "response" => [
  399. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.bye")],
  400. "reprompt" => ["type" => "PlainText", "text" => __("alexa.bye")],
  401. "shouldEndSession" => true
  402. ],
  403. "directives" => [
  404. "type" => "Display.RenderTemplate",
  405. "template" => [
  406. "type" => "BodyTemplate3",
  407. "textContent" => [
  408. "primaryText" => [
  409. "type" => "RichText",
  410. "text" => "<div align='center'>" . __("alexa.bye") . "</div>"
  411. ]
  412. ]
  413. ]
  414. ],
  415. "userAgent" => Statics::userAgent()
  416. ];
  417.  
  418. $data->session->attributes->StartOver = false;
  419. return [
  420. "response" => [
  421. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
  422. "reprompt" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
  423. "shouldEndSession" => true
  424. ],
  425. "sessionAttributes" => $data->session->attributes,
  426. "userAgent" => Statics::userAgent()
  427. ];
  428. }
  429. break;
  430.  
  431. case "AMAZON.RepeatIntent":
  432. return [
  433. "response" => [
  434. "outputSpeech" => ["type" => "PlainText", "text" => $data->session->attributes->speechOutput],
  435. "reprompt" => ["type" => "PlainText", "text" => $data->session->attributes->speechOutput],
  436. "shouldEndSession" => false
  437. ],
  438. "sessionAttributes" => $data->session->attributes,
  439. "userAgent" => Statics::userAgent()
  440. ];
  441. break;
  442.  
  443. case "AMAZON.CancelIntent":
  444. $data->session->attributes->Stop = true;
  445. return [
  446. "response" => [
  447. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.abort")],
  448. "reprompt" => ["type" => "PlainText", "text" => __("alexa.abort")],
  449. "shouldEndSession" => false
  450. ],
  451. "sessionAttributes" => $data->session->attributes,
  452. "userAgent" => Statics::userAgent()
  453. ];
  454. break;
  455.  
  456. case "AMAZON.StopIntent":
  457. $data->session->attributes->Stop = true;
  458. return [
  459. "response" => [
  460. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.abort")],
  461. "reprompt" => ["type" => "PlainText", "text" => __("alexa.abort")],
  462. "shouldEndSession" => false
  463. ],
  464. "sessionAttributes" => $data->session->attributes,
  465. "userAgent" => Statics::userAgent()
  466. ];
  467. break;
  468.  
  469. case "AMAZON.StartOverIntent":
  470. $data->session->attributes->StartOver = true;
  471. return [
  472. "response" => [
  473. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.startOver")],
  474. "reprompt" => ["type" => "PlainText", "text" => __("alexa.startOver")],
  475. "shouldEndSession" => false
  476. ],
  477. "sessionAttributes" => $data->session->attributes,
  478. "userAgent" => Statics::userAgent()
  479. ];
  480. break;
  481.  
  482. case "AMAZON.HelpIntent":
  483. return [
  484. "response" => [
  485. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.help") . $data->session->attributes->speechOutput],
  486. "reprompt" => ["type" => "PlainText", "text" => __("alexa.help") . $data->session->attributes->speechOutput],
  487. "shouldEndSession" => false
  488. ],
  489. "sessionAttributes" => $data->session->attributes,
  490. "userAgent" => Statics::userAgent()
  491. ];
  492. break;
  493.  
  494. //Pause / Resume Intents
  495. case "AMAZON.ResumeIntent":
  496. return Statics::notImplemented($request);
  497. break;
  498.  
  499. case "AMAZON.PauseIntent":
  500. return Statics::notImplemented($request);
  501. break;
  502.  
  503. //Navigation
  504. case "AMAZON.NextIntent":
  505. return $this->buildAnswer($request, __("alexa.rightAnswer", ["rightIndex" => $data->session->attributes->correctIndex, "rightText" => $data->session->attributes->correctText]));
  506. break;
  507.  
  508. case "AMAZON.PreviousIntent":
  509. return Statics::notImplemented($request);
  510. break;
  511.  
  512. case "AMAZON.MoreIntent":
  513. return Statics::notImplemented($request);
  514. break;
  515.  
  516. case "AMAZON.NavigateHomeIntent":
  517. return Statics::notImplemented($request);
  518. break;
  519.  
  520. case "AMAZON.NavigateSettingsIntent":
  521. return Statics::notImplemented($request);
  522. break;
  523.  
  524. case "AMAZON.PageUpIntent":
  525. return Statics::notImplemented($request);
  526. break;
  527.  
  528. case "AMAZON.PageDownIntent":
  529. return Statics::notImplemented($request);
  530. break;
  531.  
  532. case "AMAZON.ScrollUpIntent":
  533. return Statics::notImplemented($request);
  534. break;
  535.  
  536. case "AMAZON.ScrollDownIntent":
  537. return Statics::notImplemented($request);
  538. break;
  539.  
  540. case "AMAZON.ScrollRightIntent":
  541. return Statics::notImplemented($request);
  542. break;
  543.  
  544. case "AMAZON.ScrollLeftIntent":
  545. return Statics::notImplemented($request);
  546. break;
  547.  
  548. //On / Off Intents
  549. case "AMAZON.LoopOnIntent":
  550. return Statics::notImplemented($request);
  551. break;
  552.  
  553. case "AMAZON.LoopOffIntent":
  554. return Statics::notImplemented($request);
  555. break;
  556.  
  557. case "AMAZON.ShuffleOnIntent":
  558. return Statics::notImplemented($request);
  559. break;
  560.  
  561. case "AMAZON.ShuffleOffIntent":
  562. return Statics::notImplemented($request);
  563. break;
  564.  
  565. //Other
  566. case "AMAZON.FallbackIntent":
  567. return Statics::notUnderstood($request);
  568. break;
  569.  
  570. //Default
  571. default:
  572. Statics::logError($request, "UnknownIntent");
  573. return Statics::notUnderstood($request);
  574. break;
  575. }
  576. }
  577.  
  578. /**
  579. * @param $request
  580. * @param $outputSpeech
  581. * @return array
  582. */
  583. private function buildAnswer($request, $outputSpeech)
  584. {
  585. $data = Statics::getRequest($request);
  586. if ($data->request->intent->name == "AnswerIntent") {
  587. if ($data->request->intent->slots->answer->value == $data->session->attributes->correctIndex) {
  588. $outputSpeech .= __("alexa.right");
  589. $score = $data->session->attributes->score + 1;
  590. } else {
  591. $outputSpeech .= __("alexa.wrong") . __("alexa.rightAnswer", ["rightIndex" => $data->session->attributes->correctIndex, "rightText" => $data->session->attributes->correctText]);
  592. $score = $data->session->attributes->score;
  593. }
  594. } elseif ($data->request->intent->name == "AMAZON.NextIntent")
  595. $score = $data->session->attributes->score;
  596.  
  597. if ($data->session->attributes->currentIndex == ($data->session->attributes->numberOfGames - 1)) {
  598. $outputSpeech .= __("alexa.done");
  599. $numberOfGames = $data->session->attributes->numberOfGames;
  600. if ($score == $numberOfGames)
  601. $outputSpeech .= __("alexa.result_perfect");
  602. else
  603. $outputSpeech .= __("alexa.result", ["score" => $score, "totalQuestions" => $numberOfGames]);
  604.  
  605. $playAgain = false;
  606.  
  607. if ($score == $numberOfGames) {
  608. $outputSpeech .= trans_choice("alexa.result_answer", 1);
  609. } elseif ($score <= $numberOfGames / 5) {
  610. $outputSpeech .= trans_choice("alexa.result_answer", 4);
  611. $playAgain = true;
  612. } elseif ($score <= $numberOfGames / 2) {
  613. $outputSpeech .= trans_choice("alexa.result_answer", 3);
  614. $playAgain = true;
  615. } elseif ($score > $numberOfGames / 2) {
  616. $outputSpeech .= trans_choice("alexa.result_answer", 2);
  617. }
  618.  
  619. if ($playAgain)
  620. $outputSpeech .= __("alexa.playAgain");
  621.  
  622. return [
  623. "response" => [
  624. "outputSpeech" => ["type" => "PlainText", "text" => $outputSpeech],
  625. "reprompt" => ["type" => "PlainText", "text" => $outputSpeech],
  626. "shouldEndSession" => !$playAgain
  627. ],
  628. "directives" => [
  629. "type" => "Display.RenderTemplate",
  630. "template" => [
  631. "type" => "BodyTemplate3",
  632. "textContent" => [
  633. "primaryText" => [
  634. "type" => "RichText",
  635. "text" => "<div align='center'>" . $score . " / " . $numberOfGames . "</div>"
  636. ]
  637. ]
  638. ]
  639. ],
  640. "sessionAttributes" => [
  641. "speechOutput" => $outputSpeech,
  642. "selectedMode" => $this->attributes["selectedMode"],
  643. "numberOfGames" => 0,
  644. "StartOver" => $playAgain,
  645. "Stop" => $this->attributes["Stop"],
  646. "gameFinished" => true
  647. ],
  648. "userAgent" => Statics::userAgent()
  649. ];
  650. } else {
  651.  
  652. $questions = $data->session->attributes->questions;
  653. $questAttr = $this->versionController->nextQuestionText($request);
  654.  
  655. $outputSpeech .= __("alexa.nextQuestion") . $questAttr->question;
  656. }
  657. return [
  658. "response" => [
  659. "outputSpeech" => ["type" => "PlainText", "text" => $outputSpeech],
  660. "reprompt" => ["type" => "PlainText", "text" => $outputSpeech],
  661. "shouldEndSession" => false
  662. ],
  663. "sessionAttributes" => [
  664. "speechOutput" => $outputSpeech,
  665. "score" => $score,
  666. "currentIndex" => $questAttr->newIndex,
  667. "correctIndex" => $questAttr->correctIndex,
  668. "correctText" => $questAttr->correctText,
  669. "numberOfGames" => $data->session->attributes->numberOfGames,
  670. "selectedMode" => $this->attributes["selectedMode"],
  671. "StartOver" => $this->attributes["StartOver"],
  672. "Stop" => $this->attributes["Stop"],
  673. "gameFinished" => true,
  674. "questions" => $questions
  675. ],
  676. "userAgent" => Statics::userAgent()
  677. ];
  678. }
  679. }
  680.  
  681. class v3_0
  682. {
  683. protected $versionController;
  684.  
  685. /**
  686. * v2_0 constructor.
  687. * @param VersionController $versionController
  688. */
  689. public function __construct(VersionController $versionController)
  690. {
  691. $this->versionController = $versionController;
  692. }
  693.  
  694. protected $attributes = [
  695. "selectedMode" => "Allgemeinwissenstest",
  696. "gameFinished" => false,
  697. "StartOver" => false,
  698. "Stop" => false
  699. ];
  700.  
  701. /**
  702. * @param $request
  703. * @return array
  704. */
  705. public function index($request)
  706. {
  707. // Get Alexa Request
  708. $data = Statics::getRequest($request);
  709.  
  710. switch ($data->request->type) {
  711. case "IntentRequest":
  712. if (!isset($data->session->attributes->numberOfGames))
  713. return [
  714. "response" => [
  715. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
  716. "reprompt" => ["type" => "PlainText", "text" => __("alexa.questionsLimit")],
  717. "shouldEndSession" => false
  718. ],
  719. "directives" => [
  720. "type" => "Display.RenderTemplate",
  721. "template" => [
  722. "type" => "BodyTemplate3",
  723. "textContent" => [
  724. "primaryText" => [
  725. "type" => "RichText",
  726. "text" => "<div align='center'>" . __("alexa.questionsLimit") . "</div>"
  727. ]
  728. ]
  729. ]
  730. ],
  731. "sessionAttributes" => [
  732. "speechOutput" => __("alexa.modeChoice"),
  733. "selectedMode" => $this->attributes["selectedMode"],
  734. "numberOfGames" => 0,
  735. "score" => 0,
  736. "currentIndex" => -1,
  737. "StartOver" => $this->attributes["StartOver"],
  738. "Stop" => $this->attributes["Stop"],
  739. "gameFinished" => false
  740. ],
  741. "userAgent" => Statics::userAgent()
  742. ];
  743. else
  744. return $this->intentSwitch($request);
  745. break;
  746.  
  747. case "SessionEndedRequest":
  748. return $this->intentSwitch($request, "AMAZON.StopIntent");
  749. break;
  750.  
  751. default:
  752. Statics::logError($request, "malious IntentType");
  753. return Statics::notUnderstood($request);
  754. break;
  755. }
  756. }
  757.  
  758. /**
  759. * @param $request
  760. * @param null $intent
  761. * @return array
  762. */
  763. private function intentSwitch($request, $intent = null)
  764. {
  765. $data = Statics::getRequest($request);
  766.  
  767. if ($intent != null)
  768. $data->request->intent->name = $intent;
  769.  
  770. switch ($data->request->intent->name) {
  771. //Custom Intents
  772. case "AnswerIntent":
  773. if ($data->session->attributes->numberOfGames == null) {
  774. $numberOfGames = $data->request->intent->slots->answer->value;
  775. $questions = json_decode(json_encode($this->versionController->questionIDs($numberOfGames)));
  776. $questAttr = $this->versionController->nextQuestionText($request, $questions);
  777.  
  778. return [
  779. "response" => [
  780. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.ok") . $questAttr->question],
  781. "reprompt" => ["type" => "PlainText", "text" => $questAttr->question],
  782. "shouldEndSession" => false
  783. ],
  784. "sessionAttributes" => [
  785. "speechOutput" => $questAttr->question,
  786. "score" => 0,
  787. "currentIndex" => $questAttr->newIndex,
  788. "correctIndex" => $questAttr->correctIndex,
  789. "correctText" => $questAttr->correctText,
  790. "numberOfGames" => $numberOfGames,
  791. "selectedMode" => $this->attributes["selectedMode"],
  792. "StartOver" => $this->attributes["StartOver"],
  793. "Stop" => $this->attributes["Stop"],
  794. "gameFinished" => false,
  795. "questions" => $questions
  796. ],
  797. "userAgent" => Statics::userAgent()
  798. ];
  799. } else
  800. return $this->buildAnswer($request, "");
  801. break;
  802.  
  803. case "CodeIntent":
  804. return Statics::notUnderstood($request);
  805. break;
  806.  
  807. //Standard Intents
  808. case "AMAZON.YesIntent":
  809. if ($data->session->attributes->Stop)
  810. return [
  811. "response" => [
  812. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.bye")],
  813. "reprompt" => ["type" => "PlainText", "text" => __("alexa.bye")],
  814. "shouldEndSession" => true
  815. ],
  816. "directives" => [
  817. "type" => "Display.RenderTemplate",
  818. "template" => [
  819. "type" => "BodyTemplate3",
  820. "textContent" => [
  821. "primaryText" => [
  822. "type" => "RichText",
  823. "text" => "<div align='center'>" . __("alexa.bye") . "</div>"
  824. ]
  825. ]
  826. ]
  827. ],
  828. "userAgent" => Statics::userAgent()
  829. ];
  830. elseif ($data->session->attributes->StartOver)
  831. $data->request->type = "LaunchRequest";
  832. break;
  833.  
  834. case "AMAZON.NoIntent":
  835. if ($data->session->attributes->Stop) {
  836. $data->session->attributes->Stop = false;
  837. return [
  838. "response" => [
  839. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
  840. "reprompt" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
  841. "shouldEndSession" => false
  842. ],
  843. "sessionAttributes" => $data->session->attributes,
  844. "userAgent" => Statics::userAgent()
  845. ];
  846. } elseif ($data->session->attributes->StartOver) {
  847. if ($data->session->attributes->gameFinished)
  848. return [
  849. "response" => [
  850. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.bye")],
  851. "reprompt" => ["type" => "PlainText", "text" => __("alexa.bye")],
  852. "shouldEndSession" => true
  853. ],
  854. "directives" => [
  855. "type" => "Display.RenderTemplate",
  856. "template" => [
  857. "type" => "BodyTemplate3",
  858. "textContent" => [
  859. "primaryText" => [
  860. "type" => "RichText",
  861. "text" => "<div align='center'>" . __("alexa.bye") . "</div>"
  862. ]
  863. ]
  864. ]
  865. ],
  866. "userAgent" => Statics::userAgent()
  867. ];
  868.  
  869. $data->session->attributes->StartOver = false;
  870. return [
  871. "response" => [
  872. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
  873. "reprompt" => ["type" => "PlainText", "text" => __("alexa.playOn") . $data->session->attributes->speechOutput],
  874. "shouldEndSession" => true
  875. ],
  876. "sessionAttributes" => $data->session->attributes,
  877. "userAgent" => Statics::userAgent()
  878. ];
  879. }
  880. break;
  881.  
  882. case "AMAZON.RepeatIntent":
  883. return [
  884. "response" => [
  885. "outputSpeech" => ["type" => "PlainText", "text" => $data->session->attributes->speechOutput],
  886. "reprompt" => ["type" => "PlainText", "text" => $data->session->attributes->speechOutput],
  887. "shouldEndSession" => false
  888. ],
  889. "sessionAttributes" => $data->session->attributes,
  890. "userAgent" => Statics::userAgent()
  891. ];
  892. break;
  893.  
  894. case "AMAZON.CancelIntent":
  895. $data->session->attributes->Stop = true;
  896. return [
  897. "response" => [
  898. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.abort")],
  899. "reprompt" => ["type" => "PlainText", "text" => __("alexa.abort")],
  900. "shouldEndSession" => false
  901. ],
  902. "sessionAttributes" => $data->session->attributes,
  903. "userAgent" => Statics::userAgent()
  904. ];
  905. break;
  906.  
  907. case "AMAZON.StopIntent":
  908. $data->session->attributes->Stop = true;
  909. return [
  910. "response" => [
  911. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.abort")],
  912. "reprompt" => ["type" => "PlainText", "text" => __("alexa.abort")],
  913. "shouldEndSession" => false
  914. ],
  915. "sessionAttributes" => $data->session->attributes,
  916. "userAgent" => Statics::userAgent()
  917. ];
  918. break;
  919.  
  920. case "AMAZON.StartOverIntent":
  921. $data->session->attributes->StartOver = true;
  922. return [
  923. "response" => [
  924. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.startOver")],
  925. "reprompt" => ["type" => "PlainText", "text" => __("alexa.startOver")],
  926. "shouldEndSession" => false
  927. ],
  928. "sessionAttributes" => $data->session->attributes,
  929. "userAgent" => Statics::userAgent()
  930. ];
  931. break;
  932.  
  933. case "AMAZON.HelpIntent":
  934. return [
  935. "response" => [
  936. "outputSpeech" => ["type" => "PlainText", "text" => __("alexa.help")],
  937. "reprompt" => ["type" => "PlainText", "text" => __("alexa.help")],
  938. "shouldEndSession" => false
  939. ],
  940. "sessionAttributes" => $data->session->attributes,
  941. "userAgent" => Statics::userAgent()
  942. ];
  943. break;
  944.  
  945. //Pause / Resume Intents
  946. case "AMAZON.ResumeIntent":
  947. return Statics::notImplemented($request);
  948. break;
  949.  
  950. case "AMAZON.PauseIntent":
  951. return Statics::notImplemented($request);
  952. break;
  953.  
  954. //Navigation
  955. case "AMAZON.NextIntent":
  956. return $this->buildAnswer($request, __("alexa.rightAnswer", ["rightIndex" => $data->session->attributes->correctIndex, "rightText" => $data->session->attributes->correctText]));
  957. break;
  958.  
  959. case "AMAZON.PreviousIntent":
  960. return Statics::notImplemented($request);
  961. break;
  962.  
  963. case "AMAZON.MoreIntent":
  964. return Statics::notImplemented($request);
  965. break;
  966.  
  967. case "AMAZON.NavigateHomeIntent":
  968. return Statics::notImplemented($request);
  969. break;
  970.  
  971. case "AMAZON.NavigateSettingsIntent":
  972. return Statics::notImplemented($request);
  973. break;
  974.  
  975. case "AMAZON.PageUpIntent":
  976. return Statics::notImplemented($request);
  977. break;
  978.  
  979. case "AMAZON.PageDownIntent":
  980. return Statics::notImplemented($request);
  981. break;
  982.  
  983. case "AMAZON.ScrollUpIntent":
  984. return Statics::notImplemented($request);
  985. break;
  986.  
  987. case "AMAZON.ScrollDownIntent":
  988. return Statics::notImplemented($request);
  989. break;
  990.  
  991. case "AMAZON.ScrollRightIntent":
  992. return Statics::notImplemented($request);
  993. break;
  994.  
  995. case "AMAZON.ScrollLeftIntent":
  996. return Statics::notImplemented($request);
  997. break;
  998.  
  999. //On / Off Intents
  1000. case "AMAZON.LoopOnIntent":
  1001. return Statics::notImplemented($request);
  1002. break;
  1003.  
  1004. case "AMAZON.LoopOffIntent":
  1005. return Statics::notImplemented($request);
  1006. break;
  1007.  
  1008. case "AMAZON.ShuffleOnIntent":
  1009. return Statics::notImplemented($request);
  1010. break;
  1011.  
  1012. case "AMAZON.ShuffleOffIntent":
  1013. return Statics::notImplemented($request);
  1014. break;
  1015.  
  1016. //Other
  1017. case "AMAZON.FallbackIntent":
  1018. return Statics::notUnderstood($request);
  1019. break;
  1020.  
  1021. //Default
  1022. default:
  1023. Statics::logError($request, "UnknownIntent");
  1024. return Statics::notUnderstood($request);
  1025. break;
  1026. }
  1027. }
  1028.  
  1029. /**
  1030. * @param $request
  1031. * @param $outputSpeech
  1032. * @return array
  1033. */
  1034. private function buildAnswer($request, $outputSpeech)
  1035. {
  1036. $data = Statics::getRequest($request);
  1037. if ($data->request->intent->name == "AnswerIntent") {
  1038. if ($data->request->intent->slots->answer->value == $data->session->attributes->correctIndex) {
  1039. $outputSpeech .= __("alexa.right");
  1040. $score = $data->session->attributes->score + 1;
  1041. } else {
  1042. $outputSpeech .= __("alexa.wrong") . __("alexa.rightAnswer", ["rightIndex" => $data->session->attributes->correctIndex, "rightText" => $data->session->attributes->correctText]);
  1043. $score = $data->session->attributes->score;
  1044. }
  1045. } elseif ($data->request->intent->name == "AMAZON.NextIntent")
  1046. $score = $data->session->attributes->score;
  1047.  
  1048. if ($data->session->attributes->currentIndex == ($data->session->attributes->numberOfGames - 1)) {
  1049. $outputSpeech .= __("alexa.done");
  1050. $numberOfGames = $data->session->attributes->numberOfGames;
  1051. if ($score == $numberOfGames)
  1052. $outputSpeech .= __("alexa.result_perfect");
  1053. else
  1054. $outputSpeech .= __("alexa.result", ["score" => $score, "totalQuestions" => $numberOfGames]);
  1055.  
  1056. $playAgain = false;
  1057.  
  1058. if ($score == $numberOfGames) {
  1059. $outputSpeech .= trans_choice("alexa.result_answer", 1);
  1060. } elseif ($score <= $numberOfGames / 5) {
  1061. $outputSpeech .= trans_choice("alexa.result_answer", 4);
  1062. $playAgain = true;
  1063. } elseif ($score <= $numberOfGames / 2) {
  1064. $outputSpeech .= trans_choice("alexa.result_answer", 3);
  1065. $playAgain = true;
  1066. } elseif ($score > $numberOfGames / 2) {
  1067. $outputSpeech .= trans_choice("alexa.result_answer", 2);
  1068. }
  1069.  
  1070. if ($playAgain)
  1071. $outputSpeech .= __("alexa.playAgain");
  1072.  
  1073. return [
  1074. "response" => [
  1075. "outputSpeech" => ["type" => "PlainText", "text" => $outputSpeech],
  1076. "reprompt" => ["type" => "PlainText", "text" => $outputSpeech],
  1077. "shouldEndSession" => !$playAgain
  1078. ],
  1079. "directives" => [
  1080. "type" => "Display.RenderTemplate",
  1081. "template" => [
  1082. "type" => "BodyTemplate3",
  1083. "textContent" => [
  1084. "primaryText" => [
  1085. "type" => "RichText",
  1086. "text" => "<div align='center'>" . $score . " / " . $numberOfGames . "</div>"
  1087. ]
  1088. ]
  1089. ]
  1090. ],
  1091. "sessionAttributes" => [
  1092. "speechOutput" => $outputSpeech,
  1093. "selectedMode" => $this->attributes["selectedMode"],
  1094. "numberOfGames" => 0,
  1095. "StartOver" => $playAgain,
  1096. "Stop" => $this->attributes["Stop"],
  1097. "gameFinished" => true
  1098. ],
  1099. "userAgent" => Statics::userAgent()
  1100. ];
  1101. } else {
  1102.  
  1103. $questions = $data->session->attributes->questions;
  1104. $questAttr = $this->versionController->nextQuestionText($request);
  1105.  
  1106. $outputSpeech .= __("alexa.nextQuestion") . $questAttr->question;
  1107. }
  1108. return [
  1109. "response" => [
  1110. "outputSpeech" => ["type" => "PlainText", "text" => $outputSpeech],
  1111. "reprompt" => ["type" => "PlainText", "text" => $outputSpeech],
  1112. "shouldEndSession" => false
  1113. ],
  1114. "sessionAttributes" => [
  1115. "speechOutput" => $outputSpeech,
  1116. "score" => $score,
  1117. "currentIndex" => $questAttr->newIndex,
  1118. "correctIndex" => $questAttr->correctIndex,
  1119. "correctText" => $questAttr->correctText,
  1120. "numberOfGames" => $data->session->attributes->numberOfGames,
  1121. "selectedMode" => $this->attributes["selectedMode"],
  1122. "StartOver" => $this->attributes["StartOver"],
  1123. "Stop" => $this->attributes["Stop"],
  1124. "gameFinished" => true,
  1125. "questions" => $questions
  1126. ],
  1127. "userAgent" => Statics::userAgent()
  1128. ];
  1129. }
  1130. }
Add Comment
Please, Sign In to add comment