feat 包命名规范
This commit is contained in:
88
internal/grpcserver/server_category.go
Normal file
88
internal/grpcserver/server_category.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package grpcserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"git.hlsq.asia/mmorpg/service-common/db/kafka"
|
||||
"git.hlsq.asia/mmorpg/service-common/proto/rs/rspb"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-qgdzs/internal/dao/repository"
|
||||
)
|
||||
|
||||
// 玩法 - 类目答题
|
||||
|
||||
// GetAllCategory 获取所有类目
|
||||
func (s *Server) GetAllCategory(ctx context.Context, req *rspb.GetAllCategoryReq) (*rspb.GetAllCategoryResp, error) {
|
||||
categoryList, err := repository.NewCategoryDao(ctx, s.query).FindAll()
|
||||
if err != nil {
|
||||
return nil, utils.ErrorsWrap(err)
|
||||
}
|
||||
categories := make([]*rspb.GetAllCategoryItem, 0)
|
||||
for _, category := range categoryList {
|
||||
categories = append(categories, &rspb.GetAllCategoryItem{
|
||||
Sn: category.Sn,
|
||||
Category: category.Category,
|
||||
})
|
||||
}
|
||||
return &rspb.GetAllCategoryResp{
|
||||
Categories: categories,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CategoryGetQuestion 获取题目
|
||||
func (s *Server) CategoryGetQuestion(ctx context.Context, req *rspb.CategoryGetQuestionReq) (*rspb.CategoryGetQuestionResp, error) {
|
||||
question, err := repository.NewQuestionDao(ctx, s.query).FindByRandom(req.CategorySn)
|
||||
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.CategoryGetQuestionResp{
|
||||
Sn: question.Sn,
|
||||
Question: question.Question,
|
||||
Options: options,
|
||||
Category: category,
|
||||
Difficulty: question.Difficulty,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CategoryAnswerQuestion 回答题目
|
||||
func (s *Server) CategoryAnswerQuestion(ctx context.Context, req *rspb.CategoryAnswerQuestionReq) (*rspb.CategoryAnswerQuestionResp, 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,
|
||||
})
|
||||
|
||||
// 类目答题正确给5分,错误不得分
|
||||
if req.Answer == question.Answer {
|
||||
kafka.NewProducer().Produce(ctx, &kafka.TopicAddPoint{
|
||||
MessageSn: utils.SnowflakeInstance().Generate().Int64(),
|
||||
Source: kafka.AddPointSourceCategory,
|
||||
USN: req.USN,
|
||||
Point: 5,
|
||||
})
|
||||
}
|
||||
}
|
||||
return &rspb.CategoryAnswerQuestionResp{
|
||||
Answer: question.Answer,
|
||||
Explanation: question.Explanation,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user