Advertisement
Guest User

functions.js

a guest
Jan 8th, 2019
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable  func-names */
  2. /* eslint-disable  no-console */
  3. /* eslint-disable  no-restricted-syntax */
  4.  
  5. const Alexa = require('ask-sdk-core');
  6. const Constants = require('data');
  7.  
  8. /* HELPER FUNCTIONS */
  9.  
  10. function getBadAnswer(item) {
  11.   return `I'm sorry. ${item} is not something I know very much about in this skill. ${Constants.helpMessage}`;
  12. }
  13.  
  14. function getCurrentScore(score, counter) {
  15.  return `Your current score is ${score} out of ${counter}. `;
  16. }
  17.  
  18. function getFinalScore(score, counter) {
  19.  return `Your final score is ${score} out of ${counter}. `;
  20. }
  21.  
  22. function getCardTitle(item) {
  23.  return item.StateName;
  24. }
  25.  
  26. function getSmallImage(item) {
  27.  return `https://m.media-amazon.com/images/G/01/mobile-apps/dex/alexa/alexa-skills-kit/tutorials/quiz-game/state_flag/720x400/${item.Abbreviation}._TTH_.png`;
  28. }
  29.  
  30. function getLargeImage(item) {
  31.  return `https://m.media-amazon.com/images/G/01/mobile-apps/dex/alexa/alexa-skills-kit/tutorials/quiz-game/state_flag/1200x800/${item.Abbreviation}._TTH_.png`;
  32. }
  33.  
  34. function getImage(height, width, label) {
  35.  return Constants.imagePath.replace("{0}", height)
  36.    .replace("{1}", width)
  37.    .replace("{2}", label);
  38. }
  39.  
  40. function getBackgroundImage(label, height = 1024, width = 600) {
  41.  return Constants.backgroundImagePath.replace("{0}", height)
  42.    .replace("{1}", width)
  43.    .replace("{2}", label);
  44. }
  45.  
  46. function getSpeechDescription(item) {
  47.  return `The capital of ${item.StateName} is ${item.Capital}.  I've added ${item.StateName} to your Alexa app.  Which other state or capital would you like to know about?`;
  48. }
  49.  
  50. function formatCasing(key) {
  51.   return key.split(/(?=[A-Z])/).join(' ');
  52. }
  53.  
  54. function getQuestion(counter, property, item) {
  55.   return `Here is your ${counter}th question.  What is the ${formatCasing(property)} of ${item.StateName}?`;
  56. }
  57.  
  58. // getQuestionWithoutOrdinal returns the question without the ordinal and is
  59. // used for the echo show.
  60. function getQuestionWithoutOrdinal(property, item) {
  61.   return "What is the " + formatCasing(property).toLowerCase() + " of "  + item.StateName + "?";
  62. }
  63.  
  64. function getAnswer(property, item) {
  65.   switch (property) {
  66.     case 'Abbreviation':
  67.       return `The ${formatCasing(property)} of ${item.StateName} is <say-as interpret-as='spell-out'>${item[property]}</say-as>. `;
  68.     default:
  69.       return `The ${formatCasing(property)} of ${item.StateName} is ${item[property]}. `;
  70.   }
  71. }
  72.  
  73. function getRandom(min, max) {
  74.   return Math.floor((Math.random() * ((max - min) + 1)) + min);
  75. }
  76.  
  77. function askQuestion(handlerInput) {
  78.   console.log("I am in askQuestion()");
  79.   //GENERATING THE RANDOM QUESTION FROM DATA
  80.   const random = getRandom(0, Constants.data.length - 1);
  81.   const item = Constants.data[random];
  82.   const propertyArray = Object.getOwnPropertyNames(item);
  83.   const property = propertyArray[getRandom(1, propertyArray.length - 1)];
  84.  
  85.   //GET SESSION ATTRIBUTES
  86.   const attributes = handlerInput.attributesManager.getSessionAttributes();
  87.  
  88.   //SET QUESTION DATA TO ATTRIBUTES
  89.   attributes.selectedItemIndex = random;
  90.   attributes.quizItem = item;
  91.   attributes.quizProperty = property;
  92.   attributes.counter += 1;
  93.  
  94.   //SAVE ATTRIBUTES
  95.   handlerInput.attributesManager.setSessionAttributes(attributes);
  96.  
  97.   const question = getQuestion(attributes.counter, property, item);
  98.   return question;
  99. }
  100.  
  101. function compareSlots(slots, value) {
  102.   for (const slot in slots) {
  103.     if (Object.prototype.hasOwnProperty.call(slots, slot) && slots[slot].value !== undefined) {
  104.       if (slots[slot].value.toString().toLowerCase() === value.toString().toLowerCase()) {
  105.         return true;
  106.       }
  107.     }
  108.   }
  109.  
  110.   return false;
  111. }
  112.  
  113. function getItem(slots) {
  114.   const propertyArray = Object.getOwnPropertyNames(Constants.data[0]);
  115.   let slotValue;
  116.  
  117.   for (const slot in slots) {
  118.     if (Object.prototype.hasOwnProperty.call(slots, slot) && slots[slot].value !== undefined) {
  119.       slotValue = slots[slot].value;
  120.       for (const property in propertyArray) {
  121.         if (Object.prototype.hasOwnProperty.call(propertyArray, property)) {
  122.           const item = Constants.data.filter(x => x[propertyArray[property]]
  123.             .toString().toLowerCase() === slots[slot].value.toString().toLowerCase());
  124.           if (item.length > 0) {
  125.             return item[0];
  126.           }
  127.         }
  128.       }
  129.     }
  130.   }
  131.   return slotValue;
  132. }
  133.  
  134. function getSpeechCon(type) {
  135.   if (type) return `<say-as interpret-as='interjection'>${Constants.correctAnswer[getRandom(0, Constants.correctAnswer.length - 1)]}! </say-as><break strength='strong'/>`;
  136.   return `<say-as interpret-as='interjection'>${Constants.wrongAnswer[getRandom(0, Constants.wrongAnswer.length - 1)]} </say-as><break strength='strong'/>`;
  137. }
  138.  
  139.  
  140. function getTextDescription(item) {
  141.   let text = '';
  142.  
  143.   for (const key in item) {
  144.     if (Object.prototype.hasOwnProperty.call(item, key)) {
  145.       text += `${formatCasing(key)}: ${item[key]}\n`;
  146.     }
  147.   }
  148.   return text;
  149. }
  150.  
  151. module.export = {
  152.     getBadAnswer,
  153.     getCurrentScore,
  154.     getFinalScore,
  155.     getCardTitle,
  156.     getSmallImage,
  157.     getLargeImage,
  158.     getImage,
  159.     getBackgroundImage,
  160.     getSpeechDescription,
  161.     formatCasing,
  162.     getQuestion,
  163.     getQuestionWithoutOrdinal,
  164.     getAnswer,
  165.     getRandom,
  166.     askQuestion,
  167.     compareSlots,
  168.     getItem,
  169.     getSpeechCon,
  170.     getTextDescription
  171. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement