Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 从本地和网络查找答案
        /// 并转发给对应的ws客户端
        /// </summary>
        /// <param name="session">会话</param>
        private static void SetResponseWithAnswer(Session session)
        {
            var beginTime   = DateTime.Now;
            var optionIndex = GetOptionIndex(session, out KingQuestion kingQuestion);

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

            // 推送答案给ws客户端
            const double offsetSecondes = 3.7d;
            var          delay          = (int)beginTime.AddSeconds(offsetSecondes).Subtract(DateTime.Now).TotalMilliseconds;
            var          gameAnswer     = new WsGameAnswer
            {
                Index             = optionIndex,
                Quiz              = kingQuestion.data.quiz,
                Options           = kingQuestion.data.options,
                DelayMilliseconds = delay
            };
            var notifyData = new WsNotifyData <WsGameAnswer>
            {
                Cmd  = WsCmd.GameAnser,
                Data = gameAnswer,
            };

            WsNotifyByClientIP(notifyData, session.clientIP);


            // 改写响应结果
            if (AppConfig.ResponseAnswer == true && optionIndex > -1)
            {
                var json = JsonConvert.SerializeObject(kingQuestion);
                kingQuestion = JsonConvert.DeserializeObject <KingQuestion>(json);
                var quizData = kingQuestion.data;

                quizData.quiz = quizData.quiz + $" 老九提示:[{(char)('A' + optionIndex)}]";
                quizData.options[optionIndex] = quizData.options[optionIndex] + " [√]";

                json = JsonConvert.SerializeObject(kingQuestion);
                session.utilSetResponseBody(json);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 处理会话
        /// </summary>
        /// <param name="session">会话</param>
        /// <returns></returns>

        public static void ProcessSession(Session session)
        {
            var url = session.fullUrl;

            if (url.Contains("question/bat/findQuiz") == true)
            {
                SetResponseWithAnswer(session);
            }
            else if (url.Contains("question/bat/choose") == true)
            {
                UpdateCorrectOptions(session);
            }
            else if (url.Contains("question/bat/fightResult") == true)
            {
                var notifyData = new WsNotifyData <object> {
                    Cmd = WsCmd.GameOver
                };
                WsNotifyByClientIP(notifyData, session.clientIP);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 处理会话
        /// </summary>
        /// <param name="session">会话</param>
        /// <returns></returns>

        public static async void ProcessSessionAsync(Session session)
        {
            var url = session.fullUrl;

            if (url.Contains("question/bat/findQuiz") == true)
            {
                await SearchAnswerAsync(session);
            }
            else if (url.Contains("question/bat/choose") == true)
            {
                UpdateBestAndSave(session);
            }
            else if (url.Contains("question/bat/fightResult") == true)
            {
                var notifyData = new WsNotifyData <object> {
                    Cmd = WsCmd.GameOver
                };
                WsNotifyByClientIP(notifyData, session.clientIP);
                HistoryDataTable.Save();
            }
        }