feat kafka 改版 2
This commit is contained in:
71
internal/grpc_server/server_random.go
Normal file
71
internal/grpc_server/server_random.go
Normal file
@@ -0,0 +1,71 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// 玩法 - 随机答题
|
||||
|
||||
// RandomGetQuestion 获取题目
|
||||
func (s *Server) RandomGetQuestion(ctx context.Context, req *grpc_pb.RandomGetQuestionReq) (*grpc_pb.RandomGetQuestionResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindByRandom("")
|
||||
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.RandomGetQuestionResp{
|
||||
Sn: question.Sn,
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
Category: category,
|
||||
Difficulty: question.Difficulty,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RandomAnswerQuestion 回答题目
|
||||
func (s *Server) RandomAnswerQuestion(ctx context.Context, req *grpc_pb.RandomAnswerQuestionReq) (*grpc_pb.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 != "" {
|
||||
kafka.NewProducer().Produce(ctx, &timer.TopicQuestionAnswer{
|
||||
MessageSn: utils.SnowflakeInstance().Generate().String(),
|
||||
USN: req.USN,
|
||||
QuestionSn: question.Sn,
|
||||
QuestionAnswer: question.Answer,
|
||||
Answer: req.Answer,
|
||||
})
|
||||
|
||||
// 随机答题正确给10分,错误不得分
|
||||
if req.Answer == question.Answer {
|
||||
kafka.NewProducer().Produce(ctx, &timer.TopicAddPoint{
|
||||
MessageSn: utils.SnowflakeInstance().Generate().String(),
|
||||
Source: timer.AddPointSourceRandom,
|
||||
USN: req.USN,
|
||||
Point: 10,
|
||||
})
|
||||
}
|
||||
}
|
||||
return &grpc_pb.RandomAnswerQuestionResp{
|
||||
Answer: question.Answer,
|
||||
Explanation: question.Explanation,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user