/// <summary> /// roomID=4224224371&quizNum=1&uid=194682676 /// </summary> /// <param name="requestBody">请求体内容</param> /// <returns></returns> public static KingRequest Parse(string requestBody) { var request = new KingRequest(); var kvs = from item in requestBody.Split('&') let kv = item.Split('=') where (kv.Length == 2) select new { key = kv.First(), value = kv.Last() }; foreach (var kv in kvs) { if (kv.key.Equals("roomID", StringComparison.OrdinalIgnoreCase)) { request.roomID = kv.value; } else if (kv.key.Equals("quizNum", StringComparison.OrdinalIgnoreCase)) { request.quizNum = kv.value; } else if (kv.key.Equals("uid", StringComparison.OrdinalIgnoreCase)) { request.uid = kv.value; } } return(request); }
/// <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}"); } } }
/// <summary> /// 通过请求体内容获取获取历史数据 /// </summary> /// <param name="request">请求体内容获</param> /// <returns></returns> public static HistoryData TryGet(KingRequest request) { lock (syncRoot) { return(dictionary.Values.FirstOrDefault(item => item.KingRequest.Equals(request))); } }
/// <summary> /// 通过请求体内容获取获取http数据上下文 /// </summary> /// <param name="kingRequest">请求体内容获</param> /// <returns></returns> public static KingContext TakeByRequest(KingRequest kingRequest) { lock (syncRoot) { var index = list.FindIndex(item => item.KingRequest.Equals(kingRequest)); if (index < 0) { return(null); } var context = list[index]; list.RemoveAt(index); return(context); } }
/// <summary> /// 从本地和网络查找答案 /// 返回正确选项的索引 /// </summary> /// <param name="session">会话</param> /// <returns></returns> private static int GetOptionIndex(Session session, out KingQuestion kingQuestion) { kingQuestion = KingQuestion.FromSession(session); if (kingQuestion == null || kingQuestion.IsValidate() == false) { return(-1); } KingContextTable.Add(new KingContext { KingQuestion = kingQuestion, KingRequest = KingRequest.FromSession(session) }); // 找答案 return(SearchOptionIndex(kingQuestion)); }
/// <summary> /// 更新最佳选项并保存 /// </summary> /// <param name="session"></param> private static void UpdateBestAndSave(Session session) { session.utilDecodeRequest(); session.utilDecodeResponse(); var requestBody = Encoding.UTF8.GetString(session.RequestBody); var responseBody = Encoding.UTF8.GetString(session.ResponseBody); var kingRequest = KingRequest.Parse(requestBody); var kingAnswer = KingAnswer.Parse(responseBody); if (kingAnswer != null && kingAnswer.data != null) { var data = HistoryDataTable.TryGet(kingRequest); if (data != null) { var index = kingAnswer.data.answer - 1; data.SearchResult.Best = data.SearchResult.Options[index]; } } }
/// <summary> /// 从本地和网络查找答案 /// 并转发给对应的ws客户端 /// </summary> /// <param name="session">会话</param> /// <param name="sync">是否同步</param> /// <returns></returns> private static async Task <HistoryData> SearchHistoryDataAsync(Session session, bool sync) { session.utilDecodeRequest(); session.utilDecodeResponse(); var requestBody = Encoding.UTF8.GetString(session.RequestBody); var responseBody = Encoding.UTF8.GetString(session.ResponseBody); var kingQuestion = KingQuestion.Parse(responseBody); if (kingQuestion == null || kingQuestion.IsValidate() == false) { return(null); } var kingRequest = KingRequest.Parse(requestBody); var title = kingQuestion.data.quiz; var beginTime = DateTime.Now; var history = HistoryDataTable.TryGet(title); if (history == null) { history = new HistoryData { QuestionData = kingQuestion.data, KingRequest = KingRequest.Parse(requestBody), SearchResult = await BaiduSearcher.SearchAsync(kingQuestion, withAnswer) }; HistoryDataTable.TryAdd(history); } var data = JsonConvert.DeserializeObject <HistoryData>(JsonConvert.SerializeObject(history)); data.KingRequest = kingRequest; data.QuestionData = kingQuestion.data; data.SearchResult = history.SearchResult.CreateNewByQuestionData(kingQuestion.data); return(data); }