Exemplo n.º 1
0
        private AliceResponse SetUpGame(AliceRequest request)
        {
            Phrase        phrase;
            List <string> names = request.Request.Nlu.Tokens.Except(new [] { "и" }).ToList();

            if (request.HasIntent("only_me"))
            {
                names = new List <string> {
                    "я"
                }
            }
            ;

            if (names.Count == 0)
            {
                phrase = new Phrase(
                    "Назови подряд имена всех игроков, либо скажи, что играть будешь только ты.",
                    _prepareButtons
                    );
                return(phrase.Generate(request));
            }
            else if (names.Count > 10)
            {
                phrase = new Phrase(
                    "Пока что играть могу не более десяти человек на одном устройстве. Перечисли не более десяти имён.",
                    _prepareButtons
                    );
                return(phrase.Generate(request));
            }

            SessionState state = request.State.Session;

            _gameplayService.Start(request.State.User, state, names.ToArray());

            // read first word
            string playersNum  = names.Count.ToPhrase("игрок", "игрока", "игроков");
            string startPhrase = $"Отлично, {playersNum}, начинаем. " +
                                 $"\n\n{GameplayService.ReadWord(state, _soundEngine, ReadMode.First)}";

            return(new Phrase(startPhrase, _ingameButtons).Generate(request));
        }
    }
Exemplo n.º 2
0
        private AliceResponse MaybeRestart(AliceRequest request)
        {
            if (request.HasIntent("yes"))
            {
                return(Enter(request, restart: true));
            }

            // continue game or exit
            SessionState state = request.State.Session;

            if (state.CurrentWord != null)
            {
                _gameplayService.Resume(state);
                return(new Phrase(
                           GameplayService.ReadWord(state, _soundEngine, ReadMode.Continue),
                           _ingameButtons
                           )
                       .Generate(request));
            }

            // exit game
            return(Exit(request));
        }
Exemplo n.º 3
0
        public AliceResponse HandleRequest(AliceRequest request)
        {
            if (request.Request.Command.ToLower() == "сбросить состояние")
            {
                request.State.User    = new UserState();
                request.State.Session = new SessionState();
                return(Enter(request));
            }

            // enter
            if (request.State.Session.Step == SessionStep.None || request.IsEnter())
            {
                return(Enter(request));
            }

            // help
            if (request.HasIntent("help"))
            {
                return(Help(request));
            }

            // help
            if (request.HasIntent("hint"))
            {
                return(Hint(request));
            }

            // score
            if (request.HasIntent("score"))
            {
                return(Score(request));
            }

            // exit
            if (request.HasIntent("exit"))
            {
                return(Exit(request));
            }

            // restart
            if (request.HasIntent("restart"))
            {
                return(Restart(request));
            }

            // setup game
            if (request.State.Session.Step == SessionStep.AwaitNames)
            {
                if (request.HasIntent("restart"))
                {
                    return(Enter(request, true));
                }

                return(SetUpGame(request));
            }

            // answer
            if (request.State.Session.Step == SessionStep.Game)
            {
                if (request.HasIntent("repeat"))
                {
                    return(Repeat(request));
                }

                return(Answer(request));
            }

            if (request.State.Session.Step == SessionStep.AwaitRestart)
            {
                return(MaybeRestart(request));
            }

            throw new ArgumentOutOfRangeException();
        }
Exemplo n.º 4
0
        public AliceResponse HandleRequest(AliceRequest request)
        {
            // start
            if (request.IsEnter())
            {
                var currentTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                var resp        = request.State.User.LastEnter < currentTime - 30 * 24 * 3600000L
                    ? Phrases.FirstRun.Generate(request)
                    : Phrases.Hi.Generate(request);

                resp.UserStateUpdate.LastEnter = currentTime;
                return(resp);
            }

            // help command
            if (request.HasIntent(Intents.YandexHelp1) || request.HasIntent(Intents.YandexHelp2))
            {
                return(Phrases.Help.Generate(request));
            }

            // exit
            if (request.HasIntent(Intents.Exit))
            {
                var exit = Phrases.Exit.Generate(request);
                exit.Response.EndSession = true;
                return(exit);
            }

            // by letters
            if (request.HasIntent(Intents.ByLetters))
            {
                var w      = request.GetSlot(Intents.ByLetters, Slots.Word);
                var accent = -1;

                if (w.IsNullOrEmpty() && !request.State.Session.LastForm.IsNullOrEmpty())
                {
                    w      = request.State.Session.LastForm;
                    accent = request.State.Session.LastFormAccent;
                }
                else if (!w.IsNullOrEmpty() && _nMorph.WordExists(w))
                {
                    request.State.Session.Clear();
                    request.State.Session.LastWord = w;
                }

                return(ReadByLetters(w, accent).Generate(request));
            }

            // has word
            var filler = new [] { "слово", "слова", "слов", "словом" };
            // workaround
            var startsFromFiller = request.Request.Nlu.Tokens.Count == 2 &&
                                   filler.Contains(request.Request.Nlu.Tokens[0]);

            var hasWord = request.HasSlot(Intents.Main, Slots.Word) ||
                          request.Request.Nlu.Tokens.Count == 1 ||
                          startsFromFiller;

            var changeForm = Slots.GrammemeSlots.Any(s => request.HasSlot(Intents.Main, s));

            // no word / unknown command
            if (
                !hasWord &&
                (
                    request.State.Session.LastWord.IsNullOrEmpty() ||
                    (!changeForm && !request.HasIntent(Intents.ByLetters))
                )
                )
            {
                return(Phrases.UnknownCommand.Generate(request));
            }

            // word command
            if (hasWord)
            {
                request.State.Session.LastWord = request.HasSlot(Intents.Main, Slots.Word)
                    ? request.GetSlot(Intents.Main, Slots.Word)
                    : startsFromFiller
                        ? request.Request.Nlu.Tokens[1]
                        : request.Request.Nlu.Tokens.First();
            }

            // word not exists
            if (!_nMorph.WordExists(request.State.Session.LastWord))
            {
                var resp = Phrases.UnknownWord(request.State.Session.LastWord).Generate(request);
                request.State.Session.Clear();
                return(resp);
            }

            // word exists, find it
            var words = _nMorph.WordInfo(request.State.Session.LastWord);

            // get all slots
            var pos    = ParseEnum <Pos>(request, Slots.Pos);
            var number = ParseEnum <Number>(request, Slots.Number);
            var gender = ParseEnum <Gender>(request, Slots.Gender);
            var @case  = ParseEnum <Case>(request, Slots.Case);
            var tense  = ParseEnum <Tense>(request, Slots.Tense);
            var person = ParseEnum <Person>(request, Slots.Person);

            // filter by pos if possible
            if (pos != Pos.None && words.Any(w => w.Tag.Pos == pos))
            {
                words = words.Where(w => w.Tag.Pos == pos).ToArray();
            }

            // find forms
            var exactForms    = new List <(Word, WordForm)>();
            var nonExactForms = new List <(Word, WordForm)>();

            foreach (var w in words)
            {
                if (!changeForm)
                {
                    // get form from input word
                    var formsFound = w.ExactForms(request.State.Session.LastWord);
                    exactForms.AddRange(formsFound.Select(f => (w, f)));
                }
                else
                {
                    // get form from input data
                    var form = w.ClosestForm(gender, @case, number, tense, person, true);
                    if (form != null)
                    {
                        exactForms.Add((w, form));
                    }
                    else
                    {
                        form = w.ClosestForm(gender, @case, number, tense, person, false);
                        nonExactForms.Add((w, form));
                    }
                }
            }

            // no forms found
            if (exactForms.Count == 0 && nonExactForms.Count == 0)
            {
                return(Phrases.UnknownForm.Generate(request));
            }

            // read all found forms
            var response    = Phrases.StandardButtons;
            var formsToRead = exactForms;

            if (exactForms.Count == 0)
            {
                formsToRead = nonExactForms;
                response   += Phrases.NonExactForms;
            }

            request.State.Session.LastForm       = formsToRead.First().Item2.Word;
            request.State.Session.LastFormAccent = formsToRead.First().Item2.GetAccentIndex();

            for (var i = 0; i < formsToRead.Count; i++)
            {
                var(w, f) = formsToRead[i];
                response += ReadSingleForm(w, f, i == formsToRead.Count - 1);
            }

            return(response.Generate(request));
        }