Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* eslint-disable func-names */
- /* eslint-disable no-console */
- /* eslint-disable no-restricted-syntax */
- const Alexa = require('ask-sdk-core');
- const Constants = require('data');
- /* HELPER FUNCTIONS */
- function getBadAnswer(item) {
- return `I'm sorry. ${item} is not something I know very much about in this skill. ${Constants.helpMessage}`;
- }
- function getCurrentScore(score, counter) {
- return `Your current score is ${score} out of ${counter}. `;
- }
- function getFinalScore(score, counter) {
- return `Your final score is ${score} out of ${counter}. `;
- }
- function getCardTitle(item) {
- return item.StateName;
- }
- function getSmallImage(item) {
- 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`;
- }
- function getLargeImage(item) {
- 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`;
- }
- function getImage(height, width, label) {
- return Constants.imagePath.replace("{0}", height)
- .replace("{1}", width)
- .replace("{2}", label);
- }
- function getBackgroundImage(label, height = 1024, width = 600) {
- return Constants.backgroundImagePath.replace("{0}", height)
- .replace("{1}", width)
- .replace("{2}", label);
- }
- function getSpeechDescription(item) {
- 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?`;
- }
- function formatCasing(key) {
- return key.split(/(?=[A-Z])/).join(' ');
- }
- function getQuestion(counter, property, item) {
- return `Here is your ${counter}th question. What is the ${formatCasing(property)} of ${item.StateName}?`;
- }
- // getQuestionWithoutOrdinal returns the question without the ordinal and is
- // used for the echo show.
- function getQuestionWithoutOrdinal(property, item) {
- return "What is the " + formatCasing(property).toLowerCase() + " of " + item.StateName + "?";
- }
- function getAnswer(property, item) {
- switch (property) {
- case 'Abbreviation':
- return `The ${formatCasing(property)} of ${item.StateName} is <say-as interpret-as='spell-out'>${item[property]}</say-as>. `;
- default:
- return `The ${formatCasing(property)} of ${item.StateName} is ${item[property]}. `;
- }
- }
- function getRandom(min, max) {
- return Math.floor((Math.random() * ((max - min) + 1)) + min);
- }
- function askQuestion(handlerInput) {
- console.log("I am in askQuestion()");
- //GENERATING THE RANDOM QUESTION FROM DATA
- const random = getRandom(0, Constants.data.length - 1);
- const item = Constants.data[random];
- const propertyArray = Object.getOwnPropertyNames(item);
- const property = propertyArray[getRandom(1, propertyArray.length - 1)];
- //GET SESSION ATTRIBUTES
- const attributes = handlerInput.attributesManager.getSessionAttributes();
- //SET QUESTION DATA TO ATTRIBUTES
- attributes.selectedItemIndex = random;
- attributes.quizItem = item;
- attributes.quizProperty = property;
- attributes.counter += 1;
- //SAVE ATTRIBUTES
- handlerInput.attributesManager.setSessionAttributes(attributes);
- const question = getQuestion(attributes.counter, property, item);
- return question;
- }
- function compareSlots(slots, value) {
- for (const slot in slots) {
- if (Object.prototype.hasOwnProperty.call(slots, slot) && slots[slot].value !== undefined) {
- if (slots[slot].value.toString().toLowerCase() === value.toString().toLowerCase()) {
- return true;
- }
- }
- }
- return false;
- }
- function getItem(slots) {
- const propertyArray = Object.getOwnPropertyNames(Constants.data[0]);
- let slotValue;
- for (const slot in slots) {
- if (Object.prototype.hasOwnProperty.call(slots, slot) && slots[slot].value !== undefined) {
- slotValue = slots[slot].value;
- for (const property in propertyArray) {
- if (Object.prototype.hasOwnProperty.call(propertyArray, property)) {
- const item = Constants.data.filter(x => x[propertyArray[property]]
- .toString().toLowerCase() === slots[slot].value.toString().toLowerCase());
- if (item.length > 0) {
- return item[0];
- }
- }
- }
- }
- }
- return slotValue;
- }
- function getSpeechCon(type) {
- if (type) return `<say-as interpret-as='interjection'>${Constants.correctAnswer[getRandom(0, Constants.correctAnswer.length - 1)]}! </say-as><break strength='strong'/>`;
- return `<say-as interpret-as='interjection'>${Constants.wrongAnswer[getRandom(0, Constants.wrongAnswer.length - 1)]} </say-as><break strength='strong'/>`;
- }
- function getTextDescription(item) {
- let text = '';
- for (const key in item) {
- if (Object.prototype.hasOwnProperty.call(item, key)) {
- text += `${formatCasing(key)}: ${item[key]}\n`;
- }
- }
- return text;
- }
- module.export = {
- getBadAnswer,
- getCurrentScore,
- getFinalScore,
- getCardTitle,
- getSmallImage,
- getLargeImage,
- getImage,
- getBackgroundImage,
- getSpeechDescription,
- formatCasing,
- getQuestion,
- getQuestionWithoutOrdinal,
- getAnswer,
- getRandom,
- askQuestion,
- compareSlots,
- getItem,
- getSpeechCon,
- getTextDescription
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement