예제 #1
0
        private Task <IActionResult> ProcessHelpAndBacktrack(SkillRequest request, IntentRequest intent)
        {
            Logger.LogInformation(LoggingEvents.Game, "Help request received");

            Session session = request.Session;
            var     state   = new State(Database.Context, session);

            session.Attributes = new Dictionary <string, object>(); // clean up session attributes

            if (state.IsSessionStart)
            {
                return(Task.FromResult <IActionResult>(
                           Ok(ResponseBuilder.Ask(
                                  "Dopo aver preparato la scacchiera, posizionati in uno degli spazi sul bordo. Poi dimmi dove ti trovi, specificando colonna e poi riga.",
                                  null,
                                  session
                                  ))
                           ));
            }
            else
            {
                var coords = state.LastReached;
                if (!coords.HasValue)
                {
                    Logger.LogError(LoggingEvents.Game, "Cannot find last reached position for user");
                    return(InternalError());
                }

                (var mazeDestination, var instructions) = Chessboard.GenerateMazeForLevel(coords.Value, state.MovesCount - 1);
                if (!mazeDestination.IsValid || state.LastDestination != mazeDestination)
                {
                    Logger.LogError(LoggingEvents.Game, "Regenerated destination for maze level {0} is {1} instead of previously generated {2}", state.MovesCount, mazeDestination, state.LastDestination);
                    return(InternalError());
                }

                return(Task.FromResult <IActionResult>(
                           Ok(ResponseBuilder.Ask(
                                  new SsmlOutputSpeech {
                    Ssml = string.Format(
                        @"<speak>Torna alla casella <emphasis level=""strong"">{0}, {1},</emphasis> e girati verso {2}. Poi esegui le istruzioni: <emphasis level=""strong"">{3}</emphasis>.</speak>",
                        coords.Value.Column.FromColumnIndex(),
                        coords.Value.Row,
                        coords.Value.Direction.Value.ToLocale(),
                        instructions
                        )
                }, new Reprompt {
                    OutputSpeech = new SsmlOutputSpeech {
                        Ssml = string.Format(
                            @"<speak>Parti da <emphasis level=""strong"">{0}, {1},</emphasis> verso {2}. Esegui le istruzioni: <emphasis level=""strong"">{3}</emphasis>.</speak>",
                            coords.Value.Column.FromColumnIndex(),
                            coords.Value.Row,
                            coords.Value.Direction.Value.ToLocale(),
                            instructions
                            )
                    }
                },
                                  session
                                  ))
                           ));
            }
        }
예제 #2
0
        private async Task <IActionResult> ProcessStep(SkillRequest request, Coordinates coords)
        {
            Session session = request.Session;
            var     state   = new State(Database.Context, session);

            Logger.LogInformation(LoggingEvents.Game, "User reaches coordinates {0}", coords);
            Logger.LogTrace(LoggingEvents.Game, "{0} moves, last reached {1}", state.MovesCount, state.LastReached);

            if (!coords.IsValid)
            {
                // Something went wrong
                Logger.LogError(LoggingEvents.Game, "Invalid coordinates");

                var response = ResponseBuilder.Ask(
                    new SsmlOutputSpeech {
                    Ssml = @"<speak>Non ho capito. Ripeti le coordinate per favore.</speak>"
                },
                    new Reprompt {
                    OutputSpeech = new SsmlOutputSpeech {
                        Ssml = @"<speak>Quali sono le <emphasis level=""moderate"">coordinate</emphasis> dello spazio in cui ti trovi?</speak>"
                    }
                }
                    );
                return(Ok(response));
            }

            if (state.IsSessionStart)
            {
                // First position, can ignore direction
                if (Chessboard.IsStartPosition(coords))
                {
                    Direction startDir = Chessboard.GetStartDirection(coords);
                    Logger.LogDebug(LoggingEvents.Game, "User in {0} should look towards {1}", coords, startDir);

                    Coordinates effectiveTarget = new Coordinates(coords.Column, coords.Row, startDir);
                    state.RecordDestination(effectiveTarget, true);

                    if (effectiveTarget.Direction != coords.Direction)
                    {
                        // Hint at correct direction
                        var progResp = new ProgressiveResponse(request);
                        await progResp.SendSpeech(string.Format(
                                                      "OK. Sei in {0}, {1}. Assicurati di girarti verso {2}.",
                                                      effectiveTarget.Column.FromColumnIndex(),
                                                      effectiveTarget.Row,
                                                      effectiveTarget.Direction.Value.ToLocale()
                                                      ));

                        coords = effectiveTarget;
                    }
                }
                else
                {
                    Logger.LogDebug(LoggingEvents.Game, "User in {0}, not a valid starting position", coords);

                    return(Ok(ResponseBuilder.Ask("Devi partire in uno spazio sul bordo della scacchiera. Spostati e dimmi dove sei.", null)));
                }
            }
            else if (!coords.Direction.HasValue)
            {
                // Not first position, requires direction:
                // register coordinates in session and ask for direction
                if (session.Attributes == null)
                {
                    session.Attributes = new Dictionary <string, object>();
                }
                session.Attributes["row"] = coords.Row;
                session.Attributes["col"] = coords.Column;

                var response = ResponseBuilder.Ask(
                    new SsmlOutputSpeech {
                    Ssml = @"<speak>OK. In che direzione stai guardando?</speak>"
                },
                    new Reprompt {
                    OutputSpeech = new SsmlOutputSpeech {
                        Ssml = @"<speak>Dimmi in che <emphasis level=""moderate"">direzione</emphasis> stai guardando.</speak>"
                    }
                },
                    session
                    );
                return(Ok(response));
            }

            session.Attributes = new Dictionary <string, object>(); // clean up session attributes

            // Check for destination to reach
            var destination = state.LastDestination;

            if (destination.HasValue)
            {
                if (destination.Value.Equals(coords))
                {
                    Logger.LogInformation(LoggingEvents.Game, "Correct cell");

                    state.ReachDestination();

                    var progResp = new ProgressiveResponse(request);
                    await progResp.SendSpeech("Sei nel posto giusto!");
                }
                else if (destination.Value.LocationEquals(coords))
                {
                    Logger.LogInformation(LoggingEvents.Game, "Wrong direction, expected {0}", destination.Value);

                    var response = ResponseBuilder.Ask("Stai guardando nella direzione sbagliata. Riprova.", null, session);
                    return(Ok(response));
                }
                else
                {
                    Logger.LogInformation(LoggingEvents.Game, "Wrong cell, expected {0}", destination.Value);

                    var response = ResponseBuilder.Ask("Purtroppo sei nella casella sbagliata. Riprova.", null, session);
                    return(Ok(response));
                }
            }

            if (state.MovesCount >= Chessboard.MaxLevel)
            {
                Logger.LogInformation(LoggingEvents.Game, "User completed last level");

                return(Ok(ResponseBuilder.Tell(
                              new SsmlOutputSpeech {
                    Ssml = @"<speak>Complimenti! Hai completato <emphasis level=""moderate"">Cody Maze</emphasis> con successo.</speak>"
                }
                              )));
            }

            // Assign new destination
            Logger.LogInformation(LoggingEvents.Game, "Generating new destination (step {0})", state.MovesCount);
            (var mazeDestination, var instructions) = Chessboard.GenerateMazeForLevel(coords, state.MovesCount);
            if (!mazeDestination.IsValid)
            {
                Logger.LogError(LoggingEvents.Game, "Invalid coordinates generated for maze level {0}", state.MovesCount);
                return(await InternalError());
            }
            state.RecordDestination(mazeDestination);

            return(Ok(ResponseBuilder.Ask(
                          new SsmlOutputSpeech {
                Ssml = string.Format(
                    @"<speak>Esegui le seguenti istruzioni{1}. <emphasis level=""strong"">{0}</emphasis>.</speak>",
                    instructions,
                    (state.MovesCount <= 2) ? " e poi dimmi le tue coordinate e direzione" : string.Empty
                    )
            }, new Reprompt {
                OutputSpeech = new SsmlOutputSpeech {
                    Ssml = string.Format(
                        @"<speak>Esegui le istruzioni. <emphasis level=""strong"">{0}</emphasis>.</speak>",
                        instructions
                        )
                }
            },
                          session
                          )));
        }