Beispiel #1
0
        public static UserQuizCache InitializeUserQuizCache(string userId)
        {
            var random = new Random();

            if (UserQuizCache.ContainsKey(userId))
            {
                UserQuizCache.Remove(userId);
            }

            var userQuizes        = Quizes.OrderBy(x => random.Next()).Take(quiz_num).Select(x => x.Number).ToList();
            var currentQuizNumber = userQuizes.Skip(random.Next(0, userQuizes.Count - 1)).Take(1).First();
            var userQuizCache     = new UserQuizCache()
            {
                UserQuizes        = userQuizes,
                CurrentQuizNumber = currentQuizNumber
            };

            UserQuizCache.Add(userId, userQuizCache);

            return(UserQuizCache[userId]);
        }
Beispiel #2
0
        public async Task <IActionResult> Post()
        {
            var request = await clova.GetRequest(Request.Headers["SignatureCEK"], Request.Body);

            var userId = request.Session.User.UserId;

            answer_count = request.Session.SessionAttributes.ContainsKey("answer_count") ?
                           int.Parse(request.Session.SessionAttributes["answer_count"].ToString()) : 1;
            correct_count = request.Session.SessionAttributes.ContainsKey("correct_count") ?
                            int.Parse(request.Session.SessionAttributes["correct_count"].ToString()) : 0;
            quizNum = request.Session.SessionAttributes.ContainsKey("quiz_num") ?
                      int.Parse(request.Session.SessionAttributes["quiz_num"].ToString()) : 1;

            userQuizCache = QuizService.GetUserQuizCache(userId);
            currentQuiz   = QuizService.GetQuizByNumber(userQuizCache.CurrentQuizNumber);

            switch (request.Request.Type)
            {
            case RequestType.LaunchRequest:
                userQuizCache = QuizService.InitializeUserQuizCache(userId);
                currentQuiz   = QuizService.GetQuizByNumber(userQuizCache.CurrentQuizNumber);
                response.AddText($"イングリッシュクイズへようこそ!英語でクイズを出すので、日本語で回答してください!全部で{userQuizCache.UserQuizes.Count}問です。1問につき3度まで回答できます。");
                response.AddText($"では第{quizNum}問");
                response.AddUrl("https://eigodequiz.azurewebsites.net/assets/quizintro.mp3");
                response.AddText(currentQuiz.QuestionType);
                response.AddText(currentQuiz.Question, Lang.En);
                SaveSession(quizNum, answer_count, correct_count);
                SetReprompt();
                break;

            case RequestType.SessionEndedRequest:
                response.AddText("また挑戦してください。");     // This doesn't seems to work..
                break;

            case RequestType.IntentRequest:
                switch (request.Request.Intent.Name)
                {
                case "Answer":
                case "Clova.GuideIntent":
                case "Clova.YesIntent":
                case "Clova.NoIntent":
                    var answer = request.Request.Intent.Slots.ContainsKey("Answer") ? request.Request.Intent.Slots["Answer"] : null;
                    if (answer == null || answer.Value != currentQuiz.Answer)
                    {
                        if (answer_count == 3)
                        {
                            response.AddText($"残念!正解は{currentQuiz.Answer}でした。");
                            SetNextQuestion();
                        }
                        else
                        {
                            if (answer_count == 1)
                            {
                                if (answer != null && answer.Value == "ヒント")
                                {
                                    response.AddText($"1つ目のヒントです。");
                                }
                                else
                                {
                                    response.AddText($"{answer_count}回目の不正解です。ヒントです。");
                                }
                                response.AddText(currentQuiz.Hint1, Lang.En);
                            }
                            else
                            {
                                if (answer != null && answer.Value == "ヒント")
                                {
                                    response.AddText($"最後のヒントです。");
                                }
                                else
                                {
                                    response.AddText($"{answer_count}回目の不正解です。最後のヒントです。");
                                }
                                response.AddText(currentQuiz.Hint2, Lang.En);
                            }
                            answer_count++;
                            SaveSession(quizNum, answer_count, correct_count);
                            SetReprompt();
                        }
                    }
                    else
                    {
                        response.AddText($"正解です!");
                        correct_count++;
                        SetNextQuestion();
                    }
                    break;
                }
                break;
            }
            return(new OkObjectResult(response));
        }