Example #1
0
        /// <summary>
        /// 更新最佳选项到db
        /// </summary>
        /// <param name="session"></param>
        private static void UpdateCorrectOptions(Session session)
        {
            var kingRequest = KingRequest.FromSession(session);
            var kingAnswer  = KingAnswer.FromSession(session);

            if (kingAnswer == null || kingAnswer.IsValidate() == false)
            {
                return;
            }

            var context = KingContextTable.TakeByRequest(kingRequest);

            if (context == null)
            {
                return;
            }

            using (var sqlLite = new SqlliteContext())
            {
                var quiz       = context.KingQuestion.data.quiz;
                var quizAnswer = sqlLite.QuizAnswer.Find(quiz);

                if (quizAnswer != null)
                {
                    quizAnswer.Answer = context.GetAnswer(kingAnswer);
                    sqlLite.SaveChanges();
                    Console.WriteLine($"更新正确答案到db:{Environment.NewLine}{quizAnswer}");
                }
            }
        }
Example #2
0
        /// <summary>
        /// 查找问题答案并保存到db
        /// </summary>
        /// <param name="kingQuestion">问题</param>
        /// <returns></returns>
        private static int SearchOptionIndex(KingQuestion kingQuestion)
        {
            using (var sqlLite = new SqlliteContext())
            {
                var quiz       = kingQuestion.data.quiz;
                var quizAnswer = sqlLite.QuizAnswer.FirstOrDefault(item => item.Quiz == quiz);

                if (quizAnswer != null)
                {
                    Console.WriteLine($"从db中找到记录:{Environment.NewLine}{quizAnswer}");
                    var answer = quizAnswer.Answer?.Trim();
                    return(Array.FindIndex(kingQuestion.data.options, item => item?.Trim() == answer));
                }

                // 搜索
                var best = Searcher.Search(kingQuestion);
                if (best == null)
                {
                    Console.WriteLine($"找不到答案:{kingQuestion.data.quiz}");
                    return(-1);
                }

                if (sqlLite.QuizAnswer.Any(item => item.Quiz == quiz) == false)
                {
                    quizAnswer = new QuizAnswer
                    {
                        Answer      = best.Option,
                        Quiz        = quiz,
                        OptionsJson = JsonConvert.SerializeObject(kingQuestion.data.options)
                    };
                    sqlLite.QuizAnswer.Add(quizAnswer);
                    sqlLite.SaveChanges();
                    Console.WriteLine($"保存网络答案到db:{Environment.NewLine}{quizAnswer}");
                }
                return(best.Index);
            }
        }