Example #1
0
        /// <summary>
        /// Check the answer returned and determine the correct response
        /// then configure the next question if there is to be one storing the data
        /// inthe sessionAttributes array
        /// </summary>
        /// <param name="input"></param>
        /// <param name="innerReponse"></param>
        /// <returns>void</returns>
        private async Task <SkillResponse> EvaluatePlayWord(IntentRequest intentRequest, Session session, Context context)
        {
            var phrase = this.GetStringSlotValue(intentRequest.Intent.Slots, "Phrase");

            if (string.IsNullOrWhiteSpace(phrase) || (phrase.Trim().Length < 3))
            {
                if (phrase.Trim().Length < 3)
                {
                    this.AddOutput("Too short!");
                }
                else
                {
                    this.AddOutput("Cannot find answer.");
                }
                if (playlist.Count > 0)
                {
                    var lastword = playlist[playlist.Count - 1].spokenword;
                    this.AddOutput(this.GetChosenWordSpeech(lastword, true), lastword);
                    return(BuildResponse("Play", false));
                }
                else
                {
                    return(await GenerateNextWord(intentRequest, session, context, false));
                }
            }
            else
            {
                var lastword = playlist[playlist.Count - 1].spokenword;
                if (phrase.First().ToString().ToLower() == lastword.Last().ToString().ToLower())
                {
                    var client = new OxfordApiClient();
                    var result = client.GetLemma(phrase);
                    if ((result == null) || (result.Results == null) || (result.Results.Count() == 0))
                    {
                        this.AddOutput(GetSpeechCon(false), "");
                        this.AddOutput("Invalid word.");
                        this.AddOutput(this.GetChosenWordSpeech(lastword, true), lastword);
                        return(BuildResponse("Play", false));
                    }
                    else
                    {
                        var rootword  = result.Results[0].LexicalEntries[0].InflectionOf[0].Id;
                        var foundlist = playlist.Where(wordPlayEntry => myUtils.IsInList(rootword, wordPlayEntry.rootword)).ToList();
                        var cnt       = foundlist.Count();
                        if (foundlist.Count == 0)
                        {
                            gamescore++;
                            this.AddOutput(GetSpeechCon(true), "");
                            playlist.Add(new WordPlayEntry()
                            {
                                rootword = rootword, spokenword = phrase
                            });
                            if (counter < MAX_CHAIN)
                            {
                                //outputspeech.Add( GetCurrentScore(gamescore, counter));
                                return(await GenerateNextWord(intentRequest, session, context, false));
                            }
                            else
                            {
                                this.AddOutput(GetFinalScore(gamescore, counter), gamescore.ToString());
                                this.AddOutput(" " + string.Format(EXIT_SKILL_MESSAGE, this.SkillName), "Exiting");
                                appstate = AppState.Start;
                                InitCounters();
                                return(BuildResponse("Exit", true));
                            }
                        }
                        else
                        {
                            this.AddOutput(GetSpeechCon(false), "");
                            this.AddOutput($"This word was already spoken as {breakstrong} {foundlist[0].spokenword}.",
                                           $"This word was already spoken as {foundlist[0].spokenword}");
                            this.AddOutput(this.GetChosenWordSpeech(lastword, true), lastword);
                            return(BuildResponse("Play", false));
                        }
                    }
                }
                else
                {
                    this.AddOutput(GetSpeechCon(false), "");
                    this.AddOutput($"This word {phrase} does not start with correct letter.");
                    this.AddOutput(this.GetChosenWordSpeech(lastword, true), lastword);
                    return(BuildResponse("Play", false));
                }
            }
        }
Example #2
0
        public override async Task <SkillResponse> OnIntentAsync(IntentRequest intentRequest, Session session, Context context)
        {
            // Get intent from the request object.
            var           intent     = intentRequest.Intent;
            var           intentName = intent?.Name;
            SkillResponse response   = null;

            // Note: If the session is started with an intent, no welcome message will be rendered;
            // rather, the intent specific response will be returned.
            MakeItemList();
            appstate  = GetAppState(session);
            counter   = GetIntAttributeProperty(session.Attributes, COUNTER);
            level     = Math.Max(1, GetIntAttributeProperty(session.Attributes, LEVEL));
            gamescore = Math.Max(0, GetIntAttributeProperty(session.Attributes, GAMESCORE));
            playlist  = GetTypedAttributeProperty <List <WordPlayEntry> >(session.Attributes, PLAYLIST);
            if (playlist == null)
            {
                playlist = new List <WordPlayEntry>();
            }

            switch (intentName)
            {
            case "LevelIntent":
                var gotolevel = this.GetIntSlotValue(intent.Slots, "gotolevel");
                if (gotolevel > HIGHEST_LEVEL)
                {
                    gotolevel = HIGHEST_LEVEL;
                }
                if (appstate == AppState.Start)
                {
                    if (gotolevel < 1)
                    {
                        gotolevel = level;                      //maintain the level if not specified or misunderstood by Alexa
                    }
                    level = gotolevel;
                    this.AddOutput($"Playing level {level}. ");
                    appstate = AppState.Game;
                    response = await this.ContinuePlay(intentRequest, session, context, true);
                }
                else
                {
                    if (gotolevel > 0)
                    {
                        if (gotolevel == level)
                        {
                            this.AddOutput($"Already playing at level {level}.");
                        }
                        else
                        {
                            level = gotolevel;
                            this.AddOutput($"Switching to level {level}.");
                        }
                    }
                    response = await this.ContinuePlay(intentRequest, session, context, false);
                }
                break;

            case "PlayIntent":
                response = await EvaluatePlayWord(intentRequest, session, context);

                break;

            case "ScoreIntent":
                this.AddOutput(GetCurrentScore(gamescore, counter), gamescore.ToString());
                response = await this.ContinuePlay(intentRequest, session, context, false);

                break;

            case "MeaningIntent":
                if (playlist.Count > 0)
                {
                    var lastword = playlist[playlist.Count - 1];
                    var client   = new OxfordApiClient();
                    var result   = client.GetDictionaryEntry(lastword.spokenword);
                    var def      = "";
                    if ((result != null) && (result.Results != null) && (result.Results.Count() > 0))
                    {
                        def = result.Results[0].LexicalEntries[0].Entries[0].Senses[0].Definitions[0];
                    }
                    else
                    {
                        def = "Sorry - Could not find the meaning of this word. Lets continue play.";
                    }
                    this.AddOutput(def + "<break time='1s'/>", def);
                }

                response = await this.ContinuePlay(intentRequest, session, context, false);

                break;

            case "SkipIntent":

                var SkipSlot = this.GetStringSlotValue(intent.Slots, "SkipSlot");
                if (myUtils.IsInList(SkipSlot, "word", "letter", "alphabet"))
                {
                    var exclaim = sayas_interject + "Ooh la la !" + sayas + breakstrong;
                    this.AddOutput($"{exclaim} I am so cool. Ain't I?", "");
                    response = await GenerateNextWord(intentRequest, session, context, true);
                }
                else
                {
                    response = await this.ContinuePlay(intentRequest, session, context, false);
                }
                break;

            case AlexaConstants.RepeatIntent:
                response = await this.ContinuePlay(intentRequest, session, context, true);

                break;

            case AlexaConstants.CancelIntent:
                this.AddOutput(string.Format(EXIT_SKILL_MESSAGE, this.SkillName), "Exiting");
                if (counter > 0)
                {
                    this.AddOutput(GetFinalScore(gamescore, counter), gamescore.ToString());
                }
                response = BuildResponse("Cancel", true);
                break;

            case AlexaConstants.StartOverIntent:
                this.InitCounters();
                response = await GenerateNextWord(intentRequest, session, context, false);

                break;

            case AlexaConstants.StopIntent:

                this.AddOutput(string.Format(EXIT_SKILL_MESSAGE, this.SkillName), "Exiting");
                if (counter > 0)
                {
                    this.AddOutput(GetFinalScore(gamescore, counter), gamescore.ToString());
                }
                response = BuildResponse("Stop", true);
                break;

            case AlexaConstants.HelpIntent:
                response = BuildSimpleResponse("Help", HELP_MESSAGE, false);
                break;

            default:
                if (appstate == AppState.Game)
                {
                    response = await GenerateNextWord(intentRequest, session, context, false);
                }
                else
                {
                    response = GetWelcomeResponse();
                }
                break;
            }

            foreach (string str1 in new string[] { STATE, LEVEL })
            {
                if (session.Attributes.ContainsKey(str1))
                {
                    session.Attributes.Remove(str1);
                }
            }

            session.Attributes.Add(STATE, appstate);
            session.Attributes.Add(LEVEL, level);
            return(response);
        }