62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package grpc_server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"git.hlsq.asia/mmorpg/service-common/db/kafka"
|
|
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
|
|
"git.hlsq.asia/mmorpg/service-common/utils"
|
|
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
|
"git.hlsq.asia/mmorpg/service-qgdzs/internal/timer"
|
|
)
|
|
|
|
// 玩法 - 随机答题
|
|
|
|
// QuicklyGetQuestion 获取题目
|
|
func (s *Server) QuicklyGetQuestion(ctx context.Context, req *grpc_pb.QuicklyGetQuestionReq) (*grpc_pb.QuicklyGetQuestionResp, 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 &grpc_pb.QuicklyGetQuestionResp{
|
|
Sn: question.Sn,
|
|
Question: question.Question,
|
|
Options: options,
|
|
Category: category,
|
|
Difficulty: question.Difficulty,
|
|
}, nil
|
|
}
|
|
|
|
// QuicklyAnswerQuestion 回答题目
|
|
func (s *Server) QuicklyAnswerQuestion(ctx context.Context, req *grpc_pb.QuicklyAnswerQuestionReq) (*grpc_pb.QuicklyAnswerQuestionResp, 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, &timer.TopicQuestionAnswer{
|
|
MessageSn: utils.SnowflakeInstance().Generate().Int64(),
|
|
USN: req.USN,
|
|
QuestionSn: question.Sn,
|
|
QuestionAnswer: question.Answer,
|
|
Answer: req.Answer,
|
|
})
|
|
}
|
|
return &grpc_pb.QuicklyAnswerQuestionResp{
|
|
Answer: question.Answer,
|
|
Explanation: question.Explanation,
|
|
}, nil
|
|
}
|