package grpcserver import ( "context" "encoding/json" "git.hlsq.asia/alex/service-common/db/kafka" "git.hlsq.asia/alex/service-common/proto/rs/rspb" "git.hlsq.asia/alex/service-common/utils" "git.hlsq.asia/alex/service-qgdzs/internal/dao/repository" ) // 玩法 - 随机答题 // RandomGetQuestion 获取题目 func (s *Server) RandomGetQuestion(ctx context.Context, req *rspb.RandomGetQuestionReq) (*rspb.RandomGetQuestionResp, error) { question, err := repository.NewQuestionDao(ctx, s.query).FindByRandom(0) if err != nil { return nil, utils.ErrorsWrap(err) } options := make([]string, 0) if err = json.Unmarshal([]byte(question.Options), &options); err != nil { return nil, utils.ErrorsWrapF(err, "data: %v", question.Options) } category, _ := repository.NewCategoryDao(ctx, s.query).FindNameBySn(question.CategorySn) return &rspb.RandomGetQuestionResp{ Sn: question.Sn, Question: question.Question, Options: options, Category: category, Difficulty: question.Difficulty, }, nil } // RandomAnswerQuestion 回答题目 func (s *Server) RandomAnswerQuestion(ctx context.Context, req *rspb.RandomAnswerQuestionReq) (*rspb.RandomAnswerQuestionResp, error) { utils.ShouldBindUsn(ctx, &req.USN) question, err := repository.NewQuestionDao(ctx, s.query).FindBySn(req.Sn) if err != nil { return nil, utils.ErrorsWrap(err) } options := make([]string, 0) if err = json.Unmarshal([]byte(question.Options), &options); err != nil { return nil, utils.ErrorsWrapF(err, "data: %v", question.Options) } // 保存答题记录 if req.USN != 0 { kafka.NewProducer().Produce(ctx, &kafka.TopicQuestionAnswer{ MessageSn: utils.SnowflakeInstance().Generate().Int64(), USN: req.USN, QuestionSn: question.Sn, QuestionAnswer: question.Answer, Answer: req.Answer, }) // 随机答题正确给10分,错误不得分 if req.Answer == question.Answer { kafka.NewProducer().Produce(ctx, &kafka.TopicAddPoint{ MessageSn: utils.SnowflakeInstance().Generate().Int64(), Source: kafka.AddPointSourceRandom, USN: req.USN, Point: 10, }) } } return &rspb.RandomAnswerQuestionResp{ Answer: question.Answer, Explanation: question.Explanation, }, nil }