/// <summary> /// 从本地和网络查找答案 /// 并转发给对应的ws客户端 /// </summary> /// <param name="session">会话</param> /// <returns></returns> private static async Task SearchAnswerAsync(Session session) { 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; } var beginTime = DateTime.Now; var data = await SearchHistoryDataAsync(session, withAnswer); const double offsetSecondes = 3.7d; var delay = beginTime.AddSeconds(offsetSecondes).Subtract(DateTime.Now); var notifyData = new WsNotifyData <WsGameAnswer> { Cmd = WsCmd.GameAnser, Data = new WsGameAnswer { SearchResult = data.SearchResult, GameDelayMSeconds = (int)delay.TotalMilliseconds } }; WsNotifyByClientIP(notifyData, session.clientIP); if (withAnswer == true) { var qData = data.QuestionData; if (data.SearchResult.Best != null) { var index = data.SearchResult.Best.Index; qData.quiz = qData.quiz + $" [{(char)('A' + index)}]"; qData.options[index] = qData.options[index] + "[√]"; } var q = new KingQuestion { data = qData }; var json = JsonConvert.SerializeObject(q); session.utilSetResponseBody(json); } }
/// <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); }